Menu
Coddy logo textTech

Strings as char Arrays

Part of the Logic & Flow section of Coddy's C journey — lesson 12 of 63.

In C, there is no built-in string data type like you might find in other programming languages. Instead, strings are represented as arrays of characters. This fundamental concept is essential for understanding how text is handled in C programming.

A string in C is simply a sequence of characters stored in consecutive memory locations. To create a string, you declare a character array that's large enough to hold all the characters you need, plus one extra space for a special terminating character.

char greeting[6];  // Can hold 5 characters + terminator
char message[20]; // Can hold up to 19 characters + terminator

The most convenient way to work with strings is to initialize them directly with a string literal when you declare the array. C automatically handles the details of storing each character in the array:

char greeting[6] = "Hello";
// This stores: 'H', 'e', 'l', 'l', 'o', plus a special terminator

You can also let C automatically determine the array size by omitting the size specification when initializing with a string literal:

char greeting[] = "Hello";  // C automatically makes this size 6

Understanding that strings are character arrays is crucial because it explains how C handles text data and prepares you for working with string manipulation functions that operate on these character arrays.

challenge icon

Challenge

Easy

Write a C program that demonstrates creating and working with character arrays to store strings. Your program should:

  1. Declare a character array named greeting with enough space to hold the string "Hello" (remember to account for the null terminator)
  2. Initialize the greeting array with the string literal "Hello"
  3. Declare a second character array named message with 20 elements
  4. Initialize the message array with the string literal "Welcome"
  5. Declare a third character array named word without specifying the size, and let C automatically determine the size by initializing it with "Programming"
  6. Print all three strings using printf with the %s format specifier
  7. Print the size of each array using sizeof to demonstrate how much memory each character array occupies

Your output should display the results in the following format:

greeting: Hello
message: Welcome
word: Programming
Size of greeting: 6
Size of message: 20
Size of word: 12

This challenge tests your understanding of how to declare character arrays for storing strings, how string literals are automatically null-terminated when assigned to character arrays, and how C automatically calculates array sizes when you omit the size specification during initialization.

Cheat sheet

In C, strings are represented as arrays of characters. There is no built-in string data type.

Declare a character array with specified size:

char greeting[6];  // Can hold 5 characters + terminator
char message[20]; // Can hold up to 19 characters + terminator

Initialize with a string literal:

char greeting[6] = "Hello";
// This stores: 'H', 'e', 'l', 'l', 'o', plus a special terminator

Let C automatically determine array size:

char greeting[] = "Hello";  // C automatically makes this size 6

Print strings using %s format specifier:

printf("%s", greeting);

Get array size using sizeof:

sizeof(greeting)  // Returns total bytes occupied by the array

Try it yourself

#include <stdio.h>

int main() {
    // TODO: Write your code here
    // Declare and initialize the character arrays as specified in the challenge
    
    // Print the strings and their sizes
    printf("greeting: %s\n", greeting);
    printf("message: %s\n", message);
    printf("word: %s\n", word);
    printf("Size of greeting: %lu\n", sizeof(greeting));
    printf("Size of message: %lu\n", sizeof(message));
    printf("Size of word: %lu\n", sizeof(word));

    return 0;
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow