Thread: conio.h and system("pause")

  1. #1
    Registered User
    Join Date
    Mar 2019
    Posts
    51

    conio.h and system("pause")

    we can use system("pause") same way in C++ as in C. But I've a question. In C we need to use conio.h file to use system("pause") function.
    How can we use it here without referencing to its definition or adding any header file. I have used it with out adding any header file and it still works perfectly.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    In C we need to use conio.h file to use system("pause") function.
    No you need to use the cstdlib header to use the system() function. The conio.h header is for the obsolete console (should not be used in Win32 programs) routines.

    How can we use it here without referencing to its definition or adding any header file.
    You could create a prototype for the function, however that is not recommended. Use the proper header file <cstdlib>.

    I have used it with out adding any header file and it still works perfectly.
    Well you were probably coding in C, plus you got lucky. C++ doesn't allow default arguments so you need to tell the compiler about the function, usually by #including the proper header.

  3. #3
    Registered User
    Join Date
    Jun 2019
    Posts
    1
    Using system("pause") is poor practice. Let me explain why.
    1. It uses a lot of overhead.
    2. It calls from the command line a executable program called "pause" which has nothing to do with your current program (technically).

    It seems elegant to use as a beginner but it's just bad practice. Simply creating an extra std::cin >> statement with dummy data is preferred.

    Check out the top comments in this SO thread. It's a great read. c++ - system("pause"); - Why is it wrong? - Stack Overflow

  4. #4
    Registered User
    Join Date
    Mar 2019
    Posts
    51
    Quote Originally Posted by jimblumberg View Post
    No you need to use the cstdlib header to use the system() function. .
    It is said that we can use cstdlib and stdlib.h file as well. My question is how can we use C language libraries functions here in CPP too? Are C and CPP libraries available for both, C and CPP?

  5. #5
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    945
    Why don't you try it? You could have had an answer from your compiler already.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    The difference between cstdlib and stdlib.h includes is the "stdlib.h" adds the functions to std namespace.
    When using "cstdlib", you need to have an using statement to add what functions you want or prefix the functions with the "std" prefix.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by stahta01
    The difference between cstdlib and stdlib.h includes is the "stdlib.h" adds the functions to std namespace.
    I think you meant global namespace.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by laserlight View Post
    I think you meant global namespace.
    You are almost always correct, so, I will agree with you.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  9. #9
    Registered User
    Join Date
    Mar 2019
    Posts
    51
    Quote Originally Posted by christop View Post
    Why don't you try it? You could have had an answer from your compiler already.
    Yup I have checked already through compiler but actually I am beginner. So, one question get satisfied another arose.
    I observed that when I include C header files in my CPP program, It compiled without any error. So I guessed that it may work with each other C to CPP or CPP to C then another question, how?. Anyway I researched and mentors like you helped me to find that answers too.

  10. #10
    Registered User
    Join Date
    Mar 2019
    Posts
    51
    Quote Originally Posted by stahta01 View Post
    The difference between cstdlib and stdlib.h includes is the "stdlib.h" adds the functions to std namespace.
    When using "cstdlib", you need to have an using statement to add what functions you want or prefix the functions with the "std" prefix.

    Tim S.
    Well another question is if we need to add namespace statement or std prefix, we'll need to add it in our program and then compiler first will check in our program namespace statement or prefix std and will copy the prototypes of functions accordingly? Am I right?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Editing output message of system("pause") ?
    By alizzle in forum C++ Programming
    Replies: 8
    Last Post: 09-20-2010, 11:52 PM
  2. is system("PAUSE") required in every program?
    By xstarburstbaby in forum C++ Programming
    Replies: 21
    Last Post: 09-03-2008, 05:18 AM
  3. system("pause") in C#? (warning: noob question)
    By Wintersun in forum C# Programming
    Replies: 20
    Last Post: 03-28-2008, 02:26 AM
  4. function to replace system("pause");
    By willc0de4food in forum C Programming
    Replies: 29
    Last Post: 09-08-2005, 12:52 PM
  5. Replies: 3
    Last Post: 06-01-2004, 04:29 PM

Tags for this Thread