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 EVOEx This is the third time you basically say: "thanks for the explanation but I still don't understand can ...

  1. #16
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Hi EVOEx

    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.
    Did I really say that? Even if I did, that simply translates to "that I appreciate your effort in helping me, but this is unfortunate that I wasn't able to comprehend it out of my limited knowledge". I'm not an English speaker as I'm sure you have noticed.

    I have read almost all the posts carefully and whatever made you think that I didn't read them carefully is because of the fact that sometimes I wrongly interpret the intended meaning because of poor English. I hope you understand this. Thanks.

    Sorry if I offended you in anyway.

    Let me ask my question again. Please have a look on the below given coded which only differ in the declarations of the variables. The first one has unsigned char and the second signed char.

    The output for both is "f" which has ASCII decimal value 102. So, please now help me to understand what's going on and how the two declarations differ?

    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()
    
    {
     	signed char a = 100;
     	signed 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 . . .
    Last edited by jackson6612; 04-17-2011 at 11:50 AM.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  2. #17
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    in the unsigned char..the value of b is actually 159..but in signed char, it is warped to the negative zones....i.e after the count reaches 127 it starts again from -128 ...which is interpreted as the starting point of the extender character set by your compiler and/or os...in case of signed chars.. So the result remains same..
    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. #18
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by manasij7479 View Post
    in the unsigned char..the value of b is actually 159..but in signed char, it is warped to the negative zones....i.e after the count reaches 127 it starts again from -128 ...which is interpreted as the starting point of the extender character set by your compiler and/or os...in case of signed chars.. So the result remains same..
    Actually, I believe that signed overflow is not defined well enough to say that this will happen. It's likely, yes, but it's actually not guaranteed to produce the same result at all.

  4. #19
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    ...you are correct..I just tried it on my system and after the overflow, there was no periodic repetition of the characters..
    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 !



  5. #20
    Registered User
    Join Date
    Apr 2011
    Posts
    5
    jackson6612, it may help you to read up on the two's compliment and fully understand it. It will clear up alot of your misunderstandings.

  6. #21
    Registered User
    Join Date
    Apr 2011
    Posts
    33
    usually ascii value ranges from 0 to 127 i.e seven bits are used (2^7=128);
    if 8th bits becomes 1 then for signed char the value becomes negative..
    but for unsigned character the value is taken as positive...

  7. #22
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Quote Originally Posted by r.hackett View Post
    jackson6612, it may help you to read up on the two's compliment and fully understand it. It will clear up alot of your misunderstandings.
    The standard does not guarantee two complement is used.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 2 of 2 FirstFirst 12
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