Table of contents
1.
Introduction
2.
unsigned char
3.
Syntax
4.
Example
5.
Initializing an unsigned char
6.
Initializing an unsigned char with signed value
7.
Frequently Asked Questions
7.1.
Can an unsigned char hold negative values?
7.2.
What happens if we initialize an unsigned char with a signed value?
7.3.
What is the range of values that an unsigned char can store?
8.
Conclusion
Last Updated: Dec 6, 2024
Easy

Unsigned Char in C

Author Rahul Singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

For every coder, it is very important to understand every data type properly because it dictates how their programs handle memory and process information. In C, one such data type is the unsigned char, which is used to represent 8-bit unsigned integers. It allows you to store non-negative values ranging from 0 to 255. The unsigned char is commonly used when you are working with raw binary data or when you need to store small positive integers, which could be useful for color values in graphics or specific hardware data in embedded systems. 

Unsigned Char in C

In this article, we'll discuss the unsigned char data type in detail, like its syntax, usage & examples. 

unsigned char

The `unsigned char` data type is used to store 8-bit unsigned integers, which means it can hold values from 0 to 255. It is commonly used when dealing with raw binary data or when a program only needs to store small non-negative values. The `unsigned char` is defined in the `<limits.h>` header file.

Syntax

The syntax for declaring an `unsigned char` variable is:

unsigned char variableName;

Example

Let's look at an example of how to use `unsigned char`:

#include <stdio.h>

int main() {
    unsigned char age = 25;
    printf("Age: %d\n", age);
    return 0;
}
You can also try this code with Online C Compiler
Run Code

 

Output

Age: 25


In this example, we declare an `unsigned char` variable named `age` & assign it the value 25. We then print the value using `printf()`.

Initializing an unsigned char

You can initialize an `unsigned char` variable when you declare it like this:

unsigned char score = 95;

This creates an `unsigned char` variable named `score` & initializes it with the value 95. Let’s take this example in a program: 

#include <stdio.h>

int main() {
    unsigned char score = 95;
    printf("Score: %d\n", score);
    unsigned char grade = 'A';
    printf("Grade: %c\n", grade);
    unsigned char level = 255;
    printf("Level: %d\n", level);


    return 0;
}
You can also try this code with Online C Compiler
Run Code

 

Output:

Score: 95
Grade: A
Level: 255


In this example, we initialize the Score with the value 95, a Grade with the character 'A', and a Level with the maximum value an unsigned char can hold, which is 255. The %d format specifier is used to print integer values, while %c is used to print characters.

Initializing an unsigned char with signed value

If you try to initialize an `unsigned char` with a negative value, it will result in unexpected behavior since `unsigned char` cannot hold negative values. For example:

unsigned char value = -10; // This will not work as expected


Let’s understand this properly in a program: 

#include <stdio.h>
int main() {
    unsigned char value = -10;
    printf("Value: %d\n", value);
    unsigned char num = -1;
    printf("Num: %d\n", num);
    return 0;
}
You can also try this code with Online C Compiler
Run Code


Output:

Value: 246
Num: 255


When we attempt to initialize an unsigned char with a negative value, C performs an implicit type conversion. The binary representation of the signed value is interpreted as an unsigned value. In the first case, -10 is converted to 246, and in the second case, -1 is converted to 255.

Note: Always remember that initializing an unsigned char with a signed value can lead to unexpected results and should be avoided. Always use non-negative values within the range of 0 to 255 when working with unsigned char.

Frequently Asked Questions

Can an unsigned char hold negative values?

No, an unsigned char can only hold non-negative values from 0 to 255.

What happens if we initialize an unsigned char with a signed value?

Initializing an unsigned char with a signed value leads to implicit type conversion, resulting in unexpected behavior.

What is the range of values that an unsigned char can store?

An unsigned char can store values ranging from 0 to 255.

Conclusion

In this article, we discussed the unsigned char data type in C. We learned that unsigned char is used to store 8-bit unsigned integers, with a range of 0 to 255. We examined the syntax for declaring and initializing unsigned char variables and provided examples to understand their usage. We also discussed the consequences of initializing an unsigned char with a signed value and emphasized the importance of using non-negative values within the appropriate range.

You can also check out our other blogs on Code360.

Live masterclass