missing return statement on non void functions; just a warning?

M

macracan

I'm wondering what was the rationale that caused gcc to only have a
warning (disabled by default!!) to signal the following:

int a()
{}; // intentionally no statements

int main()
{
printf("%d", a());
return 0;
}

Function a() is declared as returning an int and the compiler doesn't
treat as an error the missing return statement.
I'm asking in this forum because I suspect that gcc crowd will just
tell me to read the standard. If I'm in the wrong forum, I hope to be
corrected.

Thank you,
Adrian
 
I

Ian Collins

macracan said:
I'm wondering what was the rationale that caused gcc to only have a
warning (disabled by default!!) to signal the following:

int a()
{}; // intentionally no statements

int main()
{
printf("%d", a());
return 0;
}

Function a() is declared as returning an int and the compiler doesn't
treat as an error the missing return statement.
I'm asking in this forum because I suspect that gcc crowd will just
tell me to read the standard. If I'm in the wrong forum, I hope to be
corrected.
You must have compiled it as C, it certainly won't compile as C++.
 
A

Alf P. Steinbach

* macracan:
I'm wondering what was the rationale that caused gcc to only have a
warning (disabled by default!!) to signal the following:

int a()
{}; // intentionally no statements

int main()
{
printf("%d", a());
return 0;
}

Function a() is declared as returning an int and the compiler doesn't
treat as an error the missing return statement.
I'm asking in this forum because I suspect that gcc crowd will just
tell me to read the standard. If I'm in the wrong forum, I hope to be
corrected.

You're in the group where people will tell you to read the standard... ;-)

When adding the missing #include, and removing erronous semicolon:

<quote>
V:\> for %i in (gnuc.bat) do @type "%~$PATH:i"
@g++ -O -pedantic -std=c++98 -Wall -finput-charset=windows-1252 %*

V:\> gnuc --version | find "g++"
g++ (GCC) 3.4.4 (mingw special)

V:\> gnuc vc_project.cpp
vc_project.cpp: In function `int a()':
vc_project.cpp:4: warning: control reaches end of non-void function

V:\>_
</quote>

Note the elegance and sheer genius-like simplicity of Windows' default
command interpreter! :)

But anyway, as a general rule, up the warning level to max, with any
compiler. With g++ note that optimization is necessary for some
warnings (I think e.g. use of uninitialized). Otherwise the machinery
that detects those bad things won't be involved and so no warnings.

It might also be necessary to turn on specific warnings.


Cheers, & hth.,

- Alf
 
J

Jack Klein

I'm wondering what was the rationale that caused gcc to only have a
warning (disabled by default!!) to signal the following:

Despite what you say below, the proper place to ask is someplace where
the gcc maintainers are active.

int a()
{}; // intentionally no statements

int main()
{
printf("%d", a());
return 0;
}

Function a() is declared as returning an int and the compiler doesn't
treat as an error the missing return statement.

As someone else already said, you would probably get different
behavior if you compiled this as C++ as well as C. This is not a
language violation in C, it merely generates undefined behavior if any
caller of the function attempts to use the non-existant return value.
I'm asking in this forum because I suspect that gcc crowd will just
tell me to read the standard. If I'm in the wrong forum, I hope to be
corrected.

It is not a language issue. Neither the C++ nor C standards define
anything like "warning" or "error". Certain syntax errors, constraint
violations, and other violations of the language rules require that
the compiler produce a diagnostic. In addition, a compiler is allowed
to issue any additional diagnostics its developers see fit.

The form and content of the diagnostic, and whether an implementation
chooses to label some diagnostics "error" and others as "warning", is
entirely a QOI (Quality Of Implementation) issue.

The fact that your compiler issued any message at all meets the
diagnostic requirement in C++, and completely exceeds it in C.

If you don't agree with the classification of "warning" versus
"error", that is entirely a matter for the compiler implementers.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top