1.5 Characters Input and Output

processing character data


text stream :

  • a sequence of characters divided into lines
  • characters + newline

getchar

  • reads the next input character from a text stream
  • returns its value
c = getchar();
 // the variable c contains the next character of input

putchar

  • prints a character each time it is called
  • interleaved with printf
putchar(c);
// prints the contents of the integer variable c as a character, usually on the screen

1.5.1 File Copying

a program that copies its input to its output one character at a time

read a character
while (character is not end-of-file indicator)
---- output the character just read
---- read a character

#include <stdio.h>
/* copy input to output; 1st version */
int main (void) {
  int c;

  c = getchar();
  while (c != EOF) { 
    putchar(c);
    c = getchar();
  }
}

!=

  • not equal to

EOF

  • end of file
#include <stdio.h>
/* copy input to output; 2nd version */
int main (void) {
  int c;

  while ((c = getchar()) != EOF) 
    putchar(c);
}

while

  1. gets a character
  2. assigns it to c
  3. tests whether the character is the end-of-file signal
    3.1. if not, the body of the while execute
    3.2 print the character
    3.3. while repeats
  4. if the end of file is reached
    4.1 while terminates
    4.2 main terminates

precedence : != > =(assignment)
c = getchar() != EOF = c = (getchar() != EOF) // 0 or 1


Exercise 1-6

verify getchar() != EOF is 0 or 1

#include <stdio.h>

int main (void) {
  int c;

  while (c = getchar() != EOF) 
    printf("%d\n", c);
  printf("%d - at EOF\n", c);
}
  1. getchar has a character to read and does not return the end of file
    'getchar() != EOF' is true
    c = 1
  2. program encounters the end of file
    the expression is false
    c = 0
    loop terminates

Exercise 1-7

write a program to print the value of EOF

#include <stdio.h>

int main (void) {
  printf("EOF is %d\n", EOF); // EOF  is -1
}

1.5.2 Character Counting

This program counts characters:

#include <stdio.h>

/* count characters in input; 1st edition */
int main (void){
  long nc = 0;
    
  while (getchar() != EOF) 
    ++nc;
    
  printf("%ld\n", nc);

  return 0;
}

++ increment by one
nc = nc + 1
++

  • concise
  • efficient

prefix operators ++nc
postfic operators nc++

long

  • 32 bits
  • %ld

int

  • 16 bits
  • max 32767

double

  • %f

float

  • double precision
  • %f
#include <stdio.h>

/* count characters in input; 2nd edition */
int main (void){
  double nc = 0;
    
  for (nc = 0; getchar() != EOF; ++nc);
    
  printf("%.0f\n", nc);

  return 0;
}

%.0f - print of the decimal point and the fraction part

null statement - the isolated semicolon

while for - boundary condition (0 length input)

  1. test at the top of the loop
  2. proceed to the loop body

1.5.3 Line Counting

the standard library - ensures -
an input stream = a sequence of lines + newline
-> line counting = counting newlines

#include <stdio.h>

/* count lines in input */
int main (void) {
  int c, nl = 0;
  
  while ((c == getchar()) != EOF)
    if (c == '\n')
      ++nl;
  printf("%d\n", nl);
  
  return 0;
}

while controls if controls ++nl increment
if

  1. test the parenthesized condition
  2. if the condition is true, executes the statements

is equal to

Language Symbol
C ==
Pascal =
4Fortan .EQ.

assignment

  • C =

character constant

  • 'a character'
  • an integer value equal to the numerical value of the character in the machine's character set
Symbol ASCII Value
'A' 65
'\n' 10

\n

  • a single character
  • an integer in expressions
  • or a string constant

Exercise 1-8

count:

  1. blanks
  2. tabs
  3. newlines
#include <stdio.h>

/* count blanks, tabs, and newlines */
int main (void) {
  int c, nb = 0, nt = 0, nl = 0; //number of blanks, tabs, newlines 
  
  while ((c == getchar()) != EOF)
    if (c == ' ') ++nb;
    else if (c == '\t') ++nt;
    else if (c == '\n') ++nl;
    
  printf("%d %d %d\n", nb, nt, nl);
  
  return 0;
}

Exercise 1-9

  1. copy its input to its output
  2. replace each string of one or more blanks by a single blank
#include <stdio.h>
/* copy input to output; 2nd version */
int main (void) {
  int c;

  while ((c = getchar()) != EOF) 
    putchar(c);
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,129评论 0 10
  • 文/吻尘 现在城乡一体化,农村的孩子也经常进城、外出旅游和智能手机以及网络的普及,视野和城里的孩子差距不大。 上个...
    吻尘阅读 4,073评论 31 12
  • 10月23日 小慧是一家创业公司的员工,入职半年多,和她同期进公司的一批员工中,已经有几个由于表现突出,被公司调到...
    栀子2017阅读 3,437评论 0 0