Annoying compiler warning

Discussion in 'C Programming' started by Bob, Dec 19, 2007.

  1. Bob

    Bob Guest

    Hi,

    I have been trying to use some inventive alternative idioms for infinite
    loops in my code, rather than the same old for(;;) and while(1) - this
    could be a nice amusing Easter-egg for any future maintenance
    programmers!

    One of my ideas was this:

    unsigned u = SHRT_MAX << 2; // misleading initialization - tee hee!
    while(++u>=0)
    {
    // loop
    }

    Unfortunately, the compiler gives a warning for this, which rather
    spoils the fun... does the compiler have to give a diagnostic here, and
    can anyone think of a way of using the same basic idea but without
    generating a warning?
     
    Bob, Dec 19, 2007
    #1
    1. Advertising

  2. Bob

    pete Guest

    Bob wrote:
    >
    > Hi,
    >
    > I have been trying to use some inventive alternative
    > idioms for infinite
    > loops in my code, rather than the same old for(;;) and while(1) - this
    > could be a nice amusing Easter-egg for any future maintenance
    > programmers!
    >
    > One of my ideas was this:
    >
    > unsigned u = SHRT_MAX << 2; // misleading initialization - tee hee!
    > while(++u>=0)
    > {
    > // loop
    > }
    >
    > Unfortunately, the compiler gives a warning for this, which rather
    > spoils the fun...
    > does the compiler have to give a diagnostic here,


    If it did, I could look it up.

    > and can anyone think of a way
    > of using the same basic idea but without generating a warning?


    How to suppress an unidentified warning?
    That's not a bad one.

    unsigned u = SHRT_MAX / 1000;
    while(--u >= 0u)
    {
    // loop
    }

    --
    pete
     
    pete, Dec 19, 2007
    #2
    1. Advertising

  3. Bob

    pete Guest

    Bob wrote:
    >
    > Hi,
    >
    > I have been trying to use some inventive alternative idioms for infinite
    > loops in my code, rather than the same old for(;;) and while(1) - this
    > could be a nice amusing Easter-egg for any future maintenance
    > programmers!
    >
    > One of my ideas was this:
    >
    > unsigned u = SHRT_MAX << 2; // misleading initialization - tee hee!
    > while(++u>=0)
    > {
    > // loop
    > }
    >
    > Unfortunately, the compiler gives a warning for this, which rather
    > spoils the fun...
    > does the compiler have to give a diagnostic here, and
    > can anyone think of a way of using the same basic idea but without
    > generating a warning?


    unsigned u;

    for (u = 1 /* or any odd number */ ; u++ > 0u; ++u)
    {
    // loop
    }

    --
    pete
     
    pete, Dec 19, 2007
    #3
  4. Bob

    John Smith Guest

    Bob wrote:
    > Hi,
    >
    > I have been trying to use some inventive alternative idioms for infinite
    > loops in my code, rather than the same old for(;;) and while(1) - this
    > could be a nice amusing Easter-egg for any future maintenance
    > programmers!
    >
    > One of my ideas was this:
    >
    > unsigned u = SHRT_MAX << 2; // misleading initialization - tee hee!
    > while(++u>=0)
    > {
    > // loop
    > }
    >
    > Unfortunately, the compiler gives a warning for this, which rather
    > spoils the fun... does the compiler have to give a diagnostic here, and
    > can anyone think of a way of using the same basic idea but without
    > generating a warning?
    >


    What message do you get from the compiler ?

    Works for me with #include <limits.h>
     
    John Smith, Dec 19, 2007
    #4
  5. Bob

    James Kuyper Guest

    Richard Heathfield wrote:
    > pete said:
    >
    > <snip>
    >
    >> unsigned u;
    >>
    >> for (u = 1 /* or any odd number */ ; u++ > 0u; ++u)

    >
    > Much better.


    But still capable of triggering a compiler warning from a sufficiently
    clever compiler.
     
    James Kuyper, Dec 19, 2007
    #5
  6. pete said:

    <snip>

    > How to suppress an unidentified warning?
    > That's not a bad one.
    >
    > unsigned u = SHRT_MAX / 1000;
    > while(--u >= 0u)


    I get a warning for that code:

    foo.c:6: warning: comparison of unsigned expression >= 0 is always true

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Dec 19, 2007
    #6
  7. pete said:

    <snip>

    > unsigned u;
    >
    > for (u = 1 /* or any odd number */ ; u++ > 0u; ++u)


    Much better.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Dec 19, 2007
    #7
  8. Bob

    Richard Bos Guest

    Bob <> wrote:

    > I have been trying to use some inventive alternative idioms for infinite
    > loops in my code, rather than the same old for(;;) and while(1) - this
    > could be a nice amusing Easter-egg for any future maintenance
    > programmers!


    May I suggest a different kind of solution for your problem?

    Ok, then I suggest that you _not_ be a pain in the arse for your
    successor.

    Richard
     
    Richard Bos, Dec 19, 2007
    #8
  9. On Dec 19, 12:40 pm, Bob <> wrote:
    > Hi,
    >
    > I have been trying to use some inventive alternative idioms for infinite
    > loops in my code, rather than the same old for(;;) and while(1) - this
    > could be a nice amusing Easter-egg for any future maintenance
    > programmers!
    >
    > One of my ideas was this:
    >
    > unsigned u = SHRT_MAX << 2; // misleading initialization - tee hee!
    > while(++u>=0)
    > {
    > // loop
    >
    > }
    >
    > Unfortunately, the compiler gives a warning for this, which rather
    > spoils the fun... does the compiler have to give a diagnostic here...


    No , the compiler doesn't have to give a warning but
    I would expect from any decent diagnostic tool (which
    includes a compiler with all warnings on) to warn you
    that the while test is always true.
     
    Spiros Bousbouras, Dec 19, 2007
    #9
  10. Bob <> writes:
    > I have been trying to use some inventive alternative idioms for infinite
    > loops in my code, rather than the same old for(;;) and while(1) - this
    > could be a nice amusing Easter-egg for any future maintenance
    > programmers!
    >
    > One of my ideas was this:
    >
    > unsigned u = SHRT_MAX << 2; // misleading initialization - tee hee!
    > while(++u>=0)
    > {
    > // loop
    > }


    If you do this kind of thing in production code, I hope I never have
    to work with you.

    > Unfortunately, the compiler gives a warning for this, which rather
    > spoils the fun...

    [...]

    Good for the compiler. You should pay attention to the warning and
    write clear code -- "for thy creativity is better used in solving
    problems than in creating beautiful new impediments to understanding"
    (Henry Spencer, "The Ten Commandments for C Programmers").

    --
    Keith Thompson (The_Other_Keith) <>
    Looking for software development work in the San Diego area.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Dec 19, 2007
    #10
  11. Bob

    Bob Guest

    On 19 Dec 2007 at 13:04, pete wrote:
    > Bob wrote:
    >>
    >> Hi,
    >>
    >> I have been trying to use some inventive alternative idioms for infinite
    >> loops in my code, rather than the same old for(;;) and while(1) - this
    >> could be a nice amusing Easter-egg for any future maintenance
    >> programmers!
    >>
    >> One of my ideas was this:
    >>
    >> unsigned u = SHRT_MAX << 2; // misleading initialization - tee hee!
    >> while(++u>=0)
    >> {
    >> // loop
    >> }
    >>
    >> Unfortunately, the compiler gives a warning for this, which rather
    >> spoils the fun...
    >> does the compiler have to give a diagnostic here, and
    >> can anyone think of a way of using the same basic idea but without
    >> generating a warning?

    >
    > unsigned u;
    >
    > for (u = 1 /* or any odd number */ ; u++ > 0u; ++u)
    > {
    > // loop
    > }


    Cool! That's a really neat solution, thanks.

    The life of a maintenance programmer is a pretty boring one, and I think
    finding the odd little amusement must make the task more fun!
     
    Bob, Dec 20, 2007
    #11
  12. Bob

    Ian Collins Guest

    James Kuyper wrote:
    > Richard Heathfield wrote:
    >> pete said:
    >>
    >> <snip>
    >>
    >>> unsigned u;
    >>>
    >>> for (u = 1 /* or any odd number */ ; u++ > 0u; ++u)

    >>
    >> Much better.

    >
    > But still capable of triggering a compiler warning from a sufficiently
    > clever compiler.


    How about:

    for( u = 1; u&1; u += 2 )

    --
    Ian Collins.
     
    Ian Collins, Dec 20, 2007
    #12
  13. Bob

    CBFalconer Guest

    pete wrote:
    >

    .... snip ...
    >
    > unsigned u;
    >
    > for (u = 1 /* or any odd number */ ; u++ > 0u; ++u)
    > {
    > // loop
    > }


    More useful snippet:

    puts("Enter answer:"); fflush(stdout);
    for (u = 1; u++; ++u) continue;

    :)

    --
    Merry Christmas, Happy Hanukah, Happy New Year
    Joyeux Noel, Bonne Annee.
    Chuck F (cbfalconer at maineline dot net)
    <http://cbfalconer.home.att.net>



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Dec 20, 2007
    #13
  14. In article <>,
    Ian Collins <> wrote:
    >James Kuyper wrote:


    >> But still capable of triggering a compiler warning from a sufficiently
    >> clever compiler.


    >How about:


    >for( u = 1; u&1; u += 2 )


    Theoretically, UINT_MAX could be even, which would lead to a wrapping
    to 0 at the end of the first pass of the loop. I'm not certain
    it would be allowed with C99, but C89 has fewer restrictions on
    value representation and could (I believe) have a non-binary
    value system.

    Hmmm... let's see... unsigned char is guaranteed not to have any
    trap value, but I don't recall that the same applies to unsigned
    int, so (for example) UINT_MAX could be 2^N-2 and 2^N-1 could be
    a trap value. Might be a bit of a nuisance to convert unsigned
    long to unsigned int if it were that way: UINT_MAX+1 is repeatedly
    subtracted, which in the nice binary case corresponds to just dropping
    leading bits, but if UINT_MAX is 2^N-2 then you effectively
    end up subtracting the higher order bytes from the lower order bytes
    to make the wrapping semantics come out right. Something for the DS9001
    perhaps.
    --
    We regret to announce that sub-millibarn resolution bio-hyperdimensional
    plasmatic space polyimaging has been delayed until the release
    of Windows Vista SP2.
     
    Walter Roberson, Dec 20, 2007
    #14
  15. Walter Roberson said:

    > In article <>,
    > Ian Collins <> wrote:
    >>James Kuyper wrote:

    >
    >>> But still capable of triggering a compiler warning from a sufficiently
    >>> clever compiler.

    >
    >>How about:

    >
    >>for( u = 1; u&1; u += 2 )

    >
    > Theoretically, UINT_MAX could be even,


    How?

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Dec 20, 2007
    #15
  16. Bob

    Flash Gordon Guest

    Bob wrote, On 20/12/07 01:29:
    > On 19 Dec 2007 at 13:04, pete wrote:
    >> Bob wrote:
    >>> Hi,
    >>>
    >>> I have been trying to use some inventive alternative idioms for infinite
    >>> loops in my code, rather than the same old for(;;) and while(1) - this
    >>> could be a nice amusing Easter-egg for any future maintenance
    >>> programmers!
    >>>
    >>> One of my ideas was this:
    >>>
    >>> unsigned u = SHRT_MAX << 2; // misleading initialization - tee hee!
    >>> while(++u>=0)
    >>> {
    >>> // loop
    >>> }
    >>>
    >>> Unfortunately, the compiler gives a warning for this, which rather
    >>> spoils the fun...
    >>> does the compiler have to give a diagnostic here, and
    >>> can anyone think of a way of using the same basic idea but without
    >>> generating a warning?

    >> unsigned u;
    >>
    >> for (u = 1 /* or any odd number */ ; u++ > 0u; ++u)
    >> {
    >> // loop
    >> }

    >
    > Cool! That's a really neat solution, thanks.
    >
    > The life of a maintenance programmer is a pretty boring one, and I think
    > finding the odd little amusement must make the task more fun!


    Having done maintenance work I can tell you that I would take the effort
    to work back through the changes to find out who put that in and then
    recommend to the head of development that they get rid of the person
    responsible and the people who let the code through review.

    If you want to do it for an IOCC entry, fine, if you want to do it for
    your own amusement, fine. If you really are doing it to make the life of
    a maintenance programmer "more fun" then you should not be working on
    software at all, not as a job and not as a contributor to open source
    projects.
    --
    Flash Gordon
     
    Flash Gordon, Dec 20, 2007
    #16
  17. -cnrc.gc.ca (Walter Roberson) writes:

    > Theoretically, UINT_MAX could be even,


    I don't think so.

    > which would lead to a wrapping
    > to 0 at the end of the first pass of the loop. I'm not certain
    > it would be allowed with C99, but C89 has fewer restrictions on
    > value representation and could (I believe) have a non-binary
    > value system.


    No, C89 says: "The representations of integral types shall define
    values by use of a pure binary numeration system."

    > Hmmm... let's see... unsigned char is guaranteed not to have any
    > trap value, but I don't recall that the same applies to unsigned
    > int, so (for example) UINT_MAX could be 2^N-2 and 2^N-1 could be
    > a trap value.


    UINT_MAX is defined to be the maximum value of an object of unsigned
    int type. This type is made up of N value bits whose values are
    consecutive powers of two from 2^0 upwards. I can see no permission
    for some values to be excluded from UINT_MAX.

    unsigned int can have trap representations, but a bit is either a
    value bit or a non-value bit. If the "top" bit is a value bit, all
    values where it is combined with settings of the other values bits are
    legal and permitted. If it is a padding bit, then you have fewer
    value bits (N is one less) but UINT_MAX is still 2^N - 1.

    This is from C99 and there may be more wriggle room in C89, but if so,
    I would guess it is unintended.

    --
    Ben.
     
    Ben Bacarisse, Dec 20, 2007
    #17
  18. Bob

    James Kuyper Guest

    Ian Collins wrote:
    > James Kuyper wrote:
    >> Richard Heathfield wrote:
    >>> pete said:
    >>>
    >>> <snip>
    >>>
    >>>> unsigned u;
    >>>>
    >>>> for (u = 1 /* or any odd number */ ; u++ > 0u; ++u)
    >>> Much better.

    >> But still capable of triggering a compiler warning from a sufficiently
    >> clever compiler.

    >
    > How about:
    >
    > for( u = 1; u&1; u += 2 )


    A compiler is free to complain about anything it wants to. Any idiom for
    a loop that never exits that is sufficiently clear to not count as
    obfuscated is sufficiently simple that a sufficiently clever compiler
    can recognize it. And a loop that never exits is a reasonable thing for
    a compiler to warn about.
    There's no guaranteed way to avoid such a message; the best you can do
    is find a way that silences the messages that a particular compiler
    generates, or learn to live with the warning messages.
     
    James Kuyper, Dec 20, 2007
    #18
  19. Bob

    James Kuyper Guest

    Bob wrote:
    > On 19 Dec 2007 at 13:04, pete wrote:
    >> Bob wrote:
    >>> Hi,
    >>>
    >>> I have been trying to use some inventive alternative idioms for infinite
    >>> loops in my code, rather than the same old for(;;) and while(1) - this
    >>> could be a nice amusing Easter-egg for any future maintenance
    >>> programmers!
    >>>
    >>> One of my ideas was this:
    >>>
    >>> unsigned u = SHRT_MAX << 2; // misleading initialization - tee hee!
    >>> while(++u>=0)
    >>> {
    >>> // loop
    >>> }
    >>>
    >>> Unfortunately, the compiler gives a warning for this, which rather
    >>> spoils the fun...
    >>> does the compiler have to give a diagnostic here, and
    >>> can anyone think of a way of using the same basic idea but without
    >>> generating a warning?

    >> unsigned u;
    >>
    >> for (u = 1 /* or any odd number */ ; u++ > 0u; ++u)
    >> {
    >> // loop
    >> }

    >
    > Cool! That's a really neat solution, thanks.
    >
    > The life of a maintenance programmer is a pretty boring one, and I think
    > finding the odd little amusement must make the task more fun!


    Your little amusement may end up causing problems when the next
    maintenance programmer wastes time trying to figure out the purpose of
    your loop control. Write clear, easy to understand code, and let the
    warnings fall where they may.
     
    James Kuyper, Dec 20, 2007
    #19
  20. Bob

    James Kuyper Guest

    Walter Roberson wrote:
    > In article <>,
    > Ian Collins <> wrote:
    >> James Kuyper wrote:

    >
    >>> But still capable of triggering a compiler warning from a sufficiently
    >>> clever compiler.

    >
    >> How about:

    >
    >> for( u = 1; u&1; u += 2 )

    >
    > Theoretically, UINT_MAX could be even,


    Section 6.2.6.2p1 of the C99 standard is quite clear about this; if N is
    the number of value bits in an unsigned type, the corresponding maximum
    is 2^N-1, which is inherently never a negative number.
    There's no correspondingly specific description for signed integer types.
     
    James Kuyper, Dec 20, 2007
    #20
    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,393
    Pete Becker
    Feb 10, 2005
  2. Teddy
    Replies:
    2
    Views:
    331
    Teddy
    May 29, 2005
  3. B. Williams

    warning C4267 and warning C4996

    B. Williams, Oct 26, 2006, in forum: C++
    Replies:
    17
    Views:
    2,652
  4. asm23
    Replies:
    9
    Views:
    948
    Hendrik Schober
    Oct 12, 2008
  5. WARNING! Prosoftstore.com is a SCAM! WARNING!

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

Share This Page