944,179 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3227
  • C++ RSS
Nov 24th, 2004
0

C compiler help (beginner question)

Expand Post »
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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Moo The Cow is offline Offline
3 posts
since Nov 2004
Nov 24th, 2004
0

Re: C compiler help (beginner question)

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.
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 163
The Queen of DaniWeb
cscgal is offline Offline
13,646 posts
since Feb 2002
Nov 24th, 2004
0

Re: C compiler help (beginner question)

Quote originally posted by Moo The Cow ...
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/...&id=1043284385
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Nov 25th, 2004
0

Re: C compiler help (beginner question)

Quote originally posted by cscgal ...
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(...)'
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Moo The Cow is offline Offline
3 posts
since Nov 2004
Nov 25th, 2004
0

Re: C compiler help (beginner question)

Quote originally posted by Moo The Cow ...
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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Nov 25th, 2004
0

Re: C compiler help (beginner question)

Quote originally posted by Dave Sinkula ...
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Moo The Cow is offline Offline
3 posts
since Nov 2004
Nov 25th, 2004
0

Re: C compiler help (beginner question)

Since you are writing a C program use getchar() to wait.
C++ Syntax (Toggle Plain Text)
  1. // you can also use just #include <iostream>
  2. // this works in DevCpp because iostream chain-includes down
  3. // to sdtio.h (not recommended for portable code)
  4.  
  5. #include <stdio.h>
  6.  
  7. int main()
  8. {
  9. printf("Hello, world!\n");
  10. getchar(); // wait
  11. return 0;
  12. }
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Nov 26th, 2004
0

Re: C compiler help (beginner question)

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.
Reputation Points: 7
Solved Threads: 1
Junior Poster in Training
harshchandra is offline Offline
68 posts
since Nov 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Quit abruptly in MFC app
Next Thread in C++ Forum Timeline: c++ digit code





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC