Why does my C program close early? - c

I am having a very confusing problem right now. I wrote a test program for myself, but sometimes after I input Crtl+C, which is what I assume to be the EOF, the program closes early without running through some more commands below the While Loop I wanted it to cancel from.
#include <stdio.h>
#include <string.h>
int main()
{
char message[140];
char* p = message;
int count;
int i = 0;
int charGT;
while((charGT=getchar()) != EOF)
{
message[i] = charGT;
i++;
printf("%d" , i);
}
printf("next");
count = strlen(p);
printf("%d", count);
printf("after");
return (0);
}
Inputting "asd\n^C" will cause my program to end early. "next" or "after" will not be printed
My theory is that somehow, the \n is causing the program to step out of the loop for a moment, and then Crtl+C is exiting the program, but I don't know how that would work.

Ctrl+C is the break command and will send an interrupt signal to your application. The default handler will cause this to exit the process.
Ctrl+D is the EOF command you are looking for.

For running on windows, to enter EOF press Ctrl+Z and then press ENTER. In UNIX systems it is Ctrl+D, in Windows Ctrl+Z.

When a program is running if you press Ctrl-c a signal is sent to abort the program and the program is aborted. However, if you want to use Ctrl-c as an input but not abort the program write one signal handler which catches the signal when you press Ctrl-c and perform the action you wanted to.

Related

Difference between Ctrl + Z and Ctrl + C

