-1
$\begingroup$

I was writing a program to count lines from an input text stream. This is the program. It compiles perfectly but when I execute ./a.out to get the output my program doesn't seem to run. I enter the text and then enter the command ^z (control z) and it shows the error mentioned. (zsh: suspended ./a.out)

I just want the program to get me the result which it is not showing.

Can you help me, I have just started learning programming.

This is the program just in case you want to check.

include <stdio.h>

int main()
{
  int blank_char, tab_char, new_line;
  blank_char = 0;
  tab_char = 0;
  new_line = 0;
  int c;
  printf("Number of blanks, tabs, and newlines:\n");
  printf("Input few words/tab/newlines\n");
  for (; (c = getchar()) != EOF;)
  {
    if ( c == ' ' ){
      ++blank_char;
    }
    if ( c == '\t' ){
      ++tab_char;
    }
    if ( c == '\n' ){
      ++new_line;
    }
  }  
  printf("blank=%d,tab=%d,newline=%d\n",blank_char,tab_char,new_line);
}
$\endgroup$
1
  • $\begingroup$ @ Pål GD- thanks, i tried contol d and it is working;but can you tell what this does $\endgroup$ Dec 16, 2021 at 18:13

1 Answer 1

1
$\begingroup$

Control+Z suspends a job, i.e. it stops it temporarily, and you can restart it by using the commands fg or bg (resume the job in the foreground or background, respectively, see also jobs --help, fg --help, bg --help). Technically, it asks the kernel to send the SIGSTOP signal to the process.

Similarly, Control+C kills the job by sending it the SIGINT signal. This makes it impossible to resume the job. (See also man kill.)

However, your program waits for end of file (EOF). Control+D (the ^D character) is a special control character that the terminal interprets as end of file (or end of transmission). Since your program has a while loop that runs until EOF, sending ^D to the program terminates the while loop.

You can read more at TLDP:Controlling Processes, TLDP:Other Key combinations, TLDP:Appendix E. Exit Codes With Special Meanings.

$\endgroup$
2
  • $\begingroup$ thank you so much for broadening my horizon on this topic.@ Pål GD $\endgroup$ Dec 16, 2021 at 18:38
  • 1
    $\begingroup$ No problem, however, in the future, a better place for these types of questions might be 1. Unix 2. Superuser 3. Stack overflow 4. Software engineering (in no particular order). $\endgroup$
    – Pål GD
    Dec 16, 2021 at 18:42

Not the answer you're looking for? Browse other questions tagged or ask your own question.