After slicing code into functions in part 6, it is time to store several values at once. Arrays line up equal-typed values in consecutive memory slots, and strings are just character arrays that end with \0. Understanding arrays now makes pointers and memory addresses much easier later.
New Terms in This Chapter
- Array: A sequence of values with the same type stored in contiguous memory
- Index: The position of an element in an array (starts at 0)
- String: A character array that ends with
\0to mark the end - Null Terminator: The
\0character that signals the end of a string - Buffer: A fixed-size array used as temporary storage for data
Key Ideas
Study Notes
- Time: roughly 60 minutes
- Prep: comfort with functions and loops
- Goal: learn array declaration/initialization and the core patterns for handling strings
Declare arrays with type name[length]. The index starts at 0, so scores[0] refers to the first value. Strings are char arrays, but they only behave properly when a null terminator is present so that functions such as printf know where to stop reading.
Follow Along with Code
Declaring and Printing an Integer Array
#include <stdio.h>
int main(void) {
int scores[5] = {95, 82, 74, 63, 88};
int i;
for (i = 0; i < 5; i++) {
printf("scores[%d] = %d\n", i, scores[i]);
}
return 0;
}
With length 5, only indices 0–4 are valid. scores[0] is the first score and scores[1] is the second. Accessing beyond the range causes undefined behavior, so always align your loop condition with the actual length.
Array Length and sizeof
#include <stdio.h>
int main(void) {
int scores[5] = {95, 82, 74, 63, 88};
size_t length = sizeof(scores) / sizeof(scores[0]);
printf("Array length: %zu\n", length);
return 0;
}
For static arrays you can compute the length with the "whole size divided by one element" pattern. size_t is an unsigned integer type used for sizes. Remember that this formula works where the array itself exists as a real array object. Once you pass the array to a function, the parameter behaves like a pointer, so the function can no longer recover the original length with the same trick.
Character Arrays and String Literals
#include <stdio.h>
int main(void) {
char name1[6] = {'A', 'l', 'i', 'c', 'e', '\0'};
char name2[] = "Alice"; // automatically adds \0
printf("name1 = %s\n", name1);
printf("name2 = %s\n", name2);
return 0;
}
String literals automatically append \0, but when you specify the array size yourself you must allocate enough space for every character plus the terminator. "Alice" therefore needs six slots.
Reading a String Safely
#include <stdio.h>
int main(void) {
char buffer[20];
printf("Enter your name: ");
scanf("%19s", buffer);
printf("Hello, %s!\n", buffer);
return 0;
}
Use %19s with scanf to limit input length and avoid buffer overflows. %s stops at whitespace, so it works for single-word input such as names. To read a sentence with spaces, fgets (covered later) is a better fit, because it can read an entire line instead of stopping at the first space.
Using Arrays with Functions
#include <stdio.h>
size_t count_warning(const int scores[], size_t length) {
size_t count = 0;
size_t i;
for (i = 0; i < length; i++) {
if (scores[i] < 60) {
count++;
}
}
return count;
}
int main(void) {
int scores[] = {95, 82, 40, 77, 58};
size_t length = sizeof(scores) / sizeof(scores[0]);
size_t warning = count_warning(scores, length);
printf("Warnings: %zu\n", warning);
return 0;
}
Once an array becomes a function argument it behaves like a pointer, so the function will not know the length unless you supply it. Always pass the length alongside the array.
Practical Example: Building a Label
#include <stdio.h>
#include <string.h>
void make_label(char *buffer, size_t size, const char *name) {
snprintf(buffer, size, "Student: %s", name);
}
int main(void) {
char label[32];
make_label(label, sizeof(label), "Minji");
printf("%s\n", label);
return 0;
}
snprintf writes up to the length you specify, which prevents the string from exceeding the buffer. That is why it is a good fit when you want to safely build a string such as a label or message. Even without exploring the full standard library, it is important to keep in mind that strings are arrays and their lengths must be managed explicitly.
Why It Matters
- Arrays are the basic unit for processing data in loops, making conditionals and repetition far more useful.
- Strings drive most user input and output, and understanding
\0prevents subtle bugs. - Calculating array length and buffer size yourself is essential when you later move on to pointers and dynamic memory.
Practice in CodeSandbox
The sandbox below uses CodeSandbox's Universal starter. For C, the key learning loop is still compile and run in the terminal, so recreate the lesson code as a source file and repeat that cycle directly.
💬 댓글
이 글에 대한 의견을 남겨주세요