Signed char

Hello!
Please, can someone help me find a link showing the simplest use of SIGNED CHAR!!!
First time in touch with that, no idea what it can be used for (==what it acutaly is)

MANY THANKS!!!

signed char is an integer type that is smaller than int which means it can't hold as big and small values as int can. signed char is always one byte while int is usually 4 bytes. If you want to know the max and min values of signed char you can use std::numeric_limits.

http://en.cppreference.com/w/cpp/types/numeric_limits

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <limits>

int main ()
{
    std::cout << "maximum value of signed char: "
              << static_cast<int>(std::numeric_limits<signed char>::max())
              << std::endl;
    std::cout << "minimum value of signed char: "
              << static_cast<int>(std::numeric_limits<signed char>::min())
              << std::endl;

    std::cout << "maximum value of int: "
              << std::numeric_limits<int>::max()
              << std::endl;
    std::cout << "minimum value of int: "
              << std::numeric_limits<int>::min()
              << std::endl;
}

Note that I had to cast the signed char to an int before printing it to avoid that it's printed as a character symbol.
Last edited on
A pointer to signed char is (unfortunately) the way how zero terminated c-strings are usually represented, i.e. functions like strcpy, strcmp, etc. are using this
Last edited on
Is signed char CHAR or INT???

Many thanks!!!
signed char is neither char nor int.
Hello!
Is signed char a letter or a number?
A letter is always stored as a number. It's how you use that number that makes the difference. Normally you would use char instead of signed char to store letters because that's the type that the standard library functions expect.
Last edited on
Is signed char a letter or a number?
Both, that is only a different representations of the same thing. For instance '0' is 48.

'1' - '0' = 1 which equivalent to 49 - 48 = 1

or

'B' - 'A' = 1 which equivalent to 66 - 65 = 1


See:
http://www.asciitable.com/
Last edited on
Everything in a computer is numbers. Those numbers are just treated differently as needed.
Hello!
I understand that, probelm is I cant imagine signed char(s) just because I cant imagine negative ascii value...I am not sure if I explained the problem well, my q qas simply , if I operate with char type variable in my program, how do I decide if to make it char or signed char type...problem is formula above, because I simply don't understnad what is acutlaly happening in there..







A signed character has an integer value in the range -128 to +127
while an unsigned char has an integer value from 0 to 255.

Sometimes this doesn't matter, but it can be important, perhaps in an encryption routine where a number is added or subtracted from the character.

Simple example:
1
2
3
4
5
6
7
    cout << "-- Unsigned character -- \n\n";
        
    unsigned char ch = 'z';
    unsigned char test = ch + 50;
    int n = test;
        
    cout << "ch = " << ch << "  n = " << n << endl;

-- Unsigned character --

ch = z  n = 172


1
2
3
4
5
6
7
    cout << "-- Signed character -- \n\n";
        
    signed char ch = 'z';
    signed char test = ch + 50;
    int n = test;
        
    cout << "ch = " << ch << "  n = " << n << endl;

-- Signed character --

ch = z  n = -84

Notice the output is different. The only thing that changed was signed / unsigned on lines 3 and 4.

If you don't keep this in mind, sometimes you may get strange or unexpected results.
Last edited on
probelm is I cant imagine signed char(s) just because I cant imagine negative ascii value
signed char and unsigned char are simply signed and unsigned values, just like ints which can also be signed or unsigned, except they are stored in 8 bits. They do not have to represent an ASCII value. The choice of a signed or unsigned char verses an int made when the size of storage is critical. Whether signed or unsigned depends on whether the value can represent a negative number.
The char type is distinct from both signed char and unsigned char. Only the char type should ever be used for holding characters - the implementation may chose for it to be either signed or unsigned, and you should not rely on one or the other. In all cases however, they all store numbers. Characters are numbers.
Topic archived. No new replies allowed.