C/C++
Table of contents 1. 2. 3. 4. 1
char C 2
char greeting [50] = " How " " are " " you?"; char greeting [50] = " How are you?"; 3
printf ("\" Ready, go!\" exclaimed John."); " Ready, go!" exclaimed John. 4
(static storage) 5
// quotes.c: # include <stdio.h> int main ( void ) { printf ("%s, %p, %c\n", "We", " are ", *" space farers "); return 0; } 6
// quotes.c: # include <stdio.h> int main ( void ) { printf ("%s, %p, %c\n", "We", " are ", *" space farers "); return 0; } We, 0 x100000f81, s 6
char s [40] = " Hello world!" 7
char s [40] = " Hello world!" char s[] = { H, e, l, l, o,, w, o, r, l, d,!, \0 }; 7
1 8
char s[] = " Hello world!" 9
char s[] = " Hello world!" char * s = " Hello world!" 9
char s[] = " Hello world!" char * s = " Hello world!" s 9
char heart [] = "I love C!"; char * head = "I love math!"; heart head 10
(1) for ( i = 0; i < 6; i ++) putchar ( heart [i]); putchar ( \n ); for ( i = 0; i < 6; i ++) putchar ( head [i]); putchar ( \n ); I I love love 11
(2) for ( i = 0; i < 6; i ++) putchar (*( heart + i)); putchar ( \n ); for ( i = 0; i < 6; i ++) putchar (*( head + i)); putchar ( \n ); I I love love 12
(3) while (* head!= \0 ) putchar (*( head ++) ); I love math! 13
(4) head heart head = heart ; heart = head ; 14
(5) heart heart [7] = R ; OR *( heart + 7) = R ; 15
(5) heart heart [7] = R ; OR *( heart + 7) = R ; 16
const char * fruit [4] = { " Apple ", " Pear ", " Orange ", " Peach " }; 4 fruit char fruit[0] fruit[1]... 17
18
char * name ; scanf ("%s", name ); name 19
char name [81]; C malloc 20
C scanf() gets() fgets() 21
gets() gets( get string) 22
gets() // name1.c # include <stdio.h> # define MAX 81 int main ( void ) { char name [ MAX ]; printf ("Hi, what s your name?\n"); gets ( name ); printf (" Nice name, %s.\n", name ); return 0; } 23
gets() Hi, what s your name? warning : this program uses gets (), which is unsafe. Xiaoping Zhang Nice name, Xiaoping Zhang. 24
gets() Hi, what s your name? warning : this program uses gets (), which is unsafe. Xiaoping Zhang Nice name, Xiaoping Zhang. gets() gets() 24
gets() 1 // name2.c: 2 # include <stdio.h> 3 # define MAX 81 4 int main ( void ) 5 { 6 char name [ MAX ]; 7 char * ptr ; 8 printf ("Hi, what s your name?\n"); 9 ptr = gets ( name ); 10 printf ("%s? Ah! %s!\n", name, ptr ); 11 return 0; 12 } 25
gets() Hi, what s your name? warning : this program uses gets (), which is unsafe. Xiaoping Zhang Xiaoping Zhang? Ah! Xiaoping Zhang! 26
fgets() gets() fgets() 27
fgets() gets() fgets() 1. fgets() n fgets() n-1 2. fgets() gets() 3. fgets() stdin() 28
fgets() 1 // name3.c 2 # include <stdio.h> 3 # define MAX 81 4 int main ( void ) 5 { 6 char name [ MAX ]; 7 char * ptr ; 8 printf ("Hi, what s your name?\n"); 9 ptr = fgets (name, MAX, stdin ); 10 printf ("%s? Ah! %s!\n", name, ptr ); 11 return 0; 12 } 29
gets() Hi, what s your name? Xiaoping Zhang Xiaoping Zhang? Ah! Xiaoping Zhang! 30
gets() Hi, what s your name? Xiaoping Zhang Xiaoping Zhang? Ah! Xiaoping Zhang! fgets fgets 30
scanf() %s scanf() scanf() gets() scanf() gets() 31
scanf() scanf() 1. %s 2. %10s scanf() 10 32
scanf() 1: _ name scanf("%s", name); Fleebert_Hup Fleebert _Hup scanf("%5s", name); Fleebert_Hup Fleeb ert_hup scanf("%5s", name); Ann_Ular Ann _Ular 33
scanf() // scan_str.c: # include <stdio.h> int main ( void ) { char name1 [11], name2 [11]; int count ; printf (" Please enter 2 names.\n"); count = scanf ("%5s %10 s",name1, name2 ); printf ("I read the %d names %s and %s.\n", count, name1, name2 ); return 0; } 34
scanf() Please enter 2 names. Jesse Jukes I read the 2 names Jesse and Jukes. Please enter 2 names. Liza Applebottham I read the 2 names Liza and Applebotth. Please enter 2 names. Portensia Callowit I read the 2 names Porte and nsia. 35
puts() C puts() fputs() printf() 36
puts() i // put_out.c: # include <stdio.h> # define DEF "I am a # defined string." int main ( void ) { char str1 [80] = "An array was initialized to me."; const char * str2 = "A pointer was initialized to me."; puts ("I m an argument to puts ()."); puts ( DEF ); puts ( str1 ); puts ( str2 ); puts (& str1 [5]) ; puts ( str2 +4) ; return 0; } 37
puts() I m an argument to puts (). I am a # defined string. An array was initialized to me. A pointer was initialized to me. ray was initialized to me. inter was initialized to me. 38
puts() puts() printf() puts() puts() 39
puts() char str1 [ 80] = " An array was initialized to me. ";... puts (& str1 [5]) ; ray was initialized to me. 40
puts() char str1 [ 80] = " An array was initialized to me. ";... puts (& str1 [5]) ; ray was initialized to me. &str1[5] str1 6 r puts() 40
puts() const char * str2 = " A pointer was initialized to me.";... puts ( str2 +4) ; inter was initialized to me. 41
puts() const char * str2 = " A pointer was initialized to me.";... puts ( str2 +4) ; inter was initialized to me. str2+4 i puts() 41
puts() 1 // nono.c: 2 # include <stdio.h> 3 int main ( void ) 4 { 5 char side_a [] = " Side A"; 6 char dont [] = { W, O, W,! }; 7 char side_b [] = " Side B"; 8 9 puts ( dont ); 10 11 return 0; 12 } 42
puts() 1 // nono.c: 2 # include <stdio.h> 3 int main ( void ) 4 { 5 char side_a [] = " Side A"; 6 char dont [] = { W, O, W,! }; 7 char side_b [] = " Side B"; 8 9 puts ( dont ); 10 11 return 0; 12 } 42
puts() WOW! Side A 43
puts() WOW! Side A dont puts() dont side_a dont 43
fputs() fputs() puts() fputs() stdout stdout stdio.h puts() fputs() 44
fputs() gets() puts() char line [81]; while ( gets ( line )) puts ( line ); 45
fputs() fgets() fputs() char line [81]; while ( fgets (line, 81, stdin )) fputs (line, stdout ); line 46
fputs() puts() gets() fputs() fgets() 47
printf() printf() puts() printf ("%s\n", string ); puts ( string ); 48
printf() printf() printf ("Well, %s, %s\n", name, MSG ); 49
C string.h 1. strlen() 2. strcat() 3. strncat() 4. strcmp() 5. strncmp() 6. strcpy() 7. strncpy() 50
strlen() int * strlen ( const char * s); s s 51
strlen() i // test_fit.c # include <stdio.h> # include <string.h> void fit ( char *, unsigned int ); int main ( void ) { char mesg [] = " Hold on to your hats, hackers."; puts ( mesg ); fit (mesg,7) ; puts ( mesg ); puts (" Let s look at some more of the string."); puts ( mesg + 8); } return 0; 52
strlen() ii void fit ( char * string, unsigned int size ) { if ( strlen ( string ) > size ) *( string + size ) = \0 ; } 53
strlen() Hold on to your hats, hackers. Hold on Let s look at some more of the string. to your hats, hackers. 54
strcat() char * strcat ( char * s1, const char * s2); s2 s1 s2 s1 55
strcmp() s1 s2 strcat() char * s1 56
strcat() i # include <stdio.h> # include <string.h> # define SIZE 80 int main ( void ) { char flower [ SIZE ]; char addon [] = " s smell like old shoes."; puts (" What is your favorite flower?"); gets ( flower ); strcat ( flower, addon ); puts ( flower ); puts ( addon ); } return 0; 57
strcat() ii 58
strcat() What is your favorite flower? Rose Roses smell like old shoes. s smell like old shoes. 59
strncat() strcat() strncat() 60
strncat() char * strcat ( char * s1, const char * s2, size_t n); s2 n s1 s2 s1 strncat() char * s1 61
strncat() i # include <stdio.h> # include <string.h> # define SIZE 30 # define BUGSIZE 4 int main ( void ) { char flower [ SIZE ]; char addon [] = " s smell like old shoes."; char bug [ BUGSIZE ]; int available ; puts (" What is your favorite flower?"); gets ( flower ); if (( strlen ( addon ) + strlen ( flower ) + 1) <= SIZE ) strcat ( flower, addon ); puts ( flower ); 62
strncat() ii puts (" What is your favorite bug?"); gets ( bug ); available = BUGSIZE - strlen ( bug ) - 1; strncat (bug, "is sdfsdf ", 1); puts ( bug ); } return 0; 63
strncat() What is your favorite flower? Rose Roses smell like old shoes. What is your favorite bug? Aphid Aphids smell 64
strcmp() int strcmp ( const char * s1, const char * s2); s1 s2 0 ASCII 65
strcmp() 66
strcmp() i /* compare. c -- this will work */ # include <stdio.h> # include <string.h> # define ANSWER " Grant " # define MAX 40 int main ( void ) { char try [ MAX ]; puts (" Who is buried in Grant s tomb?"); gets ( try ); while ( strcmp (try, ANSWER )!= 0) { puts ("No, that s wrong. Try again."); gets ( try ); } puts (" That s right!"); 67
strcmp() ii } return 0; 68
strcmp() strcmp ("A", "A") is 0 strcmp ("A", "B") is -1 strcmp ("B", "A") is 1 strcmp ("C", "A") is 2 strcmp ("Z", "a") is -7 strcmp (" apples ", " apple ") is 115 69
strcmp() strcmp ("A", "A") is 0 strcmp ("A", "B") is -1 strcmp ("B", "A") is 1 strcmp ("C", "A") is 1 strcmp ("Z", "a") is -1 strcmp (" apples ", " apple ") is 1 70
strncmp() int strncmp ( const char * s1, const char * s2, size_t n); s1 s2 n strcmp() 71
strncmp() "astro" 5 72
strncmp() i /* starsrch.c -- use strncmp () */ # include <stdio.h> # include <string.h> # define LISTSIZE 5 int main ( void ) { const char * list [ LISTSIZE ] = { " astronomy ", " astounding ", " astrophysics ", " ostracize ", " asterism " }; int count = 0; int i; for ( i = 0; i < LISTSIZE ; i ++) if ( strncmp ( list [i]," astro ", 5) == 0) 73
strncmp() ii { printf (" Found : %s\n", list [i]); count ++; } printf (" The list contained % d words beginning " " with astro.\n", count ); } return 0; 74
strncmp() Found : astronomy Found : astrophysics The list contained 2 words beginning with astro. 75
strcpy() char * strcpy ( char * s1, const char * s2); s2 s1 s1 76
strcpy() s2 (source) s1 (target) s2 s1 s2 77
strcpy() i /* copy1.c -- strcpy () demo */ # include <stdio.h> # include <string.h> # define SIZE 40 # define LIM 5 int main ( void ) { char qwords [ LIM ][ SIZE ]; char temp [ SIZE ]; int i = 0; printf (" Enter %d words beginning with q:\n", LIM ); while (i < LIM && gets ( temp )) { if ( temp [0]!= q ) printf ("%s doesn t begin with q!\n", temp ); 78
strcpy() ii } else { strcpy ( qwords [i], temp ); i ++; } } puts (" Here are the words accepted :"); for ( i = 0; i < LIM ; i ++) puts ( qwords [i]); return 0; 79
strcpy() Enter 5 words beginning with q: quit quarter quite quotient nomore nomore doesn t begin with q! quiz Here are the words accepted : quit quarter quite quotient quiz 80
strcpy() strcpy() char * 81
strcpy() # include <stdio.h> # include <string.h> # define WORDS " beast " # define SIZE 40 int main ( void ) { const char * orig = WORDS ; char copy [ SIZE ] = " Be the best that you can be."; char * ps; puts ( orig ); puts ( copy ); ps = strcpy ( copy + 7, orig ); puts ( copy ); puts (ps +6) ; return 0; } 82
strcpy() beast Be the best that you can be. Be the beast beast 83
strncpy() char * strcpy ( char * s1, const char * s2, size_t n); s2 n s1 n 84
strncpy() n n n 1 s1 85
strncpy() i # include <stdio.h> # include <string.h> # define SIZE 40 # define TARGSIZE 7 # define LIM 5 int main ( void ) { char qwords [ LIM ][ TARGSIZE ]; char temp [ SIZE ]; int i = 0; printf (" Enter %d words begin with q:\n", LIM ); while (i < LIM && gets ( temp )) { if ( temp [0]!= q ) printf ("%s doesn t begin with q!\n", temp ); 86
strncpy() ii } else { strncpy ( qwords [i], temp, TARGSIZE - 1); qwords [i][ TARGSIZE - 1] = \0 ; i ++; } } puts (" Here are the words accepted :"); for ( i = 0; i < LIM ; i ++) puts ( qwords [i]); return 0; 87
strncpy() Enter 5 words begin with q: quack quadratic quisling quota quagga Here are the words accepted : quack quadra quisli quota quagga 88
sprintf() sprintf() stdio.h printf() printf() 89
sprintf() i # include <stdio.h> # define MAX 20 int main ( void ) { char first [ MAX ]; char last [ MAX ]; char formal [2* MAX +10]; double prize ; puts (" Enter your first name :"); gets ( first ); puts (" Enter your last name :"); gets ( last ); puts (" Enter your prize money :"); 90
sprintf() ii scanf ("%lf", & prize ); sprintf ( formal, "%s, % -19s: $ %6.2 f\n", last, first, prize ); puts ( formal ); } return 0; 91
sprintf() Enter your first name : warning : this program uses gets (), which is unsafe. Teddy Enter your last name : Bear Enter your prize money : 2000 Bear, Teddy : $2000.00 92
sprintf() Enter your first name : warning : this program uses gets (), which is unsafe. Teddy Enter your last name : Bear Enter your prize money : 2000 Bear, Teddy : $2000.00 sprintf() format 92