Skip to content
UPEEC

Arrays and Strings in C

Fixed-size blocks of elements, and strings as char arrays ending in '\0'.

Easy
Comments 0
int samples[8] = {0};            // 8 ints, all zero
samples[0] = 512;

char name[] = "UPEEC";           // 6 bytes: 'U','P','E','E','C','\0'
size_t n = strlen(name);         // 5

C does not check array bounds — writing past the end corrupts memory (a buffer overflow). Use strncpy/snprintf instead of strcpy/sprintf, and always leave room for the terminating '\0'.

Spotted a mistake or want to add detail? Suggest an improvement — Content Contributors' edits are reviewed by editors before they go live.

Discussion (0)

Sign in or join free to ask questions and discuss this topic.