title: Better understanding of signal catching (ie. SIGINT/SIGQUIT) in C
body: <p>I wrote this program to catch the <kbd>Ctrl</kbd>-<kbd>C</kbd> and -<kbd>\</kbd> or <code>sigint</code> and <code>sigquit</code> functions, I commented in my understanding of what this program does. Could you correct me if I am wrong and/or maybe explain what is going on so I can have a better understanding?</p>
  
  <pre><code>//
  //  main.c
  //  Project 4
  //
  //  Found help with understanding and coding at
  // http://www.thegeekstuff.com/2012/03/catch-signals-sample-c-code/
  //
  
  #include&lt;stdio.h&gt;
  #include&lt;signal.h&gt;
  #include&lt;unistd.h&gt;
  //signal handling function that will except ctrl-\ and ctrl-c
  void sig_handler(int signo)
  {
      //looks for ctrl-c which has a value of 2
      if (signo == SIGINT)
          printf("\nreceived SIGINT\n");
      //looks for ctrl-\ which has a value of 9
      else if (signo == SIGQUIT)
          printf("\nreceived SIGQUIT\n");
  }
  
  int main(void)
  {
      //these if statement catch errors
      if (signal(SIGINT, sig_handler) == SIG_ERR)
          printf("\ncan't catch SIGINT\n");
      if (signal(SIGQUIT, sig_handler) == SIG_ERR)
          printf("\ncan't catch SIGQUIT\n");
      //Runs the program infinitely so we can continue to input signals
      while(1)
          sleep(1);
      return 0;
  }
  </code></pre>