The size and type of the variable in which you store the input has nothing to do with scanf .
scanf only the address (pointer) is passed, where you need to enter the input that it receives from the user.
Smart compilers now warn you if the format string passed to scanf does not match the type of parameters, but in principle you can even declare name as an integer:
int name;
and it will contain an input line well enough, up to three characters (the fourth for the end of the line, i.e. zero) if the size of int is 32 bits, i.e. 4 bytes
The fact that it works is a complete failure, since the input stored in scanf goes beyond the end of the allocated buffer for it ( name ).
Note: highlighting only one character for a line will never work even for input lines of only one character. You always need to consider the EOS that is used to terminate them. Therefore, name must be declared as char name[2]; , at least.
source share