Thread: To exit using return 1

  1. #1
    Registered User
    Join Date
    Feb 2016
    Posts
    20

    Thumbs up To exit using return 1

    hi
    my nephew encouraged me to take some c lessons and here I am with my questions his way and also with my first steps in learning the c, I have now this program which prints a name several times on screen. I use return 0 and 1. I'd like to hear some suggestions how to make better code, or increase the strength of my tiny software.

    Code:
    #include <stdio.h>
    
    int main (int argc, char *argv[]) {
        
        if (argc == 3) {
            
            int count;
            
            if (sscanf (argv[1], "%i", &count) != 1) {
                printf ("Error: not an integer!\n"); 
                return 1;
            } else {
                while (count > 0) {
                    printf("Hello %s!\n", argv[2]);
                    count--;
                }
            }    
        } else {
            printf("\t%s [number] [name]\n", argv[0]);
        }
            
        return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,849
    Looks good. My comments:
    • Using return 0; is fine, but if you want to use return 1, then it should be because you say, anticipate running this via a shell script where you specifically want to to inspect error return values. If not, then you should #include <stdlib.h> and return EXIT_FAILURE (and correspondingly change return 0 to return EXIT_SUCCESS).
    • If you do want to return non-zero from the main function, then it makes sense that the error output should go to stderr instead of stdout. This can be useful, e.g., when a shell script that invokes your program wants to pipe error output to an error log.
    • Instead of a while loop, perhaps a for loop is more appropriate since you are decrementing a counter.
    • Your use of if statements with else blocks is fine. However, sometimes, especially when the nesting level gets deep because of validation, a somewhat different style can be used that returns early when validation fails, e.g.,
      Code:
      #include <stdio.h>
      #include <stdlib.h>
      
      int main(int argc, char *argv[]) {
          int count;
      
          if (argc != 3) {
              printf("\t%s [number] [name]\n", argv[0]);
              return EXIT_SUCCESS;
          }
      
          if (sscanf(argv[1], "%i", &count) != 1) {
              fprintf(stderr, "Error: not an integer!\n");
              return EXIT_FAILURE;
          }
      
          for (; count > 0; count--) {
              printf("Hello %s!\n", argv[2]);
          }
      
          return EXIT_SUCCESS;
      }
      If you are compiling with respect to C99 or later, move the declaration of count to just before the sscanf call, like in the code you posted, following the principle that variables should be declared near first use.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2016
    Posts
    20
    dear laserlight,
    thank you! I see you are introducing new things to me, and I'm very glad with that.
    I think I've found another approach. I will start with a "game" that emerges as I learn more C.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. return or exit
    By ch4 in forum C Programming
    Replies: 4
    Last Post: 02-21-2009, 12:12 PM
  2. exit() vs return()
    By kryptkat in forum C Programming
    Replies: 9
    Last Post: 10-21-2006, 11:26 AM
  3. Exit with Return
    By Aidman in forum Linux Programming
    Replies: 6
    Last Post: 12-28-2005, 10:17 AM
  4. Return / Exit Issue
    By Rhodium in forum C Programming
    Replies: 4
    Last Post: 12-06-2002, 10:29 PM
  5. main() return values and exit()
    By sean in forum C Programming
    Replies: 4
    Last Post: 06-12-2002, 02:11 PM