Jump to content

confused between unsigned char and signed char declarations

- - - - -

  • Please log in to reply
10 replies to this topic

#1
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Hi :)

I understand what the declaration "unsigned int" means - it simply means that you are going to use only +ve integers starting from 0. But I don't understand what this "unsigned char" and "signed char" declarations are.

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

Please have a look on the below given codes 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?


#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:

enter a = 100

ƒ

Press any key to continue . . .



#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:

enter a = 100

ƒ

Press any key to continue . . .


I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#2
Warrior

Warrior

    Programmer

  • Members
  • PipPipPipPip
  • 130 posts
hey it's not the alphabetic f....its the f you use to define mathematical functions..check out the picture for ascii -159 and you will get it.

Also note that when you use cin>>a and if you enter 100 then the value of a='1' or a=49 because it can take only 1 character as it is a char variable.
Be a joke unto yourself!
Check out my blog at Flashcore

#3
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts

Warrior said:

hey it's not the alphabetic f....its the f you use to define mathematical functions..check out the picture for ascii -159 and you will get it.

Also note that when you use cin>>a and if you enter 100 then the value of a='1' or a=49 because it can take only 1 character as it is a char variable.

Hi Warrior

But there is no decimal value -159 on this ASCII table:
Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion

Please help me. Thanks.
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#4
Warrior

Warrior

    Programmer

  • Members
  • PipPipPipPip
  • 130 posts
there is below...in the "Extended Ascii Codes"

Extended Ascii Codes are later additions in the ascii table and they are platform dependent and will not print on some platforms like it doesn't print on my Ubuntu.
Be a joke unto yourself!
Check out my blog at Flashcore

#5
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Some people use char as a poor-man's small integer, in the desire to save memory. When using char as an integral data type, you may want to allow negatives. From an ASCII perspective, -1 will be treated as either 128 or 255 (depending, I haven't tested how it's handled).
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#6
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts

Warrior said:

Also note that when you use cin>>a and if you enter 100 then the value of a='1' or a=49 because it can take only 1 character as it is a char variable.

Thanks a lot, Warrior. I believe your input was very helpful. Unfortunately, I couldn't understand the quoted material above. Would you please explain it a bit? char could be three digit value such as 227 which stands for PI symbol on the ASCII table.

WingedPanther said:

Some people use char as a poor-man's small integer, in the desire to save memory. When using char as an integral data type, you may want to allow negatives. From an ASCII perspective, -1 will be treated as either 128 or 255 (depending, I haven't tested how it's handled).

Thanks a lot, WingedPanther. I really liked your "poor-man's small integer" phrase.:)

I couldn't understand that "integral data type" part. What is it? Would you please tell me? Thanks.

In my case, in the context of the ASCII table in my first post, -1 is given the value of 255 on the table. -127 is given value of 128 on the ASCII table. There are total 255 characters on the ASCII (127 + 127 + zero). The -150 will be given value of 106 on the table which stands for "j".


#include <iostream>


using namespace std;


int main()


{

 	

 	signed char a;

 	

 	a = -20;

 	

 	cout << a << endl;

    

    system("pause");

    

}


I have seen characters' values are automatically converted into ASCII symbols. In the below code if a=65, then this value of 65 will be converted to ASCII symbol "A". Is this possible to stop this automatic conversion to ASCII symbols?

I thank you for your help, and all the guidance.

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

#7
Warrior

Warrior

    Programmer

  • Members
  • PipPipPipPip
  • 130 posts

Quote

Thanks a lot, Warrior. I believe your input was very helpful. Unfortunately, I couldn't understand the quoted material above. Would you please explain it a bit? char could be three digit value such as 227 which stands for PI symbol on the ASCII table.

That's right a char can take the value of 227 if its unsigned!
But when you are entering a number from the console like "100" and assigning it to a char then to the program it's just a string of characters ('1','0','0') so it will assign the variable a to the character '1' .
So after cin>>a,a will be equal to '1' or ascii code 49.
Be a joke unto yourself!
Check out my blog at Flashcore

#8
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1

jackson6612 said:

I have seen characters' values are automatically converted into ASCII symbols. In the below code if a=65, then this value of 65 will be converted to ASCII symbol "A". Is this possible to stop this automatic conversion to ASCII symbols?
You can, with casting:

char a = 'a';

char b = a + 1;

std::cout << "Character " << a << " has ASCII value of " << (int)a << "\n"; // C style

std::cout << "Character " << b << " has ASCII value of " << static_cast<int>(b) << "\n"; // C++ style


A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#9
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
An integral value is simply anything that corresponds to an integer. These are things like 0, 5, -27, etc.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#10
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Thanks a lot, Warrior, Dutchman, WingedPanther. I understand it now and credit goes to you guys.

Warrior said:

That's right a char can take the value of 227 if its unsigned!
But when you are entering a number from the console like "100" and assigning it to a char then to the program it's just a string of characters ('1','0','0') so it will assign the variable a to the character '1' .
So after cin>>a,a will be equal to '1' or ascii code 49.

Warrior, I'm so sorry. I still don't get it. If it's possible and you don't mind, could you please give it a second try? Thanks.
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#11
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
What he's talking about is this: a char is typically an 8-bit byte. If it's unsigned, then 11001100 would be interpreted as 204, but if it's signed it would be interpreted using 2's complement as -52. The extended ASCII character that is displayed would be the same, regardless of the integer representation.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users