Read values in C- scanf() function

Preview

Read values in C- Formatted Input

Formatted input means the input which is arranged in a specific format. For formatted input, scanf()

function() is used. The syntax of scanf function() is:

scanf(“Control string”, arg1,arg2……);

Control string specifies the format in which data is to be entered by the user while inputting.

Control string consists of conversion character % followed by the format specifier which indicates the type of variable whose value is to be entered from the keyboard.

There are different format specifiers for different data types.

arg1,arg2,….. refer to the addresses of memory locations or variables in which data is to be stored. They are known as arguments. The arguments must be prefixed with ampersand (&) sign except string variables.

There must be a comma between control string and the argument list.


1. Inputting int type data

Data Type: int                                                           

Format Specifier: %wd                                              

Description:   

% 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.

Example1Example2
int a;
scanf(“%d”,&a);
int a,b;
scanf(“%3d%2d”,&a,&b);

 

 Program to read int type variable
#include<stdio.h>
int main()
{
int a;
printf(“Enter an int value=”);
scanf(“%d”,&a);
printf(“\n Value=%d”,a);
return(0);
}
Output
Enter an int value=15
Value=15
 

2. Inputting unsigned int type data

Data type: unsigned int      

Format Specifier: %wu                                                          

Description:

w is an integer value which specifies the width of numeric value to be read.

u is the data type specifier which specifies that an unsigned int type variable is to be read.  

Example

unsigned int a;
scanf(“%u”,&a);

Program to read unsigned int type variable
#include<stdio.h>
#include<conio.h>
main()
{
unsigned int a;
clrscr();
printf(“\nEnter an unsigned value =”);
scanf(“%u”,&a);
printf(“\nValue=%u”,a);
getch();
}
Output
Enter an unsigned value=65535
Value=65535



 3. Inputting short type data

Data type: short     

Format Specifier: %whd                                                

Description:           
w is an integer value which specifies the width of numeric value to be read.

hd is the data type specifier which specifies that a short type variable is to be read.
 

Example

short a;
scanf(“%hd”,&a);

Program to read short type variable
#include<stdio.h>
int main()
{
short a;
printf(“Enter a short value=”);
scanf(“%hd”,&a);
printf(“\nValue=%hd”,a);
return(0);
}
Output
Enter a short value=15
Value=15



 4. Inputting unsigned short type data

Data type: short

Format Specifier: %whu

Description:
w is an integer value which specifies the width of numeric value to be read.
hu is the data type specifier which specifies that an unsigned short type variable is to be read.         

Example

unsigned short a;
scanf(“%hu”,&a);

Program to read unsigned short type variable
#include<stdio.h>
int main()
{
unsigned short a;
printf(“\nEnter an unsigned short value=”);
scanf(“%hu”,&a);
printf(“\nValue=%hu”,a);
return(0);
}
Output
Enter an unsigned short value=65
Value=65

5. Inputting long type data

Data type: long      

Format Specifier: %wld                                                 

Description:
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 long type variable is to be read.   

Example

long a;
scanf(“%ld”,&a);

Program to read long type variable.
#include<stdio.h>
int main()
{
long a;
printf(“Enter a long value=”);
scanf(“%ld”,&a);
printf(“\nValue=%ld”,a);
return(0);
}
Output
Enter a long value=15
Value=15
 



 6. Inputting unsigned long type data

Data type: unsigned long                                                

Format Specifier: %wlu                                                  

Description:
w is an integer value which specifies the width of numeric value to be read.
lu is the data type specifier which specifies that an unsigned long type variable is to be read.                

Example

unsigned long a;
scanf(“%lu”,&a);

Program to read unsigned long type variable
#include<stdio.h>
main()
{
unsigned long a;
printf(“Enter an unsigned long value=”);
scanf(“%lu”,&a);
printf(“\nValue=%lu”,a);
return(0);
}
Output
Enter an unsigned long value =65
Value=65
 



 
7. Inputting float type data

Data type: float

Format Specifier: %f                                                      

Description:
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.      

Example

float a;
scanf(“%f”,&a);     
                               

Program to read float type variable
#include<stdio.h>
int main()
{
float a;
printf(“Enter a float value =”);
scanf(“%f”,&a);
printf(“\nValue=%f”,a);
return(0);
}
Output
Enter a float value=15.544444
Value=15.544444



8. Inputting double type data

Data type: double  

Format Specifier: %f, %g, %e                                      

Description:
f, g and e are the data type specifiers which specify that a double type variable is to be read. We should use %e as format specifier if we want to read the value in exponent form. There is no need to specify any field width.    

Example

double a;
scanf(“%f”,&a);                            

Program to read double type variable
#include<stdio.h>
int main()
{
float a;
printf(“Enter a double value =”);
scanf(“%f”,&a);
printf(“\nValue=%f”,a);
return(0);
}
Output
Enter a double value=15.544444
Value=15.544444
 

9. Inputting long double type data

Data type: long double                                                   

Format Specifier: %lf (small L followed by f)              

Description:
lf  is the data type specifier which specifies that a long double type variable is to be read. There is no need to specify any field width.           

Example

long double a;
scanf(“%Lf”,&a);    

Program to read long double type variable
#include<stdio.h>
int main()
{
long double n;
printf(“Enter value of long double type=”);
scanf(“%Lf”,&n);
printf(“Value=%Lf”,n);
return(0);
}
Output
Enter value of long double type==15.544444
Value=15.544444
 



10. Inputting char type data

Character data type is basically used to input values in two ways. First way is to input data for a character variable which can hold only one character. Other way is to input data for a string variable which can hold more than one character

Data type: char      

Format Specifier: %c                                                      

Description:
c is the data type specifier which specifies that a char type variable is to be read.   

Example

char a;
scanf(“%c”,&a);

Program to read char type variable
#include<stdio.h>
int main()
{
char ch;
printf(“\nEnter any character”);
scanf(“%c”,&ch);
printf(“\nch=%c”,ch);
return(0);
}
Output
Enter any characterR
ch=R



 11. Inputting string Value

Data type: char[]    

Format Specifier: %s                                                      

Description:
s is the data type specifier which specifies that a string type variable is to be read. In this format we need not to write ampersand sign(&) before the string variable name which we want to read.  

Example

char a[20];
scanf(“%s”,a);

Program to read string type variable
#include<stdio.h>
void main()
{
char name[20]; //String variable name
printf(“\nEnter your name=”);
scanf(“%s”,name);
printf(“\nName=%s”,name);
return(0);
}
Output
Enter your name=Lovejot
Name=Lovejot

Best Books of C



Lesson tags: formatted input function in c, formatted input in c, get input in c, input values in c, input values using scanf, read values in c, read variables in c, scanf function
Back to: C Programming Language