Back to C++
2026-02-119 min read

initialization of (unsigned) char arrays (C++)

Learn initialization of (unsigned) char arrays (C++) step by step with clear examples and exercises.

Why This Matters

Initializing char arrays is a crucial concept in C++ programming, as it forms the foundation for working with strings and other character-based data structures. Understanding this topic is vital for exams, interviews, and real-world coding scenarios. In this lesson, we will delve deep into initializing both signed and unsigned char arrays, emphasizing their differences, best practices, and common mistakes.

Prerequisites

To fully grasp this lesson, you should have a good understanding of:

  1. C++ basics: variables, data types, operators, and control structures
  2. Arrays in C++
  3. String handling in C++ (optional but recommended)
  4. Understanding the difference between signed and unsigned integers

Core Concept

Initializing char Arrays

In C++, you can initialize a char array by assigning individual characters to each element during declaration. Here's an example:

char myArray[5] = {'A', 'B', 'C', 'D', 'E'};

In this case, myArray is an array of 5 characters, initialized with the values 'A', 'B', 'C', 'D', and 'E'. Note that the size of the array must be specified when declaring it.

Signed char Arrays

Signed char arrays can store both positive and negative numbers within the range of -128 to 127. To declare a signed char array, simply use the char data type:

char mySignedArray[5] = {'A', 'B', 'C', 'D', 'E'}; // This will initialize the first four elements and leave the last one as '\0' (ASCII code 0)

In this example, mySignedArray is an array of 5 signed characters. However, since we are initializing it with printable ASCII characters, the last element will be automatically set to '\0', which signifies the end of a string in C++.

Unsigned char Arrays

Unlike signed char arrays, unsigned char arrays can only store non-negative integers between 0 and 255 (inclusive). To declare an unsigned char array, simply add the unsigned keyword before the char:

unsigned char myUnsignedArray[5] = {1, 2, 3, 4, 5};

In this example, myUnsignedArray is an array of 5 unsigned characters, initialized with the values 1 through 5. Since we are initializing it with integers, there will be no '\0' at the end of the array.

Aggregate Initialization

C++ also supports aggregate initialization for arrays, which allows you to initialize multiple elements at once using curly braces ({}). Here's how it works:

char myArray2[4] = {'A', 'B', 'C', 'D'}; // traditional initialization
char myArray3[] = {'A', 'B', 'C', 'D'}; // array with automatic size deduction
unsigned char myUnsignedArray2[4] = {1, 2, 3, 4}; // traditional unsigned initialization
unsigned char myUnsignedArray3[] = {1, 2, 3, 4}; // array with automatic size deduction and unsigned type

In these examples, myArray2, myArray3, myUnsignedArray2, and myUnsignedArray3 are all initialized using aggregate initialization. The difference between the traditional and automatic size deduction methods is that you must specify the size when declaring myArray2 and myUnsignedArray2, while the size is automatically determined for myArray3 and myUnsignedArray3.

Initializing char Arrays with String Literals

You can initialize a char array with a string literal by enclosing it within double quotes ("") and using aggregate initialization. For example:

char myString[6] = "Hello"; // Note that the size must be at least 6 to accommodate the '\0' terminator

In this case, myString is an array of 6 characters initialized with the string "Hello", including the null character ('\0') at the end.

Differences between Signed and Unsigned char Arrays

Although both signed and unsigned char arrays use the same data type (char), there are some key differences:

  1. Data Range: Signed char arrays can store values within the range of -128 to 127, while unsigned char arrays can only store non-negative integers between 0 and 255 (inclusive).
  2. Behavior with negative values: When a signed char array stores a value outside its range (-129 to 127), it wraps around to the opposite end of the range (e.g., -1 becomes 128, and 128 becomes -128). Unsigned char arrays do not have this behavior; they will simply truncate any value that exceeds 255.
  3. Default initialization: When initializing a signed char array without explicitly setting values, it is automatically set to '\0' (ASCII code 0), which represents the end of a string in C++. Unsigned char arrays do not have this behavior; they are initialized to zero but do not represent the end of a string.

Worked Example

Let's walk through an example of initializing both signed and unsigned char arrays using aggregate initialization:

#include <iostream>

int main() {
// Signed char array with traditional initialization
char mySignedArray[5] = {'A', 'B', 'C', 'D', 'E'};
std::cout << "Signed char array: ";
for (char c : mySignedArray) {
std::cout << c << " ";
}
std::cout << "\n";

// Signed char array with aggregate initialization and automatic size deduction
char mySignedArray2[] = {'A', 'B', 'C', 'D', 'E'};
std::cout << "Signed char array (automatic size deduction): ";
for (char c : mySignedArray2) {
std::cout << c << " ";
}
std::cout << "\n";

// Unsigned char array with traditional initialization
unsigned char myUnsignedArray[5] = {1, 2, 3, 4, 5};
std::cout << "Unsigned char array: ";
for (unsigned char u : myUnsignedArray) {
std::cout << u << " ";
}
std::cout << "\n";

// Unsigned char array with aggregate initialization and automatic size deduction
unsigned char myUnsignedArray2[] = {1, 2, 3, 4, 5};
std::cout << "Unsigned char array (automatic size deduction): ";
for (unsigned char u : myUnsignedArray2) {
std::cout << u << " ";
}
std::cout << "\n";

// Initializing a signed char array with a negative value
char mySignedArray3[] = {'A', 'B', 'C', 'D', -1}; // Note that the last element is outside the range of signed char (-128 to 127)
std::cout << "Signed char array with an out-of-range value: ";
for (char c : mySignedArray3) {
std::cout << c << " ";
}
std::cout << "\n";

// Initializing a signed char array with a string literal using aggregate initialization and automatic size deduction
char myString[6] = "Hello";
std::cout << "Char array initialized with a string literal: ";
for (char c : myString) {
std::cout << c << " ";
}
std::cout << "\n";

return 0;
}

Output:

Signed char array: A B C D E
Signed char array (automatic size deduction): A B C D E
Unsigned char array: 1 2 3 4 5
Unsigned char array (automatic size deduction): 1 2 3 4 5
Signed char array with an out-of-range value: A B C D 60 // Note that the last element (60) is the ASCII code for '`' (backtick), which is the result of wrapping around the signed char range
Char array initialized with a string literal: H e l l o

Common Mistakes

  1. Forgetting to specify the size when using traditional initialization for unsigned char arrays: Since unsigned char arrays can store non-negative integers, you must explicitly declare their size when initializing them with traditional initialization.
  1. Incorrectly assuming that signed and unsigned char arrays are interchangeable: Although they share the same data type, signed char arrays can store both positive and negative numbers, while unsigned char arrays can only store non-negative integers between 0 and 255 (inclusive).
  1. Ignoring the importance of automatic size deduction: Automatic size deduction allows you to declare an array without specifying its size, which can be useful in certain situations. However, it's essential to understand when and how to use this feature effectively.
  1. Initializing a char array with a string literal that exceeds the specified size: If you initialize a char array with a string literal and the array is not large enough to accommodate the entire string plus the null character, the behavior will be undefined. Always ensure that the array's size is at least one larger than the length of the string you are initializing it with.
  1. Initializing an unsigned char array with negative values: Unsigned char arrays can only store non-negative integers between 0 and 255 (inclusive). Attempting to initialize them with negative values will result in undefined behavior.

Practice Questions

  1. Write a program that initializes a signed char array with the values 'A', 'B', 'C', 'D', and 'E' using aggregate initialization and automatic size deduction.
  2. Write a program that initializes an unsigned char array with the values 1, 2, 3, 4, and 5 using traditional initialization and automatic size deduction.
  3. Explain the difference between signed char arrays and unsigned char arrays in terms of their data range and when to use each one.
  4. Write a program that initializes a char array with a string literal that exceeds its specified size. Demonstrate the undefined behavior that results from this action.
  5. Write a program that demonstrates how to initialize an unsigned char array with negative values and shows the resulting undefined behavior.
  6. Write a program that initializes a signed char array with a variable number of elements using aggregate initialization and automatic size deduction.
  7. Write a program that converts a signed char array to its equivalent unsigned char array, ensuring that all negative values are converted correctly.
  8. Write a program that demonstrates how to initialize a char array with a mix of signed and unsigned characters using traditional initialization.
  9. Write a program that initializes a char array with a string literal containing non-printable ASCII characters and prints out the resulting values in hexadecimal format.
  10. Write a program that demonstrates how to use dynamic memory allocation (e.g., new and delete) to create and initialize a char array with a variable number of elements.

FAQ

Q: Can I initialize a char array with mixed signed and unsigned characters?

A: Yes, you can initialize a char array with both signed and unsigned characters. However, keep in mind that the resulting values might not be what you expect due to the different data ranges of signed and unsigned char arrays.

Q: How do I determine the size of an array when using aggregate initialization and automatic size deduction?

A: When using aggregate initialization with automatic size deduction, the size of the array is determined by the number of elements inside the curly braces ({}). In the examples provided earlier, there are 5 elements in each array, so the size is automatically set to 5.

Q: Is it possible to initialize a char array with a string literal that does not include the null character?

A: Yes, it's possible to initialize a char array with a string literal that does not include the null character. However, this will result in undefined behavior when trying to use the array as a string, since there is no way to determine where the string ends. Always ensure that your string literals include the null character or handle the case of missing null characters explicitly.

Q: How can I initialize a char array with a variable number of elements?

A: To initialize a char array with a variable number of elements, you can use dynamic memory allocation (e.g., new and delete) or std::vector. These topics are beyond the scope of this lesson but are essential for working with arrays that have a varying number of elements.

Q: How do I convert a signed char array to an unsigned char array while preserving the values?

A: To convert a signed char array to an unsigned char array, you can use a loop to iterate through each element and cast it to an unsigned char before storing it in the new array. Here's an example:

#include

void signedToUnsigned(char* src, unsigned char* dest, size_t size) {
for (size_t i = 0; i < size; ++i) {
dest
initialization of (unsigned) char arrays (C++) | C++ | XQA Learn