Help with C "Hello World" - How to Stop It Closing?

  • Thread starter Thread starter madmike159
  • Start date Start date
  • Tags Tags
    Hello Hello world
Click For Summary
The discussion centers around a beginner's experience with C programming, specifically the issue of a console window closing immediately after executing a program that prints "Hello World." The user seeks a solution to prevent the window from closing upon pressing Enter, especially when using input functions like scanf(). Suggestions include using getchar() to capture key presses and implementing a loop to wait for a specific key before closing. Another recommendation is to run the program from a command-line interface rather than an IDE, which would keep the window open after execution. Overall, the focus is on managing console behavior in C programs to enhance user experience.
madmike159
Gold Member
Messages
369
Reaction score
0
I started learning C not long ago. Like most people I started with "Hello World". I relised that it closes itself unless you tell it not to.

I have this code

#include <stdio.h>

int main(char *argv[])
{
printf("Hello World!\n");
getchar();
return 0;
}

It makes it close when you press enter. The problem is when you do read input commands like scanf() it closes when you press enter. Does anyone know how I can change this to so it closes when you press something else (like esc, or Ctrl+enter)?
 
Technology news on Phys.org
Try using cin.getline(someString, 256) for input.
 
Contrapositive said:
Try using cin.getline(someString, 256) for input.

The OP wanted C, not C++.
 
You're worrying about the black-box window that pops up on a Windows computer with something like Visual Studio; this has nothing specifically do to with your program at all.

Try opening a command-line window and running your programs by typing their names there.

- Warren
 
madmike159 said:
I started learning C not long ago. Like most people I started with "Hello World". I relised that it closes itself unless you tell it not to.

I have this code

#include <stdio.h>

int main(char *argv[])
{
printf("Hello World!\n");
getchar();
return 0;
}

It makes it close when you press enter. The problem is when you do read input commands like scanf() it closes when you press enter. Does anyone know how I can change this to so it closes when you press something else (like esc, or Ctrl+enter)?

When you call getchar(), it returns a value. The value will be a code that is different depending on what key was pressed. try saying something like char key = getchar(); and then "key" will have the value of the keypress. If you want to turn this into a "pause until return is pressed", you can do something like:

Code:
while (getchar() != '\n') {} // Loop forever until return

... I *THINK* that works.

However chroot's advice is probably closer to what you want. If you do what chroot says then the window will not close when the program ends, it will just stay open. Then the "press return" nonsense will not be necessary.
 
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

  • · Replies 6 ·
Replies
6
Views
6K
Replies
7
Views
2K
  • · Replies 20 ·
Replies
20
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
14
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K