Like Tree1Likes

unsigned char declaration

This is a discussion on unsigned char declaration within the C++ Programming forums, part of the General Programming Boards category; Hi I understand the declaration "unsigned int" - it simply means that you are going to use only +ve integers ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    254

    unsigned char declaration

    Hi

    I understand the declaration "unsigned int" - it simply means that you are going to use only +ve integers starting from 0. But I don't understand what this "unsigned char" declaration is. Let's try to understand with some simple code.

    Here is ASCII table. Normal ASCII runs from 0 to 127 (decimal) and the extended from 0 to 255.

    Could you please help me with it? Thanks a lot.


    Example for Unsigned:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    
    {
     	unsigned char a = 100;
     	unsigned char b = a + 59;
     	
     	cout << "enter a = ";
     	cin >> a;
     	
    	cout << b << endl;
     	
     	system("pause");
     	
    }

    Example for signed:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    
    {
     	char a = 100;
     	char b = a + 59;
     	
     	cout << "enter a = ";
     	cin >> a;
     	
    	cout << b << endl;
     	
     	system("pause");
     	
    }
    Last edited by jackson6612; 04-15-2011 at 12:59 PM.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    signed char has a range of -128 to +127.. SO in your example, the value will automatically warped or typecasted .
    It (may be) is a good idea to use a unsigned char ..or a wchar_t if you need any sort of extended characters .
    #TO All Others.. I read in an article..which I'm unable to locate now.. that wchar_t will be used in default for the *next* compilers char 's because there is a debate in making the specification regarding unicode values. Is it true or just a rumour?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by manasij7479 View Post
    signed char has a range of -128 to +127.. SO in your example, the value will automatically warped or typecasted .
    It (may be) is a good idea to use a unsigned char ..or a wchar_t if you need any sort of extended characters .
    manasij, I'm sorry to say this but I couldn't understand your reply. Anyway, I'm still thankful for your effort.

    Would someone else please give it a try? Perhaps a different approach could make it easier for me to understand. Thanks a lot.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    A "char" isn't signed. It may be either signed or unsigned and it's its own type. If you wanted a signed char, use "signed char". If you want an unsigned char use "unsigned char". A signed char has a range of at least -128 to 127, an unsigned char of at least 0 to 255, though both may have larger ranges.

    A char can simply be used as an integer; not just to store ASCII values. Normal ASCII values range to 127, but many other characters or types of information can be stored in a char. You can store a boolean in a char; you can store your age in a char. Maybe not best practice, but I'm just showing it can be used for a lot more than just ASCII values.
    When to use unsigned? Well, if the range isn't big enough for your liking or you need to do some integer manipulation that require it to be unsigned (such as right-shifting values not in the range 0-127). But if you don't know you need it, you probably don't and a regular char is fine for you.
    phantomotap likes this.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,157
    Quote Originally Posted by manasij7479 View Post
    I read in an article..which I'm unable to locate now.. that wchar_t will be used in default for the *next* compilers char 's because there is a debate in making the specification regarding unicode values. Is it true or just a rumour?
    I would guess it is an implementation decision by a particular compiler vendor.

    That sort of thing is unlikely to fly for the C++ standard: there are target platforms for which that change would not make sense, and the standard does not prevent any implementation (i.e. compiler and library) from doing it if they so choose.

    Several vendors are not shy about attempting to "convince" the standards committee to ratify something that suits them, even if it doesn't make sense for other vendors. Such is the politics of standardisation.
    Right 98% of the time, and don't care about the other 3%.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by EVOEx View Post
    A "char" isn't signed. It may be either signed or unsigned and it's its own type. If you wanted a signed char, use "signed char". If you want an unsigned char use "unsigned char". A signed char has a range of at least -128 to 127, an unsigned char of at least 0 to 255, though both may have larger ranges.

    A char can simply be used as an integer; not just to store ASCII values. Normal ASCII values range to 127, but many other characters or types of information can be stored in a char. You can store a boolean in a char; you can store your age in a char. Maybe not best practice, but I'm just showing it can be used for a lot more than just ASCII values.
    When to use unsigned? Well, if the range isn't big enough for your liking or you need to do some integer manipulation that require it to be unsigned (such as right-shifting values not in the range 0-127). But if you don't know you need it, you probably don't and a regular char is fine for you.
    Thanks a lot, EVOEx.

    When we simply use "int" it means "signed int". I suspect the same is true of "char". Correct?

    But I have seen characters' values automatically converted into ASCII decimal values. In the below code if a=53, then b would be 5 (on ASCII table 5 has decimal value of 53). And if a=65, the b would be A. And I have checked using the prefixes "signed" or "unsigned" don't affect the end result.

    Please guide me on this. Thanks.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    
    {
     	unsigned char a = 53, b;
     	
     	b = a;
     	
     	cout << a << endl;
     	
     	system("pause");
     	
    }
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,157
    Quote Originally Posted by jackson6612 View Post
    When we simply use "int" it means "signed int". I suspect the same is true of "char". Correct?
    Wrong. It is implementation defined whether the char type is signed or unsigned.
    Right 98% of the time, and don't care about the other 3%.

  8. #8
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,143
    When we simply use "int" it means "signed int". I suspect the same is true of "char". Correct?
    The text you quoted from EVOEx answered that question. How exactly did you manage to miss it?

    Soma

  9. #9
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    Introducing ASCII into the discussion makes no sense. Even if you had a character set with 256 symbols in it, you could still use a plain old char to represent them. Some of the values might be negative, but so what? They're distinct, it doesn't matter.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  10. #10
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Try the code in your very first post giving a value of 100 to a from the console.
    Post the results.

  11. #11
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Thank you, everyone.

    Quote Originally Posted by grumpy View Post
    Wrong. It is implementation defined whether the char type is signed or unsigned.
    Hi Grumpy

    But int is always system dependent but it's not true of char. Okay. Could you please what does implementation mean in this context? Is it how a certain compiler deals with it? Please tell me. Thanks.

    Quote Originally Posted by C_ntua View Post
    Try the code in your very first post giving a value of 100 to a from the console.
    Post the results.
    Hi C_ntua

    Okay. Here are the results for the codes in my first post.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    
    {
     	unsigned char a = 100;
     	unsigned char b = a + 59;
     	
     	cout << "enter a = ";
     	cin >> a;
     	
    	cout << b << endl;
     	
     	system("pause");
     	
    }
    Output from the Command Prompt:
    Code:
    enter a = 100
    
    Press any key to continue . . .
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    
    {
     	char a = 100;
     	char b = a + 59;
     	
     	cout << "enter a = ";
     	cin >> a;
     	
    	cout << b << endl;
     	
     	system("pause");
     	
    }
    Output from the Command Prompt:
    Code:
    enter a = 100
    
    Press any key to continue . . .
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    6,157
    Quote Originally Posted by jackson6612 View Post
    But int is always system dependent but it's not true of char.
    "signed int" and "int" are equivalent, and distinct from "unsigned int". "signed char", "unsigned char", and char are distinct.

    Quote Originally Posted by jackson6612 View Post
    Could you please what does implementation mean in this context?
    Implementation refers to the implementation of the C++ language, according to the standard. Practically, that it refers to the effects delivered by the compiler, its libraries, and the host system.

    "Implementation defined behavior" is defined in the standard as "behavior, for a well-formed program construct and correct data, that depends on the implementation and that each implementation shall document."

    In other words, something that is implementation-defined may vary between compilers, libraries, or host systems. When I said that "It is implementation defined whether the char type is signed or unsigned" the practical meaning is that each compiler implementer (or vendor) chooses for themselves whether char is signed or unsigned.

    In other words, one compiler may specify char as a signed type and another compiler (or even the same compiler in a different operating mode) may specify char as unsigned. And both would be correct.
    Right 98% of the time, and don't care about the other 3%.

  13. #13
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by grumpy View Post
    Wrong. It is implementation defined whether the char type is signed or unsigned.
    Quote Originally Posted by grumpy View Post
    "signed int" and "int" are equivalent, and distinct from "unsigned int". "signed char", "unsigned char", and char are distinct.


    Implementation refers to the implementation of the C++ language, according to the standard. Practically, that it refers to the effects delivered by the compiler, its libraries, and the host system.

    "Implementation defined behavior" is defined in the standard as "behavior, for a well-formed program construct and correct data, that depends on the implementation and that each implementation shall document."

    In other words, something that is implementation-defined may vary between compilers, libraries, or host systems. When I said that "It is implementation defined whether the char type is signed or unsigned" the practical meaning is that each compiler implementer (or vendor) chooses for themselves whether char is signed or unsigned.

    In other words, one compiler may specify char as a signed type and another compiler (or even the same compiler in a different operating mode) may specify char as unsigned. And both would be correct.
    Thanks a lot, Grumpy. It was really kind of you to explain it.

    Best regards
    Jackson
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  14. #14
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    I'm sorry to ask this again but I'm still struggling with this query of signed and unsigned character. So, I would humbly request you to help me with it. You could read up my posts again in this thread to understand my problem in a better way. Thanks.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  15. #15
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by jackson6612 View Post
    I'm sorry to ask this again but I'm still struggling with this query of signed and unsigned character. So, I would humbly request you to help me with it. You could read up my posts again in this thread to understand my problem in a better way. Thanks.
    This is the third time you basically say: "thanks for the explanation but I still don't understand can you give me another one?" (and that while showing you didn't even read the posts properly). Maybe you should ask more specific questions about what you don't understand.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 10-06-2009, 09:37 AM
  2. Converting unsigned long array to unsigned char array
    By delvec28 in forum C Programming
    Replies: 2
    Last Post: 09-07-2009, 08:53 PM
  3. invalid conversion from `unsigned char*' to `char*'
    By tux88 in forum C Programming
    Replies: 6
    Last Post: 10-14-2007, 12:25 AM
  4. cast unsigned char* to (the default) signed char*
    By Mario F. in forum C++ Programming
    Replies: 24
    Last Post: 07-27-2007, 10:41 AM
  5. unsigned char vs signed char and range of values
    By Silvercord in forum C++ Programming
    Replies: 5
    Last Post: 01-22-2003, 12:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21