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 + terminatorThe 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 terminatorYou 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 6Understanding 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
EasyWrite a C program that demonstrates creating and working with character arrays to store strings. Your program should:
- Declare a character array named
greetingwith enough space to hold the string "Hello" (remember to account for the null terminator) - Initialize the
greetingarray with the string literal "Hello" - Declare a second character array named
messagewith 20 elements - Initialize the
messagearray with the string literal "Welcome" - Declare a third character array named
wordwithout specifying the size, and let C automatically determine the size by initializing it with "Programming" - Print all three strings using
printfwith the%sformat specifier - Print the size of each array using
sizeofto 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: 12This 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 + terminatorInitialize with a string literal:
char greeting[6] = "Hello";
// This stores: 'H', 'e', 'l', 'l', 'o', plus a special terminatorLet C automatically determine array size:
char greeting[] = "Hello"; // C automatically makes this size 6Print strings using %s format specifier:
printf("%s", greeting);Get array size using sizeof:
sizeof(greeting) // Returns total bytes occupied by the arrayTry 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;
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Pointers Fundamentals
What is a Pointer?Declaring PointersThe Address-Of Operator (&)The Dereference Operator (*)NULL PointersRecap: Pointer Basics4Project: Simple Text Utility
Project OverviewCounting Characters2Pointers and Arrays
Array Names as PointersArray Elements - PointersPointer ArithmeticComparing PointersRecap: Pointer Array Traversal5Pointers and Functions
Pass-by-ValuePassing Pointers to FunctionsModifying Vars via PointersA Classic Example: SwapPassing Arrays to FunctionsRecap: Function Pointer Args8Structs and Pointers
Pointers to StructsThe Arrow Operator (->)Passing Structs by ValuePassing Struct PointersDynamic Allocation of StructsRecap: Modifying Struct - Ptr11Final Recap Challenges
Recap: Dynamic String ConcatRecap: Array of StructsRecap: Word Frequency Counter3Character Arrays and Strings
Strings as char ArraysThe Null TerminatorString Input with scanfUsing strlen()Using strcpy()Using strcat()Using strcmp()Recap: Basic String Functions