Function return type

  • Thread starter Christian Christmann
  • Start date
C

Christian Christmann

Hi,

do I have to provide the return type when a function
is declared in ANSI C-99?

Or would

main ( void ) { ... }

be enough?

Gcc does not complain and I could not find any
corresponding paragraphs in the standard ISO/IEC 9899:1999.

Regards,
Christian
 
T

Tom St Denis

Christian said:
Hi,

do I have to provide the return type when a function
is declared in ANSI C-99?

tom@bigbox ~ $ cat test.c
test(void) { 1; }
tom@bigbox ~ $ gcc --std=c99 -pedantic -Wall -W -O3 -c test.c
test.c:1: warning: return type defaults to 'int'
test.c: In function 'test':
test.c:1: warning: statement with no effect
test.c:1: warning: control reaches end of non-void function

You were saying about not getting warnings?

Do yourself a favour, put return types on all of your functions.

Tom
 
V

Vladimir Oka

Christian said:
Hi,

do I have to provide the return type when a function
is declared in ANSI C-99?

Or would

main ( void ) { ... }

be enough?

Gcc does not complain and I could not find any
corresponding paragraphs in the standard ISO/IEC 9899:1999.

I think that this "feature" is "deprecated" in C99. Even if it wasn't
it's always been a good idea to spell out what you mean.
 
I

inmatarian

Christian said:
Hi,

do I have to provide the return type when a function
is declared in ANSI C-99?

Or would

main ( void ) { ... }

be enough?

Gcc does not complain and I could not find any
corresponding paragraphs in the standard ISO/IEC 9899:1999.

Regards,
Christian

I think it's os and compiler dependant on what the entry point's type
and parameter set should be, but to be on the safe side, I always go with:

int main( int argc, char ** argv )

Inmatarian.
 
P

pete

Christian said:
Hi,

do I have to provide the return type when a function
is declared in ANSI C-99?

Yes.

C89 defines: main(){return 0;}
C99 defines: int main(){}

Both versions of the language say
that empty parameter lists are obsolescent.

Best is: int main(void){return 0;}
 
R

Richard Heathfield

Christian Christmann said:
Hi,

do I have to provide the return type when a function
is declared in ANSI C-99?
Yes.

Or would

main ( void ) { ... }

be enough?

No. C99 removes implicit int from the language.
Gcc does not complain and I could not find any
corresponding paragraphs in the standard ISO/IEC 9899:1999.

gcc is not C99-conforming.

The reason you can't find anything about implicit int in the Standard is
that you're looking for something that isn't there. The closest you'll get
to finding it is the text "remove implicit int" in the list of changes.
 
J

Jack Klein

Hi,

do I have to provide the return type when a function
is declared in ANSI C-99?

Or would

main ( void ) { ... }

be enough?

Gcc does not complain and I could not find any
corresponding paragraphs in the standard ISO/IEC 9899:1999.

Actually, although some of the others could not find it, it is
specifically spelled out in C99.

The first sentence of paragraph 2 of 6.7.2 Type specifiers states
this, in a constraints clause:

"At least one type specifier shall be given in the declaration
specifiers in each declaration, and in the specifier-qualifier list in
each struct declaration and type name."

This sentence explicitly removes "implicit int" from C.

Since the definition of a function or object is also a declaration, it
is subject to the same constraints. A constraint violation requires a
diagnostic.

Perhaps you are not invoking your version of gcc in C99 conforming
mode, or that is one of the C99 features not in your version.
 
K

Keith Thompson

Richard Heathfield said:
Christian Christmann said: [...]
Gcc does not complain and I could not find any
corresponding paragraphs in the standard ISO/IEC 9899:1999.

gcc is not C99-conforming.

True, but it does conform to this particular C99 requirement when
invoked with the proper command-line options.
 
K

Keith Thompson

inmatarian said:
I think it's os and compiler dependant on what the entry point's type
and parameter set should be, but to be on the safe side, I always go
with:

int main( int argc, char ** argv )

Any conforming hosted implementation must accept
int main(int argc, char *argv[])
or
int main(void)
or equivalent (your declaration qualifies). Implementations may
define other forms, but there's hardly ever any good reason to use
them.

In a freestanding implementation, the entry point is
implementation-defined; the implementation needn't support "main" at
all. (A freestanding implementation needn't support most of the
standard library either.)
 
R

Richard Heathfield

Keith Thompson said:
Richard Heathfield said:
Christian Christmann said: [...]
Gcc does not complain and I could not find any
corresponding paragraphs in the standard ISO/IEC 9899:1999.

gcc is not C99-conforming.

True, but it does conform to this particular C99 requirement when
invoked with the proper command-line options.

The later versions, maybe. (The version I have here doesn't, because it's
slightly older than dmr, but of course that's my problem. Nevertheless,
those whose code has to compile under earlier versions of gcc need to be
aware of this.)
 

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,184
Messages
2,570,976
Members
47,536
Latest member
MistyLough

Latest Threads

Top