Terminal I-O - putchar(d); putchar('\t'); putchar(c);...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
3: Terminal I/O #include <stdio.h> Standard input is usually a keyboard, unless it has been redirected Standard output is usually the screen, unless it has been redirected EOF is a constant defined in stdio.h ; it stands for End-Of-File.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
3: Single Character I/O int getchar() to input a single character int putchar(int) to output a single character
Background image of page 2
/* File: ex1.c * Program that reads a single character and * outputs it, followed by end-of-line */ #include <stdio.h> #include <stdlib.h> int main() { int c; /* chars must be read as ints */ c = getchar(); putchar(c); putchar('\n'); return EXIT_SUCCESS; } Include statements will not be shown in other examples Example
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Example /* * Program that reads two characters and * prints them in reverse order, separated by * a tab and ending with end of line. * Error checking: Program terminates if * either of the input operations fails. Idiom?
Background image of page 4
Example if((d = getchar()) == EOF) return EXIT_FAILURE;
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 6
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 8
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 10
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: putchar(d); putchar('\t'); putchar(c); putchar('\n'); return EXIT_SUCCESS; } if((c = getchar()) == EOF) /* error, else OK */ Read Single Character Errors Placement of brackets: if(c = getchar() == EOF) The compiler interprets it as follows: if( c = getchar() == EOF ) char c ; c = getchar() &quot; Read Single Character&quot;Idio m Example /* Read a single character, if it is lower case * 'a' then output 'A'; otherwise output the input * character. Finally, output the end-of-line */ int main() { int c; if((c = getchar()) == EOF) return EXIT_FAILURE; if(c == 'a') putchar('A'); else putchar(c); putchar('\n'); return EXIT_SUCCESS; } 3: Formatted I/O printf int printf(&quot;format&quot;, exp) print formatted printf(&quot;%d&quot;, 123); printf(&quot;The value of i = %d.\n&quot;, i); printf() expects a string, so printf(&quot;\n&quot;); is correct, but printf('\n'); is wrong....
View Full Document

This note was uploaded on 02/18/2012 for the course CS 240 taught by Professor Rego during the Fall '08 term at Purdue University-West Lafayette.

Page1 / 10

Terminal I-O - putchar(d); putchar('\t'); putchar(c);...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online