Read values in C- scanf() function in C
scanf() function() is used to read different types of values in C.
Syntax: scanf(“Control-string”, &arg1,&arg2,&arg3,……);
Control-string is the format to enter data. It has % sign followed by format specifier indicating the type of variable whose value is to be input .
&arg1,&arg2,&arg3,… refer to variables whose values is to be entered by user.
1. Input int data
Format Specifier: %wd
% is the conversion character.
w is an integer value which specifies the width of numeric value to be read.
d is the data type specifier which specifies that an int type variable is to be read.
/*Program to input int data*/ |
#include<stdio.h> int main() { int n1; printf("Enter int value="); scanf("%d",&n1); printf("\n Value=%d",n1); return(0); } |
Output
|
2. Input short data
Format Specifier: %whd
% is the conversion character.
w is an integer value which specifies the width of numeric value to be read.
hdis the data type specifier which specifies that a short type variable is to be read.
Program to input short data |
#include<stdio.h> int main() { short m; printf("Enter short value="); scanf("%hd",&m); printf("\nValue=%hd",m); return(0); } |
Output |
Enter short value=25 Value=25 |
3. Input long data
Format Specifier: %wld
% is the conversion character.
w is an integer value which specifies the width of numeric value to be read.
ld is the data type specifier which specifies that a short type variable is to be read.
Program to input long data |
#include<stdio.h> int main() { long k; printf("Enter long value="); scanf("%ld",&k); printf("\nValue=%ld",k); return(0); } |
Output |
Enter a long value=1560 Value=1560 |
4. Input float data
Format Specifier: %f
% is the conversion character
f is the data type specifier which specifies that a float type variable is to be read. There is no need to specify any field width.
Program to input float data |
#include<stdio.h> int main() { float no; printf("Enter float value ="); scanf("%f",&no); printf("\nValue=%f",no); return(0); } |
Output |
Enter float value=2.55 Value=2.55 |
5. Input double/long double type data
Format Specifier: %lf
% is the conversion character
lf is the data type specifier which specifies that a double or long double type variable is to be read. There is no need to specify any field width.
Program to input double/long double type data |
#include<stdio.h> int main() { long double n1; printf("Enter long double value="); scanf("%lf",&n1); printf("Value=%lf",n1); return(0); } |
Output |
6. Input char data
Format Specifier: %c
% is the conversion character
c is the data type specifier which specifies that a char type variable is to be read.
Program to input char type data |
#include<stdio.h> int main() { char choice; printf("\nEnter your choice(Y/N)"); scanf("%c",&choice); printf("\nchoice=%c",choice); return(0); } |
Output |
Enter your choice(Y/N)Y
choice=Y
|
7. Input string data
Format Specifier: %ws
% is the conversion character
w is an integer value which specifies the number of characters which can be stored in string variable.
s is the data type specifier which specifies that a string type variable is to be read. we don’t need to use ampersand sign(&) before the string variable that we want to read
Program to input string data |
#include<stdio.h> int main() { char sname[20]; printf("\nEnter name="); scanf("%s",sname); printf("\nsname=%s",sname); return(0); } |
Output |
Enter name=Aman sname=Aman |
Best Books of C