DOC PREVIEW
IUPUI CSCI 23000 - Basic I/O – scanf()

This preview shows page 1-2-3 out of 10 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Slide 1Formatting Input with ScanfSlide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Other Input / OutputDale RobertsBasic I/O – scanf()Basic I/O – scanf()CSCI 230Department of Computer and Information Science,School of Science, IUPUIDale Roberts, Dale Roberts, LecturerLecturerDepartment of Computer and Information ScienceDepartment of Computer and Information ScienceIUPUIIUPUIDale RobertsFormatting Input with Formatting Input with ScanfScanfscanfscanfInput formattingInput formattingCapabilitiesCapabilitiesInput all types of dataInput all types of dataInput specific charactersInput specific charactersSkip specific charactersSkip specific charactersFormatFormatscanfscanf((format-control-stringformat-control-string, other-arguments, other-arguments););Format-control-string: describes formats of inputsFormat-control-string: describes formats of inputs%*wlx%*wlx[*]:[*]: optional conversion only, result is not stored optional conversion only, result is not stored[w]:[w]: optional minimum width (wider if necessary). The padding character is optional minimum width (wider if necessary). The padding character is blank normally and zero if the field width was specified with a leading blank normally and zero if the field width was specified with a leading zerozero[l]:[l]: long integer long integer[x]:[x]: see the table next page see the table next pageOther-argumentsOther-argumentsPointers to variables where input will be stored (Pointers to variables where input will be stored (address of variables)address of variables)Can include field widths to read a specific number of characters from the streamCan include field widths to read a specific number of characters from the streamDale RobertsConversion specifier DescriptionIntegersddiioouux or Xx or Xh or lh or lRead an optionally signed decimal integer. The corresponding argument is a pointer to integerRead 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.Dale RobertsFloating-point Numbere,E,f,g,GI or LRead 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 inputCharacters and stringsccssRead a character. The corresponding argument is a pointer to char no null (‘\0’) is addedRead 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.Dale RobertsScan set[scan charScan a string for a set of characters that are stored in an array.Miscellaneousppnn%% 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 inputDale RobertsFormatting Input with ScanfFormatting Input with ScanfScan setsScan setsSet of characters enclosed in square brackets Set of characters enclosed in square brackets [][]Preceded by Preceded by %% sign signScans input stream, looking only for characters in scan Scans input stream, looking only for characters in scan setsetWhenever a match occurs, stores character in specified arrayWhenever a match occurs, stores character in specified arrayStops scanning once a character not in the scan set is foundStops scanning once a character not in the scan set is foundInverted scan setsInverted scan setsUse a caret Use a caret ^^: : [^aeiou][^aeiou]Causes characters not in the scan set to be storedCauses characters not in the scan set to be storedSkipping charactersSkipping charactersInclude character to skip in format controlInclude character to skip in format controlOr, use Or, use ** (assignment suppression character) (assignment suppression character)Skips any type of character without storing itSkips any type of character without storing itDale Roberts1 /* Fig 9.20: fig09_20.c */2 /* Reading characters and strings */3 #include <stdio.h>45 int main()6 { 7 char x, y[ 9 ];8 9 printf( "Enter a string: " );10 scanf( "%c%s", &x, y );1112 printf( "The input was:\n" );13 printf( "the character \"%c\" ", x );14 printf( "and the string \"%s\"\n", y );1516 return 0;17 }Enter a string: SundayThe input was:the character "S" and the string "unday" /* initialize variables *//* input *//* print */Example:Program Output:Dale Roberts1 /* Fig 9.22: fig09_22.c */2 /* Using an inverted scan set */3 #include <stdio.h>45 int main()6 { 7 char z[ 9 ] = { '\0' };8 9 printf( "Enter a string: " );10 scanf( "%[^aeiou]", z );11 printf( "The input was \"%s\"\n", z );1213 return 0;14 }Enter a string: StringThe input was "Str" /* initialize variables *//* input *//* print */Example:Program Output: Example:int i,float x;char name[50];scanf(“%d %f %s”, &i, &x, name);With input: 2554.32E-1Thompson25  i5.432  x“Thompson”  nameDale Roberts1 /* Fig 9.24: fig09_24.c */2 /* Reading and discarding characters from the input stream */3 #include <stdio.h>45 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 );1718 return 0;19 }Enter a date in the form mm-dd-yyyy: 11-18-2000month = 11 day = 18 year = 2000 Enter a date in the form mm/dd/yyyy: 11/18/2000month = 11 day = 18 year = 2000 Example:Program Output:Dale RobertsOther Input / OutputOther Input / Outputputs(line)puts(line) Print a string to standard output and append a newlinePrint a string to standard output and append a newlineExampleExample::puts(“12345”);puts(“12345”);putchar(c)putchar(c) Print a character to standard outputPrint a


View Full Document

IUPUI CSCI 23000 - Basic I/O – scanf()

Download Basic I/O – scanf()
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Basic I/O – scanf() and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Basic I/O – scanf() 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?