Character Arrays and String Handling Functions in C

C has no dedicated “string” data type. Instead, a string is simply a character array that ends with a special marker: the null character '\0'. This is the final post of the Unit II series — Post 4 covered two-dimensional arrays.

Declaring and Initializing Strings

char name[20] = "Hello";        // C automatically appends '\0' after 'o'
char greeting[] = "Hi there";    // size inferred: 9 characters + 1 for '\0' = 10
char letters[5] = {'H', 'e', 'l', 'l', 'o'};  // NO null terminator — NOT a valid C string!
The null terminator is not optional: A char array is only treated as a valid C “string” if it ends with ‘\0’. Functions like printf(“%s”, …) and strlen() keep reading memory byte-by-byte until they hit a ‘\0’ — if it’s missing, they’ll keep reading past the array’s actual bounds into unrelated memory, printing garbage or crashing. Always leave room for it: char name[20] can hold at most 19 real characters plus the terminator.

Reading a String

#include <stdio.h>

int main() {
    char name[30];

    printf("Enter your name: ");
    scanf("%s", name);   // stops reading at the first whitespace!

    printf("Hello, %s!\n", name);

    return 0;
}
scanf(“%s”, …) stops at whitespace: If the user types “Rahul Sharma”, scanf(“%s”, name) only captures “Rahul” — it treats the space as the end of input. To read a full line including spaces, use fgets(name, sizeof(name), stdin) instead.

Reading a Full Line with fgets()

#include <stdio.h>

int main() {
    char fullName[50];

    printf("Enter your full name: ");
    fgets(fullName, sizeof(fullName), stdin);

    printf("Hello, %s", fullName);   // fgets keeps the newline, so no extra \n needed here

    return 0;
}

String Handling Functions (<string.h>)

The standard library provides ready-made functions for common string operations — you should almost never write your own loop to do these from scratch.

Function Purpose
strlen(s) Returns the length of s (not counting ‘\0’)
strcpy(dest, src) Copies src into dest (overwrites dest)
strcat(dest, src) Appends src onto the end of dest
strcmp(s1, s2) Returns 0 if equal, negative/positive otherwise
#include <stdio.h>
#include <string.h>

int main() {
    char first[20] = "Hello";
    char second[20] = "World";
    char combined[50];

    printf("Length of first: %d\n", (int) strlen(first));

    strcpy(combined, first);
    strcat(combined, ", ");
    strcat(combined, second);
    strcat(combined, "!");
    printf("Combined: %s\n", combined);

    if (strcmp(first, second) == 0) {
        printf("first and second are equal\n");
    } else {
        printf("first and second are NOT equal\n");
    }

    return 0;
}

Sample Output

Length of first: 5
Combined: Hello, World!
first and second are NOT equal
Why strcmp() doesn’t return true/false directly: strcmp() returns 0 for equal strings (which is why the check is strcmp(a,b) == 0, not just strcmp(a,b) as if it were a boolean) — a negative or positive number otherwise, based on which string would come first alphabetically/by character code. This trips up beginners who instinctively expect “equal” to mean a truthy return value.

Manually Reversing a String (Without strrev, Which Isn’t Standard)

#include <stdio.h>
#include <string.h>

int main() {
    char str[100];
    printf("Enter a string: ");
    scanf("%s", str);

    int len = strlen(str);
    for (int i = 0; i < len / 2; i++) {
        char temp = str[i];
        str[i] = str[len - 1 - i];
        str[len - 1 - i] = temp;
    }

    printf("Reversed: %s\n", str);

    return 0;
}

Sample Output

Enter a string: hello
Reversed: olleh
Why strrev() wasn’t used: strrev() exists on some compilers (like older Windows/Turbo C) but is NOT part of the standard C library — it won’t compile on gcc/Linux/most modern setups. Writing the swap loop yourself (as above) is portable everywhere and, more importantly, is exactly the kind of exercise these lab-style problems are designed to test.

Summary

  • C strings are character arrays terminated by '\0' — there is no separate string type.
  • scanf("%s", ...) stops at the first whitespace; use fgets() to read a full line with spaces.
  • <string.h> provides strlen, strcpy, strcat, strcmp, and more — use these instead of writing your own loops for standard operations.
  • strcmp() returns 0 for equality, not a simple true/false.

This wraps up Unit II: Repetition Statements, Arrays, and Strings. Up next: Unit III, covering searching, sorting, functions (with a link back to the dedicated 3-part Functions series), and recursion.

Further Reading

Leave a Comment