No warnings for prototype erros 
Author Message
 No warnings for prototype erros

I've compiled this chunk of code with quite a few compilers, and not
one has reported any warnings, not even where I pass a `double' to a
function taking an `int'.  Are the compilers missing something
important going on, or am I?

-----------------

#include <limits.h>
extern int printf(const char *, ...);

static void foo(int i)
{
    printf("Got     %d\n", i);

Quote:
}

int main() {
    unsigned long l = ULONG_MAX;
    int i           = INT_MAX;
    double d        = 1.0e37;

    printf("Passing %d\n", i);
    foo(i);

    printf("Passing %u\n", l);
    foo(l); /* should get warning */

    printf("Passing %g\n", d);
    foo(d); /* definitely should get warning */

    return 0;

Quote:
}

/*
 * This code compiled, without warnings, on the following systems:
 *     SparcStation, `gcc -Wall'.
 *     HP 710, `c89 -Aa'.
 *     DecStation, `cc'.
 *     RS/6000, `cc'.
 */

--
Ken Cox                                 Viewlogic Systems
Software Guy                            293 Boston Post Rd W

"Do I have to go through the Cross-Bronx Expressway?"



Sun, 28 Jan 1996 07:05:33 GMT  
 No warnings for prototype erros

Quote:
>I've compiled this chunk of code with quite a few compilers, and not
>one has reported any warnings, not even where I pass a `double' to a
>function taking an `int'.  Are the compilers missing something
>important going on, or am I?

....

Quote:
>static void foo(int i)
>{
>    printf("Got     %d\n", i);
>}
>int main() {

....

Quote:
>    double d        = 1.0e37;
>    printf("Passing %g\n", d);
>    foo(d); /* definitely should get warning */

Given the declaration for foo the argument in a call of foo is
(silently) converted to an int if it has an arithmetic type. So in the
above case the compiler shouldn't issue any warnings.

----------
Mikael Nygaard
Aarhus University
Denmark



Sun, 28 Jan 1996 18:28:23 GMT  
 No warnings for prototype erros

Quote:
>I've compiled this chunk of code with quite a few compilers, and not
>one has reported any warnings, not even where I pass a `double' to a
>function taking an `int'.  Are the compilers missing something
>important going on, or am I?

The compiler is happily converting the double to an int in the code you
showed (based on the prototype). If you look at some ANSI C references,
you will see that this is way it is supposed to be done ... <yech> I
would rather get an error than a silent cast, but ... this is just my
opinion.

In the beginning, I too figured that this was not the way I wanted
things done, but I have learned to live with it. On some compilers, you
can turn on increased warning messages, and they will tell you when
casting the value in the function call.

Various lints on unix boxes, and some pc lint products, will also catch
this.

                                                                Z

--
-------------------------------------------------------------------------
| Syed Zaeem Hosain          P. O. Box 610097            (408) 441-7021 |

-------------------------------------------------------------------------



Sun, 28 Jan 1996 16:27:46 GMT  
 No warnings for prototype erros

Quote:
> I've compiled this chunk of code with quite a few compilers, and not
> one has reported any warnings, not even where I pass a `double' to a
> function taking an `int'.  Are the compilers missing something
> important going on, or am I?

You are: prototypes also cause conversions.  When you define a function
with an int parameter and then call it with an unsigned or double
argument, the argument is converted just like it would be in an
assignment statement.  As long as the assignment is valid, there's no
warning.
----
Larry Jones, SDRC, 2000 Eastman Dr., Milford, OH  45150-2789  513-576-2070

Rats.  I can't tell my gum from my Silly Putty. -- Calvin


Tue, 30 Jan 1996 05:46:06 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Help with gcc warnings/function prototype

2. Warning Call to Function w/no Prototype in

3. free as a function argument and compiler warning on prototype

4. (Help), linking erros when changing MFC linking

5. strang compilation erros

6. Warning when no warning due

7. Warning, Danger, Warning

8. Need help with annoying warning: LINK : warning LNK4199: /DELAYLOAD:mtxex.dll ignored

9. Dealing with warnings & warning levels in VC

10. how to suppress non warning warnings

11. warning C4786: STL template warnings

12. VC Warning level 4 and Warning C4244

 

 
Powered by phpBB® Forum Software