Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C - execution trouble

  1. Jul 19, 2005 #1
    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
     
  2. jcsd
  3. Jul 19, 2005 #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
     
  4. Jul 19, 2005 #3
    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.

    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
     
  5. Jul 19, 2005 #4
    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 ?
     
  6. Jul 19, 2005 #5
    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
     
  7. Jul 19, 2005 #6

    robphy

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

    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();" .
     
  8. Jul 19, 2005 #7
    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: C - execution trouble
  1. Trouble with computer (Replies: 2)

  2. Laptop Troubles (Replies: 6)

  3. Wireless troubles (Replies: 14)

  4. Overclocking Troubles. (Replies: 11)

  5. DSL trouble (Replies: 1)

Loading...