C String Assignment and Manipulation: Declarations, Initialization, and Common Functions Explained

1. Introduction

In the C programming language, strings are treated simply as arrays of characters. Because of this characteristic, they must be handled differently from strings in other languages. In particular, when assigning or initializing strings, you must include a null character (\0) to terminate the string properly. In this article, under the theme of “C string assignment,” we’ll cover everything from basic declarations to methods of assignment and manipulation, as well as important precautions for maintaining program stability.

2. Declaring and Initializing Strings

In C, strings are declared through arrays of characters. Below are some common ways to declare and initialize them.

Declaration and Initialization Using Arrays

Strings can be declared and initialized through arrays as follows:

char greeting[] = "Hello";

In the example above, the string greeting is initialized with “Hello,” and a null character (\0) is automatically appended at the end. In C, you can use the = operator to initialize arrays at the time of declaration, and it is common to omit the size specification in this method.

Adding a Null Character

If you initialize a string character by character, you must manually add the null character, as shown below:

char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};

Without this null character, C’s string manipulation functions will not work correctly, potentially reading beyond the memory boundary and causing unintended behavior.

3. How to Assign Strings

In C, you cannot directly assign one string to another. To copy the contents of one string variable to another, you use the strcpy function.

Basic Usage of strcpy

strcpy is part of the standard library <string.h> and can be used as follows:

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

int main() {
    char source[] = "Hello";
    char destination[10];
    strcpy(destination, source);
    printf("Copied string: %s\n", destination);
    return 0;
}

In this code, the contents of source are copied into destination, resulting in destination holding “Hello.” Since strcpy does not account for array size, be careful to avoid buffer overflows.

4. String Manipulation

C provides several convenient functions for string manipulation. Below, we’ll cover commonly used ones like strlen, strcat, and strcmp.

Getting String Length: strlen

To get the length of a string, use the strlen function. This function returns the number of characters excluding the null character.

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

int main() {
    char str[] = "Hello";
    printf("String length: %zu\n", strlen(str));
    return 0;
}

Concatenating Strings: strcat

The strcat function appends one string to another.

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

int main() {
    char greeting[20] = "Hello";
    char name[] = " World";
    strcat(greeting, name);
    printf("Concatenated string: %s\n", greeting);
    return 0;
}

Comparing Strings: strcmp

The strcmp function compares two strings lexicographically. It returns 0 if they are equal, or a positive/negative value if they differ.

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

int main() {
    char str1[] = "Hello";
    char str2[] = "World";
    int result = strcmp(str1, str2);
    if (result == 0) {
        printf("Strings are equal.\n");
    } else {
        printf("Strings are different.\n");
    }
    return 0;
}

This lets you check whether strings are identical or determine their order in dictionary terms.

5. Handling Strings with Pointers

Strings can also be handled using pointers. This allows more flexible memory management, but incorrect operations can cause errors or crashes, so caution is required.

Assigning Strings with Pointers

When using pointers, you can declare and assign them like this:

#include <stdio.h>

int main() {
    char *greeting = "Hello";
    printf("%s\n", greeting);
    return 0;
}

As shown, a pointer can directly point to a string literal. However, the contents of a string literal cannot be modified.

6. Summary and Precautions

String assignment and manipulation in C are prone to subtle errors such as buffer overflows or invalid pointer access. When using functions like strcpy or strcat, always account for the array size and allocate sufficient buffer space. Similarly, when working with pointers, handle null characters and memory management with care. If implemented correctly, string operations can be an effective tool for data processing.

侍エンジニア塾