设为首页 加入收藏

TOP

C lang:character input and output (I/O)(一)
2019-10-09 20:04:15 】 浏览:151
Tags:lang:character input and output I/O

Xx_Introduction

  • Character input and output is by more line character conpose of the text flow 
  • Define name common use capital  letter,easy read.
  • The Standard C Library ----->provide I/O model ------>use character flow way.

Ax_Application

  • file copy,charater count,line count,word count

Bx_Method

  • I/O model common use getchar and putchar,interactive use of putchar and printf.
    1 getchar()     //read next character
    2 putcahr()     //print next character
    3 printf()        //print next(bunch) character
  • File Copy
    • file copy version 1
       1 #include<stdio.h>
       2 
       3 int main()
       4 { 5 int c; 6 7 c = getchar(); 8 while(c != EOF){ 9 putchar(c); 10 c = getchar(); 11 } 12 }
    • file copy version 2
       1 #include<stdio.h>
       2 
       3 int main()
       4 { 5 int c; 6 7 while((c = getchar())!= EOF){ 8  putchar(c); 9  } 10 }

      != : unequal to. priority overtop assignment(=)             EOF:end of file

    • Conclusion:computer use bit storage of character and any data type.
    • Assignment can portion of expression.
    • Complex statement simple easy read,but so hard understand.
    • Due to unequal to relational operator(!=) priority not overtop assignment(=),so c expression use bracket.
       1 #include <stdio.h>
       2 
       3 int main()
       4 { 5 int c; 6 7 while (c = getchar() != EOF){ 8 printf("%d\n",c); 9  } 10 printf("%d - at EOF\n",c); 11 return 0; 12 }

      if not use bracket,will priority operation EOF,value by 1,if input end or else print "c - at EOF".

    • Print EOF value programming
      1 #include <stdio.h>
      2 
      3 int main()
      4 { 5 printf("EOF is %d\n",EOF); 6 return 0; 7 }

      character constant EOF is in <stdio.h> file definition,value by -1 

    • In other system can by definition other value. 
  • Charater Count(2019-10-8 update)
    • Character count version 1
       1 #include <stdio.h>
       2 
       3 int main()
       4 {
       5     // version 1
       6     long nc;
       7 
       8     nc = 0;
       9     while (getchar() != EOF)
      10         ++nc;
      11     printf("%ld\n",nc-1);
      12     return 0;
      13 }

      ++(--) is operater, be euivalent to eg(nc = nc + 1);impression add one.

    • ++(--) use a prefix effect in variable before add.in suffix it's first call variable before use progressive increase.
    • Long than int more big,long support 32bit int support 16bit,but different system imparity,long in printf use %ld.
    • Character version 2
      1 #include <stdio.h>
      2 int main()
      3 {
      4     double nc;
      5     for (nc = 0; getchar() != EOF;++nc)
      6     ;
      7     printf("%.0f\n", nc-1);
      8     return 0;
      9 }

      double and float use %f format output.double more than float.

    • For circulation a circulation body must exsit,null statement for alone semicolon(;).
    • Important is circulation in execute programs before must judge test condition whether it meets. 
  • Line Count
    • mind:line equal to line break number.
    • Line count program
       1 #include <stdio.h>
       2 int main()
       3 {
       4     int c,nl;
       5 
       6     nl = 0;
       7     while ((c = getchar()) != EOF)
       8         if (c == '\n')
       9             ++nl;
      10     printf("%d\n",nl);
      11     return 0;
      12 }

       ==:mean equal.      'A':mean character constant.corresponding ASCII number.

  • Count blanks,tabs,and newlines.
    •  

      version 1

    •  1 #include <stdio.h>
       2 int main()
       3 {
       4     int c, nb, nt, nl;
       5 
       6     nb = 0;     /* number of blanks     */
       7     nt = 0;     /* number of tabs       */
       8     nl = 0;     /* number of newlines   */
       9     while ((c = getchar()) != EOF){
      10         if ( c == ' ')
      11             ++nb;
      12         if ( c == '\t')
      13             ++nt;
      14         if ( c == '\n')
      15             ++nl;
      
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇[笔记] 二级指针(pointer to poin.. 下一篇用结构体解析Pascal字符串

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目