首页 > 代码库 > Linux C 程序 (ONE)

Linux C 程序 (ONE)

1.Linux 下第一支C程序,控制台打印一句话。

 1 vi first.c  //linux新建文件 2  3 #include<stdio.h> 4  5 int main() { 6     printf("welcome to Linux ,this is the first C program!");     7     return 0; 8 } 9 10 编译;gcc -o first first.c //linux编译文件11 执行: ./first //linux执行文件

2.第二只C程序:了解C程序的结构,实现两整数相加。

 1 vi second.c 2  3 #include<stdio.h> 4 int main() { 5     int x , y , sum ; 6     x = 100 ; 7     y = 200 ;  8     sum = x + y ;  9     printf("sum is %d",sum);10     return 0;11 }12 //编译执行命令与第一支C程序相同。

3.整形数据 占字节数

 1 #include<stdio.h> 2  3 int main() { 4     short int i; 5     int j; 6     long int k; 7     int a,b,c; 8     a = sizeof(i); 9     b = sizeof(j);10     c = sizeof(k);    11     12     printf("a is %d\n",a);13     printf("b is %d\n",b);14     printf("c is %d\n",c);15     16     //return 0; don‘t write return is OK?17 }18 output :19 a is 220 b is 421 c is 4

4.浮点型数据占字节数,浮点数小数位数限制

 1 #include<stdio.h> 2  3 int main(){ 4         float i ; 5         double j; 6         int a , b ; 7         a = sizeof(i); 8         b = sizeof(j); 9         printf("a is %d \n b is %d \n",a , b);// bit number10         11         12         float c = 88888.88888;13         double d = 88888888888.88888888;14         printf("c is %f \n d is %f \n",c,d);//%f小数最多输出六位15         16 }17 18 output:19 a is 420 b is 821 c is 88888.890625 //i是单精度浮点数,有效位数为7,整数占据5位,小数占2位,第二位位四舍五入结果,后面均为无效数字22 d is 88888888888.888885//j双精度,有效16位,整数占11位,小数占5位,后面为无效数。

5.字符型数据

 1 //C语言字符用‘‘单引号:eg : ‘A‘ 2 //转义字符:\n,换行,相当于enter 3 //          \t,跳到下一个tab位置,相当于tab键 4 //                    \b,退格,将当前位置移到前一列,相当于backspace 5 //                    \\,反斜杠字符 6 //                    \‘,单引号字符 7 //                    \",双引号字符 8 //                    \0,空字符,用在字符串中 9 //                    \ddd,一到三位8进制代表的字符,如\101代表字符A10 //                    \xhh,1到2位十六进制代表的字符,如\x41代表字符A11 //字符变量定义:char c1 , c2 = ‘A‘; 占1字节,8bit,   ‘\n‘是一个转义字符12 13 #include<stdio.h>14 15 int main(){16     int c1 ,c2 ; 17     char c3;18     printf("c3 is %d \n",sizeof(c3));19     20     c1 = a - A;21     c2 = b - B;22     c3 = c - 32;23     24     printf("c1 is %d and c2 is %d \n" , c1 ,c2);25     printf("c3 is %d and %c \n",c3,c3);26     return 0;27 }28 29 30 output:31 c3 is 132 c1 is 32 and c2 is 3233 c3 is 67 and C 

 

Linux C 程序 (ONE)