473,387 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Why aren't signed/unsigned type clashes checked?

I just wrote some quick tests and my compiler (gcc) doesn't seem to flag
passing signed arguments to functions expecting unsigned types (and
presumably vice versa). Is there any particular reason why this is not
considered at least something worthy of a warning? I know that some
implementations complain when you don't get the formatting arguments
correct for printf(), but I would think something like this should be
enforced more strictly.

Thanks,

-Clint
Nov 13 '05 #1
10 2205
Clint Olsen wrote:
I just wrote some quick tests and my compiler (gcc) doesn't seem to flag
passing signed arguments to functions expecting unsigned types (and
presumably vice versa). Is there any particular reason why this is not
considered at least something worthy of a warning? I know that some
implementations complain when you don't get the formatting arguments
correct for printf(), but I would think something like this should be
enforced more strictly.


Yup, you're right even with "-Wall -W -std=c99 -pedantic" it doesn't
cause a diagnostic.

I'm going to assume there is a -W flag for that particular warning
[though I don't know what it is off the top of my head].

For the most part that really isn't an "error" since the function is
prototyped the compiler will just cast it to (int) before passing it to
the function.

Just as if it were double and you passed "1" [which is by default an int].

Tom

Nov 13 '05 #2
Clint Olsen wrote:

I just wrote some quick tests and my compiler (gcc) doesn't seem to flag
passing signed arguments to functions expecting unsigned types (and
presumably vice versa). Is there any particular reason why this is not
considered at least something worthy of a warning? I know that some
implementations complain when you don't get the formatting arguments
correct for printf(), but I would think something like this should be
enforced more strictly.


First off, it's perfectly legal C. Function prototypes
don't require that you supply an expression whose type matches
the argument, instead, they cause the supplied value to be
converted to the argument type. You'll get a diagnostic for
impossible conversions (e.g., sqrt("two")), but sqrt(2) and
sqrt(2.0) are effectively identical.

There's a hint in Henry Spencer's "Ten Commandments for C
Programmers" that there may have been debate when prototypes
were being invented about whether they should behave this way.
That is, there may have been a faction that wanted sqrt(2) to
generate an error instead of generating a conversion. If there
was such a debate (I wasn't there), it must have been resolved
the other way, possibly on grounds of convenience and possibly
to avoid breaking old programs.

The convenience and potential breakage would probably have
been large, too. For example, here's a piece of innocent-looking
code that would break if prototypes were "enforceable:"

char buff[20];
memset (buff, 0, 20);

.... because `20' is an `int', and the third argument to memset()
must be a `size_t'. Now, it's easy to argue (and I'd agree)
that `sizeof buff' is a far better way to spell `20', but it's
not always that easy:

right_justify(string, length)
char *string;
int length;
{
int datalen = strlen(string);
memmove (string + length - datalen, string, datalen + 1);
memset (string, ' ', length - datalen);
}

Again, superior practice would use `size_t' values instead of `int',
but `size_t' didn't even exist when these debates (may have) taken
place; masses and masses of pre-existing code used `int' to count
things, and the acceptance of the then-nascent Standard would have
been adversely affected if all those masses of code suddenly stopped
compiling. "For no good reason," at that. As the Rationale puts it:

Existing code is important, existing implementations
are not. A large body of C code exists of considerable
commercial value. Every attempt has been made to ensure
that the bulk of this code will be acceptable to any
implementation conforming to the Standard. The C89
Committee did not want to force most programmers to
modify their C programs just to have them accepted by
a conforming translator.

Of course, a decade and a half have elapsed since prototypes
came on the scene, and the landscape may have changed -- Y2K
alone may have put quite a lot of older code to rest. A practice
that was virtually universal in Olden Days may now have become
rare enough to warrant the "suspicious" label today. (On the
other hand, it may have become more common: sqrt(2) is all right
now, but was an error before prototypes came along.) If you have
the patience, try using gcc with the "-Wconversion" option to
compile some large body of existing code; the big question is
whether the resulting diagnostics will be useful or just noisy.

--
Er*********@sun.com
Nov 13 '05 #3
In article <3F***************@sun.com>, Eric Sosman wrote:

First off, it's perfectly legal C. Function prototypes
don't require that you supply an expression whose type matches
the argument, instead, they cause the supplied value to be
converted to the argument type. You'll get a diagnostic for
impossible conversions (e.g., sqrt("two")), but sqrt(2) and
sqrt(2.0) are effectively identical.

There's a hint in Henry Spencer's "Ten Commandments for C
Programmers" that there may have been debate when prototypes
were being invented about whether they should behave this way.
That is, there may have been a faction that wanted sqrt(2) to
generate an error instead of generating a conversion. If there
was such a debate (I wasn't there), it must have been resolved
the other way, possibly on grounds of convenience and possibly
to avoid breaking old programs.


Thanks for the detailed explanation. I would counter to this mindset that
while we don't want to break existing code and allow sensible type
promotions to occur, we should be able to tell the compiler to optionally
flag these as warnings (or info). Gcc has enough goofy warnings already.

-Clint
Nov 13 '05 #4

"Clint Olsen" <cl***@0lsen.net> wrote in message
news:sl*******************@poly.0lsen.net...
I just wrote some quick tests and my compiler (gcc) doesn't seem to flag
passing signed arguments to functions expecting unsigned types (and
presumably vice versa). Is there any particular reason why this is not
considered at least something worthy of a warning? I know that some
implementations complain when you don't get the formatting arguments
correct for printf(), but I would think something like this should be
enforced more strictly.

Thanks,

-Clint


There are other compilers than gcc. CodeWarrior from Metrowerks will flag
it.
I'm sure some lint programs will flag it as well.

Carsten Hansen
Nov 13 '05 #5
Clint Olsen wrote:

Thanks for the detailed explanation. I would counter to this mindset that
while we don't want to break existing code and allow sensible type
promotions to occur, we should be able to tell the compiler to optionally
flag these as warnings (or info). Gcc has enough goofy warnings already.


... including "-Wconversion". Is it close enough to
what you're looking for? (Note that it is *not* among those
enabled by "-W -Wall".)

--
Er*********@sun.com
Nov 13 '05 #6
In article <3F***************@sun.com>, Eric Sosman wrote:

... including "-Wconversion". Is it close enough to what you're looking
for? (Note that it is *not* among those enabled by "-W -Wall".)


Ahh yes, just what the doctor ordered. I can see why they don't
necessarily turn this on by default. It gets damn chatty even with legal
code...

-Clint
Nov 13 '05 #7
In article <yr*********************@bgtnsc05-news.ops.worldnet.att.net>,
Carsten Hansen wrote:

There are other compilers than gcc. CodeWarrior from Metrowerks will flag
it. I'm sure some lint programs will flag it as well.


I have access to both the Intel Compiler and GCC, and both of those (by
default) behaved as I described. So, it wasn't like I had my head buried
in the proverbial "GCC sand."

-Clint
Nov 13 '05 #8
> I have access to both the Intel Compiler and GCC, and both of those (by
default) behaved as I described. So, it wasn't like I had my head buried
in the proverbial "GCC sand."


The online version of the GCC manual has an Option Index:
http://gcc.gnu.org/onlinedocs/gcc-3....ion-Index.html

As Eric Sosman pointed out, -Wconversion looks promising.

Warn if a prototype causes a type conversion that
is different from what would happen to the same
argument in the absence of a prototype. This includes
conversions of fixed point to floating and vice versa,
and conversions changing the width or signedness of a
fixed point argument except when the same as the
default promotion.

Also, warn if a negative integer constant expression
is implicitly converted to an unsigned type. For example,
warn about the assignment x = -1 if x is unsigned. But
do not warn about explicit casts like (unsigned) -1.

Nov 13 '05 #9
Eric Sosman wrote:
... including "-Wconversion". Is it close enough to
what you're looking for?


-Wconversion is nearly useless for ISO/ANSI C. It's meant to be used
when converting K&R to ISO/ANSI C code. In this case the different
handling of arguments can lead to quite a few surprises in code that
doesn't expect it.

Philipp

--
Philipp Thomas <pt*****@suse.de>
SuSE Linux AG, Deutschherrnstr. 15-19, D-90429 Nuremberg, Germany

Nov 13 '05 #10
In <3F***************@sun.com> Eric Sosman <Er*********@sun.com> writes:
First off, it's perfectly legal C. Function prototypes
don't require that you supply an expression whose type matches
the argument, instead, they cause the supplied value to be
converted to the argument type. You'll get a diagnostic for
impossible conversions (e.g., sqrt("two")), but sqrt(2) and
sqrt(2.0) are effectively identical.

There's a hint in Henry Spencer's "Ten Commandments for C
Programmers" that there may have been debate when prototypes
were being invented about whether they should behave this way.
That is, there may have been a faction that wanted sqrt(2) to
generate an error instead of generating a conversion. If there
was such a debate (I wasn't there), it must have been resolved
the other way, possibly on grounds of convenience and possibly
to avoid breaking old programs.


Actually, the idea is very simple: conversion is performed as if by
assignment. You couldn't outlaw sqrt(2) without equally outlawing
double x = 2. Otherwise, the language would have become a mess.

It's already quite messy in this area, for backward compatibility
with K&R C: "strange" type conversions occur when non-prototyped or
variadic functions are called (integral promotions and floats get
expanded to doubles). People who never used pre-ANSI C must be quite
disgusted when they learn these rules (and very motivated to avoid
calling unprototyped functions, even if they have to live with variadic
functions). Making another set of more or less arbitrary rules for
prototyped functions would have been unacceptable, while conversion as
if by assignment is a very natural solution.

Requiring perfect matches would have simply resulted in plenty of casts
in the function calls, whenever the argument type didn't match the
parameter type, which happens quite often in practice.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #11

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

Similar topics

19
by: MiniDisc_2k2 | last post by:
Okay, here's a question about the standard. What does it say about unsigned/signed mismatches in a comparison statement: char a = 3; unsigned char b = 255; if (a<b) Now what's the real...
3
by: Siemel Naran | last post by:
Hi. Is there a way to convert the type signed int to the type unsigned int, char to unsigned char, signed char to unsigned char, and so on for all the fundamental integer types? Something like ...
8
by: Rade | last post by:
Following a discussion on another thread here... I have tried to understand what is actually standardized in C++ regarding the representing of integers (signed and unsigned) and their conversions....
9
by: Fred Ma | last post by:
Hello, I've been trying to clear up a confusion about integer promotions during expression evaluation. I've checked the C FAQ and C++ FAQ (they are different languages, but I was hoping one...
9
by: dam_fool_2003 | last post by:
For int data type the default range starts from signed to unsigned. If we don't want negative value we can force an unsigned value. The same goes for long also. But I don't understand why we have...
10
by: tinesan | last post by:
Hello fellow C programmers, I'm just learning to program with C, and I'm wondering what the difference between signed and unsigned char is. To me there seems to be no difference, and the...
10
by: =?iso-8859-2?B?SmFuIFJpbmdvuQ==?= | last post by:
Hello everybody, this is my first post to a newsgroup at all. I would like to get some feedback on one proposal I am thinking about: --- begin of proposal --- Proposal to add...
7
by: somenath | last post by:
Hi All, I am trying to undestand "Type Conversions" from K&R book.I am not able to understand the bellow mentioned text "Conversion rules are more complicated when unsigned operands are...
6
by: Kislay | last post by:
Consider the following code snippet unsigned int i=10; int j= - 2; // minus 2 if(i>j) cout<<"i is greater"; else cout<<"j is greater"; Since i is unsigned , j is greater . I know why , but...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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,...

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.