C - execution trouble

  • Thread starter Dr.Brain
  • Start date
  • #1
538
2

Main Question or Discussion Point

I am a newbie to C and i am learning it all by myself without any teacher.

Ok i wrote this programme in my notepad and saved it as dd.c on my desktop:


#include <stdio.h>

int main()
{
int a, b, c;
a = 5;
b = 7;
c = a + b;
printf("%d + %d = %d\n", a, b, c);
return 0;
}


Please tell me if there is a flaw in my coding . I downloaded a free C-compiler , and wanted to convert my .c to .exe , executable file .
So what I did was that I opened my .c file with this compiler and then saved it agains as dd.exe on my desktop , but when i opened the .exe by clicking on it , it displays a series of programmes and tells me to choose the most apt programme out of all those.So how do i run my programme. Through my code, I want that the output on my screen displays 5+7=12 . Should I go to DOS for execution or what????? and how do I reach DOS from XP? :blushing:


BJ
 

Answers and Replies

  • #2
538
2
and ye sone more thing , is the space necessary between %d and + i, and betweeen a.b and c in the following code:

printf("%d + %d = %d\n", a, b, c);


??

BJ
 
  • #3
508
1
I do not see any error in your code.

You should not simply save the file as .exe, but you have to instruct your compiler to compile the file that you wrote in notepad (you can save your file with any extension that you like, but you will have to compile the file so that it will become an executable before you get something that you can execute).

You should do something like: open the file in your "compiler" and choose compile. If you say what compiler you use people can give more appropriate advise.

Dr.Brain said:
and how do I reach DOS from XP?
start menu --> run --> cmd<enter>

______________
printf("%d + %d = %d\n", a, b, c);
would generate the same as
printf("%d + %d = %d\n",a,b,c);
both give:
5 + 7 = 12

printf("%d+%d=%d\n",a,b,c);
would give
5+7=12
 
  • #4
538
2
I use IccWin32 freeware C-compiler.

Ok i compiled the .c file using my compiler , AFter the process completed , it created some files in the same folder as that of .c file . I found the .exe file and clicked on it , it shows a MSDOS window which disappears in less than half a second :(

where is 5+7=12 ?
 
  • #5
538
2
Ok i just managed to know that the MSDOS windows shows 5 + 7 =12 for a fraction of a second . Is there a command to increase the viewing time of this DOS window.

BJ
 
  • #6
robphy
Science Advisor
Homework Helper
Insights Author
Gold Member
5,577
810
You have to open a command shell: Start->Run->cmd [do not use: the more limited command.com], navigate to that directory: cd "somedirectory" [you may have to use quotes around the directory name], then execute that program by typing in its name.

If you want to avoid the command line interface, you can also add a few lines that ask for input.

For instance, "scanf("%d", &a);", which waits for an integer to be assigned to a. See http://computer.howstuffworks.com/c7.htm .

You could also use the declaration "char reply;" , later followed by what amounts to a HIT A KEY TO CONTINUE: "reply = getch();" .
 
  • #7
1,356
2
spaces don't matter unless its between to variable names
scanf(); kbhit(); getch(); getche(); some time command delay(i think its actually delay())

if your runninga console programm its usually stanard to include

int main(int argc,char**argv)....though you don'treally need it for such simpe programme its a good habit to remember it though because some APIs require you to use it.
 

Related Threads on C - execution trouble

  • Last Post
Replies
3
Views
1K
Replies
2
Views
1K
  • Last Post
Replies
2
Views
2K
  • Last Post
Replies
2
Views
2K
Replies
12
Views
834
  • Last Post
Replies
1
Views
2K
  • Last Post
Replies
15
Views
6K
  • Last Post
Replies
1
Views
3K
  • Last Post
Replies
11
Views
2K
  • Last Post
Replies
1
Views
2K
Top