C 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.

Advertisements

But the initial values given to the variables are fixed and does not change. Sometimes the user wants to enter the input values themselves and check the results for different input values.

For example

int main{
    int a , b, c;
    a = 10; no
    b = 20;
    c = a + b;
    printf("%d",c);
}

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 built-in function called scanf() which helps read input value during the run-time of the program. There is not needed 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.

int number;
scanf("%d", &number);

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.

Advertisements
  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.

#include <stdio.h>
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;
}

Output

Enter total no of apples purchased:10
Enter total no of oranges purchased:33
____________________________________________
74.500000

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

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

Sometimes it is easier to use functions to read string or character type variable values. The C language has two popular functions 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.

#include <stdio.h>
#include <stdlib.h>
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;
}

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.

Enter the Grade of a Student:A
Your grade is A

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.

#include <stdio.h>
#include <stdlib.h>
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;
}

Output – gets ()

The output of the above program is given below.

Enter Your Name:  Jack Sparrow
Your name is Jack Sparrow    

References

  • 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

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Exit mobile version