Forward Declaration

P

Prasoon

My question is :

When the Forward declaration is absolutely necessary ?

(A) If a function returns a non - integer quantity

(B) If the function call precedes its definition

(C) If the function call precedes its definition and the function
returns a non integer quantity

(D) None of the above

As far as I know its correct option should be B. Please let me know if
I am wrong.

Prasoon
 
E

Eric Sosman

Prasoon said:
My question is :

When the Forward declaration is absolutely necessary ?

(A) If a function returns a non - integer quantity

(B) If the function call precedes its definition

(C) If the function call precedes its definition and the function
returns a non integer quantity

(D) None of the above

As far as I know its correct option should be B. Please let me know if
I am wrong.

Under the rules of the current "C99" Standard, (B) is
correct. Under the rules of the earlier "C90" Standard, none
of the answers -- not even (D)! -- is entirely correct.

Best practice: Declare every function before calling it.
(A definition is also a declaration.)
 
O

osmium

Prasoon said:
My question is :

When the Forward declaration is absolutely necessary ?

(A) If a function returns a non - integer quantity

(B) If the function call precedes its definition

(C) If the function call precedes its definition and the function
returns a non integer quantity

(D) None of the above

As far as I know its correct option should be B. Please let me know if
I am wrong.

I think that is a good example of why multiple choice tests have severe
shortcomings.
 
P

Prasoon

The person who asked me this question said the correct option is C,how
could it be correct?

Can you elaborate a bit.

Prasoon
 
K

Keith Thompson

Richard Heathfield said:
In C99, *always* - implicit function declarations were removed from
the language by C99. The rest of this article presumes C90.

I disagree, but my disagreement is only regarding the definition of
"forward declaration".

void foo(void) { /* ... */ }
int main(void) { foo(); return 0; }

I don't call the declaration of foo that's part of the definition of
foo a "forward declaration". I think of a "forward declaration" as
one that precedes, and is separate from, the definition of the same
entity.

Since the C standard doesn't define "forward declaration", no
definitive resolution is possible (unless one of the standards the C
standard refers to happens to define the term sufficiently
rigorously).

[...]
No, in C90, strictly speaking you don't need a forward declaration for
a function call if the function returns int and takes no parameters.
(It may be that I am being overly restrictive and there may be other
cases where you don't need the forward declaration, but I'm too lazy
to go look it up.)

A forward declaration is unnecessary in C90 if the function returns
int, the function isn't variadic, all parameters are of types that are
unchanged by argument promotions, and all arguments in the call
promote to types matching the parameter types.

[...]
 

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

Similar Threads


Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top