C/C++ /
Table of contents 1. 2. 3. 4. 1
i # include <stdio.h> # include <string.h> // density of human body : 1. 04 e3 kg / m ^3 # define DENSITY 1. 04 e3 int main ( void ) { float weight, volume ; int size, letters ; char name [40]; printf ("Hi! What s your first name?\n"); scanf ("%s", name ); printf ("%s, what s your weight in kilograms?\n ", name ); 2
ii } scanf ("%f", & weight ); size = sizeof name ; letters = strlen ( name ); volume = weight / DENSITY ; printf ("Well, %s, your volume is %2.2 f cubic meters.\n", name, volume ); printf (" Also, your first name has % d letters,\ n", letters ); printf (" and we have % d bytes to store in it.\ n ", size ); return 0; 3
Hi! What s your first name? xiaoping xiaoping, what s your weight in kilograms? 60 Well, xiaoping, your volume is 0. 06 cubic meters. Also, your first name has 8 letters, and we have 40 bytes to store in it. 4
string " Once more you open the door!" 5
C C char # include <stdio.h> int main ( void ) { char greeting [6] = { H, e, l, l, o, \0 }; printf (" Greeting message : "); printf ("%s\n", greeting ); return 0; } 6
C++ C++ C C C C++ C++ string C++ string 7
// C style string # include <iostream > using namespace std ; int main () { char greeting [6] = " Hello "; cout << " Greeting message : "; cout << greeting << endl ; return 0; } // C++ string class # include <iostream > # include <string > using namespace std ; int main ( void ) { string greeting = " Hello "; cout << " Greeting message : "; cout << greeting << endl ; return 0; } 8
C C \0 \0 0 ASCII 0 \0 1 9
: array 10
: array char name [40]; 40 char 10
: array char name [40]; 40 char [] name [] 40 char 10
C \0 char greeting [10] = { H, e, l, l, 0, \0 }; 11
C \0 char greeting [10] = { H, e, l, l, 0, \0 }; char greeting [10] = " Hello "; 11
# include <stdio.h> # define PRAISE " What s a super marvelous name!" int main ( void ) { char name [40]; printf (" What s your name?\n"); scanf ("%s", name ); printf (" Hello, %s. %s\n", name, PRAISE ); return 0; } 12
# include <stdio.h> # define PRAISE " What s a super marvelous name!" int main ( void ) { char name [40]; printf (" What s your name?\n"); scanf ("%s", name ); printf (" Hello, %s. %s\n", name, PRAISE ); return 0; } What s your name? Xiaoping Zhang Hello, Xiaoping. What a super marvelous name! 12
# include < iostream > # include < string > using namespace std ; # define PRAISE " What s a super marvelous name!" int main ( void ) { string name ; cout << " What s your name?" << endl ; cin >> name ; cout << " Hello, " << name << ". " << PRAISE << endl ; return 0; } 13
# include < iostream > # include < string > using namespace std ; # define PRAISE " What s a super marvelous name!" int main ( void ) { string name ; cout << " What s your name?" << endl ; cin >> name ; cout << " Hello, " << name << ". " << PRAISE << endl ; return 0; } What s your name? Xiaoping Zhang Hello, Xiaoping. What a super marvelous name! 13
C \0 name scanf() name & name %s scanf() C++ cin 14
"x" x x "x" "x" x \0 15
C char name[40] = "Hello"; sizeof name name 40 strlen(name) name \0 5 C++ string name = "Hello"; sizeof name name.size() \0 5 16
# include <stdio.h> # include <string.h> # define PRAISE " What s a super marvelous name!" int main ( void ) { char name [40]; printf (" What s your name?\n"); scanf ("%s", name ); printf (" Hello, %s. %s\n", name, PRAISE ); printf (" Your name of % ld letters occupises % ld memory cells.\n", strlen ( name ), sizeof name ); printf (" PRAISE has % ld letters and occupies % ld memory cells.\n", strlen ( PRAISE ), sizeof PRAISE ); return 0; } 17
What s your name? Xiaoping Hello, Xiaoping. What a super marvelous name! Your name of 8 letters occupied 40 memory cells. PRAISE has 30 letters and occpied 31 memory cells. 18
i # include < iostream > # include < string > # include < cstring > using namespace std ; # define PRAISE " What s a super marvelous name!" int main ( void ) { string name ; cout << " What s your name?" << endl ; cin >> name ; cout << " Hello, " << name << ". " << PRAISE << endl ; cout << " Your name of " << name. size () 19
ii } << " letters occupies " << sizeof name << " memory cells." << endl ; cout << " PRAISE has " << strlen ( PRAISE ) << " letters and occupies " << sizeof PRAISE << " memory celss." << endl ; return 0; 20
What s your name? Xiaoping Hello, Xiaoping. What s a super marvelous name! Your name of 8 letters occupies 32 memory cells. PRAISE has 30 letters and occupies 31 memory celss. 21
string.h strlen C (1) printf scanfs stdio.h (2) strlen string.h 22
printf printf printf 23
sizeof strlen name = "Morgan" sizeof name : 40 strlen ( name ): 6 M o r g a n \0 24
sizeof strlen sizeof PRAISE : 29 strlen ( PRAISE ): 28 sizeof 25
sizeof sizeof ( float ) sizeof ( char ) sizeof name sizeof 2. 15 sizeof ( name ) sizeof (2.15) 26
// circle1.c: # include <stdio.h> int main ( void ) { float radius, circum, area ; radius = 1; area = 3. 1415926 * radius * radius ; circum = 2 * 3. 1415926 * radius ; printf (" radius = %f, circum = %f, area = %f\n", radius, circum, area ); return 0; } 27
// circle2.c: # include <stdio.h> int main ( void ) { float radius, circum, area ; float pi = 3. 1415926; radius = 1; area = pi * radius * radius ; circum = 2 * pi * radius ; printf (" radius = %f, circum = %f, area = %f\n", radius, circum, area ); return 0; } 28
// circle3.c: # include <stdio.h> # define PI 3. 1415926 int main ( void ) { float radius, circum, area ; radius = 1; area = PI * radius * radius ; circum = 2 * PI * radius ; printf (" radius = %f, circum = %f, area = %f\n", radius, circum, area ); return 0; } 29
// circle4.c: # include <stdio.h> int main ( void ) { float radius, circum, area ; const float PI = 3. 1415926; radius = 1.; area = PI * radius * radius ; circum = 2 * PI * radius ; printf (" radius = %f, circum = %f, area = %f\n", radius, circum, area ); return 0; } 30
# define NAME value 31
# define NAME value C 31
# define NAME value C #define " " # define BEEP \a # define TEE T # define ESC \033 # define OOPS " Now you have done it!" 31
# define B = 20 32
# define B = 20 B = 20 20 c = a + B; c = a + = 20; 32
33
# include <stdio.h> # define MAX (a,b) ((a) > (b)? (a) : (b)) int main ( void ) { printf (" max (1, 2) = %d\n", MAX (1, 2)); printf (" 1.0 + max (1.1+2.2, 3.0) = %.2 f\n", 1.0 + MAX (1.1+2.2, 3.0) ); return 0; } 33
# include <stdio.h> # define MAX (a,b) ((a) > (b)? (a) : (b)) int main ( void ) { printf (" max (1, 2) = %d\n", MAX (1, 2)); printf (" 1.0 + max (1.1+2.2, 3.0) = %.2 f\n", 1.0 + MAX (1.1+2.2, 3.0) ); return 0; } max (1, 2) = 2 1.0 + max (1.1+2.2, 3.0) = 4.30 33
# include <stdio.h> # define MAX (a,b) (a) > (b)? (a) : (b) int main ( void ) { printf (" max (1, 2) = %d\n", MAX (1, 2)); printf (" 1.0 + max (1.1+2.2, 3.0) = %.2 f\n", 1.0 + MAX (1.1+2.2, 3.0) ); return 0; } 34
# include <stdio.h> # define MAX (a,b) (a) > (b)? (a) : (b) int main ( void ) { printf (" max (1, 2) = %d\n", MAX (1, 2)); printf (" 1.0 + max (1.1+2.2, 3.0) = %.2 f\n", 1.0 + MAX (1.1+2.2, 3.0) ); return 0; } max (1, 2) = 2 1.0 + max (1.1+2.2, 3.0) = 3.30 34
# include <stdio.h> # define PRINT_ SQUARE ( x) printf (" the square of " #x " is %d.\n", (x) * (x)); int main ( void ) { PRINT_ SQUARE (3) ; PRINT_SQUARE (3+2) ; return 0; } 35
# include <stdio.h> # define PRINT_ SQUARE ( x) printf (" the square of " #x " is %d.\n", (x) * (x)); int main ( void ) { PRINT_ SQUARE (3) ; PRINT_SQUARE (3+2) ; return 0; } the square of 3 is 9. the square of 3+2 is 25. 35
# include <stdio.h> # define PRINT_ SQUARE ( x) printf (" the square of " #x " is %d.\n", x * x); int main ( void ) { PRINT_ SQUARE (3) ; PRINT_SQUARE (3+2) ; return 0; } 36
# include <stdio.h> # define PRINT_ SQUARE ( x) printf (" the square of " #x " is %d.\n", x * x); int main ( void ) { PRINT_ SQUARE (3) ; PRINT_SQUARE (3+2) ; return 0; } the square of 3 is 9. the square of 3+2 is 11. 36
# include <stdio.h> # define PRINT (a) \ do { \ printf ("%s: %d\n", #a, a); \ printf ("%d: %d\n", a, a); \ } while (0) ; int main ( void ) { PRINT (3) ; PRINT (3+2) ; return 0; } 37
# include <stdio.h> # define PRINT (a) \ do { \ printf ("%s: %d\n", #a, a); \ printf ("%d: %d\n", a, a); \ } while (0) ; int main ( void ) { PRINT (3) ; PRINT (3+2) ; return 0; } 3: 3 3: 3 3+2: 5 5: 5 37
# include <stdio.h> # define X(n) x##n # define PXN (n) printf ("x"#n" = %d\n", x##n); int main ( void ) { int X (1) = 12; PXN (1) ; int X (2) = 24; PXN (2) ; int X (3) = 36; PXN (3) ; return 0; } 38
# include <stdio.h> # define X(n) x##n # define PXN (n) printf ("x"#n" = %d\n", x##n); int main ( void ) { int X (1) = 12; PXN (1) ; int X (2) = 24; PXN (2) ; int X (3) = 36; PXN (3) ; return 0; } x1 = 12 x2 = 24 x3 = 36 38
\ 39
\ # ## # x #x ## #define X(n) x##n X(2) x2. #define CONS(a,b) (a##e##b) CONS(2,3) 2e3. 39
const C90 const const int MONTHS = 12; MONTHS 40
1: %a p- %A P- %c %d %e e- %E E- %f %g %f %e %e -4 %G %f %E %E -4 41
2: %i %d %o %p %s %x 0-f %X 0-F 42
printf() printf(control-string, item1, item2,...); item1, item2,... (Control-string) 43
printf() printf(control-string, item1, item2,...); item1, item2,... (Control-string) printf( "You look great in %s\n", color ); 43
printf() printf(control-string, item1, item2,...); item1, item2,... (Control-string) printf( "You look great in %s\n", color ); 43
% %% printf (" Once more you open the door!\n"); printf ("%s%d\n", " area = ", area ); printf ("%d%% = %f\n", 30, 0.3) ; 44
%d // width.c: # include <stdio.h> # define N 1000 int main ( void ) { printf ("*%d*\n", N); printf (" *%2 d*\n", N); printf (" *%10 d*\n", N); printf (" *% -10 d*\n", N); printf (" *%010 d*\n", N); return 0; } 45
%d // width.c: # include <stdio.h> # define N 1000 int main ( void ) { printf ("*%d*\n", N); printf (" *%2 d*\n", N); printf (" *%10 d*\n", N); printf (" *% -10 d*\n", N); printf (" *%010 d*\n", N); return 0; } *1000* *1000* * 1000* *1000 * * 0000001000* 45
%d %d 46
%d %d %md m m m 46
%d %d %md m m m %-md m m m 46
%d %0 md m m 0 m 47
// flags.c: # include <stdio.h> int main ( void ) { printf ("%x %X %#x %#X\n", 31, 31, 31, 31) ; printf ("*%d*\n", 42) ; printf ("*% d*\n", 42) ; printf ("*% d*\n", -42); printf (" *%5 d*\n", 6); printf (" *%5.3 d*\n", 6); printf (" *%05 d*\n", 6); printf (" *%05.3 d*\n", 6); return 0; } 48
%d 1f 1F 0 x1f 0 X1F *42* * 42* * -42* * 6* * 006* *00006* * 006* 49
%d % d 50
%d % d %m.nd, %0md, %0m.nd %5.3d %05d %05.3d 0 0 50
%x, %#x, %X, %#X %x, %#x, %X, %#X %x 1f %X 1F %#x 0x1f %#X 0X1F 51
%f, %e, %E // floats.c: # include <stdio.h> int main ( void ) { const double RENT = 3852. 42; printf ("*%f*\n", RENT ); printf ("*%e*\n", RENT ); printf (" *%4.2 f*\n", RENT ); printf (" *%3.1 f*\n", RENT ); printf (" *%10.3 f*\n", RENT ); printf (" *%10.3 e*\n", RENT ); printf (" *%10.3 E*\n", RENT ); printf (" *%+4.2 f*\n", RENT ); printf (" *% -10.2 f*\n", RENT ); printf (" *%010.2 f*\n", RENT ); printf (" *%10. f*\n", RENT ); printf (" *%.4 f*\n", RENT ); return 0; } 52
%f, %e, %E * 3852. 990000* *3.852990 e +03* *3852.99* *3853.0* * 3852.990* * 3.853 e +03* * 3.853 E +03* *+3852.99* *3852.99 * * 0003852. 99* 53
%f, %e, %E %m.nf %m.ne %m.ne m n 54
%f, %e, %E %m.nf %m.ne %m.ne m n %. nf n 54
%f, %e, %E %m.nf %m.ne %m.ne m n %. nf n %m.f m 54
%s // strings.c: # include <stdio.h> # define WORD " Hello World!" int main ( void ) { printf (" *%2 s*\n", WORD ); printf (" *%15 s*\n", WORD ); printf (" *%4.5 s*\n", WORD ); printf (" *% -15.5 s*\n", WORD ); printf (" *%.5 s*\n", WORD ); return 0; } 55
%s // strings.c: # include <stdio.h> # define WORD " Hello World!" int main ( void ) { printf (" *%2 s*\n", WORD ); printf (" *%15 s*\n", WORD ); printf (" *%4.5 s*\n", WORD ); printf (" *% -15.5 s*\n", WORD ); printf (" *%.5 s*\n", WORD ); return 0; } * Hello World!* * Hello World!* * Hello * * Hello * * Hello * 55
%f, %e, %E %s 56
%f, %e, %E %s %ms m m m 56
%f, %e, %E %s %ms m m m %m.ns m n m n m n n 56
%f, %e, %E %s %ms m m m %m.ns m n m n m n n %. ns n 56
printf() # include <stdio.h> int main ( void ) { int bph2o = 100; int rv; rv = printf (" Hello \n"); printf (" the printf function printed % d character.\n", rv); return 0; } 57
printf() # include <stdio.h> int main ( void ) { int bph2o = 100; int rv; rv = printf (" Hello \n"); printf (" the printf function printed % d character.\n", rv); return 0; } 100 C is water s boiling point. the printf function printed 32 character. printf() 57
printf() %n printf() %n %n 58
printf() %n printf() %n %n // printf_n.c: # include < stdio.h> int main ( void ) { int c1, c2; printf (" Hello Wuhan % nuniversity!%n\n", &c1, & c2); printf ("c1 = %d, c2 = %d\n", c1, c2); return 0; } 58
printf() %n printf() %n %n // printf_n.c: # include < stdio.h> int main ( void ) { int c1, c2; printf (" Hello Wuhan % nuniversity!%n\n", &c1, & c2); printf ("c1 = %d, c2 = %d\n", c1, c2); return 0; } Hello Wuhan University! c1 = 12, c2 = 23 58
printf() scanf() printf() scanf() 59
printf() scanf() printf() scanf() scanf() & scanf() & 59
scanf() // input.c: # include <stdio.h> int main ( void ) { int age ; double weight ; char name [20]; printf (" Enter your name, age and weight :\n"); scanf ("%s", name ); scanf ("%d, %lf", &age, & weight ); printf ("%s: %d %f\n", name, age, weight ); return 0; } 60
scanf() Enter your name, age and weight : Xiaoming 23 100 Xiaoming : 23 100.000000 61
scanf() scanf() %c printf() %f %e %E %g %G float double scanf() float double l 62
scanf %c %d %e, %f, %g, %a %E, %F, %G, %A %i %o %p 63
scanf %s %u %x, %X 64
scanf % 65
scanf % * %*d digit %10s hh signed char unsigned char % hhd %hhu ll long long unsigned long long %lld %llu 65
scanf() %hd, %hi %ho, %hx, %hu %ld, %li %lo, %lx, %lu %le, %lf, %lg %Le, %Lf, %Lg short unsigned short long unsigned long double long double 66
scanf() %hd, %hi %ho, %hx, %hu %ld, %li %lo, %lx, %lu %le, %lf, %lg %Le, %Lf, %Lg short unsigned short long unsigned long double long double %d, %i, %o %x int %e, %f, %g float 66
scanf() 67
scanf() scanf ("%d, %d", &n, &m); 67
scanf() scanf ("%d, %d", &n, &m); 12, 23 12, 23 12, 23 12, 23 67
scanf ("%d and %d", &n, &m); 68
scanf ("%d and %d", &n, &m); 12 and 23 12 and 23 12 and23 68
%c scanf ("%d%d",&n,&m); scanf ("%d %d",&n,&m); 69
%c scanf ("%c",&ch) scanf (" %c",&ch) 70
scanf() scanf() 0 (end of file) EOF EOF stdio.h -1 71
printf() * i // varwidth.c: # include <stdio.h> int main ( void ) { unsigned width, precision ; int number = 256; double weight = 123. 5; printf (" What field width?\n"); scanf ("%d", & width ); printf (" The number is :%* d\n", width, number ); printf (" Now enter a width and a precision :\ n") ; scanf ("%d %d", & width, & precision ); 72
printf() * ii } printf (" Weight =%*.* f\n", width, precision, weight ); return 0; 73
printf() * What field width? 6 The number is: 256 Now enter a width and a precision : 8 3 Weight = 123.500 74
scanf() * scanf() * % 75
scanf() * // skip2.c: # include <stdio.h> int main ( void ) { int n; printf (" Please enter three integers :\n"); scanf ("%*d %*d %d", &n); printf (" The last integer was %d\n", n); return 0; } 76
scanf() * // skip2.c: # include <stdio.h> int main ( void ) { int n; printf (" Please enter three integers :\n"); scanf ("%*d %*d %d", &n); printf (" The last integer was %d\n", n); return 0; } Please enter three integers : 10 20 30 The last integer was 30 76