Why does scanf give a max value in case of “int” but crash the program in case of “char” when the limit is exceeded?
#include<stdio.h>
main(){
int a;
char ch[10];
scanf("%d",&a);
printf("%d",a);
scanf("%s",ch);
printf("%s",ch);
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It crashes your program in this case because
scanf()has aninherentlyunsafe interface for strings. It has no way of knowing that your parameterchis an array large enough to hold a 9-character string plus its terminating nul. As a result, it is perfectly happy to keep reading characters fromstdinand storing them in memory well past the end of the array.If your program crashes, you are actually lucky. In the worst case, the attacker has used a carefully crafted string to manipulate the content of the stack in such a way that he has gained control of your computer. This is an example of a buffer overflow attack. It sounds unlikely, but it has been documented to occur on a large number of occasions.
Used for only numbers,
scanfis generally safe enough, but it is not very good at handling errors in the input. As a result, it is usually a good idea to use something likefgets()to read the input (it has a buffer length parameter to control overflow) andsscanf()to parse from that buffer, testing its return values for sanity as you go.Edit: As the comment from R points out, I overstated the dangers inherent to the
scanfinterface. With care to correctly use the field width specifier on all strings,scanfbecomes safer. But then you take responsibility for guaranteeing that the specified width does fit within the buffer. For the example, you should writescanf("%9s",ch);because your buffer was declared to be ten bytes long and you need room for the terminating nul.Note that you should also be testing the return value from
scanf(). It returns the number of fields it successfully matched, orEOFif an I/O error occurred. It might return 0 if the user entered “abc” when it expected a number, for instance.