Strings in C Programming

In C, a string is a sequence of characters terminated by a null character ('\0'). Unlike other programming languages, C does not have a built-in string data type; instead, strings are implemented as arrays of characters. Strings in C are implemented as character arrays with a null terminator to mark the end. Although they are low-level compared to languages with built-in string types, C provides a wide range of library functions for effective string manipulation. Understanding how strings work in memory and how to safely handle them is crucial for developing reliable C applications.

Strings are declared as character arrays.

Syntax:

char string_name[size];

Initialization Methods:

Character Array Initialization with Characters:

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

String Literal Initialization:

char greeting[] = "Hello";

The compiler automatically adds the null character at the end.

  • The size must accommodate the null terminator.
  • If declared with fixed size and fewer characters are assigned, the remaining are filled with null characters.

String Input:

Using scanf():

scanf("%s", string_name);

  • Reads input until a whitespace is encountered.
  • Does not allow spaces in strings.

Using gets():

  • Reads an entire line including spaces until a newline.
  • Considered unsafe due to lack of bounds checking.

Using fgets():

  • Reads a line safely with a limit on character count.

fgets(string_name, size, stdin);

String Output:

Using printf():

printf("%s", string_name);

Using puts():

puts(string_name);

  • Automatically appends a newline at the end.

String Manipulation Functions

C provides several standard library functions for string operations, defined in the string.h header file.

Common String Functions:

Function Purpose
strlen() Returns the length of the string
strcpy() Copies one string to another
strcat() Concatenates (appends) two strings
strcmp() Compares two strings
strncpy() Copies up to n characters
strncat() Appends up to n characters
strncmp() Compares up to n characters
strchr() Finds first occurrence of a character
strstr() Finds first occurrence of a substring

1. strlen() – Get Length of String

  • Returns the number of characters before the null terminator.

  • Syntax:

    int len = strlen(string_name);
    

2. strcpy() – Copy One String to Another

  • Copies contents of one string to another.

  • Syntax:

    strcpy(destination, source);
    

3. strcat() – Concatenate Strings

  • Appends one string to the end of another.

  • Syntax:

    strcat(destination, source);
    

4. strcmp() – Compare Two Strings

  • Compares two strings lexicographically.

  • Returns:

    • 0 if equal
    • <0 if first string is less
    • 0 if first string is greater

  • Syntax:

    int result = strcmp(string1, string2);
    

String Storage in Memory

  • Each character takes 1 byte.
  • A string like "Hello" occupies 6 bytes ('H', 'e', 'l', 'l', 'o', '\0').
  • String arrays are stored in contiguous memory blocks.

Notes:

  • Always ensure strings have enough space for the null terminator.
  • Use fgets() instead of gets() for safe input handling.
  • Remember to #include <string.h> when using string functions.
  • Modifying string literals directly is undefined behavior.