Discussion: Deciphering scanf()
Execute code to understand the output and gain insights into input pattern matching.
We'll cover the following...
Run the code
Now, it's time to execute the code and observe the output.
#include <stdio.h> int main() { char buffer[32]; printf("Type something: "); scanf("%[ABC]", buffer); printf("You typed: %s\n", buffer); return(0); }
C code for the given puzzle
Understanding the output
The program prompts for input, but only uppercase letters ABC are allowed:
Type something: ALPHAYou typed: A
Code output
Note: Any character input other than ABC returns garbage. The word should also start with the characters given in the string (which in our case is "ABC").
The scanf()
function is a formatted input function, so it’s not well suited for reading text input, though it’s often used this way in introductory programming books and ...