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

  • Thread starter Thread starter madmike159
  • Start date Start date
  • Tags Tags
    Hello Hello world
AI Thread 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.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
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...
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
Back
Top