The problem is with your scanf()
. When accepting values, you must add the &
before the variable. The unary &
returns the address of the variable next to it, and scanf()
then stores the value at that address. But note that you do not need to use &
in printf()
unless you actually want to print the address. In short, change your scanf()
's to
scanf("%d",&num1);
and
scanf("%d",&num2);
Here's your working code code
#include<stdio.h>
int main()
{
int num1=0,num2=0;
//printing hello world
//printf("Hello World!");
printf("Enter number 1 : ");
scanf("%d",&num1); // see here
printf("Enter number 2 : ");
scanf("%d",&num2); // and here
int num3 = num1+num2;
printf("The sum of %d and %d is %d",num1,num2,num3);
}
The error with void main()
is that it is no longer accepted. On older versions like TurboC, you can use void main()
, but the standard clearly states that we should not use void for main()
, instead you should use int main()
.
Read this for reference
What should main() return in C and C++?
And, don't use <conio.h>
. It's not supported in the standard. If you want to clear the screen, add the header file <stdlib.h>
and use system("cls");
Regarding a replacement for getch()
, you can just use getchar()
. ( although in some programs, you will have to use two or more getchar()
's )
There's one thing you should know, and that is that both TurboC and DevC++ are outdated.
You should probably get Code Blocks.
You get the "Declaration not allowed here" error because prior to C99 ( your IDE TurboC runs on an older version than C99 ) , variables had to be declared at the beginning of a block. You can use Declaration not allowed here error in C as reference