Why no warning?

Discussion in 'C Programming' started by Hill Pang, Jun 6, 2012.

  1. Hill Pang

    Hill Pang Guest

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

    char c = 0xff;
     
    Hill Pang, Jun 6, 2012
    #1
    1. Advertising

  2. Hill Pang

    Noob Guest

    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.
     
    Noob, Jun 6, 2012
    #2
    1. Advertising

  3. Hill Pang

    Hill Pang Guest

    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.
     
    Hill Pang, Jun 6, 2012
    #3
  4. Hill Pang

    James Kuyper Guest

    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
     
    James Kuyper, Jun 6, 2012
    #4
  5. Hill Pang

    Noob Guest

    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
     
    Noob, Jun 6, 2012
    #5
  6. Hill Pang

    Hill Pang Guest

    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?
     
    Hill Pang, Jun 6, 2012
    #6
  7. China Blue Max <> writes:

    > In article <>,
    > 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;

    >
    > 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.
     
    Ben Bacarisse, Jun 6, 2012
    #7
  8. Hill Pang

    James Kuyper Guest

    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
     
    James Kuyper, Jun 6, 2012
    #8
  9. Hill Pang

    Willem Guest

    Ben Bacarisse wrote:
    ) China Blue Max <> writes:
    )
    )> In article <>,
    )> 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;
    )>
    )> 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
     
    Willem, Jun 6, 2012
    #9
  10. 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
     
    Ralph Spitzner, Jun 6, 2012
    #10
  11. Hill Pang

    James Kuyper Guest

    On 06/06/2012 10:57 AM, Ralph Spitzner 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;

    >
    > 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.
     
    James Kuyper, Jun 6, 2012
    #11
  12. Willem <> writes:

    > Ben Bacarisse wrote:
    > ) China Blue Max <> writes:
    > )
    > )> In article <>,
    > )> 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;
    > )>
    > )> 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".

    --
    Ben.
     
    Ben Bacarisse, Jun 6, 2012
    #12
  13. Ralph Spitzner <> writes:

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


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

    --
    Ben.
     
    Ben Bacarisse, Jun 6, 2012
    #13
  14. Hill Pang

    Jorgen Grahn Guest

    On Wed, 2012-06-06, James Kuyper wrote:
    > 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


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

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, Jun 7, 2012
    #14
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pete Becker
    Replies:
    0
    Views:
    1,379
    Pete Becker
    Feb 10, 2005
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    911
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,002
    Smokey Grindel
    Dec 2, 2006
  4. B. Williams

    warning C4267 and warning C4996

    B. Williams, Oct 26, 2006, in forum: C++
    Replies:
    17
    Views:
    2,635
  5. WARNING! Prosoftstore.com is a SCAM! WARNING!

    , Jul 8, 2007, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    319
Loading...

Share This Page