Thread: new to C programming, need help

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

    new to C programming, need help

    Hello, I am new to programming in general. I am reading Absolute beginners guide to C. A very good book so far. But when I started my compiler (Bloodshed, dev.) I wrote the first very simple program. Where it says "Hello world", type of thing. but it didn't stay like it should have, when I hit execute it just flashed for a mili-second, then went back to the compiler. I have heard of this problem I think, but I know not how to fix it. Any help will be marvelous.
    Thanks.

    - Michael

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    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.*

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    yea it does that cause it executes the program a very simple one at that and cause its so simple it just closes instantly

    to fix:

    Code:
    char holder;
    printf("hello");
    getch(holder);
    just press enter or enter a character and press enter. its just an input function so you can have the screen freeze unless using some other compiler(microsoft for instance) just do that at all of your programs to catch the screen.
    hooch

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    2
    Hey, thanks.
    I didn't know where to add the "Char holder;" code... Do I just add that before "Return 0;"... I don't understand what you mean here....

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    Or alternatively, go Start->run->type CMD, click ok

    then just navigate to the folder with your program in, and type your program name in, this way, they program will be run in that window, so there is no need to add anything extra such as 'char holder' to the code.

  6. #6
    Registered User Afrinux's Avatar
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    46
    Quote Originally Posted by MikeMeister123
    Hey, thanks.
    I didn't know where to add the "Char holder;" code... Do I just add that before "Return 0;"... I don't understand what you mean here....
    getchar(); just before the return will do.

    Code:
    int main( .., ...){
      //code
    
     getchar();
     return 0;
    }

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Note : need to clear the input buffer before calling the getchar to be on a saver side. sample code to clear the buffer. make sure to call this fucntion , when getchar is placed even though the console windows goes away soon.


    Code:
    void clear_buffer(void)
    { 
        int ch;
        while((ch=getchar())!='\n' && ch != EOF);
    }
    ssharish2005
    Last edited by ssharish2005; 01-28-2006 at 06:31 AM.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    This code is pretty bad:
    Code:
    char holder;
    printf("hello");
    getch(holder);
    1. getch() is non-standard.
    2. If you really want to use it, and your compiler has it, you need another header file (<conio.h> for Dev-C++).
    3. getch() takes no arguments -- it returns the value. So you can just discard it (and you don't need holder).
    4. Even if getch() did take an argument as the place to store the value, you would need to pass it as a pointer to get the value.


    This code's okay:
    Code:
    int main( .., ...){
      //code
    
     getchar();
     return 0;
    }
    getchar() is in <stdio.h>, which most programs include. But that won't work if there's more in the input buffer than just a '\n'. (And // comments are C99 . . . . )

    ssharish2005's code is the best. Use it instead of the other stuff.

    All of this is covered in Dave's FAQ link.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed