Why no warning?

H

Hill Pang

I was expecting a warning for this assignment, but can't get from GCC, why? isn't it an overflow?

char c = 0xff;
 
N

Noob

Hill said:
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.
 
H

Hill Pang

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

James Kuyper

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
 
N

Noob

James said:
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
 
H

Hill Pang

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?
 
B

Ben Bacarisse

China Blue Max said:
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.
 
J

James Kuyper

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
 
W

Willem

Ben Bacarisse wrote:
)
)> In article <[email protected]>,
)>
)>> 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
 
R

Ralph Spitzner

Hill said:
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
 
J

James Kuyper

You're basically setting c to -127, which is no legal offense :p

He thought, for reasonable (but incorrect) reasons, that he was setting
c to 255. When there's a possible difference between common reasonable
(but incorrect) expectations and the way a compiler actually works, a
sufficiently good compiler should warn you, and gcc does do so. It just
needs the right options turned on to do so.
 
B

Ben Bacarisse

Willem said:
Ben Bacarisse wrote:
)
)> In article <[email protected]>,
)>
)>> 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?

Yes, I hope I did not imply otherwise. I should have snipped the
example since it was not what I was talking about. I just wanted to
point out that there is nothing special about conversions between
"unsigned to signed of the same size".
 
B

Ben Bacarisse

Ralph Spitzner said:
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....

Do you really think the OP has a machine that uses sign-and-magnitude
representation and has a signed char type that uses it? :) (C on
machines with odd representations of negative numbers often have and
unsigned plain char type).
 
J

Jorgen Grahn

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

Another data point: my Makefiles almost always say
CFLAGS=-Wall -Wextra -pedantic -std=c99 -g -O3
(with some variations on the -O level).

/Jorgen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top