C Programming Tutorial – Reading Input Values

In C language, variables are assigned values directly and then we can use them in different types of expressions and get the results. But the initial values given to the variables are fixed and does not changes. Sometimes the user wants to enter the input values themselves and check the results for different input values.

For example

[cc lang=”c” tab_size=”4′]
int main{

int a , b, c;

a = 10;

b = 20;

c = a + b;

printf(“%d”,c);

}
[/cc]

In the example above, the variable a and b has fixed values – 10 and 20, which decide the result of the program. If the user wants to test the program with a different set of input values, he or she needs to change the source code every time.

Reading Input with scanf()

Fortunately C programming language has a builtin function called scanf() which helps read input value during the run-time of the program. There is not need to change the input values from source code every time user wants a different input value.

The function scanf() is a C standard library function. It means that the C library supports different features of C programming language using a list of header files. To use any library function such as scanf() you need to include the header file containing that library function.

The scanf() is part of header file – stdio.h

Syntax for scanf()

The syntax for scanf() is given below.

[cc lang=”c” tab_size=”4″ line_numbers=”off”]
int number;
scanf(“%d”, &number);
[/cc]

First we declared a variable called number of type integer. Then we used scanf statement. This statement has two important elements.

  1. format specifier
  2. address of operator

Format specifier

The format specifier tells what data type to read or write. It is very important to specifier what you are reading during the execution of a program, otherwise the C program will not recognize the input values. These are the common format specifiers.

  1. %d – for integer
  2. %f – for float
  3. %c – for character type
  4. %s – for string type

Address of Operator (&)

You may come across this operator time during this tutorial. This operator (&) is called the “address of” operator and it tells the memory location of the variable, so that, we can read input value and store at that address.

Example Program

In the following program, we will read quantity of two fruits that a customer purchased and then calculate the total amount payable.

[cc lang=”c” tab_size=”4″]
#include

int main()
{
float unit_price_apple, unit_price_orange;
int total_no_of_apples, total_no_of_oranges;
int i;
float total_cost = 0.0;

/* let’s assign initial values to unit prices */

unit_price_apple = 2.50;
unit_price_orange = 1.50;

/* Read the quantity of apples and oranges purchased by the customer */

printf(“Enter total no of apples purchased:”);
scanf(“%d”, &total_no_of_apples);
printf(“Enter total no of oranges purchased:”);
scanf(“%d”, &total_no_of_oranges);

/* Compute total costs */

total_cost = (total_no_of_apples * unit_price_apple) + (total_no_of_oranges * unit_price_orange);

for(i=0;i<45;i++) printf("_"); printf("\n"); printf("%f\n", total_cost); system("pause"); return 0; }[/cc]

Output

The input and output of the example program is given below.

Output - Example Program
Output – Example Program

Reading String or Char Inputs – getchar() and gets()

Sometimes it is easier to use use builtin functions to read string or character type variable values. The C language has two popular function in the library for this purpose.

  1. getchar()
  2. gets()

getchar()

The getchar() is part of stdio.h header file and helps reading a single character. The following example show the usage and syntax for getchar() library function.

[cc lang=”c” tab_size=”4″]
#include
#include

int main()
{
char grade;

printf(“Enter the Grade of a Student:”);
grade = getchar();
printf(“\n\n”);

printf(“Your grade is %c\n”,grade);

system(“PAUSE”);
return 0;
}
[/cc]

Output – getchar()

When the program is executed, user must input a grade for a student. The getchar() function will receive the input grade which is a single character and assign it to variable grade. The grade is printed as an output to the program.

Output - getchar()
Output – getchar()

gets()

The gets() function reads a string of characters. It is also part of stdio.h header file and a standard C library function. It is very useful in many string related programs.

A string is nothing but an array of characters with a terminating null character (\0). The gets() function copies the string of characters until a newline or end-of-file (EOF) is reached. To help you understand the syntax and usage of this function check the example program below.

[cc lang=”c” tab_size=”4″]
#include
#include

int main()
{
char name[100];

printf(“Enter Your Name:”);
gets(name);
printf(“\n\n”);

printf(“Your name is %s\n”,name);

system(“PAUSE”);
return 0;
}
[/cc]

Output – gets()

The output of the above program is given below.

Output - gets()
Output – gets()

Bibliography

Balagurusamy, E. 2000. Programming in ANSI C. Tata McGraw-Hill Education,.

Brian W. Kernighan, Dennis M. Ritchie. 1988. C Programming Language, 2nd Edition. Prentice Hall.

Kanetkar, Yashavant. 20 November 2002. Let us C. Bpb Publications.

 

Advertisements