Greetings. I saw a piece of C ++ code somewhere (or C - xs :)) and decided to compile it and see what happens ^ _ ^. Here is my code:

#include <stdio.h> int main(int argc, char * argv[]) { printf("Hello, World\n"); return 0; } 

Um ... Like here :). After compiling under Windows, an .exe file appears. When you click on it, the console opens (for a split second) and closes ... How to make it not close? :)

ps. PROFI - Please do not matter - C ++ has not yet explored but it is interesting to learn :).
pps. Give a promise that Microsoft will not show this piece of code - And they will introduce it into their programs: DDDDDD
ppps Thank you in advance :)

  • 2
    for the second and third post-script, the abbreviations PPS and PPPS are used, not PSS and PSSS - andrybak
  • one
    Good answer to the question ... I see you are a pro :) Thank you. - BomBom
  • before <pre> <code> return 0; </ code> </ pre> add the line <pre> <code> system ("pause"); </ code> </ pre> - chip
  • Well, it’s necessary what a respectful attitude to the compilation - write her name with a capital letter ... - skegg
  • And then! And the FIG will stop working and not the .exe file ...: D Thank you :) - BomBom

6 answers 6

Option with system("pause")

 #include <cstdio> #include <cstdlib> // Здесь объявлена функция system() int main(int argc, char * argv[]) { printf("Hello, World\n"); system ("pause"); return 0; } 

The system() function calls a new instance of the shell and runs a command in it from the string passed to it as a parameter.

C getchar() :

 #include <cstdio> int main(int argc, char * argv[]) { printf("Hello, World\n"); getchar(); return 0; } 

The getchar() function is used to read and pass any variable entered on the command line symbol. She will wait until something is entered. Here she just waits and after pressing ENTER, the program continues to run, and the entered character is not transmitted anywhere. Therefore, a false call.

    And finally, the simplest and most terrible option: for (;;);

    Creepy is that it loads the CPU core at 100%.

    • really horror :) I will not go - you never know: D but keep it up ... - BomBom
    • as a while (1); - skegg
    • Some compilers give a warning that while (1) uses a constant expression. Therefore, it is better to use for (;;). - gammaker
    • Yes, we saw their warnings .... - skegg
    • And by the way, which compilers? And while (1 = 1) will pass? - skegg

    Several options, add system("pause"); make dummy input: cin.get(); getchar(); etc. cin.get(); getchar(); etc. , something should stop, experiment;)

    • I kind of said that I don’t really shy ... make dummy input: cin.get(); getchar(); etc. cin.get(); getchar(); etc. - For me, this does not mean anything ^ _ ^ <br> <br> ps. system("pause"); - Wrote wherever possible (seemingly). The compiler swears, they say, an error. I'm compiling on Dev-cpp :) - BomBom

    As an option. Create a .bat file in a folder with exe-shniki with the contents of "cmd" (without quotes). When you launch it, the console opens and from it you are already launching the desired file.

      You can also create a bat or cmd file in a folder with a program with the following contents:

       program pause 

      where program is the name of the program. I run other console programs this way to read the output.

        Keep the code (working, but wrong :)), at the same time learn the stdio stream functions:

         #include <stdio.h> int main(int argc, char *argv[]) { printf("Hello, World\n"); printf("Press ENTER for exit...\n"); scanf("enter key"); return 0; } 
        • one
          If you explain what's wrong, I'll put a plus :) - BomBom
        • Yes, just a guy wrote that he did not dig in the pros-crosses. And I myself have used C ++ for the last time a long time ago. I remember the simple cin / cout functions for the console, and for formatted printf I / O ... I decided without brain that I had to force the user to press Enter, i.e. "\ n". - Vyacheslav Kirichenko