Thread: Bit confused about void main() vs int main()

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    40

    Bit confused about void main() vs int main()

    I am a bit confused about void main() vs int main().

    My book says the void is included when the function returns no value. Can someone explain to me what is meant by "returns no value."

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The simple rule of thumb is to use int main because it is undeniably standard.

    As for "returns no value": do you know what is meant by "a function returns a value"?
    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
    Jan 2013
    Posts
    40
    Quote Originally Posted by laserlight View Post
    The simple rule of thumb is to use int main because it is undeniably standard.

    As for "returns no value": do you know what is meant by "a function returns a value"?
    Isn't it meant just the way the phrase "returns a value" sounds?

  4. #4
    Registered User
    Join Date
    Jan 2013
    Posts
    2
    The keay word void means that the function does not return any value but int means that the function returns an integer value..

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    As in, what do you know about calling and defining functions where return values are concerned? If you don't know anything, then you should re-read the part of your book that talks about it.
    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

  6. #6
    Registered User
    Join Date
    Jan 2013
    Posts
    40
    Quote Originally Posted by laserlight View Post
    As in, what do you know about calling and defining functions where return values are concerned? If you don't know anything, then you should re-read the part of your book that talks about it.
    Basically, from what I understand, using "return" causes the function in the program to stop it's current routine and return back to the beginning of the function.

    Is that incorrect?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, that is incorrect. Control returns to the caller, i.e., the function that called the function.
    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

  8. #8
    Registered User
    Join Date
    Jan 2013
    Posts
    40
    Quote Originally Posted by laserlight View Post
    Yes, that is incorrect. Control returns to the caller, i.e., the function that called the function.
    I think that's what I meant, but phrased poorly.

    So in the following program:
    Code:
    int stringLength (const char string[])
    {
    int count = 0;
    while ( string[count] != '\0' )
    ++count;
    return count;
    }
    int main (void)
    {
    int stringLength (const char string[]);
    const char word1[] = { 'a', 's', 't', 'e', 'r', '\0' };
    const char word2[] = { 'a', 't', '\0' };
    const char word3[] = { 'a', 'w', 'e', '\0' };
    printf ("%i %i %i\n", stringLength (word1),
    stringLength (word2), stringLength (word3));
    return 0;
    }



    the stringLength function returns the value of count for each word array defined. Basically the value of count is stringlength (word).

    So the value that is returned is equal to the function(respective argument)?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes.
    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

  10. #10
    Registered User
    Join Date
    Jan 2013
    Posts
    40
    Quote Originally Posted by laserlight View Post
    Yes.
    Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. int main vs void main
    By Annonymous in forum C Programming
    Replies: 21
    Last Post: 10-21-2011, 01:20 AM
  2. [DEBATE]int main VS void main?
    By sudox in forum C++ Programming
    Replies: 20
    Last Post: 11-26-2010, 03:18 PM
  3. Why do people still use main() or main(void)?
    By Christopher2222 in forum C Programming
    Replies: 18
    Last Post: 06-06-2007, 06:34 PM
  4. A question about void(main) and int(main)
    By edd1986 in forum C Programming
    Replies: 2
    Last Post: 03-05-2005, 03:18 PM
  5. void main(), int main(), argc, argv[]????
    By Jonny M in forum C Programming
    Replies: 3
    Last Post: 03-06-2002, 09:12 AM