How to take input from user / to use C language basic function scanf / taking formated input from user / input from user prespective

Now when we have learnt how to print the basic text at the screen, some people might have thought that why would we always need to write something on the screen while we are coding our program, so the answer is that, the printf is basically a printing command, in order to take input from user we would use scanf, that takes input from user and transfer it to the processing machine.

Scanf can take input in the form of characters1, integers2, float3 etc. These three are some of the data types of C language, remaining data types would be cleared in further articles. By using scanf you can take information from user, it can ask questions from the user like, if someone is student we can make a form that asks his/her name, class, roll number, total marks and obtained marks. For this we would need English alphabets for name, class, total marks and obtained marks would be numbers and class roll number would be alphanumeric4(Alpha numeric means the characters comprising of alphabets as well as numbers).

The syntax used for scanf is simple and just like printf (learnt in lesson-3), the difference that creates sense is the symbol of Ampersand (&), that refers to some address. The information or data that we enter is saved at some place in memory and this sign refers to that address. For taking an input from user, we first have to make room for that storage; this room may be of an integer, character or float type. We would start from the integer is it would be easy to understand and making the firm basis.

Integer is not fully written, instead we use keyword for it, that is int ( The further keywords are displayed at the end of the article). We first have to take an integer and name it, like int first.That refers that a variable integer named first is allotted in memory, it takes two bytes in memory, and can hold up numbers from -127 to 127. A small piece of code may solve the further confusion.



void main(void)



int first;

printf(“Enter a number:”);




Here scanf is the command in focus and if you see, ampersand (&) sign is used and the number you enter would be automatically saved to the location named first. The %d sign refers to the data type presentation for integers. Likewise %f and %c are used for floats and characters respectively. Therefore the float would be using decimal point numbers and character would be using all alphabets.

These integers and floats can be used for simple and complicated calculations and can effectively store the answers. Characters can be used for storing the alpha-numeric data.

A simple program for calculation of two numbers must be seen to clear the blurred concept.



void main(void)



int num1,num2,result;

printf(“Enter two numbers separated by comma: ”);












As you see the division wouldn’t be working properly, so we have to use float for this. Now come to the float and character aspect of this article. Float would be used for numbers having decimal point. Take an example, when you calculate percentage of something it may be not exactly 85 or 86, may be a value between it, this value cannot be stored in integer, we have to take the float type variable to store it. Let the float named first is taken, we can easily put a decimal point number in it. Consider an example:



void main(void)



float num1,num2,result;

printf(“Enter two numbers separated by comma”);


printf(“\nThe result is= %f”,result);



Where %f that was told before is the format-specifier for floats.


You have to create a program that takes the input of five subjects, adds them and calculates the percentage. (Hint: You have to use integer as well as float variables)

It would be really easy for you if you have picked all the concepts in last two articles.

Now think over a complete data of a student and by using character, integers and floats make it in a pleasing view, by using a simple scanf statement. We would first ask all the required fields from the user and then would display it in an elegant manner, all of us know what would be the elegant one. As we have already learnt that the C language doesn’t change its line automatically, so we have to do it ourselves else the display would be like this:

Name:RockistaniClass:First yearRollno:CS-0XXDepartment:ComputersystemsPercentage:82.76%

This is surely not the right way. Now consider the code below:



void main(void)



char name,class,rollno,department;

int total_marks,obtained_marks,percentage;


printf(“\nWhat is your name?\n”);


printf(“In which class do you study?\n”);


printf(“What is your discipline name?”);


printf(“Enter your marks obtained last year:”);


printf(“Enter total marks:”);



printf(“\n\nStudent Data:\n”);

printf(“\nName: %c”,name);

printf(“\nClass: %c”,class);

printf(“\nDiscipline: %c”,department);

printf(“\nPercentage: %f”,percentage);


}    //main ends

Where “//” lines are used for single line comment, likewise /* and */ are used for multiple lines comment.


You have to create a form for the job of an employee, the fields that must be entered are Name, Age, High school, Education, Working Experience and Last Salary.

The result must be well formatted and pleasant to eye.


Asjad Azeem.

  1. No trackbacks yet.

Leave a Reply

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

You are commenting using your 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

%d bloggers like this: