w3resource

What is an unsigned char in C Programming?

What is an unsigned char in C with examples and usage

In C programming, unsigned char is a data type used for storing small, non-negative integer values. Unlike the regular char type, which can store both positive and negative values, unsigned char only stores positive values, allowing it to hold a wider range of non-negative values within the same memory space.

Characteristics of unsigned char:

  • Storage Size: Typically occupies 1 byte (8 bits) in memory, the same as char.
  • Range: Can hold values from 0 to 255, as opposed to char, which typically ranges from -128 to 127.
  • Usage: Commonly used for handling raw data (like binary data or individual bytes in files) and for representing small, non-negative numbers. It's also used to ensure portability when dealing with data that should always be positive.

Example: Basic Usage of unsigned char:

The following example demonstrates how an unsigned char can store values within the 0–255 range.

Code:

#include <stdio.h> 
int main() {
    unsigned char value = 200;
    printf("The value of unsigned char: %u\n", value);

    // Incrementing to show behavior at the upper boundary
    value += 56;  // 200 + 56 = 256, which rolls over to 0
    printf("After overflow, the value is: %u\n", value);

    return 0;
}

Output:

The value of unsigned char: 200
After overflow, the value is: 0

Explanation:

Here, value is set to 200 and then incremented by 56. Since the maximum value for an unsigned char is 255, adding 56 results in an overflow, and the value wraps around to 0. This behavior shows how unsigned char handles overflow by rolling over within its range.

Example: Using unsigned char for Binary Data

unsigned char is frequently used to handle raw binary data since it provides an exact representation of byte values (0 to 255). This example reads each byte in an array and prints it as a hexadecimal value.

Code:

#include <stdio.h> 
int main() {
    unsigned char data[] = {0xFF, 0x4B, 0xA2, 0x00};
    size_t length = sizeof(data) / sizeof(data[0]);

    printf("Binary data in hexadecimal:\n");
    for (size_t i = 0; i < length; i++) {
        printf("%02X ", data[i]);  // Prints each byte in 2-digit hexadecimal format
    }

    return 0;
}

Output:

Binary data in hexadecimal:
FF 4B A2 00

Explanation:

In this example, each element of the data array is treated as raw binary data. unsigned char makes it easy to store and print each byte in hexadecimal format, commonly used in data processing and file handling.

Summary of unsigned char in C:

  • Non-negative Values: Represents only non-negative values from 0 to 255.
  • Used for Binary Data: Ideal for handling raw data, binary files, or low-level data manipulation.
  • Overflow Behavior: Rolls over to 0 if a value exceeds 255.


Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/c-programming-exercises/c-snippets/what-is-an-unsigned-character-in-c.php