t03BBasicIOScanf

t03BBasicIOScanf - Basic I/O – scanf()...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Basic I/O – scanf() www.kite-legends.blogspot.com Dale Roberts Formatting Input with Scanf Formatting Scanf scanf Input formatting Capabilities Input all types of data Input specific characters Skip specific characters Format scanf(format-control-string, other-arguments); Format-control-string: describes formats of inputs %*wlx [*]: optional conversion only, result is not stored [w]: optional minimum width (wider if necessary). The padding character is blank normally and zero if the field width was specified with a leading zero zero [l]: long integer [x]: see the table next page Other-arguments Pointers to variables where input will be stored (address of variables) Can include field widths to read a specific number of characters from the stream www.kite-legends.blogspot.com Dale Roberts Conversion specifier Description Integers d i o u x or X h or l Read an optionally signed decimal integer. The corresponding argument is a pointer to integer Read an optionally signed decimal, octal, or hexadecimal integer. The corresponding argument is a pointer to integer. Read an octal integer. The corresponding argument is a pointer to unsigned integer. Read an unsigned decimal integer. The corresponding argument is a pointer to unsigned integer. Read a hexadecimal integer. The corresponding argument is a a pointer to unsigned integer. Place before any of the integer conversion specifiers to indicate that a short or long integer is to be input. www.kite-legends.blogspot.com Dale Roberts Floating-point Number e,E,f,g, G I or L Read a floating point value. The corresponding argument is a pointer to a floating point variable. Place before any of the floating point conversion specifiers to indicate that a double or long double value is to be input Characters and strings c s Read a character. The corresponding argument is a pointer to char no null (‘\0’) is added Read a string. The corresponding argument is a pointer to an array of type char that is large enough to hold the string and a terminating null (‘\0’) character which is automatically added. www.kite-legends.blogspot.com Dale Roberts Scan set [scan char Scan a string for a set of characters that are stored in an array. Miscellaneous Read an address of the same form produced when an address is output with %p in a printf statement Store the number of characters input so far in this scanf. The corresponding argument is a pointer to integer Skip a percent sign (%) in the input www.kite-legends.blogspot.com Dale Roberts Formatting Input with Scanf Scan sets Set of characters enclosed in square brackets Set Preceded by % sign Preceded Scans input stream, looking only for characters in scan Scans set set Whenever a match occurs, stores character in specified array Stops scanning once a character not in the scan set is found Inverted scan sets Use a caret ^: [^aeiou] Use [^aeiou] Causes characters not in the scan set to be stored Skipping characters Include character to skip in format control Or, use * (assignment suppression character) Or, Skips any type of character without storing it www.kite-legends.blogspot.com Dale Roberts Example: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 /* Fig 9.20: fig09_20.c */ /* Reading characters and strings */ #include <stdio.h> int main() { char x, y[ 9 ]; /* initialize variables */ /* input */ /* print */ printf( "Enter a string: " ); scanf( "%c%s", &x, y ); printf( "The input was:\n" ); printf( "the character \"%c\" ", x ); printf( "and the string \"%s\"\n", y ); return 0; } Program Output: Enter a string: Sunday The input was: the character "S" and the string "unday" www.kite-legends.blogspot.com Dale Roberts Example: int i, float x; char name[50]; scanf(“%d %f %s”, &i, &x, name); With input: 25c54.32E-1cThompson Example: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 /* Fig 9.22: fig09_22.c */ /* Using an inverted scan set */ #include <stdio.h> int main() { char z[ 9 ] = { '\0' }; 25 ⇒ i 5.432 ⇒ x “Thompson” ⇒ name /* initialize variables */ /* input */ /* print */ printf( "Enter a string: " ); scanf( "%[^aeiou]", z ); printf( "The input was \"%s\"\n", z ); return 0; } Program Output: Enter a string: String The input was "Str" www.kite-legends.blogspot.com Dale Roberts Example: 1 /* Fig 9.24: fig09_24.c */ 2 /* Reading and discarding characters from the input 3 #include <stdio.h> stream */ 4 5 int main() 6{ 7 int month1, day1, year1, month2, day2, year2; 8 9 printf( "Enter a date in the form mm-dd-yyyy: " ); 10 scanf( "%d%*c%d%*c%d", &month1, &day1, &year1 ); 11 printf( "month = %d day = %d year = %d\n\n", 12 month1, day1, year1 ); 13 printf( "Enter a date in the form mm/dd/yyyy: " ); 14 scanf( "%d%*c%d%*c%d", &month2, &day2, &year2 ); 15 printf( "month = %d day = %d year = %d\n", 16 month2, day2, year2 ); 17 18 return 0; 19 } Program Output: Enter a date in the form mm-dd-yyyy: 11-18-2000 month = 11 day = 18 year = 2000 Enter a date in the form mm/dd/yyyy: 11/18/2000 month = 11 day = 18 year = 2000 www.kite-legends.blogspot.com Dale Roberts Other Input / Output puts(line) putchar(c) gets(line) Print a string to standard output and append a newline Print puts(“12345”); Example: Example: Example: Print a character to standard output Print putchar(‘A’); char buf[128]; Read a string from standard input (until a newline is entered) Read gets(buf); /* space is OK, and the ‘\n’ won’t be read in */ Newline will be replaced by ‘\0’ getchar() Get a character from standard input int c; Example: Example In-memory Format Conversion c = getchar(); getchar(); /* c must be int */ int sprintf(string, control, variables); sscanf(string, control, address-of-variables); Example: sprintf(buf, “project%03d%02d.dat”,year,month); sprintf(buf, Sscanf(buf, “project%03d%02d”,&year,&month); www.kite-legends.blogspot.com Dale Roberts ...
View Full Document

Ask a homework question - tutors are online