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;
char c = 0xff;
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.
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.
James said:You need the -pedantic option:
gcc -pedantic -c overchar.c
overchar.c:1: warning: overflow in implicit constant 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.
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.
I was expecting a warning for this assignment, but can't get from GCC, why? isn't it an overflow?
char c = 0xff;
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.
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?
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
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?
Ralph Spitzner said:You're basically setting c to -127, which is no legal offense
You'll get into trouble if you try to _compare_ this value
to something else....
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
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.