C C 2017 3 8
1. 2. 3. 4. char 5. 2/101 C
1. 3/101 C
C = 5 (F 32). 9 F C 4/101 C
1 // fal2cel.c: Convert Fah temperature to Cel temperature 2 #include<stdio.h> 3 int main(void) 4 { 5 float fah, cel; 6 printf("please input the Fah temperature:\n"); 7 scanf("%f", &fah); 8 cel = (fah - 32.) * 5./9.; 9 printf("%.2f F = %.2f C\n", fah, cel); 10 return 0; 11 } 5/101 C
$ gcc fah2cel.c $./a.out Please input the Fahrenheit temperature: 78 78.00 F = 25.56 C 6/101 C
float fah, cel; float %f %.2f scanf %f scanf &fah fah fah 7/101 C
scanf() printf() scanf() printf() 8/101 C
2. 9/101 C
1. 2. 10/101 C
12 0-3 3.1415-1.23 a b 11/101 C
12/101 C
C 12/101 C
#define #define 13/101 C
price.c 1 #include<stdio.h> 2 #define PRICE 100 3 int main(void) 4 { 5 int num, total; 6 num = 10; 7 total = num * PRICE; 8 printf("total=%d\n", total); 9 return 0; 10 } 14/101 C
15/101 C
16/101 C
a 3 17/101 C
18/101 C
42 42.0 19/101 C
int signed _Bool long void _Complex short unsigned char float double _Imaginary 20/101 C
int long short unsigned signed char # $ % * float double long double _Bool true false _Complex _Imaginary 21/101 C
3. 22/101 C
1. 2. 1 23/101 C
0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 : 10 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 10 1 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1-10 : -10 24/101 C
int int 25/101 C
int 1 // int_max_min.c: print INT_MIN and INT_MAX 2 #include<stdio.h> 3 #include<limits.h> 4 int main(void) 5 { 6 printf("range of int is %d ~ %d\n", INT_MIN, INT_MAX); 7 printf("sizeof int = %lu bytes\n", sizeof(int) ); 8 return 0; 9 } 26/101 C
int $ gcc int_max_min.c $./a.out range of int is -2147483648 ~ 2147483647 sizeof int = 4 bytes 27/101 C
int int int var; int var1, var2; int 28/101 C
int int 1. int n; n = 1; 2. scanf int n; scanf("%d", &n); 3. int n = 1; 29/101 C
int int a = 1; int b = 2, c = 3; int d, e = 4; // valid, but not good 30/101 C
int 31/101 C
int int a; int b=2; a 2 b : 31/101 C
int 1 // print1.c: display some properties of printf() 2 #include<stdio.h> 3 int main(void) 4 { 5 int a = 10; 6 int b = 2; 7 printf("doing it right: "); 8 printf("%d - %d = %d\n", a, b, a-b); 9 printf("doing it wrong: "); 10 printf("%d - %d = %d\n", a); 11 return 0; 12 } 32/101 C
int $ gcc print1.c $./a.out Doing it right: 10-2 = 8 Doing it wrong: 10-73832 = 771 33/101 C
int print a %d %d printf 34/101 C
C 0x 0X 16 0x10 0X10 0 16 020 35/101 C
1 // bases.c: print 100 in decimal, octal and hex 2 #include <stdio.h> 3 int main(void) 4 { 5 int x = 100; 6 printf("dec = %d; octal = %o; hex = %x\n", x, x, x); 7 printf("dec = %d; octal = %#o; hex = %#x\n", x, x, x); 8 return 0; 9 } 36/101 C
$ gcc bases.c $./a.out dec = 100; octal = 144; hex = 64 dec = 100; octal = 0144; hex = 0x64 37/101 C
%d %o %#o %x %X %#x %#X 38/101 C
C 3 int short long unsigned 39/101 C
short (int) %hd,%ho,%hx long (int) %ld,%lo,%lx long long (int) C99 %lld,%llo,%llx 40/101 C
unsigned %u (int) 16 0-65535 unsigned C90 %lu long (int) unsigned long long (int) C99 %llu 41/101 C
signed short short int signed short signed short int 42/101 C
C short int long int CPU long int short 43/101 C
44/101 C
1 #include <stdio.h> 2 #include <limits.h> 3 int main(void) 4 { 5 int i = INT_MAX; 6 unsigned int j = UINT_MAX; 7 printf("i = %d, i+1 = %d, i+2 = %d\n", 8 i, i+1, i+2); 9 printf("j = %u, j+1 = %u, j+2 = %u\n", 10 j, j+1, j+2); 11 return 0; 12 } 45/101 C
$ gcc IntOverflow.c $./a.out i = 2147483647, i+1 = -2147483648, i+2 = -2147483647 j = 4294967295, j+1 = 0, j+2 = 1 46/101 C
unsigned int 0 int -2147483648 47/101 C
4. char 48/101 C
char char char a, b, =, +,? char 49/101 C
char char char ASCII 5 5 5 char 50/101 C
char char a, b; char c; 51/101 C
A grade char grade = A ; 52/101 C
char grade; grade = A ; grade = A; grade = "A"; grade = 65; // char // // // // A 53/101 C
ASCII 54/101 C
char a = x ; char b = y ; x y ASCII 120 121 a b a: 0 1 1 1 1 0 0 0 b: 0 1 1 1 1 0 0 1 55/101 C
C 56/101 C
printf() %c %d 57/101 C
1 // charcode.c: displays code number for a char 2 #include <stdio.h> 3 int main(void) 4 { 5 char c; 6 printf("please input a character:\n"); 7 scanf("%c", &c); 8 printf("the code for %c is %d\n", c, c); 9 return 0; 10 } 58/101 C
$ gcc charcode.c $./a.out Please input a character: A the code for A is 65 59/101 C
ch 0 1 0 0 0 0 1 1 %c %d A 65 60/101 C
5. 61/101 C
1 000 000 000 1.0 10 9 1.0e9 123 000 1.23 10 5 1.23e5 322.56 3.2256 10 2 3.2256e2 0.000 056 5.6 10 5 5.6e-5 62/101 C
C float double long double 63/101 C
C float 32 6 10 37 10 +37 double 64 10 float C long double double IEEE float IEEE R32.24 double IEEE R64.53 64/101 C
1. (sign) 0 1 2. (exponent) 3. (mantissa) 65/101 C
32 1 8 23 30 22 0 : float 66/101 C
64 1 62 11 51 52 0 : double 67/101 C
R32.24 R64.53 120.5 1110110.1 1.1101101 2 6 68/101 C
1. 2 n. 1 23 24 69/101 C
24 (9) 10 = (1001) 2, 1 24 float 6 70/101 C
8-127 128 +127 71/101 C
8.25 1. 8.25 1.0001 2 3 72/101 C
8.25 1. 8.25 1.0001 2 3 2. 0 3 + 127 = 130 = (1000 0010) 2 (0001) 2 72/101 C
8.25 1. 8.25 1.0001 2 3 2. 0 3 + 127 = 130 = (1000 0010) 2 (0001) 2 3. 0 1000 0010 000 1000 0000 0000 0000 0000 130 = 3 + 127 000 1 1.0001 2 3 72/101 C
120.5 1. 120.5 1.1101101 2 6 73/101 C
120.5 1. 120.5 1.1101101 2 6 2. 0 6 + 127 = 133 = (1000 0101) 2 (1101101) 2 73/101 C
120.5 1. 120.5 1.1101101 2 6 2. 0 6 + 127 = 133 = (1000 0101) 2 (1101101) 2 3. 0 1000 0010 110 1101 0000 0000 0000 0000 133 = 6 + 127 110 1101 1.1101101 2 6 73/101 C
01000010111001011000000000000000, 74/101 C
1. 0 1000 0101 110 0101 1000 0000 0000 0000 75/101 C
1. 0 1000 0101 110 0101 1000 0000 0000 0000 2. 0 (1000 0101) 2 = 133 133 127 = 6 (1.1100101 2 6 ) 2 = (1110010.1) 2 = 114.5. 75/101 C
1 // float_double.c: 2 #include <stdio.h> 3 int main(void) 4 { 5 float f1 = 2.2, f2 = 2.25; 6 double g1, g2; 7 g1 = (double) f1; 8 g2 = (double) f2; 9 printf("g1 = %.13f, g2 = %.13f\n", g1, g2); 10 return 0; 11 } 76/101 C
$ gcc float_double.c $./a.out g1 = 2.2000000476837, g2 = 2.2500000000000 77/101 C
2.2 2.25 78/101 C
2.25 0 1000 0001 001 0000 0000 0000 0000 0 1000 0001 001 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 79/101 C
2 0. 2 2 0. 4 0 2 0. 8 0 2 1. 6 1 2 1. 2 1 2 0. 4 0 80/101 C
2.2 10.0011 0011 0011 0011 0011... 81/101 C
2.2 0 1000 0001 000 1100 1100 1100 1100 0 1000 0001 000 1100 1100 1100 1100 1100 1100 1100 1100 1100 1100 1100 1100 82/101 C
e E 10-1.56E+12, 2.87e-3 83/101 C
+2.87e-3 2.87e-3 2E5 19.28 2 // is an integer 3.e12.45E-5 84/101 C
1.56 E+12 // wrong double some float some = 4.0 * 2.0; 4.0 2.0 double 64 float 85/101 C
f F float 2.3f 3.4e9F l L long double l 1 L 54.3l 4.3E9L 86/101 C
1 // float1.c: What is the output? 2 #include<stdio.h> 3 int main(void) 4 { 5 float x = 0.1; 6 if (x == 0.1) 7 printf("if\n"); 8 else if (x == 0.1f) 9 printf("else IF\n"); 10 else 11 printf("else\n"); 12 } 87/101 C
$ gcc float1.c $./a.out ELSE IF 88/101 C
$ gcc float1.c $./a.out ELSE IF x 0.1 0.1f 0.1 0.1f 0.1 0.1!= 0.1f x == 0.1f 88/101 C
1 // float2.c: What is the output? 2 #include<stdio.h> 3 int main(void) 4 { 5 float x = 0.1; 6 printf("%lu %lu %lu\n", sizeof(x), sizeof(0.1), sizeof(0.1f)); 7 return 0; 8 } 89/101 C
$ gcc float2.c $./a.out 4 8 4 90/101 C
$ gcc float2.c $./a.out 4 8 4 90/101 C
1 // float3.c: What is the output? 2 #include<stdio.h> 3 int main(void) 4 { 5 float x = 0.5; 6 if (x == 0.5) 7 printf("if\n"); 8 else if (x == 0.5f) 9 printf("else IF\n"); 10 else 11 printf("else\n"); 12 } 91/101 C
$ gcc float2.c $./a.out IF 92/101 C
$ gcc float2.c $./a.out IF x 0.5 0.1f 0.5 0.5f 0.5 0.5 == 0.5f x == 0.5 92/101 C
%f float double %e %Lf %Le long double 93/101 C
1 // showf_pt.c: 2 #include <stdio.h> 3 int main(void) 4 { 5 float a = 32000.; 6 double b = 2.14e9; 7 long double c = 5.32e-5; 8 printf("%f can be written as %e\n", a, a); 9 printf("%f can be written as %e\n", b, b); 10 printf("%lf can be written as %Le\n", c, c); 11 return 0; 12 } 94/101 C
$ gcc showf_pt.c $./a.out 32000.000000 can be written as 3.200000e+04 2140000000.000000 can be written as 2.140000e+09 0.000053 can be written as 5.320000e-05 95/101 C
1 #include <stdio.h> 2 #include <limits.h> 3 int main(void) 4 { 5 float toobig = 1e39; 6 printf("toobig = %f\n",toobig); 7 return 0; 8 } 96/101 C
$ gcc float_overflow.c $./a.out toobig = inf 97/101 C
overflow C inf 98/101 C
1 #include <stdio.h> 2 int main(void) 3 { 4 float a,b; 5 a = 2.e20 + 1; 6 b = a - 2.e20; 7 printf("b = %f\n", b); 8 return 0; 9 } 99/101 C
$ gcc float_err.c $./a.out b = 4008175468544.000000 100/101 C
2.0e20 1 21 21 float 6 7 101/101 C