-4

I am making a simple program to add two numbers. I've done everything correct but somehow it's showing an error.

I am using TurboC for windows7 64bit (downloaded from filezilla)... I've also used devcpp but but there its showing error in using void main()... Why this is so? Why it is not working?

Also, can anybody suggest some good software for programming console-based for projects C, C++, etc.?

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int num1=0,num2=0;

//printing hello world
//printf("Hello World!");

printf("Enter number 1 : ");
scanf("%d",num1);
printf("Enter number 2 : ");
scanf("%d",num2);
int num3 = num1+num2;
printf("The sum of %d and %d is %d",num1,num2,num3);
getch();
}

enter image description here enter image description here

CinchBlue
  • 6,046
  • 1
  • 27
  • 58
Raunak Hajela
  • 103
  • 2
  • 10
  • 1
    You don't need a better software, you need to read a book about the C language. – axiac Mar 25 '15 at 11:53
  • 3
    Looks like it uses pretty old C standard and does not allow you to declare variables in the middle of a function. – Predelnik Mar 25 '15 at 11:53
  • I know C language and have also used Turbo C in my school and there it just works pretty fine. They have windows xp and earlier versions installed in there machines. It's just with the case of Windows7 – Raunak Hajela Mar 25 '15 at 12:12

3 Answers3

3

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

Community
  • 1
  • 1
Arun A S
  • 6,421
  • 4
  • 29
  • 43
  • thank you... you said conio.h is not supported in standard then how can I clear my screen? – Raunak Hajela Mar 25 '15 at 12:05
  • there is one more problem... while assigning num 3 in line 15 it's showing an error "declaration is not allowed here"... how to resolve that? – Raunak Hajela Mar 25 '15 at 12:07
  • @RaunakHajela , Move the `int num3` to the begining of the program, and use `num3 = num1 + num2 ;` – Arun A S Mar 25 '15 at 12:17
  • that I already did and it's working, I am just asking why it's not working like I did before? – Raunak Hajela Mar 25 '15 at 12:19
  • Thanks sir. Now, I am running same program in CodeBlocks and it's showing this error "Can't find compiler executable in your configured search path's for GNU GCC compiler". Now, what to do? – Raunak Hajela Mar 25 '15 at 12:28
  • @RaunakHajela , Take Settings -> Compiler -> Toolchain Executables and click Auto Detect. If it doesn't work that means you probably haven't installed a compiler, or you installed it in a different path. – Arun A S Mar 25 '15 at 12:31
  • It's not working. Will you help me how to install a compiler in my system? – Raunak Hajela Mar 25 '15 at 12:37
  • @RaunakHajela , For Windows 2000/XP/Vista/7 Try [this](http://sourceforge.net/projects/codeblocks/files/Binaries/13.12/Windows/codeblocks-13.12mingw-setup-TDM-GCC-481.exe/download) or [this](http://sourceforge.net/projects/codeblocks/files/Binaries/13.12/Windows/codeblocks-13.12mingw-setup.exe/download) . For any other OS, visit [this](http://www.codeblocks.org/downloads/26) and download the appropriate one and repeat the steps I gave you in the earlier comments. Make sure to download one with a compiler. – Arun A S Mar 25 '15 at 12:43
  • tried but not working.... getting same error "Can't find compiler executable in your configured search path's for GNU GCC compiler".. :-/ – Raunak Hajela Mar 25 '15 at 13:13
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/73764/discussion-between-arun-a-s-and-raunak-hajela). – Arun A S Mar 25 '15 at 13:14
  • @RaunakHajela , lets move this to chat. Come [Here](http://chat.stackoverflow.com/rooms/73764/discussion-between-arun-a-s-and-raunak-hajela) . Give me a reply over there when you reach there. – Arun A S Mar 25 '15 at 13:14
1

I'm only focusing on the error:

printf("Enter number 1 : ");
scanf("%d",&num1); //use & for input
printf("Enter number 2 : ");
scanf("%d",&num2); //use & for input
GorvGoyl
  • 42,508
  • 29
  • 229
  • 225
1

When you use scanf(), you must provide the address of the variable you write to using &.

scanf("%d",num1);

should become:

scanf("%d",&num1); //add the & to refer to the address
CinchBlue
  • 6,046
  • 1
  • 27
  • 58