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 
/* 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 
/* 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 

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 

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

1.5.2 Character Counting

This program counts characters:

#include 

/* 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 

/* 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 

/* 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 

/* 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 
/* copy input to output; 2nd version */
int main (void) {
  int c;

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

你可能感兴趣的:(1.5 Characters Input and Output)