By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,332 Members | 1,427 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,332 IT Pros & Developers. It's quick & easy.

Function return type

P: n/a
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
Jun 27 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a

Christian Christmann wrote:
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

Jun 27 '06 #2

P: n/a

Christian Christmann wrote:
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.

Jun 27 '06 #3

P: n/a
Christian Christmann wrote:
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.
Jun 27 '06 #4

P: n/a
Christian Christmann wrote:

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;}

--
pete
Jun 27 '06 #5

P: n/a
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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jun 27 '06 #6

P: n/a
On Tue, 27 Jun 2006 15:35:59 +0200, Christian Christmann
<pl*****@yahoo.de> wrote in comp.lang.c:
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.

--
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.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jun 27 '06 #7

P: n/a
Richard Heathfield <in*****@invalid.invalid> writes:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 27 '06 #8

P: n/a
inmatarian <wc*****@gmail.com> writes:
Christian Christmann wrote:
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 )


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.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 27 '06 #9

P: n/a
Keith Thompson said:
Richard Heathfield <in*****@invalid.invalid> writes:
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.)
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jun 27 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.