Scanf in C

Scanf in C

The Input Data can entered into the memory from a standard input device(Keyboard). There is scanf() library function are use for entering input data in c. This Scanf() can take All data types of values like(Numerical, character, string) as input. The scanf() function read the data input data from console. The scanf() function can written as–

scanf("control string",address1,address2,......);

scanf function in c programming or reading input data in c, in This scanf() function should have at least two parameters. the first parameter is a control string which is know conversion specification character. It should be in double quotes. The conversion specification characters may one or more than one. It depend on user want to numbers of variable inputs and the others parameters are addressed of variables. In the scanf() function at least one address should inside. The address of the variable is represent by preceding the variable name and follow by an ampersand (&) sign. This sign is know as address operator and its gives the starting address of the variable in memory. Example–

int main()
{
int age=18;
scanf("%d",&age);
}

In this example. in the first parameter control string contains only one conversion specification character %d, which indicates that only one integer value should be enter as input

int main()
{
char grade='A';
scanf("%c",&grade);
}

In this example in the first parameter contain conversion specification character %c. which means the single character should be enter as input. This value stored in the grade variable. for example-

float salary=300.60;
scanf("%f",&salary);

In the above example first parameter control string contains the conversion specification character %f, which means that floating point any number input as user input this entered value will be stored in salary variable

char str[30]
scanf("%s",&str);

In the above example first parameter conversion specifier %s indicates the String should be input. and its stored in the str variable which is string type. we can use ampersand (&) or without ampersand (&) before the variable

More then one value we can stored using scanf() function there is some examples —

#include<stdio.h>
void main()
{

int salary,age;
scanf("%d%d",&salary,&age);
}

void main()
{
char grade='A',int salary=1000;
scanf("%c%d",&grade,&salary);
}

void main()
{
char grade='A',int salary=1000;
float average=89.67;
scanf("%c%d%f",&grade,&salary,&average);
}

//SCANF() function Example
#include<stdio.h>
int main()
{
int fnum;
printf("What is your Lucky number:");
scanf("%d ",&fnum);
printf(" %d is my Luck number,",fnum);
return (0);
}

What is your Lucky number: 6
6 is my Lucky number,