Why do you require ampersand (&) in the scanf function. What will the output or type of error (compile or runtime) be in the following C code?
#include <stdio.h>
void main() {
int a;
printf("enter integer:");
scanf("%d", a);
}
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.
The
&in C is an operator that returns the address of the operand. Think of it this way, if you would simply givescanfthe variableawithout the&, it will be passed to it by-value, which meansscanfwill not be able to set its value for you to see. Passing it by-reference (using&actually passes a pointer toa) allowsscanfto set it so that the calling functions will see the change too.Regarding the specific error, you can’t really tell. The behavior is undefined. Sometimes, it might silently continue to run, without you knowing
scanfchanged some value somewhere in your program. Sometimes it will cause the program to crash immediately, like in this case:Compiling it shows this:
And executing shows a segmentation fault: