I am trying to find the sum of two integers. I entered the following program into the Visual Studio and it is telling me that there are errors. Can someone tell me where they are please?

#include <stdio.h>
int main( void )
{
int integer1;
int integer2;
int sum;

printf( "Enter first integer\n" );
scanf( "&d", &integer1 );
printf( "Enter second integer\n" );
scanf( "%d", &integer2 );

sum = integer1 + integer2;
printf( "Sum is %d\n" , sum );

return 0;
}

6 answers

Compilers usually tell you where the errors are. Some part of the output or screen should be saying something about a line number, or if you click on the error message in an IDE, it really should bring you to the line.

The only error I can see looks like a typo, where you typed an ampersand instead of a percent:

scanf( "&d", &integer1 );

should read

scanf( "%d", &integer1 );
Thanks! But even after I changed the ampersand, it still gives me an error. It says

error C2065: integer1 undeclared identifier
error C2065: integer2 undeclared identifier

and refers to the lines where there is the scanf of both integers.
OK. That would imply that the compiler isn't seeing the lines

int integer1;
int integer2;

or, more precisely, isn't seeing them as declaring local variables at that point.

Is it possible you've changed something unintentionally in trying to find a fix?

I just pasted the code above - your original code - into an old version of VC++ I have on my machine, fixed the percent in scanf, and the program compiles are runs perfectly.

Try doing exactly what I did, and see whether it works now.
It still won't work but thanks a lot!!
I don't know what happened but it worked now when I pasted the code that you used! Thanks a lot! You were really helpful :)
Great! I can' see the code I pasted a minute ago now. Dunno how that happened.

Anyway, get used to it! :-) One of the peculiarities of programming is that sometimes things mysteriously don't work, and then mysteriously do when you look at them again.

Actually, there's no mystery.

What happens is that, in looking at the same chunk of code again and again, you just stop seeing something wrong or obvious. Your eye sees it, but your brain ignores it.

The best remedy is to have somebody else take a look at the same code. It will likely be obvious to them. The next best is to type the offending line or lines in again, so that you won't ignore characters.

The effect never goes away, but you get better at handling it with practice.

Glad I could help!