Sunday, October 14, 2007

Input-output functions

Suppose you develop a piece of software to be used for computerizing a bank’s operations. In the code for this software, you would write a program to store details of the bank’s customers. In this program let us assume that you have used the assignment operator (refer to the previous article) for storing customer details such as name, age and telephone number in the respective variables. How would you enter the details of a new customer?

You would do this by opening the program, assigning the values to variables, and compiling the program again. However, this is a cumbersome process, as you have to recompile the program each time that you make a change. Moreover, when using an assignment operator, you may not know in advance the values that you want to assign to the variables.

Data and input functions

For the reasons mentioned above, almost every program should include statements used to request input from users while the program is running. Such statements are called input statements. The C compiler has built-in input functions for storing data (entered through a keyboard) in variables while the program is running. These input functions facilitate interactive programming, i.e., you can interact with a running program. One such widely used function in C is scanf().

The scanf() function

The syntax of scanf() and that of printf() look alike except that an ‘&’ symbol precedes every variable in the case of scanf(). This function is used to input different kinds of values — int, float, and char.

Read the following code. It demonstrates the use of the scanf() function:

main()
{
1. int a;
2. float b;
3. char c;
4. printf(“Enter an integer:”);
5. scanf(“%d”,&a);
6. printf(“Enter a float:”);
7. scanf(“%f”,&b);
8. printf(“Enter a character:”);
9. scanf(“%c”,&c);
10. printf(“You entered %d %f %c”, a, b, c);


When you run this program, it prompts you to enter first an integer, then a float, and finally a character. After you enter these values, the last printf() function displays all the three values entered.

Other input functions

Apart from the scanf() function, there are also other functions such as getchar() and gets() to store characters and strings (set of characters). The following program demonstrates the use of these functions.



#include
main ()
{
1. char a;
2. printf (“Enter any character:”);
3. a=getchar();
4. printf(“The character you entered is:%c”,a);


Observe how the function getchar() is used in this program. This function receives a character entered by the user and stores it in the variable a. The printf() function written below the getchar() statement displays on the screen the character that you entered from the keyboard. C, however, also provides a function called putchar() to display the character on the screen. You can use this function in the above program instead of printf(). The following statement is written to use putchar() in the above program.

putchar(a);

Accepting strings using gets() function

In the previous articles, you saw a program that displayed a string (Hello, World) on the screen using the printf() function. Now you will see how to accept a string from keyboard and display it.

In this program:

 The first statement — char a[25]; — informs the compiler to set aside 25 locations to store 25 characters.
 The second statement prompts the user by displaying the message “Enter a string” on the screen.
 The third statement accepts the string from the keyboard.
 The fourth statement displays the string that you entered on the screen.
 %s stands for ‘format string’ to print strings.

You can also get the same result for the above program by using another kind of output function, puts(). This function is used exclusively for handling strings. It can’t be used for printing numeric data. The following statement would be written to use puts() in the above program.
Try it out:

1. Write a program to accept and store the values 15, 30.5 and ‘A’ in the variables i, f, and c using the function scanf(), and to display them using the printf() function.

2. Write a program to accept and store a character (say, ‘$’) in the variable ch using the function getchar() and t display it on the screen using the function putchar().

3. Write a program to accept and store a string (say, “Hi-Tech City”) in the variable city and to display it on the screen using the function puts().

No comments: