INTRODUCTION TO C++ C FORMATTED INPUT/OUTPUT Original slides are from http://sites.google.com/site/progntut/ Dept. of Electronic Engineering, NCHU
Outline 2 printf and scanf Streams (input and output) gets, puts getchar, putchar
Formatting output with printf 4 Syntax printf("format control string", other arguments); Format control string could include Conversion specifications (begin with %) Flags Filed widths Precisions Literal characters Other arguments: optional Each one corresponds to a conversion specification
Example of printf 5 conversion specification int numdogs = 2, numcats = 1; printf("i have %d dogs and %d cats\n", numdogs, numcats); format control string argument
Printing integers 6 Positive, negative, and zero Integer conversion specification: %d: signed decimal integer %i: signed decimal integer (different from %d in scanf) %o: unsigned octal integer %u: unsigned decimal integer %x: unsigned hex integer (0-9, a-f) %X: unsigned hex integer (0-9, A-F) %h or %l: placed before any integer conversion specifier to indicate a short or long integer
Example of printing integer 7 01 /* Fig 9.2: fig09_02.c*/ 02 /* Using the integer conversion specifiers */ 03 #include <stdio.h> 04 05 int main(void) 06 { 07 printf("%d\n", 455); 08 printf("%i\n", 455); 09 printf("%d\n", +455); 10 printf("%d\n", -455); 11 printf("%hd\n", 32000); 12 printf("%ld\n", 2000000000L); 13 printf("%o\n", 455); 14 printf("%u\n", 455); 15 printf("%u\n", -455); 16 printf("%x\n", 455); 17 printf("%x\n", 455); 18 return 0; 19 } /* end main */ Output: 455 455 455-455 32000 2000000000L 707 455 4294955841 1c7 1C7
Printing floating-point number 8 Floating point or exponential notation 153.2 = 1.532 x 10 2 = 1.532E2 Floating point conversion specification: %e or %E: Display floating-point in exponential notation %f: Display floating-point in fixed-point notation %g or %G: Display floating-point in either exponential or fixed-point based on the magnitude of the value %L: Placed before any floating-point conversion specifier to indicate a long double
Example of printing floating-point 9 01 /* Fig 9.4: fig09_04.c*/ 02 /* Printing with floating-point conversion specifiers */ 03 #include <stdio.h> 04 05 int main(void) 06 { 07 printf("%e\n", 1234567.89); 08 printf("%e\n", +1234567.89); 09 printf("%e\n", -1234567.89); 10 printf("%e\n", 1234567.89); 11 printf("%f\n", 1234567.89); 12 printf("%g\n", 1234567.89); 13 printf("%g\n", 1234567.89); 14 15 return 0; 16 } /* end main */ Output: 1.2345678e+006 1.2345678e+006-1.2345678e+006 1.2345678E+006 1234567.890000 1.234567e+006 1.234567E+006
Printing Strings and Characters 10 %c: Print character Single quotes for character constant ( z, a, or \n ) Print the first character of a string %s: printf("%c, string ); // output: s Print string Double quotes for strings ( z, string ) Can not print characters s s s s \0
Example of printing strings 11 01 /* Fig 9.5: fig09_05.c*/ 02 /* Printing string and characters */ 03 #include <stdio.h> 04 05 int main(void) 06 { 07 char ch = 'a'; /* char */ 08 char string[] = "This is a string."; /* char array */ 09 const char *stringptr = "Also a string"; /* char pointer */ 10 11 printf("%c\n", ch); 12 printf("%s\n", "This is a string."); 13 printf("%s\n", string); 14 printf("%s\n", stringptr); 15 16 return 0; 17 } /* end main */ a This is a string. This is a string. Also a string Output
Other conversion specifiers 12 %p Display the address of a pointer value %% Print a percent sign (%)
Printing with field widths 13 Size of field in which data is printed If the width is larger than data, default right justified If the field width is too small, it is increased to fit data Minus sign occupies one character in the field Integer width inserted between % and conversion specifier %4d: field width is 4 printf( print %4d, 23); p r i n t 2 3
Example of field widths 14 printf( %4d\n, 1); printf( %4d\n, 12); printf( %4d\n, 123); printf( %4d\n, 1234); printf( %4d\n, 12345); printf( %4d\n, -1); printf( %4d\n, -12); printf( %4d\n, -123); printf( %4d\n, -1234); printf( %4d\n, -12345); 1111 1112 1123 1234 12345 11-1 1-12 -123-1234 -12345
Printing with precision 15 Usage format % + dot (.) + precision number + conversion specifies Example: %.4d, %.4f Definition varies with data types For integer Minimal number of digits to print If data is too small, prefixed with zeros Example: printf( %.4d\n, 12); => ourput: 0012 For floating point Number of digits to appear after decimal Example: printf( %.4f\n, 12.34) => output: 12.3400 For string Maximum number of characters to be written from string
Example of precision 16 int i = 123; printf( %.4d\n%.9d\n, i, i); double f = 1234.5678; printf( %.3f\n%.3e\n%.4g\n, f, f, f,); char s*+ = happy birthday ; printf( %.11s\n, s); 0123 000000123 1234.567 1.234e+003 1235 happy birth
Flags 17 Placed right after (%) Several flags may be combined Flag Description - Left justify the output + Display + in front of a positive value (space) Display a space in front of a positive value # Display 0 in front of an octal integer Display 0x or 0X in front of a hex integer 0 Pad a field with leading zeros
Example of flags 18 printf( %5d students\n, 100); printf( %-5d students\n, 100); printf( %05d students\n, 100); printf( %+d\n%d\n, 100, 100); printf( % d\n%d\n, 100, -100); printf( %#o\n, 1427); printf( %#x\n%#x\n, 1427, 1427); 00100 students 10000 students 00100 students +100 100 100-100 02623 0x593 0X593
Escape sequences 19 Escape sequence Output \ Single quote ( ) \ Double quote ( ) \? Question mark (?) \\ Backslash (\) \a Bell \b Backspace (Move the cursor back one position) \f Move the cursor to the start of the next page \n Move the cursor to the next line \r Move the cursor to the beginning of the current line \t Move the cursor to the next horizontal tab position \v Move the cursor to the next vertical tab position
Formatting output with scanf 20 Syntax scanf("format control string", &var1, &var2, ); int a; scanf( %d, &a); // read integer double f; scanf( %lf, &f); // read double floating point char ch; scanf( %c, &ch); // read character char s[10]; scanf( %s, s); // read string (should notice the size of string )