The ProgrammersTalk Community
Forum Register Search Today's Posts Mark Forums Read
Register

Go Back   The ProgrammersTalk Community > General Programming > C / C++


Welcome to the The ProgrammersTalk Community forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.
Reply
 
LinkBack Thread Tools    Display Modes   
  #1 (permalink)  
Old 07-22-2007, 05:00 PM
Lee's Avatar
Lee Lee is offline
PT Staff*
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: Blackpool, UK
Posts: 616
iTrader: (0)
Lee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enough
Signed/Unsigned Char

Hey,

After having some compiler problems i figured i will need to learn when to use what. My question is, when do you know that you need to use signed char, unsigned char or just char?

Any help would be great, i need to know!

Thanks
Lee
Reply With Quote
  #2 (permalink)  
Old 07-22-2007, 07:24 PM
rpgfan3233 rpgfan3233 is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jul 2007
Posts: 118
iTrader: (0)
rpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura about
There isn't really any difference when it comes to C and C++. The languages state that the default of any integer type (char, short (int), int, long (int) and long long (int) in C99) is signed. With normal integers, determining whether to use signed vs unsigned is as simple as asking yourself the following question: do you wish to allow for negative values? If you want to allow yourself to use negative values (or your users if you are creating an API or your fellow coders if you are creating an open source application for others to freely modify), choose signed. Note that with the char data type, signed vs unsigned doesn't matter. 0 to 127 is the same. -128 to -1 (signed) is the same as 128 to 255 (unsigned), even in binary.

That's probably why there is an option in some versions of Visual Studio to allow for changing the default to unsigned char. After all, working with negative values seems somewhat unnatural.

__________________
"C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off."
-- Bjarne Stroustrup, creator of what is now known as C++
For more quotes by Bjarne Stroustrup, check out http://www.research.att.com/~bs/bs_faq.html#really-say-that.
Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following 3 Users Say Thank You to rpgfan3233 For This Useful Post:
HelloWorld (07-22-2007), Lee (07-23-2007), TeraTask (07-22-2007)
  #3 (permalink)  
Old 07-22-2007, 09:55 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,111
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
What's the difference between signed or unsigned char? I never heard that term before

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #4 (permalink)  
Old 07-22-2007, 10:19 PM
TeraTask's Avatar
TeraTask TeraTask is offline
PT Staff*
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 3
Join Date: Jun 2007
Location: Reno, NV
Posts: 428
iTrader: (0)
TeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enough
RPG said it well, let me state it simply:

Unsigned means no negative values
Signed means negative values are allowed.

When using a signed number, an extra bit is used for the +/- indication. To use memory most effectively, choose the appropriate type. Most of the time you won't notice any difference if you choose incorrectly, but your program will be more robust if you choose the proper type.

__________________
Jeremy Miller
Content Farmer - Optimized Automated Blog Posting

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following 2 Users Say Thank You to TeraTask For This Useful Post:
HelloWorld (07-22-2007), Lee (07-23-2007)
  #5 (permalink)  
Old 07-23-2007, 04:25 AM
Lee's Avatar
Lee Lee is offline
PT Staff*
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: Blackpool, UK
Posts: 616
iTrader: (0)
Lee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enough
Thank you both, i will just have to try with my program and see which goes best, if any.
Reply With Quote
  #6 (permalink)  
Old 07-23-2007, 11:26 AM
Bench Bench is offline
Full Programmer
Join Date: Jul 2007
Location: UK
Posts: 111
iTrader: (0)
Bench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished road
There is another, more subtle reason why you may choose to switch to unsigned integral types - this is for portability when reading & writing binary data.

The problem with negative numbers is that there is no agreed standard way to represent them. Some systems may use one's complement, where the range of numbers represented by 8 bits is -127 through to +127, other systems may use two's complement, where the range of numbers for 8 bits is -128 to +127 (Under one's complement, there is such thing as a 'negative zero' - when the sign bit is set to 1, and all other bits are zero)

This poses the problem that you must be sure what system your target platform uses, else your program can end up exhibiting very strange behaviour between different platforms.

On the other hand, unsigned integral types are all represented the same way, and differences depend on the size of a byte (in bits). (Although endian'ness and the number of bytes between different types are a factor)

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!

Last edited by Bench : 07-23-2007 at 11:31 AM.
Reply With Quote
The Following 2 Users Say Thank You to Bench For This Useful Post:
HelloWorld (07-23-2007), Lee (07-23-2007)
  #7 (permalink)  
Old 07-23-2007, 11:29 AM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,111
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
That's really interesting point Bench,
I never knew that on the different platform they read it differently. Do you have a certain example from your experiences? Like Linux vs. Windows?

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #8 (permalink)  
Old 07-23-2007, 12:18 PM
Lee's Avatar
Lee Lee is offline
PT Staff*
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: Blackpool, UK
Posts: 616
iTrader: (0)
Lee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enough
Thanks again, i will be making a game with C++ and DirectX to be played on a windows system, for doing that what should i use? (The book says to use the /J so it changes things to unsigned char so is that what i should type instead of just char?)
Reply With Quote
  #9 (permalink)  
Old 07-23-2007, 01:50 PM
TeraTask's Avatar
TeraTask TeraTask is offline
PT Staff*
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 3
Join Date: Jun 2007
Location: Reno, NV
Posts: 428
iTrader: (0)
TeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enough
I think you're stressing on it too hard Lee. Unless you're dealing with the ends of the spectrum on values, then you just need to pay attention to this rule:

Quote:
Unsigned means no negative values
Signed means negative values are allowed.
That's really all there is to it.

__________________
Jeremy Miller
Content Farmer - Optimized Automated Blog Posting

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following User Says Thank You to TeraTask For This Useful Post:
Lee (07-23-2007)
  #10 (permalink)  
Old 07-23-2007, 02:58 PM
Bench Bench is offline
Full Programmer
Join Date: Jul 2007
Location: UK
Posts: 111
iTrader: (0)
Bench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished road
Indeed. The Ones complement vs twos complement problem only occurs when reading binary data between different kinds of hardware (Luckily most modern PC hardware uses twos complement anyway). The kind of situations where you'll encounter the difference will be embedded devices, old mainframes, Digital signal processors, calculators..

Provided you don't attempt to mix signed and unsigned values in the same expression, you won't go too far wrong.

(Here's a horrible example of what may go wrong if you do )
Code:
#include <iostream>

int main()
{
    char ch = -1;
    unsigned int i = ch;
    std::cout << i ;
}

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!

Last edited by Bench : 07-23-2007 at 03:06 PM.
Reply With Quote
The Following User Says Thank You to Bench For This Useful Post:
Lee (07-23-2007)
Reply


Thread Tools
Display Modes

   Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 08:12 AM. Powered by vBulletin
Copyright © 2000 - 2007, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO © 2007 ProgrammersTalk Sedo - Buy and Sell Domain Names and Websites project info: programmerstalk.net Statistics for project programmerstalk.net etracker® web controlling instead of log file analysis


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50