Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

scanf in c

So what is Scanf Function in C?
What is the use of Scanf function in C language?
In this post we are going to see the detailed explanation about scanf function and its use in writing program.
The scanf function is a library function.Library functions are by default declared in the C language.
Scanf function in c allows compiler to accept the input given by the user using keyboard and is often referred to as counter part of printf function because printf function is used to display output on the screen whereas it takes input.

  • For us to be able to use scanf() function,it is necessary to use #include at the beginning of the program.As discussed earlier it is a preprocessor directive and it's objective will be discussed later.For now just remember to use it wherever you are using scanf() function.
  • In C programming the scanf() function is used to read different type of data.The different type of data includes integer,real,character.It is also used to input character strings(strings will be discussed later in other post).
The general code used for scanf() function is:
 
Syntax of the scanf function

A simple program showing the use of scanf function in C program:
#include

int main()

{

int a;
float b;
printf("enter the integer, a real number");
scanf("%d%f", &a, &b);
printf("entered integer=%d, entered real number=%f", a, b );
return 0;
}



The output of the given program is:
Output of the given program in c

As you can see in the above program different format strings are used for different data types:
  • integer uses %d
  • float(real) uses %f
  • character uses %c
  • strings uses %s

Use of ampersand(&):-

Also you can see the use of ampersand(&) before each variables in the scanf() function's code.It is the most important thing in the scanf() code.It describe the "address of the given variable" in the memory location being used.
Actually it is used to pass on the value given by the user of the variable to its memory location which is given by the "&" and then the value is stored in the memory location.
In short way we can say it is used to assign the value given by the user to the variable (here the variable are a,b).

Things to be noted while using the scanf function in C language:

These are some common things one must remember while using the scanf() function in the program:-
  • The first thing to be noted is that it is case-sensitive.Almost every component of the C program is case-sensitive.The function and its components should be written in small letters.
  • The second thing to be noted is that while specifying the format strings there should be no space in between them.They should be written in the same way as it is written in the program.
  • The third thing is how to give input while running the C program.For single variable we directly give the value when asked for and then press Enter key. But for more than one variable we can use either Enter key,Spacebar or Tab key to seperate the given input of different variables.
Here is the link to my channel on the youtube.You can go there for further reference.



Previous post




This post first appeared on Tutorials Hub, please read the originial post: here

Subscribe to Tutorials Hub

Get updates delivered right to your inbox!

Thank you for your subscription

×