DEV Community

Cover image for Skip any value in scanf() function in C programming language
Md. Fahim Bin Amin
Md. Fahim Bin Amin

Posted on

Skip any value in scanf() function in C programming language

What we want to do?

Suppose, we will take three integer values from the user, but we want to ignore the second integer value even though we are taking input value for that. What we can do? The first thing that come to our mind is that, we can simply use two format specifiers for that (%d%d) so that we don't have to take the middle value at all. The thing is, we need to take three integer values from the user and skip the middle integer value (also, we need to stop that value from assigning into any variable). How can we do that?

If we want to skip any data from assigning into variables in the scanf() function in C programming language, then we can use the asterisk (*) symbol right after the percent (%) symbol in the scanf() function. So, we can do that by simply adding an asterisk * after the %.

scanf("%d %*d %d", &a, &b);
Enter fullscreen mode Exit fullscreen mode

Skip value in scanf fucntion

It will receive the three integer values as input, but it will not assign the second integer value into any variable.

How does asterisk (*) work here?

An asterisk (*) right after the percent symbol (%) means that the datum read by the format specifier will not get stored in any variable. In short, asterisk (*) means that the value will be read, but it will not be written into variable. Also, scanf() does not expect variable pointer in its parameter list for this value.

Summary

In short, the * basically means the specifier is getting ignored (That means that the integer is getting read, but it is not getting assigned).

I hope you enjoyed this read!

For technology related some cool technique, specially if you are interested in Java Object Oriented Programming based Bengali content, then you can visit my YouTube channel.

If you want to get yourself updated with many cool project related stuff, then you can 👨‍💻follow me on GitHub, LinkedIn, Twitter.

Top comments (0)