Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Why no warning?

Reply
Thread Tools

Why no warning?

 
 
Hill Pang
Guest
Posts: n/a
 
      06-06-2012
I was expecting a warning for this assignment, but can't get from GCC, why? isn't it an overflow?

char c = 0xff;
 
Reply With Quote
 
 
 
 
Noob
Guest
Posts: n/a
 
      06-06-2012
Hill Pang wrote:

> I was expecting a warning for this assignment, but can't get from GCC, why?
> isn't it an overflow?
>
> char c = 0xff;


1) consider the gcc options -fsigned-char and -funsigned-char
and ask yourself: "is char a signed or unsigned type?"

2) consider the gcc option -Wstrict-overflow=n
(-Wall enables -Wstrict-overflow=1)
Note however that "overflow" only makes sense in the context
of signed arithmetic, since the language properly defines the
"wrap-around" semantics for unsigned arithmetic.
 
Reply With Quote
 
 
 
 
Hill Pang
Guest
Posts: n/a
 
      06-06-2012
On Wednesday, June 6, 2012 6:04:09 PM UTC+8, Noob wrote:
> Hill Pang wrote:
>
> > I was expecting a warning for this assignment, but can't get from GCC, why?
> > isn't it an overflow?
> >
> > char c = 0xff;

>
> 1) consider the gcc options -fsigned-char and -funsigned-char
> and ask yourself: "is char a signed or unsigned type?"
>
> 2) consider the gcc option -Wstrict-overflow=n
> (-Wall enables -Wstrict-overflow=1)
> Note however that "overflow" only makes sense in the context
> of signed arithmetic, since the language properly defines the
> "wrap-around" semantics for unsigned arithmetic.


1) I am running the GCC on X86, I am sure by default the char should be signed, and even I add the option -fsigned-char, the result is same.
2) I tried -Wall and -Wstrict-overflow=1, no luck to get the warning.

 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      06-06-2012
On 06/06/2012 06:17 AM, Hill Pang wrote:
> On Wednesday, June 6, 2012 6:04:09 PM UTC+8, Noob wrote:
>> Hill Pang wrote:
>>
>>> I was expecting a warning for this assignment, but can't get from GCC, why?
>>> isn't it an overflow?
>>>
>>> char c = 0xff;

>>
>> 1) consider the gcc options -fsigned-char and -funsigned-char
>> and ask yourself: "is char a signed or unsigned type?"
>>
>> 2) consider the gcc option -Wstrict-overflow=n
>> (-Wall enables -Wstrict-overflow=1)
>> Note however that "overflow" only makes sense in the context
>> of signed arithmetic, since the language properly defines the
>> "wrap-around" semantics for unsigned arithmetic.

>
> 1) I am running the GCC on X86, I am sure by default the char should be signed, and even I add the option -fsigned-char, the result is same.
> 2) I tried -Wall and -Wstrict-overflow=1, no luck to get the warning.


You need the -pedantic option:

gcc -pedantic -c overchar.c
overchar.c:1: warning: overflow in implicit constant conversion
--
James Kuyper
 
Reply With Quote
 
Noob
Guest
Posts: n/a
 
      06-06-2012
James Kuyper wrote:

> You need the -pedantic option:
>
> gcc -pedantic -c overchar.c
> overchar.c:1: warning: overflow in implicit constant conversion


Alternatively, one may consider -Wsign-conversion:
> Warn for implicit conversions that may change the sign of an integer
> value, like assigning a signed integer expression to an unsigned
> integer variable. An explicit cast silences the warning.


Or rather -Wconversion in this specific scenario:
> Warn for implicit conversions that may alter a value. This includes
> conversions between real and integer, like abs (x) when x is double;
> conversions between signed and unsigned, like unsigned ui = -1; and
> conversions to smaller types, like sqrtf (M_PI). Do not warn for
> explicit casts like abs ((int) x) and ui = (unsigned) -1, or if the
> value is not changed by the conversion like in abs (2.0). Warnings
> about conversions between signed and unsigned integers can be
> disabled by using -Wno-sign-conversion.


$ gcc -Wconversion -c warn.c
warn.c: In function 'foo':
warn.c:3:3: warning: conversion to 'char' alters 'int' constant value
 
Reply With Quote
 
Hill Pang
Guest
Posts: n/a
 
      06-06-2012
