Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 161 Introduction to Programming and Problem Solving Chapter 13 Console IO Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.

Similar presentations


Presentation on theme: "CS 161 Introduction to Programming and Problem Solving Chapter 13 Console IO Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from."— Presentation transcript:

1 CS 161 Introduction to Programming and Problem Solving Chapter 13 Console IO Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from ECE 103 material by prof. Phillip Wong @ PSU

2 Syllabus Console I/O Output Functions Input Examples

3 2 Common Console I/O Functions The C++ standard and streamio libraries contain functions to print output to and read input from the console “Console” refers to:  Output device (e.g., monitor or output stream)  Input device (e.g., keyboard or input stream) The header file contains prototypes for the console I/O functions #include

4 3 Output Functions int putchar( int c );  putchar() writes a character c to the console  If successful, the character written is returned. If not successful, the EOF value is returned Note: EOF is a pre-defined macro  The expected argument c is of type integer. If c is type char, it is converted to an integer first  Make sure no integer of value > 255 is passed!  Best to pass a char literal or char type object

5 4 Example: #include // old fashioned C int main( void ) { // main int x = 'e'; char y = 's'; putchar( 'Y' ); // output: ‘Y’ putchar( x );// ouptut: ‘e’ putchar( y );// output: ‘s’ putchar( 33 );// 33 is '!' in ASCII return 0; } //end main Output: !

6 5 int printf( const char * format, … );  printf() writes formatted output to the console  You have already learned cin and cout  If successful, the number of characters written is returned; else a negative value is returned, indicating error!  format is a string that contains any combination of literal text and conversion specifiers  … is an optional argument list of expressions whose values are to be printed  For each item in the list, there must be a corresponding conversion specifier in format

