This question may look stupid but that’s how people learn :).I learned C++ where you didn’t have to worry about referencing int variable in cin>> . Today I was learning filehandling in C for school work but When I tried to run the program with this syntax, I got error.
scanf("%d\n %s\n",id, name);
But when I referenced id in scanf, the error was gone.i-e;
scanf("%d\n %s\n",&id, name);
Why is this so? I will be very thankful for answering this noob question..
scanfis a function that expects a pointer to a variable so that it can store the value it reads into the location pointed to by the pointer.nameis presumably some sort of (char?) array, so the identifier for that variable already refers to an address, so the&operator is not necessary. It is however for theintvariable (I’m assumingidis some sort ofint) you need to get the address to pass toscanfin the call.