Thread: system("pause"); in C?

  1. #1
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286

    system("pause"); in C?

    I have tried to write system("pause"); in DevCpp, but it doesn't work until I include a C++ library so DevCpp knows it is a C++ program. What do you use in C instead? getch()?

  2. #2
    Register User andor's Avatar
    Join Date
    Aug 2006
    Location
    Novi Sad
    Posts
    42
    Quote Originally Posted by TriKri
    I have tried to write system("pause"); in DevCpp, but it doesn't work until I include a C++ library so DevCpp knows it is a C++ program. What do you use in C instead? getch()?
    No its not standard. Use getchar instead

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    system() itself should be standard (although the "pause" part is probably platform specific), but you'll need to include <stdlib.h> (I think) to use it.

    In DevCpp the C++ headers include it for you, that's why you don't apparently need to include it when using <iostream> for example.

  4. #4
    Register User andor's Avatar
    Join Date
    Aug 2006
    Location
    Novi Sad
    Posts
    42
    I meant getch is not standard

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You need the <stdlib.h> header to use system (as mentioned); in Standard C++, this would be <cstdlib> (and you'd need a using statement or use std::system). It executes a system (DOS) command, and so is unportable, unsafe, slow, and generally undesireable and unrecommended.

    Some compilers support getch(), which usually gets a keypress without the user having to hit enter; Dev-C++ has it in <conio.h>. It too is non-standard, though.

    For the standard solution, use getchar() (for C). (You'll have to press <enter>, though.) Something like this:
    Code:
    int c;  /* must be int to hold EOF */
    
    while((c = getchar()) != '\n' && c != EOF);
    This is covered in this FAQ, which tells you to read this one.
    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.

  6. #6
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286

    Smile

    Ok, thank's for all the answers.

Popular pages Recent additions subscribe to a feed