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

Discussion Overview

The discussion revolves around a common issue faced by beginners in C programming regarding the behavior of console applications that close immediately after execution. Participants explore methods to prevent the console window from closing, particularly in relation to handling input and program termination.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • The original poster (OP) describes their experience with a "Hello World" program that closes upon pressing Enter after using getchar(). They seek a solution to keep the program running until a different key is pressed.
  • One participant suggests using cin.getline(someString, 256) for input, although this is noted to be a C++ approach rather than C.
  • Another participant points out that the issue may be related to the environment (e.g., Visual Studio) rather than the code itself, recommending running the program from a command-line window instead.
  • A later reply proposes modifying the getchar() function to capture key presses and suggests a loop to wait for a newline character, indicating uncertainty about its effectiveness.
  • There is a suggestion that following chroot's advice may be a better solution to prevent the console from closing automatically.

Areas of Agreement / Disagreement

Participants express differing views on the best approach to prevent the console from closing, with no consensus reached on a single solution. Some focus on code modifications, while others emphasize the execution environment.

Contextual Notes

There are limitations regarding the assumptions about the programming environment and the specific behavior of input functions in C versus C++. The discussion does not resolve the effectiveness of the proposed solutions.

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.
 

Similar threads

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