7 6  A conversion specifier inside printf() string: Begins with % Ends with a conversion character Unix has pretty incomplete printf man-page  Between the % and the conversion character are optional formatting values (in this order): – to left-justify output (default is right-justify) + to force display of + or – sign Number that specifies a minimum field width. (period), which separates field width from precision Number that specifies the precision (# of digits printed after a decimal point)

8 7 Variable Typeprintf() SpecifierComment Integer Values short, int% d, % i long% ld, % li int%u%u unsigned int% x, % X hexadecimal Floating-Point Values float, double% f % e, % E % g, % G fixed decimal format scientific notation shorter of %f or %e long double% Lf, % Le, % LE % Lg, % LG Character & String Values char%c%c char *%s%s string Miscellaneous address%p%p memory address To print % symbol%

9 8  An escape sequence embedded in the format string performs special actions  A sequence starts with the '\' character SequenceDescriptionSequenceDescription \a alert (bell) character \v vertical tab \b backspace \\ backslash \f formfeed \' single quote \n newline \" double quote \r carriage return \t horizontal tab \xhh hexadecimal number

10 9 Example: #include int main( void ) { // main printf("Wake up!\n"); printf("\n"); /* Blank */ printf("Summer "); printf("is coming.\n"); printf(" See \"you\"\nlater.\n"); return 0; } //end main Wake up! Summer is coming. See "you" later.

11 10 Example: #include int main( void ) { // main int c = 65; // an ’A’ char ch = 'B'; // obvious printf( "c = %c\n", c ); printf( "c = %d\n", c ); printf( "ch = %c\n", ch ); printf( "ch = %d\n", ch ); return 0; } //end main c = A c = 65 ch = B ch = 66

12 11 Example: #include int main( void ) { // main int x = 5, y = 9; printf( "%d ", 12 ); printf( "[%d]\n", 3*x+y ); printf( "x equals %d\n", x ); printf( "x=%d y=%d\n", x, y ); printf( "x=%3d y=%3d\n", x, y ); printf( "x=%3d y=%3d\n", 12, 7 ); return 0; } //end main 12 [24] x equals 5 x=5 y=9 x= 12 y= 7

13 12 Example: #include int main( void ) { // main float u = 5.0; double v = 1.75; const char * str = "PSU rocks!"; printf( "%c %d %f\n", 65, 65, (float)65 ); printf( "%f\n", 2/3.0 ); printf( "u=[%f] v=[%f]\n", u, v ); printf( "%.1f %.2f %.3f\n", v, v, v ); printf( "%12.3f\n", v ); printf( "%12.3e\n", 254*v ); printf( "%s\n", str ); printf( "[%15s]\n", str ); printf( "[%-15s]\n", str ); return 0; } //end main A 65 65.000000 0.666667 u=[5.000000] v=[1.750000] 1.8 1.75 1.750 1.750 4.445e+02 PSU rocks! [ PSU rocks!]

14 13 Example: #include #define V1 1.0 #define V2 1.5e-7 #define V3 1.5e7 int main( void ) { // main printf( "V1 (%.8f) = %.8f\n", V1 ); printf( "V1 (%.8g) = %.8g\n", V1 ); // %g is shorter of: %f and %e printf( "V1 (%.8e) = %.8e\n\n", V1 ); printf( "V2 (%.8f) = %.8f\n", V2 ); printf( "V2 (%.8g) = %.8g\n", V2 ); // %g is shorter of: %f and %e printf( "V2 (%.8e) = %.8e\n\n", V2 ); printf( "V3 (%.8f) = %.8f\n", V3 ); printf( "V3 (%.8g) = %.8g\n", V3 ); // %g is shorter of: %f and %e printf( "V3 (%.8e) = %.8e\n", V3 ); return 0; } //end main V1 (%.8f) = 1.00000000 V1 (%.8g) = 1 V1 (%.8e) = 1.00000000e+00 V2 (%.8f) = 0.00000015 V2 (%.8g) = 1.5e-07 V2 (%.8e) = 1.50000000e-07 V3 (%.8f) = 15000000.00000000 V3 (%.8g) = 15000000 V3 (%.8e) = 1.50000000e+07

15 14 Input Functions int getchar( void );  getchar() reads a single character from the console  If successful, the next character from the console is read and returned, converted to int  If not successful, the EOF value is returned

16 15 Example: #include int main( void ) { // main int ch; printf( "Enter character: ” ); ch = getchar(); printf( "ch = %c\n", ch ); return 0; } //end main Enter character: A ch = A

17 16 Example: #include int main( void ) { // main int ch1, ch2; printf( "Enter ch1: ” ); ch1 = getchar(); printf( "Enter ch2: ” ); ch2 = getchar(); printf( "ch1 = %c ch2 = %c\n", ch1, ch2 ); return 0; } //end main Enter ch1: A Enter ch2: ch1 = A ch2 = Why does this look wrong?

18 17 getchar() works with buffered line input: Buffer contents after pressing ' A ' and 'Enter': ch1=getchar() gets the ' A ' for ch1 After reading the first character in the buffer, the "next character" arrow is updated: ch2=getchar() gets the ' \n ' for ch2 'A''\n''A''\n' Arrow indicates "next character" to read.

19 18 A workaround for the buffer problem: #include int main( void ) { // main int ch1, ch2; printf( "Enter ch1: " ); ch1 = getchar(); getchar(); // Remove pending '\n’ printf( "Enter ch2: " ); ch2 = getchar(); printf( "ch1 = %c ch2 = %c\n", ch1, ch2 ); return 0; } //end main Enter ch1: A Enter ch2: B ch1 = A ch2 = B

20 19 int scanf( const char * format, … );  scanf() reads formatted input from the console  If successful, the number of items read is returned  If not successful, EOF is returned, -1 on Unix  format is a string that contains any combination of conversion specifiers  … is an optional argument list of variable addresses where the input values are to be stored  For each item in the list, there must be a corresponding conversion specifier in format

21 20 The “address” of a variable is the memory location that holds the variable's value The & character is C’s “address-of” operator When storing the value read by scanf(), you must specify the address of the variable that will receive the value Example: int x;... scanf( "%d", &x ); 1. Read character string of digits ‘0’..‘9’ into the input buffer; only ‘0’..’9’ due to %d 2. Convert the string value to an integer 3. Store the value at the address of int variable x

22 21 Variable Typescanf() Specifier Integer Values int% d, % i short% hd, % hi long int% ld, % li unsigned int%u%u Floating-Point Values float% f, % e, % E, % g, % G double% lf, % le, % lE, %lg, % lG long double% Lf, % Le, % LE, %Lg, % LG Character Values char%c%c Character String (whitespace delimited – not full text) char *%s%s

23 22 Example: #include int main( void ) { // main char cvar; int ivar; float fvar, fv2; double dvar; scanf( "%c", &cvar ); scanf( "%f", &fvar ); /* Use %f for floats */ scanf( "%lf", &dvar ); /* Use %lf for doubles */ scanf( "%d %f %f", &ivar, &fvar, &fv2 ); return 0; } //end main

24 23 Example: Common error – missing & in scanf() // source file: test.c #include int main( void ) { // main int x; // OK to leave uninitialized printf( "Enter x: ” ); scanf( "%d", x ); printf( "%d\n", x ); return 0; } //end main Notice x instead of &x $ gcc -ansi -Wall -pedantic test.c test.c: In function 'main': test.c:7: warning: format argument is not a pointer (arg 2) $./a.out Enter x: 45 Segmentation fault (core dumped)

25 24 Reminder for printf and scanf Specifiers Output → If value to display with printf is of type:  int: % d  long int: % ld  float, double: % f % e % g  long double: % Lf % Le % Lg Input → If value to input with scanf is of type:  int: % d  long int: % ld  float: % f % e % g  double: % lf % le % lg  long double: % Lf % Le % Lg printf uses the same specifiers for both float and double types. scanf uses separate specifiers for the float and double types.


Download ppt "CS 161 Introduction to Programming and Problem Solving Chapter 13 Console IO Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from."

Similar presentations


Ads by Google