Program doesn't show up

This is a discussion on Program doesn't show up within the C Programming forums, part of the General Programming Boards category; Hi, I am a beginner in C. Actually I started few days ago. I have a problem when I compile ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    2

    Program doesn't show up

    Hi,

    I am a beginner in C. Actually I started few days ago.

    I have a problem when I compile a program with DJGPP (on WinXP). The program seems to compile fine but when I run it, it does the task it should be doin but it closes immidiately after that and doesn't let me see it.

    My first program is like this:

    Code:
    #include <stdio.h>
    
    main()
    
    {
    	printf("This is my first program");
    }
    I tried to put in the return 0; thing but it doesn't solve the problem. I know some probably laugh when they see that but im a beginner. Help pls.

  2. #2
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    Read the faq before posting, or atleast do a board search. This is what you are looking for.

    Also, it should be int main() not main() and main returns 0 on successful execution of the program, so put a return 0 in the end.
    Last edited by PING; 01-05-2006 at 02:55 PM.
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  3. #3
    Rabble Rouser Slacker's Avatar
    Join Date
    Dec 2005
    Posts
    116
    That happens a lot with IDE's. What you need to do is pause the program somehow before it ends and closes the window. The "best" way according to a lot of people is to ask for input before the return:
    Code:
    #include <stdio.h>
    
    int main()
    {
      printf("This is my first program");
    
      getchar(); /* I pause the program */
    
      return 0;
    }
    The only problem with this is if stdin, where getchar gets chars from, isn't empty. In that case, you need to clean it up by reading and discarding characters until there's nothing left:
    Code:
    #include <stdio.h>
    
    int main()
    {
      printf("This is my first program");
    
      while ( getchar() != '\n' )
        ;
      getchar(); /* I pause the program */
    
      return 0;
    }
    Note that I'm keeping it as simple as possible since you're new to the language. I don't want to overwhelm you with the more complicated rock solid way to go about things.

  4. #4
    Cached User mako's Avatar
    Join Date
    Dec 2005
    Location
    Germany.Stuttgart
    Posts
    69
    not sure if this only works for windows seeing as it's a system call, but system("PAUSE"); waits for any key to continue,.. try this:

    Code:
    #include <stdio.h>
    
    /* this is a comment: if you use "int " before the name of a function,
    you tell the compiler that this function will return an integer value.
    That's why you need the "return 0;" at the end in order for it to
    compile. */
    
    int main()
    {
      printf("This is my first program");
    
      system("PAUSE");
    
      return 0;
    }

    Code:
    #include <stdio.h>
    
    /* if you write "void " before the program name you tell the compiler
    that the program won't return a value. Which means you can skip
    the "return 0;". Some people here claim that this way of doing it is
    unclean though. Either way, you HAVE to write something before
    the program name in order for it to compile... */
    
    void main()
    {
      printf("This is my first program");
    
      system("PAUSE");
    }
    Last edited by mako; 01-05-2006 at 03:53 PM.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    > Some people here claim that this way of doing it is unclean though.
    It isn't a claim, it's a simple fact in the ANSI-C standard. In a hosted environment, main returns int - no ifs, buts or maybes about it.

    > fflush(stdin); //this cleans out stdin aswell
    Rubbish - just read the FAQ will you.

  6. #6
    Cached User mako's Avatar
    Join Date
    Dec 2005
    Location
    Germany.Stuttgart
    Posts
    69
    Quote Originally Posted by Salem
    > Some people here claim that this way of doing it is unclean though.
    It isn't a claim, it's a simple fact in the ANSI-C standard. In a hosted environment, main returns int - no ifs, buts or maybes about it.
    yet I've never had a problem compiling with void. Maybe my code's not complex enough,.. and where's this FAQ you keep talking about?..

    anyways, the problem with this guys code was that he didn't have anything befor the function name and I just wanted to explain why there need sto be something there with an example as support. I'll make sure I only use "int main" from now on... Now where's this FAQ at?...
    Last edited by mako; 01-05-2006 at 04:07 PM.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Last edited by Dave_Sinkula; 01-05-2006 at 04:17 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Jan 2006
    Posts
    2
    Thanks all have been very helpful. I searched for this problem but didnt find anything

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 10:17 PM
  3. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  4. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 06:49 PM
  5. Date program starts DOS's date
    By jrahhali in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2003, 04:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21