First of all: do not use gets
, this function is not safe, because it doesn't
prevent buffer overflows. If you input more than 999 characters you will have
a buffer overflow. Use fgets
instead.
What you are describing is something concerning your terminal, not the C
language. Which terminal are you using?
Depending on how much text was selected (for example you select more than one line),
a new line ('\n'
) is also copied, if you paste
that onto your terminal, the pasted new line acts as if you've pressed ENTER,
the line is entered (gets()
returns), printf
prints the line on the terminal and then program ends.
Depending how you open the terminal, the terminal may also close immediately
after the program closes.
This has nothing to do with C but how you use your terminal.
EDIT
I've overseen from the title that you are using the Codeblock IDE, right? If
you click on Run, then a console is opended and your program is executed.
Most of the time, the standard setting is that the console closes immediately
after the program ends. I don't know if you can change these settings in the
Codeblock configuration (something like "don't close terminal on exit"). If
you cannot change this configuration, you could end your program like this, so
that terminal doesn't close immediately:
#include <stdio.h>
int main(void)
{
// your progam here
printf("Press ENTER to end");
getchar();
return 0;
}
I personally don't like this, a program on the console shouldn't do that
(unless it's necessary for some reason). A better thing would be that you
open the terminal, go to the directory of your compiled program and execute
it yourself. Thus when the program ends, the terminal stays alive.