Thread: exit(0) doubt

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    28

    exit(0) doubt

    i want to know how to use the exit(0) in the following code...... also the difference between exit(0) and exit(1)... thanks

    Code:
    int check(char *s1)
    {
      int i=0;
      while(*(s1+i) != '\0')
          i++;
      if(i != 15)
        {
            printf("\nLength of string should be equal to 15\n");
            exit(0);
        }
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Calling the exit function ends your program - it's like using "return" in main. What you put in the parentheses is the value that gets returned. So "exit(0)" is like "return 0" in main.

    The actual value you return is called an "exit" code and is used by the operating system to know if your program ran successfully or encountered some error. 0 usually means there was no problem. You can use other numbers to mean different things as you wish - they can be useful if you call your program from another program - the calling program can receive the exit code and act accordingly.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    28
    can exit(0) be used in a local function so that the program ends like wat i 've used in the above code???

    actually the above code returns an error

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    28
    sorry, the above returns the following warning msg on compilation

    warning: incompatible implicit declaration of built-in function ‘exit’

  5. #5
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Have you included #include<stdlib.h>?
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    can exit(0) be used in a local function so that the program ends like wat i 've used in the above code???
    Yes, it can.
    bit∙hub [bit-huhb] n. A source and destination for information.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by phoneix_hallows View Post
    can exit(0) be used in a local function so that the program ends like wat i 've used in the above code???
    Yes. Although usually in this situation you'll do exit(some_other_number) to indicate that your program did not complete its task.

  8. #8
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by phoneix_hallows View Post
    can exit(0) be used in a local function so that the program ends like wat i 've used in the above code???

    actually the above code returns an error
    This might be helpful
    Originally Posted by C Standard, 1999 Edition, Section 7.20.4.3
    Code:
    #include <stdlib.h>
    void exit(int status);The exit function causes normal program termination to occur. If more than one call to the exit function is executed by a program, the behavior is undefined.

    First, all functions registered by the atexit function are called, in the reverse order of their registration, except that a function is called after any previously registered functions that had already been called at the time it was registered. If, during the call to any such function, a call to the longjmp function is made that would terminate the call to the registered function, the behavior is undefined.

    Next, all open streams with unwritten buffered data are flushed, all open streams are closed, and all files created by the tmpfile function are removed.

    Finally, control is returned to the host environment. If the value of status is zero or EXIT_SUCCESS, an implementation-defined form of the status successful termination is returned. If the value of status is EXIT_FAILURE, an implementation-defined form of the status unsuccessful termination is returned. Otherwise the status returned is implementation-defined.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  9. #9
    Registered User
    Join Date
    Aug 2009
    Posts
    28
    thanks for clarifying guys

  10. #10
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. function calling: basic doubt
    By doubty in forum C Programming
    Replies: 10
    Last Post: 06-23-2009, 02:31 AM
  2. Doubt in pointer.
    By shwetha_siddu in forum C Programming
    Replies: 5
    Last Post: 03-21-2009, 01:28 AM
  3. Replies: 4
    Last Post: 12-10-2006, 07:08 PM
  4. Doubt abt Storage!
    By kalamram in forum C Programming
    Replies: 1
    Last Post: 04-21-2006, 05:30 AM
  5. Greatest C++ Doubt
    By vasanth in forum C++ Programming
    Replies: 15
    Last Post: 02-28-2002, 04:41 AM