C/C++ C/C++
Table of contents 1. 2. 3. 4. char 5. 1
C = 5 (F 32). 9 F C 2
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 (" %.2 f F = %.2 f C\n", fah, cel ); 10 return 0; 11 } 3
$ gcc fah2cel. c $./a. out Please input the Fahrenheit temperature : 78 78. 00 F = 25. 56 C 4
float fah, cel ; float %f %.2f scanf() %f scanf() &fah fah fah 5
scanf() printf() scanf() printf() 6
: 1. 2. 7
12 0-3 3.1415-1.23 a b 8
: 9
: : C/C++ 9
# define #define 10
# include < stdio.h> # define PRICE 100 int main ( void ) { int num, total ; num = 10; total = num * PRICE ; printf (" total =%d\n", total ); return 0; } 11
# include < stdio.h> # define PRICE 100 int main ( void ) { int num, total ; num = 10; total = num * PRICE ; printf (" total =%d\n", total ); return 0; } 11
: 12
C C++ a 3 13
42 42.0 14
int signed _Bool long void _Complex short _Imaginary unsigned char float double 15
int long short unsigned signed char float double long double true false C _Bool C++: bool C complex C++ complex 16
# include < stdio.h> int main ( void ) { _Bool is_ printed = 1; if ( is_printed ) printf (" Hello world!\n"); return 0; } 17
# include < stdio.h> int main ( void ) { _Bool is_ printed = 1; if ( is_printed ) printf (" Hello world!\n"); return 0; } # include < iostream > using namespace std ; int main () { bool is_ printed = true ; if ( is_printed ) cout << " Hello world!" << endl ; return 0; } 17
1: C complex.h creal cimag conj cabs carg 18
# include <stdio.h> # include < complex.h> int main ( void ) { double complex a = 1 + 1I; printf ("a = (%.2f, %.2 f)\n", creal (a), cimag (a)); double complex b = conj ( a); printf ("b = (%.2f, %.2 f)\n", creal (b), cimag (b)); printf (" arg = %.5 f\n", carg (a)); printf (" abs = %.5 f\n", cabs (a)); return 0; } 19
2: C++ complex real imag conj abs arg 20
# include < cstdio > # include < complex > using namespace std ; int main ( void ) { complex < double > a (1.0, 1.0) ; printf ("a = (%.2f, %.2 f)\n", a. real (), a. imag ()); complex < double > b = conj (a); printf ("b = (%.2f, %.2 f)\n", b. real (), b. imag ()); printf (" arg = %.5 f\n", arg (a)); printf (" abs = %.5 f\n", abs (a)); return 0; } 21
1. 2. 1 22
0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1: 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 2: -10 23
int int C limits.h C++ limits std::numeric_limits C 24
int // int_info.c # include < stdio.h> # include < limits.h> int main ( void ) { printf (" range of int is % d ~ % d\ n", INT_MIN, INT_MAX ); printf (" sizeof int = %lu bytes \n", sizeof ( int )); return 0; } 25
int // int_info.c # include < stdio.h> # include < limits.h> int main ( void ) { printf (" range of int is % d ~ % d\ n", INT_MIN, INT_MAX ); printf (" sizeof int = %lu bytes \n", sizeof ( int )); return 0; } $ gcc int_info.c $./a. out range of int is - 2147483648 ~ 2147483647 sizeof int = 4 bytes 25
int // int_info. cpp # include < cstdio > # include < limits > using namespace std ; int main () { printf ( " range of int is %d ~ %d\n", numeric_limits <int >:: min (), numeric_limits <int >:: max () ); printf ( " sizeof int = %lu bytes \n", sizeof ( int ) ); return 0; } 26
int // int_info. cpp # include < cstdio > # include < limits > using namespace std ; int main () { printf ( " range of int is %d ~ %d\n", numeric_limits <int >:: min (), numeric_limits <int >:: max () ); printf ( " sizeof int = %lu bytes \n", sizeof ( int ) ); return 0; } $ g++ int_info. cpp $./a. out range of int is - 2147483648 ~ 2147483647 sizeof int = 4 bytes 26
int int int var ; int var1, var2 ; int 27
int int 1. int n; n = 1; 2. scanf int n; scanf ("%d", &n); 3. int n = 1; 28
int int a = 1; int b = 2, c = 3; int d, e = 4; // valid, but not good 29
int 30
int int a; a int b=2; 2 b 3: 30
int // print1. c: display some properties of printf () # include < stdio.h> int main ( void ) { int a = 10; int b = 2; printf (" Doing it right : "); printf ("%d - %d = %d\n", a, b, a-b); printf (" Doing it wrong : "); printf ("%d - %d = %d\n", a); return 0; } 31
int // print1. c: display some properties of printf () # include < stdio.h> int main ( void ) { int a = 10; int b = 2; printf (" Doing it right : "); printf ("%d - %d = %d\n", a, b, a-b); printf (" Doing it wrong : "); printf ("%d - %d = %d\n", a); return 0; } $ gcc print1. c $./a. out Doing it right : 10-2 = 8 Doing it wrong : 10-73832 = 771 31
int print() a %d %d printf() 32
C 0x 0X 16 0x10 0X10 0 16 020 33
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; oct = %o; hex = %X\n", x, x, x); 7 printf (" dec = %d; oct = %#o; hex = %#X\n", x, x, x); 8 return 0; 9 } 34
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; oct = %o; hex = %X\n", x, x, x); 7 printf (" dec = %d; oct = %#o; hex = %#X\n", x, x, x); 8 return 0; 9 } $ gcc bases. c $./a. out dec = 100; oct = 144; hex = 64 dec = 100; oct = 0144; hex = 0 x64 34
%d %o %#o %x %X %#x %#X 35
C/C++ 4 int short long unsigned signed short (int) %hd, %ho, %hx long (int) %ld, %lo, %lx long long (int) %lld, %llo, % llx unsigned (int) %u unsigned long (int) unsigned long long (int) %lu %llu 36
C/C++ 4 int short long unsigned signed short (int) %hd, %ho, %hx long (int) %ld, %lo, %lx long long (int) %lld, %llo, % llx unsigned (int) %u unsigned long (int) unsigned long long (int) %lu %llu signed short short int signed short signed short int 36
? 37
? CPU long int short 37
? CPU long int short C/C++ short int long int 37
38
# include <stdio.h> # include <limits.h> int main ( void ) { int i = INT_MAX ; unsigned int j = UINT_ MAX ; printf ("i = %d, i+1 = %d, i+2 = %d\n", i, i+1, i +2) ; printf ("j = %u, j+1 = %u, j+2 = %u\n", j, j+1, j +2) ; return 0; } $ gcc intoverflow. c $./a. out i = 2147483647, i +1 = -2147483648, i +2 = -2147483647 j = 4294967295, j +1 = 0, j +2 = 1 39
unsigned int 0 int 2147483648 40
unsigned int 0 int 2147483648 40
char
char : char char a, b, =, +,? char 41
char char char ASCII 5 5 5 char 42
char char a, b; char c; A grade char grade = A ; 43
char char a, b; char c; A grade char grade = A ; A char grade ; // char grade = A ; // grade = A; // grade = "A"; // grade = 65; // 43
ASCII 44
ASCII 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 44
C/C++ 45
printf() %c %d ASCII 46
printf() %c %d ASCII // charcode. c: displays code number for a char # include <stdio.h> int main ( void ) { char c; printf (" Please input a character :\n"); scanf ("%c", &c); printf (" the code for %c is %d\n", c, c); return 0; } 46
printf() %c %d ASCII // charcode. c: displays code number for a char # include <stdio.h> int main ( void ) { char c; printf (" Please input a character :\n"); scanf ("%c", &c); printf (" the code for %c is %d\n", c, c); return 0; } $ gcc charcode. c $./a. out Please input a character : A 46
ch 0 1 0 0 0 0 1 1 %c %d A 65 47
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 48
C float double long double 49
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 50
// float.c # include <stdio.h> # include <float.h> int main ( void ) { printf (" ----float ---\n"); printf (" range : %e ~ %e\n", FLT_MIN, FLT_MAX ); printf (" epsilon : %e\n", FLT_EPSILON ); printf (" size : %lu bytes \n\n", sizeof ( float )); printf (" ---- double ---\n"); printf (" range : %e ~ %e\n", DBL_MIN, DBL_MAX ); printf (" epsilon : %e\n", DBL_EPSILON ); printf (" size : %lu bytes \n\n", sizeof ( double )); printf (" ---- long double ---\n"); printf (" range : %Le ~ %Le\n", LDBL_MIN, LDBL_MAX ); printf (" epsilon : %Le\n", LDBL_EPSILON ); printf (" size : %lu bytes \n", sizeof ( long double )); return 0; } 51
----float --- range : 1.175494 e -38 ~ 3.402823 e +38 epsilon : 1.192093 e -07 size : 4 bytes ---- double --- range : 2.225074 e -308 ~ 1.797693 e +308 epsilon : 2.220446 e -16 size : 8 bytes ---- long double --- range : 3.362103 e -4932 ~ 1.189731 e +4932 epsilon : 1.084202 e -19 size : 16 bytes 52
i // float. cpp # include <iostream > # include <limits > using namespace std ; int main ( void ) { cout << " ----float ----" << endl << " range : " << numeric_limits < float >:: min () << " ~ " << numeric_limits < float >:: max () << endl << " prec : " << numeric_limits < float >:: epsilon () << endl << " size : " << sizeof ( float ) << endl << endl ; cout << " ---- double ----" << endl << " range : " << numeric_limits < double >:: min () << " ~ " << numeric_limits < double >:: max () << endl << " prec : " << numeric_limits < double >:: epsilon () << endl 53
ii << " size : " << sizeof ( double ) << endl ; } cout << " ---- long double ----" << endl << " range : " << numeric_limits < long double >:: min () << " ~ " << numeric_limits < long double >:: max () << endl << " prec : " << numeric_limits < long double >:: epsilon () << endl << " size : " << sizeof ( long double ) << endl ; return 0; 54
----float ---- range : 1.17549 e -38 ~ 3.40282 e +38 prec : 1.19209 e -07 size : 4 ---- double ---- range : 2.22507 e -308 ~ 1.79769 e +308 prec : 2.22045 e -16 size : 8 ---- long double ---- range : 3.3621 e -4932 ~ 1.18973 e +4932 prec : 1.0842 e -19 size : 16 55
1. (sign) 0 1 2. (exponent) 3. (mantissa) 56
R32.24 R64.53 (120.5) 10 = (1110110.1) 2 (1.1101101 2 6 ) 2 57
R32.24 R64.53 (120.5) 10 = (1110110.1) 2 (1.1101101 2 6 ) 2 1. 2 n. 1 23 24 57
24 (9) 10 = (1001) 2, 1 24 float 6 58
8 127 128 +127 59
8.25 1. 8.25 1.0001 2 3 60
8.25 1. 8.25 1.0001 2 3 2. 0 3 +127 = 130 = (1000 0010) 2 (0001) 2 60
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 60
120.5 1. 120.5 1.1101101 2 6 61
120.5 1. 120.5 1.1101101 2 6 2. 0 6 +127 = 133 = (1000 0101) 2 (1101101) 2 61
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 61
01000010111001011000000000000000, 62
1. 0 1000 0101 110 0101 1000 0000 0000 0000 63
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. 63
// float2double. c: # include <stdio.h> int main ( void ) { float f1 = 2.2, f2 = 2.25; double g1, g2; g1 = ( double ) f1; g2 = ( double ) f2; printf ("g1 = %.13f, g2 = %.13 f\n", g1, g2); return 0; } 64
// float2double. c: # include <stdio.h> int main ( void ) { float f1 = 2.2, f2 = 2.25; double g1, g2; g1 = ( double ) f1; g2 = ( double ) f2; printf ("g1 = %.13f, g2 = %.13 f\n", g1, g2); return 0; } g1 = 2. 2000000476837, g2 = 2. 2500000000000 64
2.2 2.25 65
2.2 2.25 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 65
2 0. 2 2 0. 4 0 2 0. 8 0 2 1. 6 1 2 1. 2 1 2 0. 4 0 66
2.2 10. 0011 0011 0011 0011 0011... 67
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 68
e E 10-1.56 E+12, 2.87e -3 69
+2.87e -3 2.87e -3 2E5 19.28 2 // is an integer 3. e12.45e -5 70
1.56 E +12 // wrong double some float some = 4.0 * 2. 0; 4.0 2.0 double 64 float 71
f F float 2.3 f 3.4 e9f l L long double l 1 L 54.3 l 4.3 E9L 72
// float1. c: What is the output? # include < stdio.h> int main ( void ) { float x = 0.1; if (x == 0.1) printf ("IF\n"); else if (x == 0.1 f) printf (" ELSE IF\n"); else printf (" ELSE \n"); } 73
// float1. c: What is the output? # include < stdio.h> int main ( void ) { float x = 0.1; if (x == 0.1) printf ("IF\n"); else if (x == 0.1 f) printf (" ELSE IF\n"); else printf (" ELSE \n"); } ELSE IF 73
// float1. c: What is the output? # include < stdio.h> int main ( void ) { float x = 0.1; if (x == 0.1) printf ("IF\n"); else if (x == 0.1 f) printf (" ELSE IF\n"); else printf (" ELSE \n"); } ELSE IF x 0.1 0.1f 0.1 0.1 0.1 0.1!= 0.1f x == 0.1f 73
// float2. c: What is the output? # include < stdio.h> int main ( void ) { float x = 0.1; printf ("%lu %lu %lu\n", sizeof (x), sizeof (0.1), sizeof (0.1 f)); return 0; } 74
// float2. c: What is the output? # include < stdio.h> int main ( void ) { float x = 0.1; printf ("%lu %lu %lu\n", sizeof (x), sizeof (0.1), sizeof (0.1 f)); return 0; } 4 8 4 74
// float2. c: What is the output? # include < stdio.h> int main ( void ) { float x = 0.1; printf ("%lu %lu %lu\n", sizeof (x), sizeof (0.1), sizeof (0.1 f)); return 0; } 4 8 4 74
// float3. c: What is the output? # include < stdio.h> int main ( void ) { float x = 0.5; if (x == 0.5) printf ("IF\n"); else if (x == 0.5 f) printf (" ELSE IF\n"); else printf (" ELSE \n"); } 75
// float3. c: What is the output? # include < stdio.h> int main ( void ) { float x = 0.5; if (x == 0.5) printf ("IF\n"); else if (x == 0.5 f) printf (" ELSE IF\n"); else printf (" ELSE \n"); } IF 75
// float3. c: What is the output? # include < stdio.h> int main ( void ) { float x = 0.5; if (x == 0.5) printf ("IF\n"); else if (x == 0.5 f) printf (" ELSE IF\n"); else printf (" ELSE \n"); } IF x 0.5 0.5f 0.5 0.5f 0.5 0.5 == 0.5f x == 0.5 75
%f float double %e %Lf %Le long double 76
// showf_pt.c: # include <stdio.h> int main ( void ) { float a = 32000.; double b = 2.14 e9; long double c = 5. 32 e -5; printf ("%f can be written as %e\n", a, a); printf ("%f can be written as %e\n", b, b); printf ("%Lf can be written as %Le\n", c, c); return 0; } 77
// showf_pt.c: # include <stdio.h> int main ( void ) { float a = 32000.; double b = 2.14 e9; long double c = 5. 32 e -5; printf ("%f can be written as %e\n", a, a); printf ("%f can be written as %e\n", b, b); printf ("%Lf can be written as %Le\n", c, c); return 0; } 32000. 000000 can be written as 3. 200000 e +04 2140000000. 000000 can be written as 2. 140000 e +09 0. 000053 can be written as 5. 320000 e -05 77
# include <stdio.h> int main ( void ) { float toobig = 1 e39 ; printf (" toobig = %f\n",toobig ); return 0; } 78
# include <stdio.h> int main ( void ) { float toobig = 1 e39 ; printf (" toobig = %f\n",toobig ); return 0; } toobig = inf 78
# include <stdio.h> int main ( void ) { float toobig = 1 e39 ; printf (" toobig = %f\n",toobig ); return 0; } toobig = inf overflow C inf 78
# include <stdio.h> int main ( void ) { float a,b; a = 2. e20 + 1; b = a - 2. e20 ; printf ("b = %f\n", b); return 0; } 79
# include <stdio.h> int main ( void ) { float a,b; a = 2. e20 + 1; b = a - 2. e20 ; printf ("b = %f\n", b); return 0; } b = 4008175468544. 000000 79
# include <stdio.h> int main ( void ) { float a,b; a = 2. e20 + 1; b = a - 2. e20 ; printf ("b = %f\n", b); return 0; } b = 4008175468544. 000000 2.0e20 1 21 21 float 6 7 79