default int

D

DevarajA

I've been told that c99 forbids declarations of functions without return
type and defaulting them to return int. So why compiling the following
code with 'gcc a.c -Wall -std=c99' only gives a few warnings?
 
M

Michael Mair

DevarajA said:
I've been told that c99 forbids declarations of functions without return
type and defaulting them to return int.

Implicit int is gone from the language with C99, yes.

So why compiling the following
code with 'gcc a.c -Wall -std=c99' only gives a few warnings?

What code?

If you want to know how to invoke your compiler in C99 mode, I suggest
"man gcc" and -- if that does not help you -- gnu.gcc.help; it is
off-topic round here.

<OT>
The short of it:
gcc -std=c99 -pedantic
will do. If you want to have some more useful warnings, I suggest -O
along with -Wall.
However, gcc is not fully C99 compliant. See
<http://gcc.gnu.org/c99status.html>
for more information. There are other issues on certain target
platforms.
</OT>


Cheers
Michael
 
S

Simon Biber

DevarajA said:
I've been told that c99 forbids declarations of functions without return
type and defaulting them to return int.

Previous versions of C allowed you to leave out the return type of a
function. If you did so, it would assume that you meant to make the
function return int. This feature was called "implicit int", not
"default int".

C99 removed the "implicit int" feature, making it a "constraint
violation" to leave out the return type when declaring or defining a
function. As you should know, constraint violations must be detected by
the compiler, and at least one "diagnostic message" must be output. A
constraint violation does not mean that the compiler must refuse to
translate the program.

The C standard does not talk of "warnings" or "errors", but only of
"diagnostics". Both warnings and errors are types of diagnostics. If you
get a warning from your compiler, that is your diagnostic. A warning IS
a diagnostic message just as much as an error is. It's just that the
compiler writers decided that they could continue translation in that case.
So why compiling the following
code with 'gcc a.c -Wall -std=c99' only gives a few warnings?

You left out the code and the warnings, but I can imagine it says
something similar to this:

[sbiber@eagle c]$ cat a.c
main(){}
[sbiber@eagle c]$ gcc a.c -Wall -std=c99
a.c:1: warning: return type defaults to `int'

This compiler is behaving correctly. It diagnosed the constraint
violation by outputting a warning. End of story.
 
K

Keith Thompson

DevarajA said:
Michael Mair ha scritto:

Sorry, forgot it :)

You should show us what warnings you got in addition to the code. Not
everyone has gcc; not everyone who has gcc has the same version you
have.

Here's what I get:

% gcc a.c -Wall -std=c99
a.c:1: warning: return type defaults to `int'

The standard requires a diagnostic. It doesn't require the compiler
to reject the program. It doesn't even require the diagnostic to be
meaningful. gcc's behavior is conforming.
 
D

DevarajA

Keith Thompson ha scritto:
You should show us what warnings you got in addition to the code. Not
everyone has gcc; not everyone who has gcc has the same version you
have.

Here's what I get:

% gcc a.c -Wall -std=c99
a.c:1: warning: return type defaults to `int'

This is exactly the warning I get.
 
K

Kenneth Brody

DevarajA wrote:
[...]
I've been told that c99 forbids declarations of functions without
return type and defaulting them to return int. [...]
So why compiling the following code with 'gcc a.c -Wall -std=c99' only
gives a few warnings? [...]
main(){}
[...]
Here's what I get:

% gcc a.c -Wall -std=c99
a.c:1: warning: return type defaults to `int'

This is exactly the warning I get.

So what's the problem? You didn't specify "int" as the return type,
and it gave a warning about it, as you expected. What other warnings
did you expect, and why? (How many errors/warning should a program of
9 characters [including the newline] with no syntax errors generate?)

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:[email protected]>
 

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
474,169
Messages
2,570,919
Members
47,459
Latest member
Vida00R129

Latest Threads

Top