C 2017 3 14
1. 2. 3. 4. 2/95 C
1. 3/95 C
I 1 // talkback.c: 2 #include <stdio.h> 3 #include <string.h> 4 #define DENSITY 62.4 5 int main(void) 6 { 7 float weight, volume; 8 int size; 9 unsigned long letters; 10 char name[40]; 11 printf("hi! What s your name?\n"); 4/95 C
II 12 scanf("%s", name); 13 printf("%s, what s your weight in pounds?\n", name); 14 scanf("%f", &weight); 15 size = sizeof name; 16 letters = strlen(name); 17 volume = weight/density; 18 printf("well, %s, your volume is %2.2f cubic feet.\n", name, volume); 19 printf("also, your first name has %lu letters, \n", letters); 5/95 C
III 20 printf("and we have %d bytes to store it in.\n ", size); 21 return 0; 22 } 6/95 C
Hi! What s your name? Xiaoping Xiaoping, what s your weight in pounds? 139 Well, Xiaoping, your volume is 2.23 cubic feet. Also, your first name has 8 letters, and we have 40 bytes to store it in. 7/95 C
character string "Once more you open the door!" 8/95 C
C char 9/95 C
O n c e m o r e y o u o p e n t h e d o o r! \0 1 10/95 C
\0 (null character) C 0 ASCII 0 1 11/95 C
(array) 12/95 C
char name[40]; 40 char 13/95 C
char name[40]; 40 char name 40 char 13/95 C
\0 14/95 C
1 // praise1.c 2 #include <stdio.h> 3 #define PRAISE "What a super marvelous name!" 4 int main(void) 5 { 6 char name[40]; 7 8 printf("what s your name?\n"); 9 scanf("%s", name); 10 printf("hello, %s. %s\n", name, PRAISE); 11 return 0; 12 } 15/95 C
$ gcc praise1.c $./a.out What s your name? Xiaoping Zhang Hello, Xiaoping. What a super marvelous name! 16/95 C
scanf() name scanf name & name %s scanf Xiaoping 17/95 C
"x" x x "x" "x" x \0 18/95 C
strlen sizeof strlen 19/95 C
strlen I 1 // praise2.c: 2 #include <stdio.h> 3 #include <string.h> 4 #define PRAISE "What a super marvelous name!" 5 int main(void) 6 { 7 char name[40]; 8 printf("what s your name?\n"); 9 scanf("%s", name); 10 printf("hello, %s. %s\n", name, PRAISE); 20/95 C
strlen II 11 printf("your name of %lu letters occupied %lu memory cells.\n", strlen(name), sizeof name); 12 printf("the phrase of PRAISE has %lu letters", strlen(praise)); 13 printf(" and occpied %lu memory cells.\n", sizeof PRAISE); 14 return 0; 15 } 21/95 C
strlen $ gcc praise2.c $./a.out What s your name? Xiaoping Hello, Xiaoping. What a super marvelous name! Your name of 8 letters occupied 40 memory cells. The phrase of PRAISE has 28 letters and occpied 29 memory cells. 22/95 C
strlen string.h strlen C (1) printf scanfs stdio.h (2) strlen string.h 23/95 C
printf printf printf 24/95 C
sizeof strlen name = "Morgan" sizeof name : 40 strlen(name): 6 M o r g a n \0 25/95 C
sizeof strlen sizeof PRAISE : 29 strlen(praise): 28 sizeof 26/95 C
sizeof sizeof(float) sizeof(char) sizeof name sizeof 2.15 sizeof(name) sizeof(2.15) 27/95 C
2. 28/95 C
1 // circle1.c: 2 #include <stdio.h> 3 int main(void) 4 { 5 float radius, circum, area; 6 radius = 1; 7 area = 3.1415926 * radius * radius; 8 circum = 2 * 3.1415926 * radius; 9 printf("radius = %f, circum = %f, area = %f\n", radius, circum, area); 10 return 0; 11 } 29/95 C
radius = 1.000000, circum = 6.283185, area = 3.141593 30/95 C
1 // circle2.c: 2 #include <stdio.h> 3 int main(void) { 4 float radius, circum, area; 5 float pi = 3.1415926; 6 radius = 1; 7 area = pi * radius * radius; 8 circum = 2 * pi * radius; 9 printf("radius = %f, circum = %f, area = %f\n", radius, circum, area); 10 return 0; 11 } 31/95 C
1 // circle3.c: 2 #include <stdio.h> 3 #define PI 3.1415926 4 int main(void) { 5 float radius, circum, area; 6 radius = 1; 7 area = PI * radius * radius; 8 circum = 2 * PI * radius; 9 printf("radius = %f, circum = %f, area = %f\n", radius, circum, area); 10 return 0; 11 } 32/95 C
1 // circle4.c: 2 #include <stdio.h> 3 int main(void) { 4 float radius, circum, area; 5 const float PI = 3.1415926; 6 radius = 1.; 7 area = PI * radius * radius; 8 circum = 2 * PI * radius; 9 printf("radius = %f, circum = %f, area = %f\n", radius, circum, area); 10 return 0; 11 } 33/95 C
#define NAME value C 34/95 C
#define #define BEEP \a #define TEE T #define ESC \033 #define OOPS "Now you have done it!" 35/95 C
#define 36/95 C
#define #define B = 20 B = 20 20 c = a + B; c = a + = 20; 36/95 C
const C90 const const int MONTHS = 12; MONTHS 37/95 C
3. 38/95 C
: %a p- %A P- %c %d 39/95 C
: %e e- %E E- %f %g %f %e %e -4 %G %f %E %E -4 40/95 C
: %i %d %o %p %s 41/95 C
: %x 0-f %X 0-F 42/95 C
printf printf(control-string, item1, item2,...); item1, item2 (Control-string) 43/95 C
printf( "You look great in %s\n", color ); 44/95 C
45/95 C
% %% printf("once more you open the door!\n"); printf("%s%d\n", "area = ", area); printf("%d%% = %f\n", 30, 0.3); 46/95 C
%d %d: %md: m <m >=m %-md: m <m >=m %0md: m <m 0 >=m 47/95 C
%d 1 // width.c: 2 #include <stdio.h> 3 #define N 1000 4 int main(void) 5 { 6 printf("*%d*\n", N); 7 printf("*%2d*\n", N); 8 printf("*%10d*\n", N); 9 printf("*%-10d*\n", N); 10 printf("*%010d*\n", N); 11 return 0; 12 } 48/95 C
%d *1000* *1000* * 1000* *1000 * *0000001000* 49/95 C
%f %e %E I 1 // floats.c: 2 #include <stdio.h> 3 int main(void) 4 { 5 const double RENT = 3852.99; 6 printf("*%f*\n", RENT); 7 printf("*%e*\n", RENT); 8 printf("*%4.2f*\n", RENT); 9 printf("*%3.1f*\n", RENT); 10 printf("*%10.3f*\n", RENT); 11 printf("*%10.3e*\n", RENT); 50/95 C
%f %e %E II 12 printf("*%10.3e*\n", RENT); 13 printf("*%+4.2f*\n", RENT); 14 printf("*%-10.2f*\n", RENT); 15 printf("*%010.2f*\n", RENT); 16 return 0; 17 } 51/95 C
%f %e %E *3852.990000* *3.852990e+03* *3852.99* *3853.0* * 3852.990* * 3.853e+03* * 3.853E+03* *+3852.99* *3852.99 * *0003852.99* 52/95 C
%f %e %E %m.nf %m.ne %m.ne m n 53/95 C
%f %e %E %.nf n 54/95 C
%f %e %E %m.f m 55/95 C
I 1 // flags.c: 2 #include <stdio.h> 3 int main(void) 4 { 5 printf("%x %X %#x %#X\n", 31, 31, 31, 31); 6 printf("*%d*\n", 42); 7 printf("*% d*\n", 42); 8 printf("*% d*\n", -42); 9 printf("*%5d*\n", 6); 10 printf("*%5.3d*\n", 6); 11 printf("*%05d*\n", 6); 56/95 C
II 12 printf("*%05.3d*\n", 6); 13 return 0; 14 } 57/95 C
1f 1F 0x1f 0X1F *42* * 42* *-42* * 6* * 006* *00006* * 006* 58/95 C
%x 1f %X 1F %#x 0x1f %#X 0X1F 59/95 C
% d 60/95 C
%5.3d %05d %05.3d 0 0 61/95 C
1 // strings.c: 2 #include <stdio.h> 3 #define WORD "Hello World!" 4 int main(void) 5 { 6 printf("*%2s*\n", WORD); 7 printf("*%15s*\n", WORD); 8 printf("*%15.5s*\n", WORD); 9 printf("*%-15.5s*\n", WORD); 10 return 0; 11 } 62/95 C
*Hello World!* * Hello World!* * Hello* *Hello * 63/95 C
%15.5s printf 5-64/95 C
printf() 1 #include <stdio.h> 2 int main(void) 3 { 4 int bph2o = 100; 5 int rv; 6 rv = printf("%d C is water s boiling point.\n", bph2o); 7 printf("the printf function printed %d character.\n", rv); 8 return 0; 9 } 65/95 C
printf() 100 C is water s boiling point. the printf function printed 32 character. 66/95 C
printf() printf() 67/95 C
printf() %n printf() %n %n 68/95 C
printf() %n 1 // printf_n.c: 2 #include<stdio.h> 3 int main(void) 4 { 5 int c1, c2; 6 printf("hello Wuhan %nuniversity!%n\n", &c1, & c2); 7 printf("c1 = %d, c2 = %d\n", c1, c2); 8 return 0; 9 } 69/95 C
printf() %n $ gcc printf_n.c $./a.out Hello Wuhan University! c1 = 12, c2 = 23 70/95 C
4. 71/95 C
printf() scanf() printf() scanf() 72/95 C
scanf() scanf() & scanf() & 73/95 C
scanf() 1 // input.c: 2 #include <stdio.h> 3 int main(void) { 4 int age; 5 double weight; 6 char name[20]; 7 printf("enter your name, age and weight:\n"); 8 scanf("%s", name); 9 scanf("%d and %lf", &age, &weight); 10 printf("%s: %d %f\n", name, age, weight); 11 return 0; 12 } 74/95 C
scanf() $ gcc input.c $./a.out Enter your name, age and weight: Xiaoming 23 100 Xiaoming: 23 100.000000 75/95 C
scanf() scanf() %c 76/95 C
printf scanf printf() %f %e %E %g %G float double scanf() float double l 77/95 C
scanf %c %d %e,%f,%g,%a %E,%F,%G,%A %i %o %p 78/95 C
scanf %s %u %x,%x 79/95 C
scanf % 80/95 C
scanf * "%*d" digit "%10s" hh signed char unsigned char "%hhd" "%hhu" ll long long unsigned long long "%lld" "%llu" 81/95 C
scanf "%hd", "%hi" "%ho", "%hx", "%hu" "%ld", "%li" short unsigned short long "%lo", "%lx", unsigned long "%lu" "%le", "%lf", double "%lg" "%Le", "%Lf", long double "%Lg" 82/95 C
scanf %d, %i, %o %x int %e, %f %g float 83/95 C
scanf() 84/95 C
scanf("%d, %d", &n, &m); 12, 23 12, 23 12, 23 12, 23 85/95 C
scanf("%d and %d", &n, &m); 12 and 23 12 and 23 12and23 86/95 C
%c scanf("%d%d",&n,&m); scanf("%d %d",&n,&m); 87/95 C
%c scanf("%c",&ch) scanf(" %c",&ch) 88/95 C
scanf scanf() 0 (end of file) EOF EOF stdio.h -1 89/95 C
printf() * I 1 // varwidth.c: 2 #include <stdio.h> 3 int main(void) 4 { 5 unsigned width, precision; 6 int number = 256; 7 double weight = 123.5; 8 printf("what field width?\n"); 9 scanf("%d", &width); 10 printf("the number is:%*d\n", width, number); 90/95 C
printf() * II 11 printf("now enter a width and a precision:\n") ; 12 scanf("%d %d", &width, &precision); 13 printf("weight=%*.*f\n", width, precision, weight); 14 return 0; 15 } 91/95 C
printf * $ gcc varwidth.c $./a.out What field width? 6 The number is: 256 Now enter a width and a precision: 8 3 Weight= 123.500 92/95 C
scanf() * scanf() * % 93/95 C
scanf() * 1 // skip2.c: 2 #include <stdio.h> 3 int main(void) 4 { 5 int n; 6 printf("please enter three integers:\n"); 7 scanf("%*d %*d %d", &n); 8 printf("the last integer was %d\n", n); 9 return 0; 10 } 94/95 C
scanf() * $ gcc skip2.c $./a.out Please enter three integers: 10 20 30 The last integer was 30 95/95 C