Dev-Spot Developer Forums
September 05, 2010, 01:21:36 PM
Welcome,
Guest
. Please
login
or
register
.
Did you miss your
activation email?
1 Hour
1 Day
1 Week
1 Month
Forever
Login with username, password and session length
News
: Welcome to the LearnCpp.com forums! Enjoy your stay.
Home
Help
Search
Login
Register
Dev-Spot Developer Forums
>
Programming (all languages)
>
General Programming
>
[C++] Benifits of declaring signed/unsigned?
Pages: [
1
]
« previous
next »
Print
Author
Topic: [C++] Benifits of declaring signed/unsigned? (Read 225 times)
MtnDew
Newbie
Posts: 1
[C++] Benifits of declaring signed/unsigned?
«
on:
May 31, 2010, 04:46:15 PM »
Hello Dev-Spot,
Why is it beneficial to me to declare a variable being signed or unsigned?
Example:
unsigned short int age; // Declare a variable named age, of type SHORT INT that only accepts positive input.
Thanks,
-MtnDew
Logged
tylerfb11
Full Member
Posts: 161
What have I gotten myself into?
Re: [C++] Benifits of declaring signed/unsigned?
«
Reply #1 on:
June 01, 2010, 02:03:37 PM »
The only time that this is nessesary is when you are using varibles that are larger than normal ints, or ones that are only allowed to have positive values and such. If you are simply useing a normal interger, it's really just a formality and it's up to you whether or not you bother doing it.
-Tyler
Logged
There are 001100010 kinds of people in the world, those who understand binary, and those who don't.
Theo
Global Moderator
Hero Member
Posts: 520
Re: [C++] Benifits of declaring signed/unsigned?
«
Reply #2 on:
July 19, 2010, 01:48:19 PM »
Yup.
I'll just say it loud and clear:
The benefit of making a variable unsigned is that it can store bigger positive values.
If you don't know the bad side is that it will no longer be able to store negative values.
Cheers,
Theo
Logged
I will not give up on learning C++!!!
-_-.. Allegro vs SDL?
laserlight
Global Moderator
Sr. Member
Posts: 430
Re: [C++] Benifits of declaring signed/unsigned?
«
Reply #3 on:
July 19, 2010, 09:50:44 PM »
There are operations that, done with signed integer types, can result in implementation defined behaviour, but that result in well defined standard behaviour for unsigned integer types.
Logged
Bazaar
rocks for distributed version control!
bitt3r0blivion
Newbie
Posts: 10
Re: [C++] Benifits of declaring signed/unsigned?
«
Reply #4 on:
August 14, 2010, 10:25:03 AM »
Yeah, unsigned are good for getting larger numbers without having to use a larger variable if you know you aren't going to use negative numbers. But a little warning, I wouldn't use unsigned variables in for loops in cases like this
<code>
vector<int> v;
for(unsigned int i = v.size();i<0;i--)
{
//blah
}
</code>
just because the number will never go negative, i ran into some trouble when i was trying to write a vector class. Just be careful you aren't making silly logic mistakes like that. Other than that there aren't any downfalls to using unsigned variables.
Logged
laserlight
Global Moderator
Sr. Member
Posts: 430
Re: [C++] Benifits of declaring signed/unsigned?
«
Reply #5 on:
August 14, 2010, 10:40:16 AM »
bitt3r0blivion, I think you meant:
Code:
vector<int> v;
for(unsigned int i = v.size();i>0;i--)
{
//blah
}
But this problem can be avoided with:
Code:
for (vector<int>::size_type i = v.size(); i-- > 0;)
{
//blah
}
Logged
Bazaar
rocks for distributed version control!
Pages: [
1
]
Print
« previous
next »
Jump to:
Please select a destination:
-----------------------------
Programming (all languages)
-----------------------------
=> For Beginners
=> Game Programming
=> General Programming
=> Graphics Programming
=> Wordpress Plugins/Themes/Modding
-----------------------------
Community
-----------------------------
=> Dev-Spot.com
=> General Discussion
=> Your Announcements
Loading...