Presentation is loading. Please wait.

Presentation is loading. Please wait.

I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005

Similar presentations


Presentation on theme: "I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005"— Presentation transcript:

1 I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Narrator: Lecture 6: Input and Output in C Winter Quarter Lecture 06

2 Input/Output in C C has no built-in statements for input or output.
Engineering H192 Winter 2005 Input/Output in C C 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 do not need to memorize them, just be familiar with them. Instructor: Remind students of the #include function used at the beginning of a program: #include <stdio.h> Narrator: C itself has no idea how to handle input or output from the computer. The library function stdio.h includes the information necessary for C to understand how handle input and output on the computer it is programmed for. Concerning these libraries, you don’t need to memorize what functions are where, but be familiar with them. The names of most C libraries we will use are fairly obvious and easy to remember. Winter Quarter Lecture 06

3 Streams All input and output is performed with streams.
Engineering H192 Winter 2005 Streams 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). Instructor: Streams include input from the keyboard and files, and output to the screen and files for programs in this class. Narrator: C handles all input and output in what are called streams. A “stream” is a sequence of characters organized into lines. Each line can contain 0 or more characters and ends with what is known as a “newline” character. This will be explained more later. The ANSI C standard specifies that the system must support lines that are at least 254 characters in length including this newline character. Winter Quarter Lecture 06

4 Engineering H192 Winter 2005 Types 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. Instructor: Streams can be “connected” to other interfaces, but for the purpose of this class the standard I/O connections are acceptable. Narrator: The types of streams we are concerned with include the standard input stream called “stdin”, which is normally connected to the keyboard. Another stream called “stdout”, the standard output stream, is connected normally to the display screen or terminal being used. Another stream is the standard error stream, or “stderr.” This is also normally connected to the screen or terminal. Note that we say “normally connected” since all of these streams can be connected to other interfaces, but for the purpose of this class the default standard I/O connections are acceptable. Winter Quarter Lecture 06

5 Formatted Output with printf
Engineering H192 Winter 2005 Formatted Output with printf printf ( ) ; 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 ) ; printf ( “You entered %f and %d \n”, a, b ) ; Instructor: The % sign indicates a variable’s value should be placed in this location and can be mixed with static text such as the example provided. The letter after the % (%f and %d) tells the program what kind of variable’s value will be placed in the position. The variables are placed in the text string in the order given after the text string (the value of a is placed in the first position, the value of b is placed in the second position). Also notice the \n at the end of the printf string. This is a new line character. It is an invisible character that tells the computer to move the cursor to the next line. This is helpful when trying to format text and will be used often. Narrator: Now that we understand some of the available streams, we must also have functions which make use of the streams. The first function we will look at is printf(). This function provides for formatted output to the screen. The syntax is as shown, where “format” includes a method of displaying the values of any of the variables we have discussed, some static text, and control characters. An example of printf is shown here with some static text and two variables displayed within this text. The % sign indicates a variable’s value should be placed in this location. The letter after the % (%f and %d) tells the program what kind of variable’s value will be placed in the position. The combination of the % and these letters are called format conversion specifiers. The variables are placed in the text string in the order given after the text string (the value of a is placed in the first position, the value of b is placed in the second position). The second command shown here, scanf, will be discussed later in this lecture. Winter Quarter Lecture 06

6 Formatted Output with printf
Engineering H192 Winter 2005 Formatted Output with printf Format Conversion Specifiers: d -- displays a decimal (base 10) integer l -- used with other specifiers to indicate a "long" e -- displays a floating point value in exponential notation f -- displays a floating point value g -- displays a number in either "e" or "f" format c -- displays a single character s -- displays a string of characters Instructor: This is an abbreviated list of format conversion specifiers that can be used. The l specifier can be used with other specifiers (such as %ld or %lf). Narrator: Here are some of the most common format conversion specifiers. The %d will display variables in an integer format, the %f in a floating point format, and so on. A list of these specifiers is also available in your “C How To Program” text. Winter Quarter Lecture 06

7 Input/Output in C scanf ( ) ;
Engineering H192 Winter 2005 Input/Output in C scanf ( ) ; 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); Instructor: Much like printf, scanf uses a string of format specifiers, but in this case to determine the type of expected input from the stream. Scanf takes the information from the stream and places it in order into the variables after the format specifier. The & before the variables is required in this case. Narrator: Now that we have discussed a method of producing output, we will look at a function used to gather input. The scanf() function provides for formatted input from the keyboard. The syntax is as shown with the format again being a listing of the data types, but in this case for input, and also note the ampersand in front of the variable names. This tells the system WHERE to store the value that is being input. The ampersand before a variable provides the address in memory where the variable is actually being stored. This will be discussed more in a later lecture about what are called pointers. For now remember to include the ampersand before variables within the scanf() function. An example scanf function is shown which simply obtains a floating point and then an integer value from the keyboard and stores the values in the variables a and b respectively. Winter Quarter Lecture 06

8 Input/Output in C getchar ( ) ;
Engineering H192 Winter 2005 Input/Output in C getchar ( ) ; This function provides for getting exactly one character from the keyboard. Example: char ch; ch = getchar ( ) ; Instructor: getchar is useful when implementing a menu of some sort that requires the user to input a single character to tell the computer what to do. getchar is essentially the same as scanf(“%c”,&ch); Narrator: A shortcut function called getchar() also exists which is used to get example one character from the keyboard. This is useful when implementing a menu of some sort that requires the user to input a single character to tell the computer what to do. Notice getchar() requires no arguments and returns the character to the variable on the left hand side of the equation. Winter Quarter Lecture 06

9 Input/Output in C putchar (char) ;
Engineering H192 Winter 2005 Input/Output in C putchar (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 */ Narrator: putchar(), the opposite of getchar(), is used to put exactly one character on the screen. Putchar requires as an argument the character to put on the screen. The example shows use of both getchar() to get a character from the keyboard, and putchar() to display that same character to the screen. Winter Quarter Lecture 06

10 Input/Output in C getc ( *file ) ;
Engineering H192 Winter 2005 Input/Output in C getc ( *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 */ Instructor: Unlike getchar(), getc isn’t connected to a certain stream automatically. It is up to the user to determine which stream to connect getc to. Narrator: A somewhat more general function which operates much like getchar() is getc(). In this case getc() isn’t automatically connected to the keyboard input stream, so it is up to the programmer to specify which stream to get a character from. This is useful when the programmer wishes to get a character from a file instead of the user. The stream is specified as the argument. In the given example the program gets one character from the keyboard via the stdin standard input stream, and then gets a character from what is known as a file pointer, which will be discussed in detail in a later lecture. Winter Quarter Lecture 06

11 Input/Output in C putc ( char, *file ) ;
Engineering H192 Winter 2005 Input/Output in C putc ( 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 */ Narrator: Of course there must also exist a putc() function to output a single character to a stream of the programmer’s choice. The putc() function requires as arguments, first the character to be placed in the stream, and then the stream to place the character into. As shown in the sample, putc can be used to write to the stdout standard output stream or to a file pointer. Winter Quarter Lecture 06

12 Engineering H192 Winter 2005 Today’s Assignment G05 asks you to write a complete C program to prompt a user for input, receive the input, do a calculation and print the input and the answer on the screen. Then modify the program to add another calculation and display the new result. Turn in a copy of both g05.cpp and g05mod.cpp Winter Quarter Lecture 06


Download ppt "I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005"

Similar presentations


Ads by Google