2.1 – Accepting Data from Keyboard

by subbu on August 9, 2013

Writing more generic (general) programs:

So far we have learned how to write and execute simple C programs. In this session we will see how we can write more general programs by accepting data from kayboard.

#include<stdio.h>
int main()
{
int fir, sec, sum;
fir=45;
sec=50;
sum=fir+sec;
printf("Sum of two numbers %d",sum);
return 0;
}

The above program is designed to add only two numbers that are 45 and 50. If we want to add any other numbers then, the program must be modified. Say for example we want to add 125 and 256 then; the program must be modified as.

#include<stdio.h>
int main( )
{
int fir, sec, sum;
fir=125;
sec=256;
sum=fir+sec;
printf("Sum of two numbers %d",sum);
return 0;
}

It is not a good practice to change the program, whenever input to the program need to be changed. We must write a general program that accepts input from the keyboard while executing the program.

scanf() is similar to printf(), accepts the data from keyboard and stores into the specified variables.

In scanf(), we must specify the list of variables into which the data must be stored by accepting from the keyboard. Every variable must be pre-fixed with the symbol & called “address of” operator (will be covered later). The format string has formatting characters to represent variables list and their types.

Syntax:

scanf(” Format String “, List of addresses of variables)

Example 1:
scanf in C language
Example explained:

While execution, the scanf() waits for an integer because %d is used as a formatting character in format string. The supplied value 40 is assigned to the specified variable x. Here it is mandatory to use & as prefix to the variable, otherwise the garbage value is assigned to the variable.

Example 2:
scanf() in C language
Example explained:

In the above example scanf() accepts two integers from the keyboard and stores into the variables x and y. Here we can supply values either one by one or two at once. While supplying multiple values make sure that a space separator is given among the values otherwise treated as a single value.

Programs to practice:

Specification 1: Accept the price, quantity of a product from the keyboard and print the bill, discount which is 5% of bill and the net bill.

Program:

#include<stdio.h>
int main()
{
int qty;
float price,dis,bill,net_bill;
printf("Give the price:");
scanf("%f",&price);/*accepts real value from keyboard*/
printf("Give the quantity:");
scanf("%d",&qty);/*accepts an integer from keyboard*/
bill=price*qty;
dis=bill*5/100;
net_bill=bill-dis;
printf("Total bill %f",bill);
printf("\nDiscount %f",dis);
printf("\nNet bill %f",net_bill);
return 0;
}

Execution:
Give the price: 25.25
Give the quantity: 3
Total bill 75.750000
Discount 3.787500
Net bill 71.9625

specification 2: Accept the present month meter reading of energy meter, previous month meter reading and print the number of units consumed and the total bill, if the unit charge is 3.75 per unit.

Program:

#include<stdio.h>
int main()
{
int cmr,pmr,nu;
float bill;
printf("Enter the present month meter reading:");
scanf("%d",&cmr);
scanf("Enter the previous month meter reading:");
scanf("%d",&pmr);
nu=cmr-pmr;
bill=nu*3.75;
printf("Number of units consumed %d",nu);
printf("\nTotal bill %f",bill);
return 0;
}

Execution:
Enter the present month meter reading: 450
Enter the previous month reading: 300
Number of units consumed 150
Total bill 562.500000

Problem with scanf() in eclipse:

#include<stdio.h>
int main()
{
int x,y,z;
printf("Enter the first number:");
scanf("%d",&x);
printf("Enter the second number:");
scanf("%d",&y);
z=x+y;
printf("Sum of two numbers %d",z);
return 0;
}

Execution:
12
45
Enter the first number:Enter the second number:Sum of two numbers 57

Example explained:

While executing the above program by surprise eclipse first accepts the input from the user and then all the output would be printed at once. To make the above program as interactive program, we need to make some changes to the above program. We write fflush(stdout); statement after every printf() statement to flush output before executing scanf()

#include<stdio.h>
int main()
{
int x,y,z;
printf("Enter the first number:");
fflush(stdout);//only in eclipse
scanf("%d",&x);
printf("Enter the second number:");
fflush(stdout);//only in eclipse
scanf("%d",&y);
z=x+y;
printf("Sum of two numbers %d",z);
return 0;
}

Execution:
Enter the first number:12
Enter the second number:45
Sum of two numbers 57

The problem is rectified by introducing fflush() statement after every printf() statement. We no need to use this in any other environments.

Previous post:

Next post: