8

Anyone here knows whether this is valid?
This is the default code for a new Visual Studio C++ Console Project.

Comments
  • 2
    Seems valid to me
  • 5
    Seems good to me.
    I "think" the return 0 is optional.

    You could always hit run and see what happens.
  • 2
    @C0D4 @LinusCDE with clang it works.
  • 1
    it runs fine for me.
  • 3
    @stop I'm not a c++ dev, but my limited knowledge is is telling me it's not needed.

    #LMGTFY:
    On most operating systems returning 0 is a success status like saying "The program worked fine". In C++ it is optional to type "return 0;" at the end of the main function and the compiler includes it automatically
  • 2
    Supposedly it will return 0 if no explicit return is used.
  • 1
    What's the default value for an integer? 0 right? So if you didn't initialize the return "variable" what's going to be there. The default value: 0.
  • 4
    @C0D4 It seems to be. Only for the main though. If it's not the main, the compiler gives an error.

    @pk76 Actually, an uninitialized value has the content of whatever was in ram before that variable at the same spot.
    So it's basically random or better: undefined.

    I asked that question, because I forgot to include a return value in an Arduino-IDE and the compiler did random (undefined) stuff in the methods code only when compiled on specific platforms.
  • 1
    @LinusCDE wha, really? Serves me right for assuming behavior in a closely related language is the same. My bad.
  • 2
    @pk76 GCC compiled programs (only tested for C, not C++) will in general not return 0, if not explicitly written.
  • 2
    Define valid. It is in the c++ spec that if you don't explicitly return anything from main(), and the return type is int, that it should return 0. AFAIK all compilers obey this.

    HOWEVER! If you turn on all the switches for GCC, (--pedantic -Wall) it should generate a warning. With -Werror that warning will become an error and refuse to compile.

    Is it valid C++? Absolutely. But it is a warning. And if you play it super safe like me, it shouldn't compile.
  • 3
    @pk76 @sbiewald

    I just tested it with a simple c program using gcc and linux. The value is always 0.

    Possible ideas why:

    - Either that's because you start out with default registers of 0.

    - The OS will clear any newly reserved space that is given to any program to run to increase security.

    - The program got initialized in a still unused area of ram that may be set to zero (idk what whether ram is zeroed or random is after it got trained on post).

    But a longer running program would probably still get undefined values since their registers can be anything and you'll probably reuse your own memory at some point.

    (Using a loop confirms that.)
  • 1
    @LinusCDE Fascinating. I was really sure last year's Ubuntu's GCC made something different from it.
  • 1
    The compiler allows there to be no return statement in a none-void function, BUT the value the function returns is not defined i. e. some compiler make them return zero and for some others it is practically random (on x86 the current value in eax but that's not relevant to you right now).
  • 1
    it is fine.

    You can omit the return on the main function, it then exits with 0
  • 2
    @LinusCDE on X86 the register gets xor'ed with itself as default behaviour, so it becomes 0.

    That makes sense otherwise you'd have an application that could not deterministically exit successfully
  • 1
    Funnily enough i think you can even omit the int return type (in some compilers at least)
  • 2
    @gitreflog C replaces undefined types with int, so yeh
  • 1
    @gitreflog Well I did that accidently in Arduino (variant of gcc).
    It didn't like that at all and created broken code.
  • 1
    @LinusCDE For me i havent found C compilers that rejected it. They usually create warnings so if you have set your build to not allow warnings, that might be the cause. ( https://repl.it/@romangraef/... )
Add Comment