Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8232755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:56:06+00:00 2026-06-07T17:56:06+00:00

Ok so I was messing around on Ideone and accidentally submitted this piece of

  • 0

Ok so I was messing around on Ideone and accidentally submitted this piece of code, however to my surprise it actually compiled and ran outputting a value of 0, here.

#include <iostream>

using namespace std;

const int five(  )
{
        const int i = 5;
}

int main() {
        cout << five(  ) << endl;
        return 0;
}

I then tried this in Visual Studio, and on Codepad however both failed to compile because five() does not return a value, as one would expect. My question, is of course, why does this compile fine on Ideone even though the code, to my understanding is wrong and shouldn’t compile.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T17:56:09+00:00Added an answer on June 7, 2026 at 5:56 pm

    Plain and simply (from C++11 6.6.3 “The return statement”):

    Flowing off the end of a function is equivalent to a return with no value; this results in undefined behavior in a value-returning function.

    So the compiler is pretty much allowed to do whatever it wants. Clearly, a diagnostic is something I’d prefer from a compiler, but there are times when it can be difficult to diagnose (like when the return is inside conditional logic, and the ‘end’ of the function would never be reached).

    Note that I get the following warning with GCC 4.6.1 (using the Wall option):

    test.cpp:8:1: warning: no return statement in function returning non-void [-Wreturn-type]
    

    I’m not sure what options ideone passes to GCC (I imagine that -Wall would do the same with the 4.3.4 version that ideone uses).

    Some related information:

    In C it’s OK for a function that is declared to return a value to not actually do so in certain circumstances; in C it only results in undefined behavior if the function’s return value is actually used. Pre-standard C didn’t always support the void type, so functions that didn’t return anything were often declared to return int, explicitly or implicitly. From C99 6.9.1/12 “Function definitions”: If the } that terminates a function is reached, and the value of the function call is used by the caller, the behavior is undefined.

    Also, as mentioned in a couple comments, flowing off the end of main() is treated specially by C++ and C99 and later.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been messing around with this for hours trying different ways of ordering by
I am currently messing around with some code for an advertising network, i have
I was just messing around with templates, when I tried to do this: template<typename
I was messing around with JavaScript, and noticed that this can never be a
I have been messing around with iTunes COM from python. However, I haven't been
When messing around with Python.h I got this error: AttributeError: 'module' object has no
I have been messing around with HtmlUnit for a little bit and particularly this
I was messing around with std::ostringstream whilst looking at this question: sprintf in c++?
I was messing around with RhinoMocks this morning and couldn't run my tests because
I'm messing around with expression trees, but I'm little stuck. I have this expression:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.