Skip to main content

Overview of Strings in C

Strings Overview

A string is a sequence of characters stored in an array.

  • The end of the string is marked by a special character known as the null terminator, which is represented by the ASCII value 0.

  • Strings in C are null-terminated, meaning that the last character in the string must be the null terminator.

  • The length of a string can be found by counting the number of characters up until the null terminator.

The syntax for declaring a string in C is as follows:

char string_name[array_size];

Here, string_name is the name of the string, and array_size is the number of characters that the string can store, including the null terminator.

Here's an example of how you could declare and initialize a string in C:

char greeting[20] = "Hello, World!";

Explanation

  • greeting is the name of the string, and it can hold up to 19 characters (since the last character must be the null terminator).
  • The string is initialized with the value "Hello, World!".

Properties of strings in C

Null-Terminated

Strings in C are null-terminated, meaning that they end with a special character, called the null terminator, represented by the ASCII value 0 ('\0'). This allows the program to determine the end of the string.

Fixed-Length

Strings in C are of fixed length, determined by the size of the array that holds the characters of the string. The size of the array must be large enough to hold all the characters of the string, including the null terminator.

Mutable

Strings in C are mutable, meaning that their contents can be changed after they are declared.

Pointers

Strings in C are stored in arrays, and arrays are stored in memory as pointers. As a result, strings in C can be manipulated using pointer operations.

Character Array

A string in C is essentially a character array, where each element of the array represents a single character of the string.

Pass-by-Reference

In C, strings are passed to functions by reference, meaning that the function receives a pointer to the string, rather than a copy of the string. This allows the function to modify the contents of the string.

Built-in Library Functions

The C standard library provides a set of built-in functions for manipulating strings, such as strlen, strcpy, strcat, and others. These functions make it easier to work with strings in C.

Strings - Basic Example

Here's a basic example of a C program that uses strings:

#include <stdio.h>

int main()
{
// Declare a string to store a message
char message[20] = "Hello, World!";

// Print the message to the console
printf("%s\n", message);

// Return 0 to indicate that the program has executed successfully
return 0;
}
Output:

Explanation

  • We declare a string message with the value "Hello, World!".
  • We then use the printf function to print the contents of the string to the console.
  • The format specifier %s is used to print a string, and the \n character is used to print a newline after the message.
  • Finally, the program returns 0 to indicate that it has executed successfully.