Strings in C

    Atul Kabra3 min readUpdated

    A string in C is not a separate type — it is an array of char that ends with a special null terminator, written '\0'. That zero byte marks where the text stops, so C functions know where the string ends. Understanding the null terminator is the key to working with strings safely.

    Declaring strings

    char greeting[] = "Hello";   /* 6 chars: 'H' 'e' 'l' 'l' 'o' '\0' */
    char name[20];               /* room for up to 19 chars + '\0'   */
    

    The literal "Hello" automatically includes the hidden '\0' at the end, so "Hello" needs 6 bytes, not 5. Always size your buffer to leave room for that terminator.

    The null terminator in action

    #include <stdio.h>
    #include <string.h>
    
    int main(void) {
        char word[] = "code";
    
        /* strlen counts characters up to (but not including) '\0' */
        printf("Length: %zu\n", strlen(word));   /* 4 */
        printf("Word: %s\n", word);              /* %s prints until '\0' */
        return 0;
    }
    

    strlen returns 4 even though the array uses 5 bytes — it counts characters, stopping at '\0'.

    Reading string input safely

    To read a line of text from the user, use fgets. It lets you cap how many characters are read, which prevents buffer overflows:

    #include <stdio.h>
    #include <string.h>
    
    int main(void) {
        char name[20];
    
        printf("Enter your name: ");
        if (fgets(name, sizeof(name), stdin) != NULL) {
            /* fgets may keep the newline; strip it if present */
            name[strcspn(name, "\n")] = '\0';
            printf("Hello, %s!\n", name);
        }
        return 0;
    }
    

    Always prefer fgets for line input. The older gets function has no size limit and is unsafe — it was removed from the C standard. Never use it.

    Want to learn this properly?

    Join the waitlist for C Programming — beginner-friendly, project-first classes in Jalgaon.

    Explore C Programming

    Common string functions (from string.h)

    FunctionWhat it does
    strlen(s)length, excluding '\0'
    strcpy(dst, src)copy src into dst
    strcat(dst, src)append src onto dst
    strcmp(a, b)compare; returns 0 if equal
    #include <stdio.h>
    #include <string.h>
    
    int main(void) {
        char a[20] = "Jal";
        strcat(a, "gaon");          /* a is now "Jalgaon" */
        printf("%s\n", a);
    
        if (strcmp(a, "Jalgaon") == 0) {   /* 0 means equal */
            printf("Match!\n");
        }
        return 0;
    }
    

    Comparing strings

    You cannot compare strings with == — that compares addresses, not contents. Use strcmp, which returns 0 when the strings are equal.

    Walking through a string character by character

    Because a string is just a char array ending in '\0', you can loop over it until you hit the terminator. This is how many string functions work internally:

    #include <stdio.h>
    
    int main(void) {
        char text[] = "Hello";
        int vowels = 0;
    
        for (int i = 0; text[i] != '\0'; i++) {   /* stop at the null byte */
            char c = text[i];
            if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
                vowels++;
            }
        }
        printf("Vowels: %d\n", vowels);   /* counts lowercase vowels */
        return 0;
    }
    

    The loop condition text[i] != '\0' is the idiomatic way to walk a string without knowing its length in advance.

    Common mistakes

    • Using == to compare strings: use strcmp(a, b) == 0 instead.
    • Forgetting the null terminator's byte: a buffer for a 10-character string needs at least 11 bytes.
    • Buffer overflow with strcpy / strcat: copying into a buffer that's too small corrupts memory. Size buffers generously and check lengths.
    • Using gets: never use it — it has no bounds check. Use fgets.
    • Reading with scanf("%s", ...) without a width: it can overflow. Prefer fgets.

    FAQ

    Why is there a '\0' at the end? It tells string functions where the text stops, since C arrays don't store their own length.

    How do I store many strings? Use a 2D char array or an array of pointers to char.

    Continue at the hub C Programming, then read Arrays in C and Pointers in C.

    Want to learn this properly? Join the waitlist for our C Programming course — taught in Jalgaon.

    Want to learn this properly?

    Join the waitlist for C Programming — beginner-friendly, project-first classes in Jalgaon.

    Explore C Programming
    Atul Kabra

    Founder, Infoplanet

    Atul Kabra founded Infoplanet in 2001 and has spent over two decades teaching programming — C, C++, Java, databases and more — to students across Maharashtra.

    Related guides