Passing characters to a scanf with a %d
specifier resulted in random numbers all around 32,760. I am using the GCC available with Ubuntu 16.04.
My guess is, obviously, that this is unspecified behavior. But any idea as to what's going on under the hood?
Here is the rather obvious code for aforementioned objective:
#include <stdio.h>
void main() {
int n;
scanf("%d", &n);
printf("\n%d", n);
}
The input supplied was repeatedly the letter 'a'
followed by pressing an Enter to terminate terminal talk. The results for the first three runs were : 32765, 32764, 32767.
In response to some helpful suggestions, I've thus figured that scanf doesn't initialize n. But it does something. Consider this code:
#include<stdio.h>
int main() {
int i;
for(i=0; i<10; i++)
{
int n;
printf("%d", n);
}
}
This gives me a row of ten zeroes for the output. I'm flummoxed. The question probably isn't interesting, I get it. But I just can't not stop wondering why. I still can't rule out the effect of scanf because of this.