3 1999 10 13
1. 2. hello.c printf( Hello, world! n ); cc hello.c a.out./a.out Hello, world printf( Hello, world! n ); 2
Hello, world printf n printf 3. ( ) int num; num = 100; num 100 100 num int num num int num; num = 100; printf( I got number %d n, num); 3
printf( %d n, num)num %d printf( I got number %d n, num); I got number 100 main double double num; num = 12.34; printf( I got number %lf n, num); num printf %lf char char c; c = X ; printf( I got letter %c n, num); c printf %c 123 4. double x, y, z; 4
x = 100.0; y = 200.0; z = (x + y) / 2.0; printf( Average is %lf n, z); x,y,z x100 100.0 y200 z(x + y)/2 ( (100 + 200) / 2) z +, -, *, / ( ) 100.0 / 8.1 * 60 * 60 5. if int x; x = 20; if(x > 10){ printf( Hello! n ); else { printf( Hi! n ); Hello! x = 20;x = 5 Hi! if if( ){ 5
else { ; ; x > 10 x 10 printf( Hello! n ); printf( Hi! else n 100 Big number!100 Small number! 6. while int x; x = 100; while(x <= 300){ printf( The number I got is now %d. n, x); x = x + 50; The number I got is now 100. The number I got is now 150. The number I got is now 200. The number I got is now 250. The number I got is now 300. while while 6
while( ){ 1; 2; : { while 1 2 : x 100 x 300 while x = x + 50 x 50 x while 100,150,200,250,300 printf while 1,3,5,7,..21 while 1 + 3 + 5 + + 21 7
7. scanf scanf( %d, &n); int n %d (& ) # include <stdio.h> int x, y; printf( Input first number: ); scanf( %d, &x); printf( Input second number: ); scanf( %d, &y); printf( %d + %d = %d n, x, y, x + y); %./a.out Input first number: 10 Input second number: 20 10 + 20 = 30 % scanf( %d, &x); x 8
8. int array[5]; array[0] array[4] array[3] = 100; array[0] array[1] array[2] array[3] array[4] 100 [ ] ; int array[3]; int x,n; n = 0; while(n < 3){ printf( Please input number : ); scanf( %d, &x); array[ n ] = x; n = n + 1; n = 0; while(n < 3){ 9
printf( No. %d -> %d n, n, array[n]); n = n + 1; Please input number : 25 Please input number : 21 Please input number : 39 No. 0 -> 25 No. 1 -> 21 No. 2 -> 39 n while n 9. C a t Hello, Good C char char str[7]; str #include <string.h> 10
strcpy(str, Hello ); strcpy 0 str[0] str[1] str[2] str[3] str[4] str[5] str[6] H e l l o 0 str[1] e printf( %c n, str[1]); e printf( %s n, str); %s printf( %s n, &str[3]); lo & str str[3] 3 &str[3] 3 Mothers strcmp(str1, str2); /* str1 str2 0 */ strncmp(str1, str2, n); /* str1 str2 n 0 */ strcat(str1, str2); /* str1 str2 */ 11
strlen(str1); /* str1 strcmp #include <string.h> char str1[20], str2[20]; scanf( %s, str1); scanf( %s, str2); if(strcmp(str1, str2) == 0)printf( Same strings n ); /* str1 str2 (0) Same strings */ else printf( Different strings n ); strcat 10. (){ : : (); 12
Hello print_hello(){ printf( Hello n ); print_hello(); /* */ print_hello(){ printf( Hello ); print_hello(); printf( End of this program. n ); main print_hello(); main End of this program 13
(10 5) (5 + 10 ( )) = 35 Hello, world print_sum(int x, int y) { printf( %d n, x + y); print_sum(10,20); print_sum x, y 10, 20 30 ( ){ : : x, y 11. cat FILE *fp; 14
fp = fopen( File1, r ); File1 fgets(line, 1000, fp); File1 line line char line[1000]; fclose(fp); char line[1000]; FILE *fp; fp = fopen( File1, r ); while(fgets(line, 1000, fp)!= NULL){ printf( %s, line); fclose(fp); while(fgets(line, 1000, fp)!= NULL){ fgets NULL!= fgets(line, 1000, fp)!= NULL while 15
line line printf( %s, line); 16