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);
}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 202,529评论 5 475
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,015评论 2 379
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 149,409评论 0 335
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,385评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,387评论 5 364
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,466评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,880评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,528评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,727评论 1 295
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,528评论 2 319
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,602评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,302评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,873评论 3 306
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,890评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,132评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,777评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,310评论 2 342

推荐阅读更多精彩内容

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