954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C compiler help (beginner question)

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

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?

Moo The Cow
Newbie Poster
3 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 

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.

cscgal
The Queen of DaniWeb
Administrator
19,422 posts since Feb 2002
Reputation Points: 1,474
Solved Threads: 230
 
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?

http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1043803465&id=1043284385

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 
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(...)'

Moo The Cow
Newbie Poster
3 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 

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.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 
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.

Moo The Cow
Newbie Poster
3 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 

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;
}
vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

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.

harshchandra
Junior Poster in Training
68 posts since Nov 2004
Reputation Points: 7
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You