473,513 Members | 2,319 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Unexpected Result while compiling this code

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.

Jan 23 '06 #1
16 2354
Ico
Sunil Varma <su*******@gmail.com> wrote:

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)

--
:wq
^X^Cy^K^X^C^C^C^C
Jan 23 '06 #2
Sunil Varma wrote:
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
Jan 23 '06 #3
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
On Sun, 22 Jan 2006 22:50:40 -0800, Sunil Varma wrote:
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.

Jan 23 '06 #4
Sunil Varma <su*******@gmail.com> wrote:
Hello,

Please see the following code.

double foo(double a);
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.


--
Z (zo**********@web.de)
"LISP is worth learning for the profound enlightenment experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days." -- Eric S. Raymond
Jan 23 '06 #5

Red Wagner wrote:
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
On Sun, 22 Jan 2006 22:50:40 -0800, Sunil Varma wrote:
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.


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'

Jan 23 '06 #6
A.A
int main ()
{
double foo(double);

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

double foo (double a)
{
return a;

}

Jan 23 '06 #7
A.A wrote:
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/>
Jan 23 '06 #8
Sunil Varma wrote:

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/>
Jan 23 '06 #9
CBFalconer wrote:
You are not using strict enough warnings.
Compile with -ansi -pedantic -W -Wall flags.


I would suggest he use -Wextra instead of -W

-Wextra
(This option used to be called -W. The older name is still
supported, but the newer name is more descriptive.) Print extra warning
messages for these events:
[snip]

http://gcc.gnu.org/onlinedocs/gcc-3....ml#index-W-229
Jan 23 '06 #10
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

Jan 23 '06 #11
Lemor wrote:
When i compile this program in VC++, <snip> 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

Jan 23 '06 #12
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;
}

--
A+

Emmanuel Delahaye
Jan 23 '06 #13
A.A a écrit :
int main ()
{
double foo(double);


Ugly !
--
A+

Emmanuel Delahaye
Jan 23 '06 #14
"Frodo Baggins" <fr*********@gmail.com> writes:
Lemor wrote:
When i compile this program in VC++,

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


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;".

--
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.
Jan 23 '06 #15
Lemor wrote:
When i compile this program in VC++,


<snip>

What program? Please provide context since people might not have seen
the post you are replying to, and because Usenet propagation is not
perfect they might *never* see it. See http://cfaj.freeshell.org/google/
for details of how to do this through Google.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Jan 23 '06 #16
Frodo Baggins wrote:
Lemor wrote:
When i compile this program in VC++,

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


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.

--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Jan 23 '06 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2873
by: Salim | last post by:
Hi people, keep getting this errorParse error: parse error, unexpected T_STRING in order_fns.php line 91. the code is below for the file and I've indicated line 91 <?php function...
8
1445
by: John J | last post by:
Thankyou to those people in this newsgroup that have answered my questions and helped me to learn whilst developing my yacht race classes. I have now completed the three classes (Yacht, Race and...
3
2846
by: Teddy | last post by:
Hello all According to "Think in C++ Volume2", the code below should run smoothly: #include <iostream> #include <exception> using namespace std; class ex { };
6
19010
by: Ehartwig | last post by:
I recently created a script for user verification, solved my emailing issues, and then re-created the script in order to work well with the new PHP 5 that I installed on my server. After...
5
4475
by: r.nikhilk | last post by:
Hi, Currently, we are porting C++ applications from 32 bit to 64 bit on AIX platform. (The current version of AIX is 5.3 and xlC verison is 8.0). We are able to compile the applications by...
62
3697
by: ashu | last post by:
hi look at this code include <stdio.h> int main(void) { int i,j=2; i=j++ * ++j * j++; printf("%d %d",i,j); return 0;
11
1844
by: Vaibhav87 | last post by:
i write void main() { int j; j - = 0; printf("%d",y); } & i got the answer as 842. i tried it on various computers but the answer is same. pls help me how is this answer is?
13
3573
by: bintom | last post by:
I ran the following simple code in C++ and got unexpected results: float f = 139.4; cout << f; Output: 139.399994;
14
5485
riverdale1567
by: riverdale1567 | last post by:
Hi I am a newbie trying to get some of my first code working, yada yada yada. I have a drop down box which chooses a state then takes the post data to 'processform2.php' to use that to pull up...
0
7269
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7177
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7394
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7542
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5701
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5100
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4756
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3248
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
470
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.