K - Troubleshooting C Execution on Windows XP

  • Thread starter Thread starter Dr.Brain
  • Start date Start date
AI Thread Summary
The user is learning C programming independently and has written a simple program that adds two integers and prints the result. The code is correct, but the user is confused about how to compile and run the program. Instead of saving the file as an executable, the user needs to compile it using the C compiler, which will generate the .exe file. To run the program, the user should open a command prompt in Windows XP, navigate to the directory containing the executable, and execute the program from there. The user also inquired about the necessity of spaces in the printf statement; it was clarified that spaces do not affect the output. To keep the command prompt window open longer, the user can add input prompts in the code, such as using scanf or a key press function. Additionally, including parameters in the main function is recommended for better practice, even if not strictly necessary for simple programs.
Dr.Brain
Messages
537
Reaction score
2
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
 
Computer science news on Phys.org
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
 
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
 
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 ?
 
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
 
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();" .
 
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.
 
Back
Top