Thread: Huh. int main() acts like char main(). Whoda thunk.

  1. #1
    Registered User
    Join Date
    Jul 2012
    Location
    Michigan U.P.
    Posts
    20

    Huh. int main() acts like char main(). Whoda thunk.

    Hello:


    (Ubuntu 18.04 gcc version 7.5.0)


    Ok... here I thought; "int main()" returned an int. So I tried "return(256)" and bash ($?) reports '0'. "return(255)" reports '255'. Why does main() seem to be limiting to what looks like a CHAR?


    And, equally curious, if I try "char main()", the program compiles w/ warning. Using "return('G')" to bash ($?) reports '71', which is decimal for Capital-G.


    Recompiling back to int main(), and trying "return(512)" reports '0', and "return(513)" reports '1'. Looks like return(whatever_value % 256) is the behavior.


    For my x86_64, using #include <limits.h> reports:


    The minimum value of SHORT INT = -32768
    The maximum value of SHORT INT = 32767
    The minimum value of INT = -2147483648
    The maximum value of INT = 2147483647


    'Ya learn something every day.......
    Thx

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    wait(2): wait for process to change state - Linux man page

    Scroll down to WEXITSTATUS

    Read what it says.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,110
    main() does return an int, but Linux limits the value to the range, 0 - 255, as specified by GNU here.

    Other O/S's might allow a different range.

  4. #4
    Registered User
    Join Date
    Jul 2012
    Location
    Michigan U.P.
    Posts
    20
    Quote Originally Posted by Salem View Post
    wait(2): wait for process to change state - Linux man page

    Scroll down to WEXITSTATUS

    Read what it says.
    Sorry, I have no idea what this means, or how it applies here. Thx.

  5. #5
    Registered User
    Join Date
    Jul 2012
    Location
    Michigan U.P.
    Posts
    20
    Quote Originally Posted by rstanley View Post
    main() does return an int, but Linux limits the value to the range, 0 - 255, as specified by GNU here.

    Other O/S's might allow a different range.
    Well, that's clear enough. I am working on a 'C' tutorial series, and this cropped up unexpectedly. Thx.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 05-08-2014, 10:57 AM
  2. Replies: 3
    Last Post: 06-01-2011, 03:08 AM
  3. Circular main <- main.o dependency dropped.
    By Queatrix in forum C++ Programming
    Replies: 4
    Last Post: 10-21-2005, 02:32 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

Tags for this Thread