Skip characters while reading integers using scanf() in C
Let suppose, we want to read time in HH:MM:SS format and store in the variables hours, minutes and seconds, in that case we need to skip columns (:) from the input values.
There are two ways to skip characters:
- Skip any character using %*c in scanf
- And, by specifying the characters to be skipped
1) Skip any character using %*c in scanf
%*c skips a character from the input. For example, We used %d%*c%d and the Input is 10:20 – : will be skipped, it will also skip any character.
Example:
Program:
Output
2) By specifying the characters to be skipped
We can specify the character that are going to be used in the input, for example input is 12:12:10 then the character : can be specified within the scanf() like, %d:%d:%d.
Example:
Program:
Output
need an explanation for this answer? contact us directly to get an explanation for this answer