As per my understanding Ctrl+Z means EOF, isn't it? When Ctrl+Z is passed in as an input in the following program;
#include <stdio.h>
int main()
{
int a;
a = getchar();
if(a == EOF)
printf("Program Ended\n");
printf("Finished");
return 0;
}
It prints;
Program Ended
Finished
which is totally reasonable.
But when Ctrl+C is passed in as an input, Why does it outputs;
Program Ended, and simply end the program? Wasn't it supposed to end the program without printing anything as per the code?
PS: I'm using gcc 6.3.0
Ctrl-C maps to the ASCII control character ETX (End-of-Text - 0x03), while Ctrl-Z maps to ASCII EOF (End-of-Text - 0x26). There is also Ctrl-D for EOT (End-of-Transmission - 0x04).
Now what a particular platform does when either of these are occur in the the stdin stream is platform specific (and not C language or GCC specific). However the macro EOF has integer value -1 (rather than ASCII value 0x03)
When your code is executed at https://onlinegdb.com/4bF8adsM4 :
Ctrl-C : Program terminates immediately (causes SIGINT signal to terminate process)
Ctrl-D : a = -1, getchar() returns immediately.
Ctrl-Z : a == '\n' `getchar() returns after newline (i.e. Ctrl-Z is ignored).
On Windows, (with Microsoft C in my tests):
Ctrl-C : a = -1, getchar() returns immediately, Program terminates after outputting "Program Ended\n" with exception "Exception thrown at 0x7747B915 (KernelBase.dll) in scratch2.exe: 0x40010005: Control-C."
Ctrl-D : a == EOT, getchar() returns after newline
Ctrl-Z : a == -1, getchar() returns after newline
In the Ctrl-C case, the exception was shown in the debugger, in normal use (from the command line, nothing is reported). When run from the command line, it actually output "Finished\n" as well. It seems that the process termination is asynchronous to the I/O and program execution and the program can continue to run for some time before being terminated. I added further text, and only some of it was output. YMMV
Either way, on Windows, Ctrl-Z on stdin does trigger an EOF signal for the stdin stream, while in Unix/Linux, it is Ctrl-D. In both cases Ctrl-C causes process termination by SIGINT, but on Windows at least, it is an asynchronous signal/exception, and the process may not terminate immediately. Also in Windows, the EOF after Ctrl-Z is buffered until after newline, whereas in Unix/Linux, it aborts the input immediately.
Note that it is possible to implement a SIGINT signal handler to trap Ctrl-C to prevent process termination by that method (or handle it more elegantly).
All the other ASCII control characters have Ctrl-key mappings, what any particular platform does with these and stdin is also platform dependent.

Command clear won't take kill signal ( in infinite loop | c program )

I used the command "clear" in C language at infinite loop just for test and the kill signal does not stop it.
Why the program does not stop when i press Ctrl + C ?
#include <stdlib.h>
int main () {
while (1) {
system("clear");
}
return 0;
}
Killing clear will not help, because the infinite loop is in your own program. Kill that.
ControlC is only useful if you can get the system to pay attention. It sounds as if your terminal is too busy writing to the screen to do this.
From another terminal, run top and kill your program (which likely is near the top of the screen).
You could include a signal handler to 'catch' your CRTL-C input (which is SIGINT below) and then exit. Hopefully, this stub code will give you the idea. If not, I can provide a complete working example but would like to see you try first. :-)
#include <signal.h>
...
void stop (int signal) {
exit;
}
int main() {
signal(SIGINT, stop);
...
}
HTH

How to clear typed characters from command line during user input

In my program I have a while loop that continually gets input with fgets. I've set up a signal handler for SIGINT (Ctrl + C) so that the program will not return to the shell when Ctrl+C is entered, and continue in the while loop. However, when Ctrl+C is entered, there appears a bunch of ^C characters in the next input prompt.
Is there a way that I can clear what's typed in the command line prompt? Perhaps by placing something in my signal handler?
// SIGINT signal handler
// is there something I can place here to clear what's typed (^c) into the command line?
void siginthandler(int sig){
// do nothing (don't exit to shell)
}
int main(int argc, char*argv[]){
// change SIGINT disposition
signal(SIGINT, siginthandler);
While(1){
fgets(input, 20, stdin);
// some code...
}
}

How to end a whileloop with ctrl+c in c on windows?

So i need to write an whileloop for a program that's supposed to prompt the user with this:
char vector[10];
while(????){
print("Write a number");
scanf("%s",vector);
}
printf("Goodbye");
The program is supposed to print goodbye and close when the user presses ctrl+c.
Im pretty sure I cant use putchar in this case?
#include <windows.h>
#include <stdio.h>
static end_flag = 0;
BOOL WINAPI controlHandler(DWORD type){
if(type == CTRL_C_EVENT){
end_flag = 1;
return TRUE;
}
return FALSE;
}
int main(){
char vector[10];
if (!SetConsoleCtrlHandler(controlHandler, TRUE)) {
fprintf(stderr, "Failed SetConsoleCtrlHandler");
return -1;
}
while(!end_flag){
printf("Write a number ");
scanf("%s",vector);
}
printf("Goodbye");
return 0;
}
CTRL+Z version
#include <stdio.h>
int main(){
char vector[10];
while(1){
printf("Write a number ");
if(scanf("%s", vector)==EOF)//press CTRL+Z
break;
}
printf("Goodbye");
return 0;
}
see https://superuser.com/questions/214239/whats-the-command-prompts-equivalent-to-cygwins-ctrlz:
Depends on what you mean by "quit something"; within Windows cmd:
Ctrl+Z sends the EOF character, which could terminate a process if
you're providing input, but otherwise will probably do nothing.
Ctrl+C normally sends SIGINT to the foreground process, which should
terminate it, but programs can respond however they like - ie, they
can catch the signal but then ignore it. The command can also be
remapped to other jobs (such that for a specific program it doesn't
really send a signal) or ignored entirely.
Ctrl+Break always sends SIGBREAK, which again should terminate the
process, but unlike Ctrl+C cannot be remapped, but can still be
ignored. This is probably what you need.
I think this might be what you're looking for:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms683242%28v=vs.85%29.aspx
Post similar to this one on Stack Overflow:
Catch Ctrl-C in C
You might want to check it out. Cheers!

sleep function will abort when received a signal?

#include <signal.h>
#include <stdio.h>
void ints(int i )
{
printf("ints \n");
}
int main(void)
{
signal(SIGINT, ints);
sleep(10);
}
input Ctrl+C , the program will terminate immediately with output:
^ints
I was wondering why,in my opinion,the program should terminate after 10 seconds no matter how many times Ctrl+C is input.
sleep() is one of those functions that is never re-started when interrupted.
interestingly, it also does not return a EINT as one would expect.
It instead returns success with the time remaining to sleep.
See:
http://www.kernel.org/doc/man-pages/online/pages/man7/signal.7.html
for details on other APIs that do not restart when interrupted

Resources