Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. C Language Dictionary
  3. Pointers and Strings

Pointers and Strings

In C, strings are represented as arrays of char with a null character ('\0') at the end. Strings are commonly accessed through char * pointers, and the standard string manipulation functions are provided in <string.h>.

Syntax

// Declare a string as a char array (modifiable).
char str[20] = "Hello";
char str[] = "Hello"; // Size is determined automatically (6 bytes including the null character).

// Declare a pointer to a string literal (not modifiable).
const char *p = "Hello";

// Common standard string functions (requires #include <string.h>).
strlen(str)              // Returns the length of the string (excluding the null character).
strcpy(dest, src)        // Copies a string.
strncpy(dest, src, n)    // Copies up to n characters.
strcat(dest, src)        // Concatenates two strings.
strcmp(str1, str2)       // Compares two strings.
strchr(str, ch)          // Searches for a character and returns its address.
strstr(str, sub)         // Searches for a substring and returns its address.

Common String Functions

FunctionDescription
strlen(s)Returns the length of the string, excluding the null character.
strcpy(dest, src)Copies the string in src to dest. The dest buffer must be large enough to hold the result.
strncpy(dest, src, n)Copies up to n characters from src to dest. Recommended to prevent buffer overflows.
strcat(dest, src)Appends src to the end of dest. The dest buffer must have enough space for the combined string.
strcmp(s1, s2)Compares two strings. Returns 0 if equal, a negative value if s1 is less, or a positive value if s1 is greater.
strchr(s, c)Returns a pointer to the first occurrence of character c in the string, or NULL if not found.
strstr(s, sub)Returns a pointer to the first occurrence of substring sub in the string.

Sample Code

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

int main(void) {
    // A char array string can be modified.
    char greeting[20] = "Hello";
    greeting[0] = 'h'; // Overwrite the first character.
    printf("%s\n", greeting); // Prints 'hello'.

    // const char * is a pointer to a string literal (not modifiable).
    const char *lang = "C language";
    printf("%s\n", lang);

    // Get the length of a string.
    printf("Length: %zu\n", strlen(greeting)); // Prints 'Length: 5'.

    // Copy a string.
    char copy[20];
    strncpy(copy, greeting, sizeof(copy) - 1);
    copy[sizeof(copy) - 1] = '\0'; // Ensure null termination for safety.
    printf("Copy: %s\n", copy); // Prints 'Copy: hello'.

    // Compare strings (you cannot use == for this).
    char s1[] = "apple";
    char s2[] = "apple";
    if (strcmp(s1, s2) == 0) {
        printf("The strings are equal.\n");
    }

    // Search for a character.
    char sentence[] = "Hello, World!";
    char *found = strchr(sentence, 'W');
    if (found != NULL) {
        printf("Position of 'W': %s\n", found); // Prints 'World!'.
    }

    // Iterate over characters one by one using a pointer.
    char *p = sentence;
    int vowels = 0;
    while (*p != '\0') {
        if (*p == 'a' || *p == 'e' || *p == 'i' || *p == 'o' || *p == 'u') {
            vowels++;
        }
        p++;
    }
    printf("Vowel count: %d\n", vowels);

    return 0;
}

Notes

C has no dedicated string type — all strings are represented as char arrays terminated by a null character. Using == to compare strings compares their addresses, not their contents. Always use strcmp() to compare string values.

strcpy() and strcat() do not check buffer sizes, which can lead to buffer overflows if the destination is too small. For security, it is recommended to use the size-limited variants strncpy() and strncat().

For safe string input, use fgets(). For printf/scanf format specifiers, see printf() / fprintf().

If you find any errors or copyright issues, please .