Input/Output Week 5:Lesson 16.1 Commands (On-Line) scanf/printf Principles of Programming-I / 131101 Prepared by: Dr. Bahjat Qazzaz -------------------------------------------------------------------------------------------- Introduction We have already practiced entering data from the keyboard and displaying results on the screen. The data can be introduced to the program, mainly, by three ways: 1. By initializing the variables during program typing. This is usually done to give an initial value or to assign a constant value to the variable. Consequently, when it is executed, the program gives the same results. For example, int sum=0; float PI = 3.14. 2. The data can be passed to the program from the keyboard using one of the functions dedicated to this purpose. The data, then, will be processed and displayed on the screen. This chapter will focus on this type of functions. 3. The data can be passed to the program using files or any of the computer ports. This type of data input will be introduced later in different chapter. Data Input and Output In this chapter, we will focus on six functions which are part of C library functions and used for data input and output. These functions include: printf, scanf, getchar, putchar, gets, and puts. These functions allow the data transfer between the standard input and output devices such as keyboard and screen respectively. The scanf Function This function is used to enter different data types including any combination of numerical values, single characters, and strings. Numerical values can be integer, float, short integer, long integer, unsigned integer, etc. The string includes any combination of
characters. Although we will give examples on how to read and write strings, the strings will be explained with details in a later chapter when we talk about arrays. The letter f in the scanf means formatted scanf. And the format of the scanf is as follows: scanf( control string, argument1, argument2,, argumentn); The control string consists of the character % followed by a character which indicates the type of the corresponding argument. For example, %d is used for integer, %f is used for float, %c is used for character, %s is used for string, etc. The argument is an identifier (e.g. variable and arrays) with a specific data type. The control string must match the data type of the argument. int age; float salary; char option; char name[20]; // this is a string or an array of characters scanf( %d %f %c %s, &age, &salary, &option, name); The previous statement prompts the user to enter four kinds of data: integer, float, character, and string. The control string can be written with and without white space. (e.g. %d%f%c%s ). Each variable (or argument) must be preceded by an ampersand character &. In fact, this variable is called an address (or pointer) that indicates where the data will be stored in the computer s memory. Notice that the variable name is not preceded by the & since it is defined as an array. Array identifiers should not begin with the ampersand. The printf function The printf function is used for outputting data on the screen. We have already seen example that used the printf function for displaying different data types on the screen. The printf function can be of different formats as follow: printf( string ); printf( Welcome to An-najah University ); printf( control string, argumnet1, argument2,, argumentn);
printf( %s %d %f, name, age, salary); Notice that, the variables in the printf function are not preceded by an &. printf( combinations of string and control string, argument1, argument2, argumnetn ); printf( My name is %s, my age is %d and my salary is %f, name, age, salary); This example is similar to example 6 of the previous chapter. However, the two numbers are entered from the keyboard by the user. The results of the execution vary each time the user enters different values. int main() int value1; /* first number to be input by the user */ int value2; /* second number to be input by the user */ int sum; /* variable in which sum will be stored */ //value1 = 20; printf( Please enter value1: ); scanf( %d, &value1); //value2 = 30; printf( Please enter value2: ); scanf( %d, &value2); sum = value1 + value2; printf( Value1 = %d\n, value1); printf( Value2 = %d\n, value2); printf( The sum of %d and %d is %d\nor\n,value1, value2, sum); printf( %d + %d = %d\n, value1, value2, sum); return 0;
Q4) Write a C program to calculate the total price of buying orange, apple, and tomato. The user must input the number of kilos and the price of each item, then the program calculates and displays on the screen the total price. Escape Sequences Certain characters (e.g. the double quote, the backslash \, the apostrophe, and the question mark?) and nonprinting characters (e.g. line feed or newline, bell, null) can t be displayed on the screen or used with printf function directly. Such characters must be preceded by a backslash. For example, to display a backslash on the screen you must type printf( \\ ); The same is applied to the rest of the characters (e.g. \n for new line, \ for double quote, \? for the question mark, \t for horizontal tab, \a for bell (alert), and \0 for null). The null is a special character added to mark the end of a string. printf( Ahmad\ s House \ AH\ \a\a\a is big\n ); The following program is to define variables and initialize them to calculate the area of a room given the length and width. int length; int width; length = 10; width = 20; area = length * width; Since the values of the length and width are known (constant), the program can be written in an easier way as follows:
area = 20 * 10; The program can be written using a single statement as follows: printf( Area = %d\n, 20 * 10); Keyboard and scanf statement The above three solutions share the same problem which is that, when you run these programs they always give the same result (Area = 200). This is true since the value of the length and width are assigned constant values which are 20 and 10 respectively. To overcome this problem, we will change the program so that the program prompts the user to input (enter) the values of the length and width during the run time. Thus, each time the user runs the program, he will be prompted to enter new values. The following program code shows how the scanf works: int length; int width; scanf( %d, &length); /* The user inputs the length value from the keyboard */ scanf( %d, &width20); /* The user inputs the width value from the keyboard */ area = length * width;
Interactive programming Interactive programming allows interfacing between the computer and user. That is, the computer can ask the user to do some actions and directs him to do what he is supposed to do. To understand what do we mean by that, we have added printf statement before each of the scanf statements. Compile and run the following program to see the difference: int main( ) /* Calculate the area of a room if the length is 20 and */ /* the width is 10 */ int length; int width; printf( Please enter the length of the room: ); scanf( %d, &length); printf( Please enter the widh of the room: ); scanf( %d, &width20); area = length * width; return 0;