On Wednesday, June 6, 2012 5:42:13 PM UTC+8, Hill Pang wrote:
> I was expecting a warning for this assignment, but can't get from GCC, why? isn't it an overflow?
>
> char c = 0xff;


Thanks, every one!

Yes, with additional options, the warning shows, but for me I think it is straight to check if a const is in the represent-able range of a variable inan assignment, I think the warning should be popped up by default rather by an extra option. I am wondering if there is some special rulers behind this, is there any such ruler listed in the C standard or some place anywhere?
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      06-06-2012
China Blue Max <(E-Mail Removed)> writes:

> In article <(E-Mail Removed)>,
> Hill Pang <(E-Mail Removed)> wrote:
>
>> I was expecting a warning for this assignment, but can't get from GCC, why?
>> isn't it an overflow?
>>
>> char c = 0xff;

>
> Either char is implicitly unsigned or it used the conversion
> unsigned->signed of the same size. Unless explicitly marked, the
> implementation decides whether chars are signed; one of the default
> conversions is unsigned to signed of the same size.


Unless I've missed what you mean by "default conversions" that's a
little confusing simply by being overly specific. C specifies implicit
conversions between unsigned and signed types of all sizes. Some are
defined entirely by the standard, and others are left up to the
implementation, but there is nothing special about conversion between
signed and unsigned integer types of the same size.

--
Ben.
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      06-06-2012
On 06/06/2012 07:35 AM, Hill Pang wrote:
> On Wednesday, June 6, 2012 5:42:13 PM UTC+8, Hill Pang wrote:
>> I was expecting a warning for this assignment, but can't get from GCC, why? isn't it an overflow?
>>
>> char c = 0xff;

>
> Thanks, every one!
>
> Yes, with additional options, the warning shows, but for me I think it is straight to check if a const is in the represent-able range of a variable in an assignment, I think the warning should be popped up by default rather by an extra option. I am wondering if there is some special rulers behind this, is there any such ruler listed in the C standard or some place anywhere?


The C standard mandates certain diagnostics. gcc does not provide all of
the mandated diagnostics for any version of the C standard (and is
therefore not a conforming implementation of C) without either the -ansi
or the -std= options, plus -pedantic. There's LOTS of additional useful
warnings not mandated by the C standard, and you need to explicitly
request them; -Wall gets most of the most useful ones. I like to add
-Wpointer-arith -Wcast-align -Wstrict-prototypes -Wmissing-prototypes
 
Reply With Quote
 
Willem
Guest
Posts: n/a
 
      06-06-2012
Ben Bacarisse wrote:
) China Blue Max <(E-Mail Removed)> writes:
)
)> In article <(E-Mail Removed)>,
)> Hill Pang <(E-Mail Removed)> wrote:
)>
)>> I was expecting a warning for this assignment, but can't get from GCC, why?
)>> isn't it an overflow?
)>>
)>> char c = 0xff;
)>
)> Either char is implicitly unsigned or it used the conversion
)> unsigned->signed of the same size. Unless explicitly marked, the
)> implementation decides whether chars are signed; one of the default
)> conversions is unsigned to signed of the same size.
)
) Unless I've missed what you mean by "default conversions" that's a
) little confusing simply by being overly specific. C specifies implicit
) conversions between unsigned and signed types of all sizes. Some are
) defined entirely by the standard, and others are left up to the
) implementation, but there is nothing special about conversion between
) signed and unsigned integer types of the same size.

Since when is the literal 0xff an unsigned type?
As far as I can tell, it should be a signed integer, no?


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
Reply With Quote
 
Ralph Spitzner
Guest
Posts: n/a
 
      06-06-2012
Hill Pang wrote:
> I was expecting a warning for this assignment, but can't get from GCC, why? isn't it an overflow?
>
> char c = 0xff;


You're basically setting c to -127, which is no legal offense :-p
You'll get into trouble if you try to _compare_ this value
to something else....

-rasp


--
See why I hate Windows users?
All pain, no gain.
-Howard Chu

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Cisco 2611 and Cisco 1721 : Why , why , why ????? sam@nospam.org Cisco 10 05-01-2005 08:49 AM
Why, why, why??? =?Utf-8?B?VGltOjouLg==?= ASP .Net 6 01-27-2005 03:35 PM
Why Why Why You HAVE NO IDEA MCSE 31 04-24-2004 06:40 PM



Advertisments