question regarding main()

This is a discussion on question regarding main() within the C++ Programming forums, part of the General Programming Boards category; pardon the amature question but i cant get a answer to my question anywhere i know the in a main() ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    7

    question regarding main()

    pardon the amature question but i cant get a answer to my question anywhere

    i know the in a main() is followed by a
    return 0; to indicate sucess
    and return 1; to indicate a logic error or used as a break

    my question is, why is 0 used to indicate success? isnt it returning false?
    or am i confusing boolean with an int, different types

    p.s dont shout at me please, every guide i read simply says return 0; means a program is working, nothing in depth.

    thanks in advance

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >return 0; to indicate sucess
    Yes, or you can omit that statement because C++ automatically returns 0 from main.

    >and return 1; to indicate a logic error or used as a break
    Non-zero indicates an error. What error is implementation dependent. The cstdlib header defines EXIT_FAILURE as a portable failure code. It's much better to use that than 1 unless 1 has some special significance for your application.

    >my question is, why is 0 used to indicate success? isnt it returning false?
    In this case 0 is success and non-zero is failure. This gives you a wide range of failure codes to choose from. If the return value were treated as a strict "failed or succeeded" boolean value then 0 wouldn't make sense because it's viewed as false. However, that's not how the return value is interpreted, so you shouldn't think of 0 as false.
    My best code is written with the delete key.

  3. #3
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,236
    Anyway as Prelude noticed it depends on the executer or invoker of your app to analyze the returning value.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    7
    so basically the return value 0/success , 1/failure,error is different than the boolean use of using a 0 value to represent false and 1 to represent truth?

    i believe i understand, correct me if i am wrong. return types have different meanings depending on there type

    int main() 1/0 // not the same as the below.

    boolean 1/truth 0/false

    cheers

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >so basically the return value 0/success , 1/failure,error is different than the boolean use of
    >using a 0 value to represent false and 1 to represent truth?
    Yes, it's very different. The problem here seems to be that you're looking at explicit values. 0 means success and 1 means failure, which is correct, to an extent. It's more accurate to say that 0 means success, and non-zero means failure. You can use any number of non-zero values to signal different failures, and that exceeds the limitations of boolean values.
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Return types mean whatever you want them to mean. It is only convention to use 0 to mean success for main(), but you can do whatever you want for your application.

    For example, maybe your application returns the number of subdirectories of the current directory (including the current one). In that case, a 0 would make sense as failure because there should always be at least one in the result.

    Other times, you could have different error codes for different types of errors. For example, perhaps 1 means invalid command line arguments, 2 means file not found, etc. In that case it makes more sense to have 0 be success. This is the convention for main().

    >> int main() 1/0 // not the same as the below.
    That is the convention.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    The reasoning behind using zero for success has much to do with failure modes.
    Most function have only one way to succeed, but may have many ways to fail.

    In most assembly languages, comparing agsinst zero is faster than against anything else as it may require fewer or just shorter instructions. Zero itself is only a single number, and we only have one way to succeed, so why not use zero for success, and then all those non-zero possibilities can represent the many ways our function can fail. Genius!

    You'll find a lot of things out there that follow this approach. Some of them use <0 as failure, =0 as success, and >0 as "success but with extra information" (which is uncommon). e.g. COM/DCOM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 01:14 PM
  2. main and return question
    By BuzzBuzz in forum C Programming
    Replies: 9
    Last Post: 02-25-2009, 03:38 AM
  3. void main question..
    By transgalactic2 in forum C Programming
    Replies: 22
    Last Post: 11-19-2008, 11:59 AM
  4. C99 and int main()
    By cwr in forum C Programming
    Replies: 8
    Last Post: 09-19-2005, 07:54 AM
  5. int main (), main (), main (void), int main (void) HELP!!!
    By SmokingMonkey in forum C++ Programming
    Replies: 7
    Last Post: 05-31-2003, 10:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21