Thread: printf problem

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    7

    Arrow printf problem

    Hello forum,

    I am new to C and programmin in general, but very interested!

    I am reading, "Absolute beginners guide to C" by:Greg Parry. And towards the beginning of the book he gives a very simple "printf" example, just to display some words on screen, and I wrote this program in Dev bloodshed and the consol just flickered onscreen and then disappeared a milisecond later.

    Any help as to how I can make it stay on screen would be very helpful.

    Thank you

    servant of Pluto,

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It's in the FAQ.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    7
    Thank you very much.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    7
    Forgive my inpertanence, but the FAQ didn't tell me what code to write to fix it.



    Any help on the subject would be appreciated.Thank you

    Servant of Pluto

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I personally prefer the method of "run the application from an command prompt". But add something like "getchar();" near the end of your main() function.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    7
    please forgive my impertinence, but I did not understand te FAQ, what do I write?

    What command keeps the window open?

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by plutoismyhome View Post
    please forgive my impertinence, but I did not understand te FAQ, what do I write?

    What command keeps the window open?
    Start->Run...
    Enter the application name "cmd" (or "command" if you are using Windows 98/ME).

    You'll need to "cd" to the directory where your executable is, then you are "ready to go".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    7
    This is what i wrote but it still flashes!:
    Code:
     #include <stdio.h>
    main()
    getchar()
    {
          
          printf("this stuff is easy!\n");
          return 0;
    }

  9. #9
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    #include <stdio.h>
    
    int main()
    {
        printf("this stuff is easy!\n");
        getchar();
        return 0;
    }
    ssharish
    Last edited by ssharish2005; 09-18-2007 at 09:36 AM.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm not sure how you even got that to compile - but that besides, what are you doing to run the application?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    What you wrote won't even compile.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Just fixing a typo here
    Code:
    #include <stdio.h>
    
    int main()
    {
        printf("this stuff is easy!\n");
        getchar();
        return 0;
    }
    plutoismyhome: you need to put a semicolon ';' after the getchar().

  13. #13
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    My bad, a semicolon missed.

    EDIT: Fixed

    ssharish

  14. #14
    Registered User
    Join Date
    Sep 2007
    Posts
    69
    You can also do it this way:

    Code:
    #include <stdio.h>
    
    int main()
    {
        printf("this stuff is easy!\n");
        system("PAUSE");
        return 0;
    }

  15. #15
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by +Azazel+ View Post
    You can also do it this way:

    Code:
    #include <stdio.h>
    
    int main()
    {
        printf("this stuff is easy!\n");
        system("PAUSE");
        return 0;
    }
    getchar() is way more portable. Besides, calling the shell is slow and considered a bad habit, if the point is simply to pause the execution.

    Besides, using system should really never be necessary. In windows, everything you could ever achieve by using the oh-so limited cmd, could be achieved by using the API.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems reading entered race times C
    By loopymoo26 in forum C Programming
    Replies: 12
    Last Post: 05-23-2009, 07:38 AM
  2. Help with a C Programing Quiz project
    By dilemma in forum C Programming
    Replies: 12
    Last Post: 05-15-2009, 03:35 PM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. error in program????
    By SpEkTrE in forum C Programming
    Replies: 5
    Last Post: 11-24-2003, 06:16 PM
  5. Drawing tables in C
    By stanoman in forum C Programming
    Replies: 5
    Last Post: 10-09-2003, 10:14 AM