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. 16 2354
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
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
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.
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
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'
int main ()
{
double foo(double);
double da = 0.01, db;
db = foo (da);
exit(0);
}
double foo (double a)
{
return a;
}
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/>
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/>
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
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
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
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
A.A a écrit : int main () { double foo(double);
Ugly !
--
A+
Emmanuel Delahaye
"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.
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.
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
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 {
};
|
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...
|
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...
| |
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;
|
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?
|
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;
|
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...
|
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,...
|
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...
| |
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,...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |
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...
| |