- 8
- 0
- 约1.74万字
- 约 44页
- 2017-07-02 发布于湖北
- 举报
Chapter 4 输入输出概要1
Inputting Character Strings %ws or %wc The corresponding argument should be a pointer to a character array. %c may be used to read a single character when the argument is a pointer to a char variable. Example 4.6 Reading of strings using %wc and %ws. main() { int no; char name1[15],name2[15],name3[15]; printf(“Enter serial number and name one\n”);; scanf(“%d %15c”,no,name1); printf(“%d %15s\n\n”,no,name1); printf(“Enter serial number and name two\n”); scanf(“%d %s”,no,name2); printf(“%d %15s\n\n”,no,name2); printf(“Enter serial number and name three\n”); scanf(“%d %15s”,no,name3); printf(“%d %15s\n\n”,no,name3) } Output Enter serial number and name one 1 123456789012345 1 123456789012345r Enter serial number and name two 2 New York 2 New Enter serial number and name three 2 York Enter serial number and name one 1 123456789012 1 123456789012r Enter serial number and name two 2 New-York 2 New-York Enter serial number and name three 3 London 3 London Some versions of scanf support the following conversion specifications for strings: %[characters] %[^characters] Example 4.7 The program illustrates the function of %[] specification. main() { char address[80]; printf(“Enter address\n”); scanf(“%[ a-z]”,address); printf(“%-80s\n\n”,address); } Output Enter address new delphi 110002 new delphi main() { char address[80]; printf(“Enter address\n”); scanf(“%[^\n]”,address); //可读空格 printf(“%-80s\n\n”,address); } Output Enter address new Delphi 110 002 new Delphi 110 002 Reading Blank Spaces We have earlier seen that %s specifier cannot be used to read strings with blank spaces. But, this can be done with the help of %[] specification. Blank spaces may be included within the brackets, thus enabling the scanf to read strings with spaces. Remember that the lowercase and uppercase letters are distinct. See Example 4.7.
您可能关注的文档
- Chapter 1 副本概要1.ppt
- Chapter 11 Foundations of Control概要1.doc
- Chapter 11 Depreciation, Impairments and Depletion(中级会计)概要1.ppt
- Chapter 1 Scientific English writing概要1.ppt
- Chapter 10 Law of the Sea海洋法概要1.ppt
- Chapter 1 Introduction to Electronic Commerce概要1.ppt
- Chapter 1 Great Britain 英国历史概要1.ppt
- Chapter 10 英美概况概要1.ppt
- chapter 10,11美国文学概要1.ppt
- Chapter 12 Holidays and Festivals 美国节假日概要1.ppt
原创力文档

文档评论(0)