inclusione file

M

mary

Salve!
Credo mi sfugga qualche concetto di base.

Ho 3 file.
1) main.c , in cui richiamo semplicemente la funzione somma e ne
stampo il valore restituito
2) somma.c, in cui definisco la funzione somma
3)somma.h, in cui compare solamente il prototipo della funzione somma

Eseguo:

gcc -c main.c
gcc -c somma.c
gcc -o Somma main.o somma.o
./Somma

Credevo che il primo passo di compilazione del main avrebbe dato
errore se io non avessi incluso nel main stesso l'header con

#include "somma.h"

e invece tutto funziona allo stesso modo, sia in presenza
dell'inclusione che in sua assenza.
Come è possibile?
Grazie.
 
A

Army1987

mary wrote:
[Asks about gcc not emitting warnings for calling a function defined in
another file without including its declaration]
This group is in English, use it.comp.lang.c for Italian.

If a function is called without being declared, at least in C89 [1] it is
assumed that it is a function returning an int, as if you had declared it
with extern int somma(); BTW, gcc only emits all required diagnostics if
called with the -pedantic flag. Using -Wall and -Wextra will give even
more diagnostics.

[1] In 1999 a new version of the C standard was adopted, and "implicit
int" is no longer allowed. But most implementations still use the older
standard.

<ot>
gcc by default uses the 1989 standard plus GNU extensions. Use -ansi to
fully conform (probably modulo bugs, but I'm not aware of any relevant
one) to the old standard, and -std=iso9899:1999 to conform to C99 (but to
date gcc doesn't still implement it completely).
</ot>
 
R

Remo D.

mary ha scritto:
Credevo che il primo passo di compilazione del main avrebbe dato
errore se io non avessi incluso nel main stesso l'header con

#include "somma.h"

e invece tutto funziona allo stesso modo, sia in presenza
dell'inclusione che in sua assenza.
Come è possibile?

Compila con l'opzione -Wall per ricevere tutti i warning. Se non includi
somma.h in main otterrai:
"warning: implicit declaration of function 'somma'
ed il compilatore assumerà come prototipo: "int somma()".

Nel tuo caso, probabilmente, somma restituisce proprio un int ed i
parametri che passi sono "giusti". Il fatto che funzioni è solo
accidentale (ed il compilatore non puo' aiutarti).

Remo.D
 
R

Remo D.

mary ha scritto:
Salve!
Credo mi sfugga qualche concetto di base.
Ops! I just noticed we are on c.l.c! Sorry!

Mary, per risposte in italiano devi postare su it.comp.lang.c, qui si
parla solo in Inglese.

Apologies,
Remo.D
 
J

Joachim Schmitz

Army1987 said:
mary wrote:
[Asks about gcc not emitting warnings for calling a function defined
in another file without including its declaration]
This group is in English
In a similar situation I got slapped for saying that and got told that this
is an international group, but posting in non-english might have a serious
impact on the number of answers...
, use it.comp.lang.c for Italian.

Bye, Jojo
 
C

CBFalconer

Joachim said:
Army1987 said:
mary said:
[Asks about gcc not emitting warnings for calling a function
defined in another file without including its declaration]

This group is in English

In a similar situation I got slapped for saying that and got told
that this is an international group, but posting in non-english
might have a serious impact on the number of answers...

Precisely, except replace 'slapped' with 'informed'.
 
J

Joachim Schmitz

CBFalconer said:
Joachim said:
Army1987 said:
mary wrote:

[Asks about gcc not emitting warnings for calling a function
defined in another file without including its declaration]

This group is in English

In a similar situation I got slapped for saying that and got told
that this is an international group, but posting in non-english
might have a serious impact on the number of answers...

Precisely, except replace 'slapped' with 'informed'.
Well, that wasn't quite what it felt like :cool:

Bye, Jojo
 

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,772
Messages
2,569,591
Members
45,103
Latest member
VinaykumarnNevatia
Top