1

I'm programming in C and I download code block IDE because it's easier to use. As you know, the minimal code in C is to write: Hello World in a CMD window. When I try to launch the program via code block, it works but when I open the .exe file directly, it open and close quickly. Can someone explain me why?

#include <stdio.h> 

int main() { 
    int age = 0; 
    printf("how old are you?");
    scanf("%d", &age); 
    printf("You are %d", age); 
    getchar(); 
    return 0; 
}
Joseph Farah
  • 2,463
  • 2
  • 25
  • 36
Scoobydoo
  • 37
  • 5

3 Answers3

1

What I think you're describing is the OS destroying the temporary command window when the program is done executing. Try opening a command window yourself, and then running your .exe from there. Alternatively, use int t; scanf("%d", &t) (or something) to keep your program from finishing, thus holding the window open

VoidStar
  • 5,241
  • 1
  • 31
  • 45
1

I am guessing your program looks something like this:

#include <stdio.h>

int main()
{
    printf("Hello, world!\n");
}

The program prints Hello, world! to the screen and then ends because it has nothing left to do.

A simple fix to this is to add the function getchar() after the printf statement. This will cause the program to wait for any user input before closing. It stands for get character.

Your new program should look like this:

#include <stdio.h>

int main()
{
    printf("Hello, world!\n");
    getchar();
}

UPDATE:

#include <stdio.h> 

int main() { 
    int age = 0; 
    printf("how old are you?\n");
    scanf("%d", &age); 
    getchar();
    printf("You are %d.\n", age); 
    getchar(); 

}
Joseph Farah
  • 2,463
  • 2
  • 25
  • 36
  • But when I have a code like this: #include int main() { int age = 0; printf("how old are you?"); scanf("%d", &age); printf("You are %d", age); getchar(); return 0; } it doesn't work. Can you tell me what to do with this kind of code. – Scoobydoo Sep 27 '15 at 01:22
  • I'm sorry but I'm beginner with stack overflow so I don't really know how to organize my code – Scoobydoo Sep 27 '15 at 01:29
  • @Scoobydoo don't worry about it! I am going to add your code to the question so it can be more accessible. – Joseph Farah Sep 27 '15 at 01:32
  • @Scoobydoo try removing the `return 0`; Does that work? – Joseph Farah Sep 27 '15 at 01:34
  • @Scoobydoo I figured out the problem. Add a `\n` after all of your `printf` statements, and add a `getchar()` after the `scanf` AND at the end of your function. I will update my answer to include this new code. – Joseph Farah Sep 27 '15 at 01:41
  • @Scoobydoo let me know if this fixes the problem. – Joseph Farah Sep 27 '15 at 01:42
  • Yeah it worked but can you explain me why it worked now after adding what you told me – Scoobydoo Sep 27 '15 at 01:46
  • @Scoobydoo absolutely. When you hit `enter` after the `scanf` call (entering your age) that counted as an extra character. As a result, that extra character was funneled off to the `getchar()`. Because `getcahr()` was satisfied your program did not wait for further user input. – Joseph Farah Sep 27 '15 at 01:48
  • @Scoobydoo so the first time we only had 1 `getchar()`. The extra character (created when you hit enter after typing your age) went there. By giving the `enter` a place (siphoning it off to the *first* `getchar()`) we allowed the *second* `getchar()` at the end of the program to do its job and wait for further user input. – Joseph Farah Sep 27 '15 at 01:53
  • @Scoobydoo this resource may help http://stackoverflow.com/questions/1391548/why-doesnt-getchar-wait-for-me-to-press-enter – Joseph Farah Sep 27 '15 at 01:54
  • But why did I have to add a `\n` to all my `printf` – Scoobydoo Sep 27 '15 at 01:57
  • @Scoobydoo it isn't necessary--it just makes it clear where the `getchar()` is functioning. Sorry, i should have been more clear. – Joseph Farah Sep 27 '15 at 01:58
  • Yeah me too I noticed that when I remove it,it also works – Scoobydoo Sep 27 '15 at 01:59
  • @Scoobydoo anytime man. Happy coding!! and don't forget to mark this as correct, the explanation in this discussion will be helpful to other programmers that had the same problem as you. – Joseph Farah Sep 27 '15 at 02:00
  • @no problem. best of luck! – Joseph Farah Sep 27 '15 at 02:01
  • Thank you but how do we make the discussion as correct? Sorry, I'm a beginner so I don't really know – Scoobydoo Sep 27 '15 at 02:03
  • @Scoobydoo underneath the voting symbols (where you up- or down-vote a post), there is a large, gray check mark. Click that and it marks the question as correct. – Joseph Farah Sep 27 '15 at 02:04
  • @Scoobydoo don't worry about it :D – Joseph Farah Sep 27 '15 at 02:08
0

Put a getchar() as the last line of main:

int main()
{
    // code
    getchar();

    return 0;
}
lost_in_the_source
  • 10,998
  • 9
  • 46
  • 75