what is the reason ?

C

codergem

It will not run. This is fine.
main()
{
unsigned int i;
for(i=1;i>-2;i--)
printf("c aptitude");
}

but in the same code if i ll make unsigned int as short unsigned int
then it will run for an infinite loop.
what is the reason?
thanks.
 
I

Igmar Palsenberg

It will not run. This is fine.
main()
{
unsigned int i;
for(i=1;i>-2;i--)
printf("c aptitude");
}

but in the same code if i ll make unsigned int as short unsigned int
then it will run for an infinite loop.
what is the reason?

Turn on compiler warnings and you can probably figure this out yourself.


Igmar
 
E

Eric Sosman

It will not run. This is fine.
main()
{
unsigned int i;
for(i=1;i>-2;i--)
printf("c aptitude");
}

but in the same code if i ll make unsigned int as short unsigned int
then it will run for an infinite loop.
what is the reason?

The "usual arithmetic conversions."
 
A

Ancient_Hacker

well in general you're hoping that an unsigned int will somehow count
down to -2.

Unlikely on the face of it, but you say it works fine.


That may be, but only due to a binary coincidence.


If you change it to a short, then it may seem to wrap around funny,
like from 2, 1, 0, -65535.

In any case, if you listen to the compiler warnings, you wouldnt have
this problem.

Styrongly suggest yuou use -warn=99 or whatever. "C" is hard enough--
you can't afford to ignore the few warnigns the compiler can put out.
 
F

Flash Gordon

Ancient_Hacker wrote:

Please provide context. You've been here long enough to know it is expected.

The post you were replying to said

| It will not run. This is fine.
| main()
| {
| unsigned int i;
| for(i=1;i>-2;i--)
| printf("c aptitude");
| }
|
| but in the same code if i ll make unsigned int as short unsigned int
| then it will run for an infinite loop.
| what is the reason?
| thanks.
well in general you're hoping that an unsigned int will somehow count
down to -2.

Unlikely on the face of it, but you say it works fine.


That may be, but only due to a binary coincidence.

Or it could be because it does exactly what the C standard requires it
does. Look up the usual arithmetic promotions.
If you change it to a short, then it may seem to wrap around funny,
like from 2, 1, 0, -65535.

I somehow doubt it will wrap from 0 to -65535 under any conditions ;-)
In any case, if you listen to the compiler warnings, you wouldnt have
this problem.

Styrongly suggest yuou use -warn=99 or whatever. "C" is hard enough--
you can't afford to ignore the few warnigns the compiler can put out.

This advice is good. For details of how to get a compiler to produce
decent warnings you are best asking on a group dedicated to your
compiler. Or reading the manual/help, of course!
 
P

Peter Nilsson

Igmar said:
Turn on compiler warnings and you can probably figure this out yourself.

Despite being riddled with issues, there is nothing in the code that
requires a
C90 compiler to issue a diagnostic. I know of at least 2 compilers that
woundn't
issue the particular desired warning, no matter how high you turned
them up.

IIRC, the gcc diagnostic warns about the cause, but doesn't elaborate
on the
issue. So, given the demonstration of coding so far, I seriously doubt
that the
OP will figure it out.

Fortunately, the problem (actually its mirror) is covered in the FAQ...

http://www.c-faq.com/expr/preservingrules.html
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top