Smart questions
Smart people
 Find A ForumFind An Expert
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Remember Me

Are you a
Computer / IT professional?
Join Tek-Tips now!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Just copy and paste the

#### Feedback

"...At last there is indeed a website/forum that deals with professional and serious matters. Keep up with the good work!!"

#### Geography

Where in the world do Tek-Tips members come from?

# Dummy question(2)

 Forum Search FAQs Links Jobs Whitepapers MVPs
 maur3r (TechnicalUser) 31 Oct 06 9:53
Can somebody please tell me why the first code works and second not (of course the only difference is changing type of i var. from int to unsigned int) and maybe evaulate a solution  how to change a first code using unsigned type and not receive an error.

#### CODE

int a[3];
a[0]=0; a[1]=1; a[2]=2;
unsigned int i=0;
for(i=2;i>=0;i--)
cout<<a[i]<<endl;

#### CODE

int a[3];
a[0]=0; a[1]=1; a[2]=2;
int i=0;
for(i=2;i>=0;i--)
cout<<a[i]<<endl;

Regards, Martin

(OS and comp.)
using Linux and gcc

 trollacious (Programmer) 31 Oct 06 10:23
In a for loop, the counter is decremented and then evaluated to continue the loop.  With unsigned data types, decrementing a value of 0 will result in a value that's the maximum value for that data type.

The same bit pattern that represents -1 in a signed 16-bit integer represents +65535 in the unsigned equivalent.

for (i = 2; i >= 0; i--)

is evaluated the same as

for (i = 2; i > -1; i--)

which will NOT work with unsigned integers because they can never be negative.

In plain English, your loop says:

#### Quote:

Repeat this while i is greater than or equal to 0 and decrement i after each time through.

When i equals 0, the loop continues.  After that iteration, i is decremented again (from the previous value of 0) and checked to see if it's less than or equal to 0.  An unsigned can never be less than 0, which means the generated code will never evaluate i as less than zero to terminate the loop.

Lee
 Salem (Programmer) 31 Oct 06 12:34
 unsigned int i=0;for(i=2;i>=0;i--)I get\$ gcc -W -Wall -ansi -pedantic -O2 foo.cfoo.c: In function `main':foo.c:24: warning: comparison of unsigned expression >= 0 is always true --
 maur3r (TechnicalUser) 31 Oct 06 15:29
 Now I see it clearly. Thank you Regards, Martin(OS and comp.)using Linux and gcc
 gaift (Programmer) 15 Nov 06 12:48
 also, from what maur3r stated, when you subtract an unsigned long you will get a wrap around, so to use the first algorithm you posted start from 3 end at 1, when you store in the array however youd have to use a[i-1], which i believe is not good style but if you wanted to keep your first convention the way i suggested should work.Regards,Mhanny

Close Box

# Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!