how come such code can compile?

L

linq936

Hi,
I am having some hard time in reading a code, to me it should not
compile, but it does!

Here are the code structure,

file1.c

#include "common_herders.h"

main(){
if (func1() != 0){
... some stuff ...
}
}

I think this file should not compile because func1() is not declared
before it is called! The definition of func1() is in another file,
file2.c. The compile command is like this,

gcc -O2 file1.c file2.c -o executable.elf \
-Wl,-T -Wl,LinkScr -g -I./include/ -L./lib/

I checked out that "common_heasers.h", there is no declaration of
func1().

Do you see any reason?
 
J

Jean-Claude Arbaut

To see the answer:

gcc -Wall -W -c file1.c

It should print
"warning: implicit declaration of function 'func1'"
 
M

Morris Dovey

(e-mail address removed) affirmed:

| Hi,
| I am having some hard time in reading a code, to me it should not
| compile, but it does!
|
| Here are the code structure,
|
| file1.c
|
| #include "common_herders.h"
|
| main(){
| if (func1() != 0){
| ... some stuff ...
| }
| }
|
| I think this file should not compile because func1() is not
| declared before it is called! The definition of func1() is in
| another file, file2.c. The compile command is like this,
|
| gcc -O2 file1.c file2.c -o executable.elf \
| -Wl,-T -Wl,LinkScr -g -I./include/ -L./lib/
|
| I checked out that "common_heasers.h", there is no declaration of
| func1().
|
| Do you see any reason?

Sure. With no declaration, the compiler "assumes" that func1() returns
an int and does not require arguments. If you enable the appropriate
warning (sorry, I don't have a gcc reference handy) it'll give you a
diagnostic message.
 
L

linq936

Thanks for your pointing, I do not see the warning.

But I do get a new knowledge, forward declaration is not a hard
requirement. It is good to know this.

Thanks.
 
J

Jean-Claude Arbaut

Thanks for your pointing, I do not see the warning.

You have no warning with -Wall -W ? That looks strange, which version is
your gcc ? Maybe you have a different option to enable all warnings, try
"man gcc" or whatever.
 
L

linq936

Actually func1() does have argument, it is an integer. I did not show
it in my original post.

Because this is C, now C++, so there is no function name overriden,
argument list is not important. This is my guess though.
 
L

Lawrence Kirby

(e-mail address removed) affirmed:

| Hi,
| I am having some hard time in reading a code, to me it should not
| compile, but it does!

A C99 compiler should generate a diagnostic but there is no requirement
that compilers for older versions of C do.
| Here are the code structure,
|
| file1.c
|
| #include "common_herders.h"
|
| main(){
| if (func1() != 0){
| ... some stuff ...
| }
| }
|
| I think this file should not compile because func1() is not
| declared before it is called! The definition of func1() is in
| another file, file2.c. The compile command is like this,

Pre-C99 if no declaration is in scope the compiler acts as if there is an
implicit declaration of the form

extern int func();
|
| gcc -O2 file1.c file2.c -o executable.elf \
| -Wl,-T -Wl,LinkScr -g -I./include/ -L./lib/
|
| I checked out that "common_heasers.h", there is no declaration of
| func1().
|
| Do you see any reason?

Sure. With no declaration, the compiler "assumes" that func1() returns
an int and does not require arguments. If you enable the appropriate
warning (sorry, I don't have a gcc reference handy) it'll give you a
diagnostic message.

No, the *declaration* above does not specify no arguments, it specifies
unknown arguments but no variable argument list. The caller must ensure
that the arguments passed are compatible with the function definition
after the default argument promotions have been applied.

Lawrence
 
D

Default User

Actually func1() does have argument, it is an integer. I did not show
it in my original post.

Because this is C, now C++, so there is no function name overriden,
argument list is not important. This is my guess though.


Please quote a relevant portion of the previous message when replying.
To do so from the Google interface, don't use the Reply at the bottom
of the message. Instead, click "show options" and use the Reply shown
in the expanded headers.



Brian
 
P

Peter Shaggy Haywood

Groovy hepcat Morris Dovey was jivin' on Wed, 22 Jun 2005 19:32:15
-0500 in comp.lang.c.
Re: how come such code can compile?'s a cool scene! Dig it!
(e-mail address removed) affirmed:

| #include "common_herders.h"
|
| main(){
| if (func1() != 0){
| ... some stuff ...
| }
| }
|
| I think this file should not compile because func1() is not
| declared before it is called! The definition of func1() is in
| another file, file2.c. The compile command is like this,
|
| gcc -O2 file1.c file2.c -o executable.elf \
| -Wl,-T -Wl,LinkScr -g -I./include/ -L./lib/
|
| I checked out that "common_heasers.h", there is no declaration of
| func1().

Sure. With no declaration, the compiler "assumes" that func1() returns
an int and does not require arguments.

Not quite. Actually it should make no assumptions about what
arguments, if any, the function requires. If the number and types
(after default argument promotion) of arguments differ from those of
the function's formal parameters, the behaviour is undefined.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 
O

Old Wolf

Morris said:
(e-mail address removed) affirmed:

| main(){
| if (func1() != 0){
| ... some stuff ...
| }
| }
|
| I think this file should not compile because func1() is not
| declared before it is called! The definition of func1() is in
| another file, file2.c.

Sure. With no declaration, the compiler "assumes" that func1()
returns an int and does not require arguments.

It assumes that func1() returns an int, but makes no assumption
about the arguments.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top