Discussing different formats of scanf()

scanf() is basically a function declared in stdio.h and it takes input from standard input stream i.e keyboard and places it in the desired location.

scanf() has two parts :

????1. Format string??2. Arguments

?Format string specifies the type of data you want to store in a particular variable. Data arguments are generally pointers with references/addresses.

?It is because we want to store value in our named memory location i.e variables. That is why we use &(ampersand) before argument. But this is not the case

?which is achieved in case of strings.


?????????????????For example : scanf("%d", &a);????//will read integers(normal)

???????????????????????????????scanf("%hd", &a);???//will read integers(short int)

???????????????????????????????scanf("%ld", &a);???//will read long int

???????????????????????????????scanf("%f", &a);????//will read float

???????????????????????????????scanf("%c", &a);????//will read single character

???????????????????????????????scanf("%x", &a);????//will read hexadecimal (0x..)

???????????????????????????????scanf("%X", &a);????//will read hexadecimal (0X..)

???????????????????????????????scanf("%o", &a);????//will read octal (0..)

???????????????????????????????scanf("%[]", &a);???//will read scanset from defined set

???????????????????????????????scanf("%*d", &a);???//will skip reading

???????????????????????????????scanf("%[^\n]s, &a); //will scan everything except \n

???????????????????????????? scanf("%n", &a);????//will assign the no of successful inputs to (a)

For the above examples we write (&) beacuse they are single locations of memory/ memory of one variable

?If I declare an array of character or strings then the format to read input is :

??????????????????????????char s[100];

scanf("%s", s); //no & because we know name of array is a constant pointer to the first //element of array

LIMITATIONS :

?While reading a string or sentence as input scanf() stops reading when it encounters a white space character or typically an escape sequence.

No alt text provided for this image

Output of the above program :

No alt text provided for this image

Clearly it can be seen from above program that scanf() stops reading when it encounters white space.

To avoid above problem we use some versions of formatted scanf() and other functions in stdio.h

?1. scanf("%[^\n]s", str);

?2. scanf("%[^\n]%*c", str);

?3. use of gets() and fgets()

?1. use of scanf("%[^\n]s", str)...see the below program;

No alt text provided for this image

Output of the above program :

No alt text provided for this image

What it exactly does is it excludes the whitespace character from the scanset character and allows the user to input till '\n' is not inputted.

2. use of scanf("%[^\n]%*c", str)...see the below program;

No alt text provided for this image

Output :

No alt text provided for this image

It is same as previous one but it also excludes the '\n' and prints the string with white-spaces but this will not work because the last statement will insert a line feed i.e '\n', from the previous line. So writing scanf("\n"); before the last statement ensures no error;

3. Above two formatted inputs of scanf() are not so good practice of using and we can replace it by gets()/fgets(). ?fgets()/gets() are declared also in stdio.h and it also take user input untill '\n' is entered.

4. fgets() works same as gets() but it is used in File handling and to read from file.

See the below program :

No alt text provided for this image
No alt text provided for this image

Hope it helps !

要查看或添加评论,请登录

Apurba Sundar Nayak的更多文章

  • Day4 of #daily_code

    Day4 of #daily_code

    Not so much time to code today. Despite that I want a few things to be shared.

  • Day3 of #daily_code

    Day3 of #daily_code

    Learnt a bunch of things today. ----------------------------------------------------- 1.

  • Day2 of #daily_code

    Day2 of #daily_code

    Learnt a bunch of things today…

  • Day1 of #daily_code

    Day1 of #daily_code

    Learnt Shallow Copy and Deep Copy concept in C++ ------------------------------------------------ Generally if we want…

社区洞察

其他会员也浏览了