C programming scanf stdio.h


In C programming the <stdio.h> scanf function allows inputting of data from the input stream.Simply put this function allows programmer to read data from the user using his keyboard or any input stream like file.The declaration of the function is given below.

int scanf(const char * restrict format , …);

Parameters:
format -This argument will consist of the specifier.

-A lists of variables to hold the data entered by the programmer.

Return type
int -Returns the value of the macro EOF if an input failure occurs before any conversion.Otherwise, the scanf function returns the number of input items assigned, which can be fewer than provided for,or even zero,in the event of an early matching failure.

**Note the scanf function is equivalent to fscanf with ‘stdint‘ interposed before the arguments to scanf.

Link : C fscanf stdio.h

All the conversion specifier rules apply while using ‘scanf’.A code example is given below.

Code example

char name[20] ;
int age , ret ;

printf(“Enter your name and age\n”);

ret=scanf(“%s %d”, name , &age);

printf(“\n\nret=%d”, ret);
printf(“\nName=%s \nAge=%d”, name , age);

Output,

Enter your name and age
Meme 123

ret=2
Name=Meme
Age=123

The first specifier ‘%s’ is meant for the string ‘name’ and the second specifier is meant for the variable ‘age’ and If you run the program you are asked to enter the name and age.These name and age are stored in the variable ‘name’ and ‘age’,in this way you retain the data entered by the user using ‘scanf’.




Point to note

Most of the conversion specifier defined for scanf is same as printf,however,conversion specifications in the scanf( ) functions have no flags and no precision options.The general syntax of conversion specifications for the scanf( ) functions is as follows:

%[*][field_width][length_modifier]specifier

When is ‘*‘ used and what does ‘field width‘ means and ‘length modifier‘ means is given below.

Field width use

One thing you must know while using ‘scanf’ is if the data inputted is string then only the string before the white space is read.

Code example

char st[10];

scanf(“%s” , st);
printf(“st=%s” , st);

Running the program,

My name is
st=My

Note here only the string “My” is stored in the ‘st’ variable.If you want to read the whole string then you must use the function ‘fgets’.But if we use field width specifier we can specify the number of characters to be read.

Code example

char String[30];

printf(“Enter any string”);
if(scanf(“%20s” , String) <1) //scanf read only 20 characters
{
fprintf(stderr , “Reading data from standard input failed..”);
}

printf(“Your string:%s” , String);

Suppose if you run the program,

Enter any string
AnAllocattorClassAclassThatAllocatesMemory

Your string=AnAllocattorClassAcl

If you count the number of characters contain in the String variable it will turn out to be 20.

Using ‘*’ character

In the third code example we will see how to discard some characters.Consider the example given below.

Code example

char c;

scanf(“%c” , &c);
printf(“c=%c” , c);

Running the program,

string
c=s

Inputting the string “string” assigns the first character ‘s’ to c.Now let’s try to discard the first two characters “st” and assign ‘r’ to ‘c’.To do this the syntax given below will be used


%*[the number of character to be discarded][specifier]

Code example

char c;

scanf(“%*2c”); //this call discard the first two characters
scanf(“%c” , &c);
printf(“c=%c” , c);

Running the program

string
c=r

Note in the first scanf call no variable is provided as second argument.

Using length modifier

The next code example shows to read all characters including white space up to punctuation mark.

Code example

char str[30] , c;

scanf(“%511[^.!?]%c”, st , &c);

printf(“\nst=%s \nc=%c”, st , c);

Running the program

Corecplusplustutorial rocks!
st=Corecplusplustutorial rocks
c=!

Now even the white space is read and the ‘!’ character is stored in ‘c’ variable.Note here you can use any character besides punctuation marks to ‘direct’ scanf to read character up to the specified point.


Related links

->C printf stdio.h

->C vscanf stdio.h

->C sscanf stdio.h