New version page

OSU ENGR H192 - LECTURE NOTES

Documents in this Course
Strings

Strings

22 pages

Load more
Upgrade to remove ads
Upgrade to remove ads
Unformatted text preview:

I/O in CInput/Output in CStreamsTypes of Streams in CFormatted Output with printfSlide 6Slide 7Slide 8Slide 9Slide 10Slide 11Engineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 6 P. 1Winter QuarterI/O in CLecture 6Engineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 6 P. 2Winter QuarterInput/Output in C•The C language has no built-in statements for input or output. •A library of functions is supplied to perform these operations. The I/O library functions are listed the “header” file <stdio.h>. You can look them up online at:http://www.cplusplus.com/reference/clibrary/cstdio/•You do not need to memorize all of them, just be familiar with the more common ones we’ll use.Engineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 6 P. 3Winter QuarterStreams•All input and output is performed with streams.•A "stream" is a sequence of characters organized into lines.•Each line consists of zero or more characters and ends with the "newline" character.•ANSI C standards specify that the system must support lines that are at least 254 characters in length (including the newline character).Engineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 6 P. 4Winter QuarterTypes of Streams in C•Standard input stream is called "stdin" and is normally connected to the keyboard•Standard output stream is called "stdout" and is normally connected to the display screen.•Standard error stream is called "stderr" and is also normally connected to the screen.Engineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 6 P. 5Winter QuarterFormatted Output with printfprintf ( ) ;•This function provides for formatted output to the screen. The syntax is:printf ( “format”, var1, var2, … ) ;•The “format” includes a listing of the data types of the variables to be output and, optionally, some text and control character(s). •Example:float a ; int b ;scanf ( “%f%d”, &a, &b ) ; From Keyboardprintf ( “You entered %f and %d \n”, a, b ) ;Engineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 6 P. 6Winter QuarterFormatted Output with printf•Format Conversion Specifiers:d or i -- displays a decimal (base 10) integer l -- used with other specifiers to indicate a "long"e -- displays a floating point value in exponential notationf -- displays a floating point value g -- displays a number in either "e" or "f" formatc -- displays a single characters -- displays a string of charactersEngineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 6 P. 7Winter QuarterInput/Output in Cscanf ( ) ;•This function provides for formatted input from the keyboard. The syntax is:scanf ( “format” , &var1, &var2, …) ;•The “format” is a listing of the data types of the variables to be input and the & in front of each variable name tells the system WHERE to store the value that is input. It provides the address for the variable.•Example:float a; int b;scanf (“%f%d”, &a, &b);Engineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 6 P. 8Winter QuarterInput/Output in Cgetchar ( ) ;•This function provides for getting exactly one character from the keyboard.•Example: char ch; Declaring a character variable ch = getchar ( ) ;Engineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 6 P. 9Winter QuarterInput/Output in Cputchar (char) ;•This function provides for printing exactly one character to the screen.•Example:char ch;ch = getchar ( ) ; /* input a character from kbd*/putchar (ch) ; /* display it on the screen */Engineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 6 P. 10Winter QuarterInput/Output in Cgetc ( *file ) ;•This function is similar to getchar( ) except the input can be from the keyboard or a file.•Example:char ch;ch = getc (stdin) ; /* input from keyboard */ch = getc (fileptr) ; /* input from a file */Engineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 6 P. 11Winter QuarterInput/Output in Cputc ( char, *file ) ;•This function is similar to putchar ( ) except the output can be to the screen or a file.•Example:char ch;ch = getc (stdin) ; /* input from keyboard */putc (ch, stdout) ; /* output to the screen */putc (ch, outfileptr) ; /*output to a file


View Full Document
Download LECTURE NOTES
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 LECTURE NOTES 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 LECTURE NOTES 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?