In C, when &
is absent for an argument in scanf()
, no compilation error is produced; instead, the displayed results are wrong (i.e. a semantic error occurs).
Consider the following code:
char str[30];
int a;
printf("Enter the value");
scanf("%s %d", str, a); // This is the statement in question.
printf("You entered %s %d", str, a);
Here I know str
is a character array so it will have a base address, and thus will not produce a compilation error. But why does the absence of &
for the argument a
not result in a compilation error?
Also str
gives correct output, but the integer is always producing the value -28770 as the output. Why is this?