C Programming · Lesson 5 of 10 · Updated ·
Arrays and Strings in C — 1D, 2D, char Arrays and string.h
An array stores many values of the same type in consecutive memory. A string in C is just an array of characters with a special end marker.
1. Arrays
#include <stdio.h>
int main(void) {
int marks[5] = {78, 92, 65, 88, 71};
int sum = 0, max = marks[0];
int n = sizeof(marks) / sizeof(marks[0]); /* number of elements */
for (int i = 0; i < n; i++) {
sum += marks[i];
if (marks[i] > max) max = marks[i];
}
printf("n=%d sum=%d max=%d avg=%.1f\n", n, sum, max, sum / (float)n);
return 0;
}
n=5 sum=394 max=92 avg=78.8
- Indexes go from 0 to n − 1.
- C does not check bounds:
marks[10]compiles and reads random memory (or crashes). Be careful. int a[5] = {1, 2};→ the rest become 0:{1, 2, 0, 0, 0}.
2. 2D arrays (matrices)
#include <stdio.h>
int main(void) {
int a[2][3] = {{1, 2, 3}, {4, 5, 6}};
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) printf("%d ", a[i][j] * 10);
printf("\n");
}
return 0;
}
10 20 30
40 50 60
In memory, a 2D array is stored row by row (row-major): 1, 2, 3, 4, 5, 6.
3. Strings: char arrays ending with '\0'
#include <stdio.h>
#include <string.h>
int main(void) {
char name[] = "SKILLS";
printf("%s %zu %zu\n", name, strlen(name), sizeof(name));
name[0] = 's';
printf("%s %c\n", name, name[2]);
return 0;
}
SKILLS 6 7
sKILLS I
The null character '\0' marks the end. Every string function stops when it reaches it.
4. string.h functions
#include <stdio.h>
#include <string.h>
int main(void) {
char first[20] = "Skills";
char second[] = "101";
char copy[20];
strcpy(copy, first); /* copy = "Skills" */
strcat(first, second); /* first = "Skills101" */
printf("%s %s %zu\n", copy, first, strlen(first));
printf("%d %d\n", strcmp("abc", "abc") == 0, strcmp("apple", "banana") < 0);
return 0;
}
Skills Skills101 9
1 1
| Function | Does |
|---|---|
strlen(s) | length (without '\0') |
strcpy(dst, src) | copy src into dst |
strcat(dst, src) | append src to dst |
strcmp(a, b) | 0 if equal; < 0 if a comes first; > 0 otherwise |
strncpy, strncat | the same with a size limit (safer) |
⚠️
strcpyandstrcatdon't check space. Ifdstis too small, they write past the end — a buffer overflow, a classic security bug. Always make the destination big enough.
Never compare strings with
==— that compares addresses. Usestrcmp.
5. Reading strings
char city[30];
scanf("%29s", city); /* no & — the array name is already an address; reads one word */
fgets(city, sizeof city, stdin); /* reads a whole line (with spaces), safely */
6. Classic programs
Reverse a string in place
#include <stdio.h>
#include <string.h>
int main(void) {
char s[] = "pointer";
int l = 0, r = (int)strlen(s) - 1;
while (l < r) {
char t = s[l]; s[l] = s[r]; s[r] = t;
l++; r--;
}
printf("%s\n", s);
return 0;
}
retniop
Count vowels
#include <stdio.h>
#include <ctype.h>
int main(void) {
char s[] = "Learn C Programming";
int v = 0;
for (int i = 0; s[i] != '\0'; i++) {
char c = (char)tolower((unsigned char)s[i]);
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') v++;
}
printf("vowels = %d\n", v);
return 0;
}
vowels = 5
Common mistakes
- Reading or writing past the end of an array.
- Forgetting space for
'\0'(a 6-letter word needschar s[7]). - Comparing strings with
==; using&with%sin scanf.
Quick recap
- Arrays: same type, consecutive memory, index 0…n−1, no bounds checking.
sizeof(arr) / sizeof(arr[0])= number of elements (only where the array was declared).- Strings end with
'\0'; strlen excludes it, sizeof includes it. - Use strcmp to compare, and keep destination buffers big enough.