正在加载图片...
等价于 static char cl[5}{c,h,a','r;0}; 不等价于 static char c2[4}={ch’;a,r} 7.4字符串的处理函数 1.puts0和 gets函数 (1).putO函数 pusO函数用来向标准输出设备(屏幕)写字符串并换行, 其调用格式为 puts(s); 其中s为字符串变量(字符串数组名) puts函数的作用与语 printf("sn",s)相同。 例 maino char s20=" Thank you";/*定义字符串数组和指针变量* puts(s) 说明 ① puts函数只能输出字符串,不能输出数值或进行格式变 换 ②可以将字符串直接写入 puts函数中。如 puts("Hello, Turbo C2.0") (2). gets函数 gets函数用来从标准输入设备(键盘)读取字符串直到回车结束, 但回车符不属于这个字符串。其调用格式为 gets(s) 其中s为字符串变量(字符串数组名) 例:78 main char s]: printf("What's your name? n") gets(s) 等待输入字符串直到 回车结束* puts(s) *将输入的字符串输出* puts("How old are you? " puts(f),等价于 static char c1[5]={‘c’,’h’,’a’,’r’,’\0’}; 不等价于 static char c2[4]={‘c’,’h’,’a’,’r’}; 7.4 字符串的处理函数 1.puts()和 gets()函数 (1). puts()函数 puts()函数用来向标准输出设备(屏幕)写字符串并换行, 其调用格式为: puts(s); 其中 s 为字符串变量(字符串数组名)。 puts()函数的作用与语 printf("%s\n", s)相同。 例: main() { char s[20]= "Thank you"; /*定义字符串数组和指针变量*/ puts(s); } 说明: ① puts()函数只能输出字符串, 不能输出数值或进行格式变 换。 ② 可以将字符串直接写入 puts()函数中。如: puts("Hello, Turbo C2.0"); (2). gets()函数 gets()函数用来从标准输入设备(键盘)读取字符串直到回车结束, 但回车符不属于这个字符串。其调用格式为: gets(s); 其中 s 为字符串变量(字符串数组名)。 例:7.8 main() { char s[20]; printf("What's your name?\n"); gets(s); /*等待输入字符串直到 回车结束*/ puts(s); /*将输入的字符串输出*/ puts("How old are you?"); puts(f); }
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有