Learn Programming, Tech & Coding · Free Online Tools

IT Question Answer
Back to C
C Arrays and Strings Guide

C Arrays and Strings Guide

C3,020 viewsBy Admin
carraysstrings

Arrays in C

An array is a contiguous block of same-type elements. C arrays are fixed-size and zero-indexed.

int nums[5] = {1, 2, 3, 4, 5};
nums[0];          // 1
int len = sizeof(nums) / sizeof(nums[0]); // 5

Strings = Char Arrays

C has no string type — strings are arrays of char ending in a null terminator \0.

char name[] = "Sara";   // {'S','a','r','a','\0'}
printf("%s", name);     // Sara
printf("%c", name[0]);  // S

Common string.h Functions

FunctionDoes
strlenLength
strcpyCopy
strcatConcatenate
strcmpCompare

Looping Over an Array

for (int i = 0; i < 5; i++) {
    printf("%d ", nums[i]);
}

FAQs

Why the null terminator?

It marks where the string ends since arrays don't store length. More in our C section.

Can arrays grow?

Static arrays can't — use malloc/realloc for dynamic ones.