Thread: Little Confused..?

  1. #1
    Newbie ikidd's Avatar
    Join Date
    Dec 2004
    Location
    Australia
    Posts
    1

    Little Confused..?

    Ok i've been reading a book called "C for Dummies" which has alot of padding. It show you some basic things like printf.. and how to compile.

    When you run these "programs" in the book they run though cmd, but the program doesn't complete (or it does but you don't see it), you see it closes as soon as the program is finish. What do I need to add to the code you keep it open untill someone exits.

    Thanks.

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Hi, welcome to the forums!

    You can use the getchar() function at the end of your program. More details are available in this FAQ entry.
    How do I get my program to wait for a keypress?

    Make sure you have read this before posting code.
    Last edited by anonytmouse; 12-09-2004 at 10:41 AM.

  3. #3
    * S T U D E N T *
    Join Date
    Oct 2003
    Posts
    30
    welcome!

    im a bit of a newbie too! you can of course put while(1) after the code is executed in the int main() function, which basically means while forever (or user closes program) so eg:

    Code:
    #include <stdio.h>
    
    int main()
    {
       printf("Hello World\n"); /* a trivial example */
    
       while(1) /* while forever */
       {
          /* do nothing */
       }
       
       return(0);
    }
    hope this helps!

    --CHriS
    'rm -fr /bin/laden'
    'kill all'

  4. #4
    Hamster without a wheel iain's Avatar
    Join Date
    Aug 2001
    Posts
    1,385
    I would avoid using this method - it effectively means that you have to force your program to stop and affects portability (eg *nix based operating systems dont really like having processes killed). I would stick to the getchar method

    hth
    Monday - what a way to spend a seventh of your life

  5. #5
    * S T U D E N T *
    Join Date
    Oct 2003
    Posts
    30
    Quote Originally Posted by iain
    I would avoid using this method - it effectively means that you have to force your program to stop and affects portability (eg *nix based operating systems dont really like having processes killed). I would stick to the getchar method

    hth
    ooooooh! fair enough, i will read about such great things!

    --CHriS
    'rm -fr /bin/laden'
    'kill all'

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by ian
    eg *nix based operating systems dont really like having processes killed
    In what way?
    If you understand what you're doing, you're not learning anything.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Mine cackles gleefully when it gets a chance to kill a process. But maybe that's just mine...

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    Why do I imagine you have the Doom process management utility Quzah?
    Demonographic rhinology is not the only possible outcome, but why take the chance

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Confused
    By jeev2005 in forum C Programming
    Replies: 5
    Last Post: 06-01-2006, 02:04 PM
  2. Confused
    By (TNT) in forum C# Programming
    Replies: 1
    Last Post: 11-23-2005, 04:49 PM
  3. why wont this compile?!? :confused:
    By jdude in forum C++ Programming
    Replies: 5
    Last Post: 11-25-2004, 01:13 AM
  4. confused.. in selecting my line of deapth
    By jawwadalam in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-04-2003, 01:21 PM
  5. Extern Question, really confused
    By SourceCode in forum C Programming
    Replies: 10
    Last Post: 03-26-2003, 11:11 PM