C Program to take Input from Keyboard

Program to Take Input From the Keyboard

Last program that we made out was only to print out something on the screen, what if we want to take input from keyboard for displaying it or for doing some arithmetic operation on it.

For this purpose, there is a function scanf(); to take input from keyboard. Before using this function, Let’s know about Format Specifiers.

In C we define variables using data types, the basic data types are int, char and float.

Suppose we want to store the value 5 in memory, we know 5 is a integer number because there is no decimal point in this number. So for storing 5 in memory, we will have to declare a variable of int type i.e. int num;. Then we can store the value in that num variable of integer type. This statement tells the compiler to reserve a memory space for storing the integer value and give this memory space the name num. Remember the size of int is 2 byte.

Same, we can declare a variable to store any decimal point value like 5.5 (Real/Float value), then we will use the statement float num;. The size of float is 4 byte. Similarly, to store a character, we will use keyword char to declare the variable of char type (char alph;). The size of char is 1 byte.

Now after declaring the variable, to store the value in it, we have to write the format specifier in scanf(); function of that data type. To tell the compiler what type of value will be input from keyboard.

The format specifier for int is %d, for float is %f and for char is %c.

Also when we want to print out the value a variable stored or to print out the result of any arithmetic operation we did with two variables. We then also have to write the format specifier, depending what type of value we are going to print.

And for taking the input from keyboard, we also have to tell the compiler the address of memory space, where the value will be store. This will achieve by using &(ampersand) “address of” operator in scanf(); function following the name of variable.

Let’s Write Program For It ►►

#include<stdio.h>
#include<conio.h>
 
void main()
{
   int a,b,sum;

   printf("Enter the value of a and b\n");
   scanf("%d %d",&a,&b);

   sum=a+b;

   printf("Sum = %d\n",sum);
 
getch();   
}

I think you already understood the program. It’s all what Ih told you. Using printf funtion I print out to enter a number and by scanf, I store those number in the variable a and b, The %d %d are format specifier, first for a and second for b. Then we pass the address of a and b using ‘address of’ operator (&).

Then we calculate the sum of a and b, and print out using printf. The %d in printf function is to print the value of sum (which is integer).

And always remember before using these variables, we must first have to declare them to tell the compiler what type of value will they store and what will be the name of that variable to use as a reference.

// The “\n” in the printf function is for new line. It takes cursor into the new line.

►►Write Your Program◄◄

 

►► Make a Program to input any float type value like 5.6 and  calculate the sum of those floating point values, and print out the sum on the screen.

Remember you have to use the format specifier %f in every function, instead of %d and declare all variables as a floating type variable using float keyword. As “float a,b,sum;”

◄◄Prev                                                                                                               ►►Next

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

spreading the power of programming …

%d bloggers like this: