473,397 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Annoying compiler warning

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

Dec 19 '07 #1
29 1899
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
Dec 19 '07 #2
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>
Dec 19 '07 #3
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
Dec 19 '07 #4
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
Dec 19 '07 #5
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
Dec 19 '07 #6
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.
Dec 19 '07 #7
Bob <no@spam.comwrote:
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
Dec 19 '07 #8
On Dec 19, 12:40 pm, Bob <n...@spam.comwrote:
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.

Dec 19 '07 #9
Bob <no@spam.comwrites:
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) <ks***@mib.org>
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"
Dec 19 '07 #10
Bob
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!

Dec 20 '07 #11
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.
Dec 20 '07 #12
Walter Roberson said:
In article <5s*************@mid.individual.net>,
Ian Collins <ia******@hotmail.comwrote:
>>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
Dec 20 '07 #13
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
Dec 20 '07 #14
ro******@ibd.nrc-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.
Dec 20 '07 #15
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

Dec 20 '07 #16
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.
Dec 20 '07 #17
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.
Dec 20 '07 #18
Walter Roberson wrote:
In article <5s*************@mid.individual.net>,
Ian Collins <ia******@hotmail.comwrote:
>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.
Dec 20 '07 #19
James Kuyper said:
Walter Roberson wrote:
>>
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.
ITYM "never an even number".

But of course, it is easy to show that this is wrong. If N is, say,
15.000044+ (i.e. if there are 15.000044+ value bits in the unsigned type),
then the maximum is 32768, which is an even number.

--
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
Dec 20 '07 #20
Richard Heathfield wrote:
James Kuyper said:
>Walter Roberson wrote:
>>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.

ITYM "never an even number".

But of course, it is easy to show that this is wrong. If N is, say,
15.000044+ (i.e. if there are 15.000044+ value bits in the unsigned type),
then the maximum is 32768, which is an even number.
I suppose you're correct; the standard never specifically requires that
there be an integer number of value bits. :-)
Dec 20 '07 #21
Richard Heathfield wrote:
James Kuyper said:
>Walter Roberson wrote:
>>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.

ITYM "never an even number".

But of course, it is easy to show that this is wrong. If N is, say,
15.000044+ (i.e. if there are 15.000044+ value bits in the unsigned type),
then the maximum is 32768, which is an even number.
The Standard requires a "pure binary representation," but
the representation you suggest can only be called "impure."

--
Eric Sosman
es*****@ieee-dot-org.invalid
Dec 20 '07 #22
James Kuyper wrote:
Richard Heathfield wrote:
>James Kuyper said:
>>Walter Roberson wrote:
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.

ITYM "never an even number".

But of course, it is easy to show that this is wrong. If N is, say,
15.000044+ (i.e. if there are 15.000044+ value bits in the unsigned
type), then the maximum is 32768, which is an even number.

I suppose you're correct; the standard never specifically requires
that there be an integer number of value bits. :-)
The standard never specifically requires C to be restricted to computer
programming alone. :-)

Dec 20 '07 #23
Eric Sosman wrote:
Richard Heathfield wrote:
>But of course, it is easy to show that this is wrong. If N is, say,
15.000044+ (i.e. if there are 15.000044+ value bits in the unsigned
type), then the maximum is 32768, which is an even number.

The Standard requires a "pure binary representation," but
the representation you suggest can only be called "impure."
Rubbish. "Bloody stupid" would be a reasonable alternative.

--
NonJohnson Hedgehog
"Our future looks secure, but it's all out of our hands"
- Magenta, /Man and Machine/

Dec 20 '07 #24
Eric Sosman wrote:
>
.... snip ...
>
The Standard requires a "pure binary representation," but
the representation you suggest can only be called "impure."
Ahh, now we are getting to something of general interest.

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

Dec 20 '07 #25
In article <wI******************************@comcast.com>,
Eric Sosman <es*****@ieee-dot-org.invalidwrote:
The Standard requires a "pure binary representation," but
the representation you suggest can only be called "impure."
Is that the C99 Standard? Is there a corresponding C89 clause?

I've been reviewing C89 but I have not yet found in there any
requirement of binary representation, beyond that implied by the definition
of "bit" and "byte". I also have not found any requirement in C89
that the complete possible span of binary be usable as values.

If there is anything in C89 that would prevent UINT_MAX from being
2^N-2 with 2^N-1 reserved as a trap value, could someone point me
to the relevant C89 clauses?
--
"All is vanity." -- Ecclesiastes
Dec 20 '07 #26
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
In article <wI******************************@comcast.com>,
Eric Sosman <es*****@ieee-dot-org.invalidwrote:
> The Standard requires a "pure binary representation," but
the representation you suggest can only be called "impure."

Is that the C99 Standard? Is there a corresponding C89 clause?
From C89 6.1.2.5 "Types": "The representations of integral types
shall defines values by use of a pure binary numeration system."
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}
Dec 20 '07 #27
In article <87************@blp.benpfaff.org>,
Ben Pfaff <bl*@cs.stanford.eduwrote:
>ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
>In article <wI******************************@comcast.com>,
Eric Sosman <es*****@ieee-dot-org.invalidwrote:
>> The Standard requires a "pure binary representation," but
the representation you suggest can only be called "impure."
>Is that the C99 Standard? Is there a corresponding C89 clause?
>From C89 6.1.2.5 "Types": "The representations of integral types
shall defines values by use of a pure binary numeration system."
Thanks. It appears the C89 clause is 3.1.2.5; "6.1.2.5" must be the C90
numbering.

I do see the quoted sentance in C89 now, but even with all of the
discussions there about subranges and representations, it is not
yet clear to me that in C89, UINT_MAX could not be 2^N-2 with 2^N-1
reserved. (It would require more than 16 bits for int, but that's not
difficult at all these days.)
--
"Any sufficiently advanced bug is indistinguishable from a feature."
-- Rich Kulawiec
Dec 20 '07 #28
Walter Roberson wrote:
....
If there is anything in C89 that would prevent UINT_MAX from being
2^N-2 with 2^N-1 reserved as a trap value, could someone point me
to the relevant C89 clauses?
Well, the fact that the standard didn't officially acknowledge the
possibility of trap values until C99 would be a bit of a barrier to
that possibility.
Dec 20 '07 #29
Ian Collins wrote:
[About infinite loops]
How about:

for( u = 1; u&1; u += 2 )
It's the reversed form of the Italian humorous threat "Ti prendo a
schiaffi a due a due finché non diventano dispari", "I'm giving you slaps
two by two until they become odd".
BTW, what about
#define HELL_TEMPER 1.416785e32 /* actually, Planck temperature */
#define FREEZING_TEMPER 273.15
#define HELL_FREEZES_OVER (HELL_TEMPER < FREEZING_TEMPER)

while (!HELL_FREEZES_OVER) { ... }
?

--
Army1987 (Replace "NOSPAM" with "email")
Dec 29 '07 #30

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Elbert Lev | last post by:
# here is the problem I ran into: class foo: def __init__(self, host): self.f() self.r = True def f(self): if self.r: #<do something>
1
by: Hafeez | last post by:
I am having real trouble compiling this code http://www.cs.wisc.edu/~vganti/birchcode/codeHier/AttrProj.tgz The attachment shows errors when compiled using the current version of g++ in a...
2
by: Teddy | last post by:
Hello all I've just upgraded my g++ compiler to 3.3.3 on Cygwin. But there is always a annoying warnig message "no new line at end of file" no matter what code you writes. How to get rid of...
29
by: junky_fellow | last post by:
Consider the following piece of code: struct junk { int i_val; int i_val1; char c_val; }; int main(void) {
34
by: Bob | last post by:
Hi, The compiler gives Warning 96 Variable 'cmdSource' is used before it has been assigned a value. A null reference exception could result at runtime. Dim cmdSource as SQlClient.SQLDataReader...
52
by: Michael Hopkins | last post by:
Hi all We all know that C++ is a cleverly conceived multi-paradigm language that sacrifices very little in efficiency for what it delivers in terms of type-safety, encapsulation and generic...
11
by: zeppe | last post by:
Hi all, I've a problem. The code that follows creates a warning in both gcc and visual c++. However, I think it's correct: basically, there is a function that return an object of a derived...
3
by: gil | last post by:
Hi, I'm trying to find the best way to work with compiler warnings. I'd like to remove *all* warnings from the code, and playing around with the warning level, I've noticed that compiling with...
10
by: asm23 | last post by:
Hi, I'm using Intel C++ compiler 9 to compiler a project. But, there are a lot of warning saying like "virtual function override intended....". I searched old messages in Google groups, someone...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.