Unexpected Result while compiling this code

S

Sunil Varma

Hello,

Please see the following code.

int main ()
{
double da = 0.01, db;
db = foo (da);
}
double foo (double a)
{
return a;
}

when i compile this code using gcc i'm getting type-mismatch warnings
but not compilation error.

Please help me in finding out what is going on.

Thanks in advance.
 
I

Ico

Sunil Varma said:
int main ()
{
double da = 0.01, db;
db = foo (da);
}
double foo (double a)
{
return a;
}

when i compile this code using gcc i'm getting type-mismatch warnings
but not compilation error.

Your code calls foo() before it is declared. Move the function above
main(), or declare a proto before main :

double foo (double a);

If you call a function before the compiler knows its definition, it will
assume the function returns an int. Later on, when it actually sees the
definition for the first time, it will tell you about the type-mismatch,
because the initial assumption (foo() returning int) is inconsistent
with the actual definition (foo() returning double)
 
V

Vladimir S. Oka

Sunil said:
Hello,

Please see the following code.

double foo(double);
int main ()
{
double da = 0.01, db;
db = foo (da);
}
double foo (double a)
{
return a;
}

when i compile this code using gcc i'm getting type-mismatch
warnings but not compilation error.

Please help me in finding out what is going on.

Thanks in advance.

Without the line I have added above (forward declaration of
foo), the compiler does not know about foo() the first time it
sees it, and, as the standard requires, assumes it returns an
int, hence your warning.

Cheers

Vladimir
 
R

Red Wagner

Which warnings are you seeing? When I tried to compile that here (gcc
4.0.2) it _did_ give me an error with a reference to function foo, as
replicated here:

$ gcc test.c
test.c:8: error: conflicting types for 'foo' test.c:4: error: previous
implicit declaration of 'foo' was here

This is because function foo() had not been defined before it was used,
adding the function prototype :

double foo (double a);

at the top of your code cause the code to compile cleanly.

-Red
 
S

Sunil Varma

Red said:
Which warnings are you seeing? When I tried to compile that here (gcc
4.0.2) it _did_ give me an error with a reference to function foo, as
replicated here:

$ gcc test.c
test.c:8: error: conflicting types for 'foo' test.c:4: error: previous
implicit declaration of 'foo' was here

This is because function foo() had not been defined before it was used,
adding the function prototype :

double foo (double a);

at the top of your code cause the code to compile cleanly.

-Red

Thanks for the reply.

These are the warning messages i got when i compiled on gcc 3.2.2

[sunilkumar@deiserver sunilkumar]$ gcc c1.c -o c1.out
c1.c:10: warning: type mismatch with previous implicit declaration
c1.c:6: warning: previous implicit declaration of `foo'
c1.c:10: warning: `foo' was previously implicitly declared to return
`int'
 
A

A.A

int main ()
{
double foo(double);

double da = 0.01, db;
db = foo (da);
exit(0);
}

double foo (double a)
{
return a;

}
 
V

Vladimir S. Oka

A.A said:
int main ()
{
double foo(double);

double da = 0.01, db;
db = foo (da);
exit(0);
}

double foo (double a)
{
return a;

}

Please quote what you're replying to, otherwise many people
won't know what you're talking about. If you're posting from
Google see sig below...

Your example is fine, as long as foo() is going to be used from
within main() only. Finding out why can be a good exercise...

Cheers

Vladimir


--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
 
C

CBFalconer

Sunil said:
Please see the following code.

int main ()
{
double da = 0.01, db;
db = foo (da);
}
double foo (double a)
{
return a;
}

when i compile this code using gcc i'm getting type-mismatch warnings
but not compilation error.

Please help me in finding out what is going on.

You are not using strict enough warnings. Compile with -ansi
-pedantic -W -Wall flags.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
 
L

Lemor

When i compile this program in VC++,
i got error that no prototype given,i.e.
the declaration if the function
double foo(double a)
should be there before the main()

secondly,
int main() should return a variable as it is of int type
and not of void
 
F

Frodo Baggins

Lemor said:
When i compile this program in VC++,
int main() should return a variable as it is of int type
and not of void

int main() means that main() returns an int to the OS when it quits :
0 for no error & implementation-defined value in case of error.

Regards,
Frodo B
 
E

Emmanuel Delahaye

Sunil Varma a écrit :
int main ()
{
double da = 0.01, db;
db = foo (da);
}
double foo (double a)
{
return a;
}

when i compile this code using gcc i'm getting type-mismatch warnings
but not compilation error.

You are breaking the "define before use" implicit rule. Change your
layout for:

static double foo (double a)
{
return a;
}

int main (void)
{
double da = 0.01, db;
db = foo (da);

/* for C90 compatibility */
return 0;
}
 
F

Flash Gordon

Frodo said:
int main() means that main() returns an int to the OS when it quits :
0 for no error & implementation-defined value in case of error.

Actually, there is a standard return value for error, EXIT_FAILURE from
stdlib.h, although precisely what it means and how you can test for it
in the environment are implementation defined. Also, some systems have
lots of implementation defined success values, but they are not portable.
 
K

Keith Thompson

Frodo Baggins said:
int main() means that main() returns an int to the OS when it quits :
0 for no error & implementation-defined value in case of error.

More precisely, returning 0 or EXIT_SUCCESS denotes success, returning
EXIT_FAILURE denotes failure, and anything else is
implementation-defined. (EXIT_SUCCESS and EXIT_FAILURE are defined in
<stdlib.h>.) Or you can call exit() rather than executing a return
statement.

In C99, falling off the end of main() is equivalent to executing a
"return 0;", but IMHO it's a bad idea to depend on it; it's easy
enough to add an explicit "return 0;".
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top