Hi, this is the first time that I've used C. Anyway, I downloaded a C compiler (dev-C++), and I typed in this:

#include <stdio.h>

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

When I run the program, it doesn't print the "Hello, world!", but I just see a dos window quickly flash on my screen. What (and where) should I add to this code to prevent this?

Recommended Answers

All 7 Replies

First of all, change your main() line to be int main()

The problem that you are having is that the program opens, does what it's supposed to do, and then closes. At the end of the program, right above the return 0 line, try entering:

char x;
cin >> x;

What that will do is prompt for a character to be input right before the program closes. Therefore, the program won't automatically close until it gets user input.

First of all, change your main() line to be int main()

At the end of the program, right above the return 0 line, try entering:

char x;
cin >> x;

What that will do is prompt for a character to be input right before the program closes. Therefore, the program won't automatically close until it gets user input.

I tried the char x; suggestion, but it still doesn't work :( . When I tried typing in "cin >> x;", I got an error saying that 'cin' is undeclared.

Dave's suggestion to look into the FAQ didn't work either. The faq says to type
system("PAUSE");
before the return 0 line, but I get an error that says:
implicit declaration of function `int printf(...)'

I tried the char x; suggestion, but it still doesn't work :( . When I tried typing in "cin >> x;", I got an error saying that 'cin' is undeclared.

Dave's suggestion to look into the FAQ didn't work either. The faq says to type
system("PAUSE");
before the return 0 line, but I get an error that says:
implicit declaration of function `int printf(...)'

Uh, try reading it again and note the differences from what you have.

Uh, try reading it again and note the differences from what you have.

Oops, I now realize that I was somehow looking at the wrong FAQ. Oh well, it works now.

Since you are writing a C program use getchar() to wait.

// you can also use just   #include <iostream>
// this works in DevCpp because iostream chain-includes down 
// to sdtio.h  (not recommended for portable code)

#include <stdio.h> 

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

u can use getch() at the end of the code for the screen to be still till u press any key.Or else u can use alt+F5 key to see the output window.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.