Empire C:Basic 1

一、首先,从一个C程序开始:

 1 ///Name:Hello World
 2 ///Author:JA
 3 ///Date:2015-2-4
 4 
 5 
 6 
 7 #include <stdio.h>                      //包含标准库的信息
 8 
 9 void main1()                            //定义名为main的函数,它不接受参数值
10 {                                     //main函数的语句都被括在花括号中
11     printf("hello world
");             //main函数调用库函数printf以显示字符序列;
代表换行符
12                    
13 }
View Code

如果你感兴趣,就顺带了解一下何为C语言

二、变量与算术表达式

1.声明

在C语言中,所有变量先声明后使用。声明通常放在函数起始处,在任何可执行语句之前。声明用于说明变量的属性,它由一个类型名和一个变量表组成。说明变量的性质,并不分配存储单元。

2.基本数据类型

  • int
  • float
  • char
  • double

三、for语句

1.初始化

2.控制循环测试条件部分

3.增加步长

 1 ///Name:华氏——摄氏
 2 ///Author:JA
 3 ///Date:2015-2-4
 4 
 5 
 6 
 7 #include<stdio.h>
 8 
 9 //void main()
10 //{
11 //    int fahr, celsius;
12 //    int lower, upper, step;
13 //
14 //    lower = 0;
15 //    upper =300;
16 //    step = 20;
17 //
18 //    fahr = lower;
19 //    while (fahr <= 300)
20 //    {
21 //        celsius = 5 * (fahr - 32) / 9;
22 //        printf("%d	%d
", fahr, celsius);
23 //        fahr = fahr + 20;
24 //        //getchar();
25 //    }
26 //
27 //}
28 
29 //void main()
30 //{
31 //    int fahr;
32 //    for (fahr = 0; fahr <= 300; fahr = fahr + 20){
33 //        printf("%3d %6.1f
",fahr, (5.0 / 9.0)*(fahr - 32));
34 //    }
35 //    getchar();
36 //}
37 
38 #define LOWER 0
39 #define UPPER 300
40 #define STEP 20
41 
42 void main()
43 {
44     int fahr;
45 
46     for (fahr = LOWER; fahr <= UPPER; fahr = fahr + STEP)
47         printf("%3d %6.1f
", fahr, (5.0 / 9.0)*(fahr - 32));
48     //getchar();
49 }
View Code

四、符号常量

1.幻数:在程序中使用300,20等

2.#define指令可以把符号名(或称为符号常量)定义为一个特定的字符串

五、字符输入输出

1.文件复制

2.字符统计

3.行计数

4.单词计数

 1 ///Name:字符输入输出
 2 ///Author:JA
 3 ///Date:2015-2-4
 4 
 5 
 6 
 7 /*文件复制*/
 8 #include<stdio.h>
 9 
10 void main()
11 {
12     int c;
13     while ((c = getchar())!= EOF){  //EOF:END OF FILE
14         putchar(c);
15     
16     }
17 }
18 
19 /*字符计数*/
20 #include<stdio.h>
21 
22 void main()
23 {
24     double nc;
25     for (nc = 0; getchar() != EOF; nc++)
26         ;                //空语句
27     printf("%.0f
", nc);
28 }
29 
30 /*行计数*/
31 #include<stdio.h>
32 
33 void main()
34 {
35     int c, n1;
36     n1 = 0;
37     while ((c = getchar())!=EOF)
38     if (c == '
')
39         ++n1;
40     printf("%d
", n1);
41 }
42 
43 /*单词计数*/
44 #include<stdio.h>
45 #define IN   1
46 #define OUT  0
47 
48 void main()
49 {
50     int c, n1, nw, nc, state;
51 
52     state = OUT;
53     n1 = nw = nc = 0;
54     while (c = getchar() != EOF)
55     {
56         ++nc;
57         if (c == '
')
58             ++n1;
59         if (c == ' ' || c == '
' || c == '	')
60             state = OUT;
61         else if (state = OUT){
62             state = IN;
63             ++nw;
64         }
65     }
66     printf("%d %d %d
", n1, nw, nc);
67 }
View Code

六、数组

1.数组下标从0开始

2.数组下标可以是任何整型表达式,包括变量常量

七、函数

1.为计算的封装提供了一种简便方法

2.函数声明中不允许有参数列表

八、参数——传值调用

1.被调用函数不能直接修改主调函数中变量的值,而只能修改其私有的临时副本的值。

九、外部变量与作用域

1.外部变量必须在所有函数之外,且只能定义一次。

2.extern

原文地址:https://www.cnblogs.com/joeaaron007/p/4242361.html