#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int a;
char c[1];
printf("\n%d\n", a);
gets(c);
printf("\n%d\n", a);
return 0;
}
When reading c
using gets
the value of a
, printed previously, is printed as having the value: 0
; when using scanf("%c", &c);
as a replacement for gets(c);
the value of a
stays the same throughout the code.
I can't seem to figure out why this is, could someone please explain how this is possible?
Just to get this out of the way, NEVER NEVER NEVER NEVER use
gets
: it will introduce a point of failure in your program. It was deprecated in the C99 standard and has been removed from the C2011 standard. It is evil.Having said all that, let's walk through all the issues in your code.
Remember that in C a string is a sequence of characters terminated by a 0-valued byte. This means that to store an N-character string, you must set aside N+1
char
elements of storage. Yourc
array is sized to hold 1 element, meaning the only string it can store is the empty string.The problem with
gets
is that it doesn't know how big the target buffer is; when you pass an array as a parameter, all the called function receives is a pointer to the first element. If you type in 10 non-whitespace characters, or 100, or 1000,gets
will happily store the excess to the memory immediately following your array; this is why youra
variable gets overwritten. This is also whygets
has finally been removed from the language standard; that behavior enabled a lot of malware exploits.The reason your
scanf
call didn't cause a problem is because you used the%c
conversion specifier, which only reads a single character from the input stream. Had you used the%s
conversion specifier, you would have seen the same result as you did with thegets
call.So, you need to do two things: first, you need to declare
c
to be large enough to hold the largest string you expect plus 1 for the 0 terminator:then you need to use
fgets
to read the input: