C String

Strings in C

In C programming language, a string is an array of characters terminated by a null character ('\0'). It is a sequence of characters that represent a textual data.

  • For example, the string “hello” in C programming language is represented as an array of characters:
char str[] = {'h', 'e', 'l', 'l', 'o', '\0'};
  • Alternatively, you can also initialize a string using a string literal, which is a sequence of characters enclosed in double quotes:
char str[] = "hello";

In C programming, strings are commonly used for tasks such as storing and manipulating text, input and output operations, and passing information between functions.

There are various functions provided by the standard library of C language for manipulating strings, such as strlen, strcpy, strcat, strcmp, and many more.

Ways to declare and initialize strings in C

In C, there are several ways to declare and initialize strings:

  1. Using double quotes: Strings can be initialized using double quotes. Especially for String literals. For example:
char str[] = "Hello, world!";
  1. Using braces: Strings can be initialized using braces. Initializing as individual characters. For example:
char str[] = {'H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '!', '\0'};

The ‘\0’ character at the end is the null terminator, which indicates the end of the string.

  1. Using a pointer: Strings can be declared as a pointer to a character array and initialized using the assignment operator. For example:
char *str = "Hello, world!";
  1. Using a character array: Strings can be declared as a character array and initialized by assigning individual characters. For example:
char str[13];
str[0] = 'H';
str[1] = 'e';
str[2] = 'l';
str[3] = 'l';
str[4] = 'o';
str[5] = ',';
str[6] = ' ';
str[7] = 'w';
str[8] = 'o';
str[9] = 'r';
str[10] = 'l';
str[11] = 'd';
str[12] = '!';
str[13] = '\0';

These are some of the ways to declare and initialize strings in C.

Declaring character array with and without size

In C, character arrays can be declared with or without specifying their size.

Declaring a character array without specifying the size:

char str[] = "Hello";

In this case, the size of the array is automatically determined by the size of the string. The size of the array is equal to the number of characters in the string plus one for the null terminator character.

Declaring a character array with specifying the size:

char str[10] = "Hello";

In this case, the size of the array is explicitly specified as 10.

  • If the string assigned to the array is shorter than the specified size, the remaining elements of the array are automatically initialized with null characters (‘\0’).
  • If the string assigned to the array is longer than the specified size, the compiler may issue a warning or error.

–> NOTE – It’s important to note that when using character arrays without a size declaration, modifying the contents of the array can lead to undefined behavior if the modified string exceeds the automatically determined size of the array. Therefore, it’s generally recommended to declare character arrays with an explicit size whenever possible.

String literals behavior in C

In C programming language, a string literal is a sequence of characters enclosed in double quotes, such as “Hello, World!”.

  • String literals in C are stored as an array of characters in read-only memory, also known as the data segment. This means that attempting to modify a string literal will result in undefined behavior, which could include a segmentation fault or other runtime errors.

For example, the following code attempts to modify a string literal:

// create a character pointer
char* str = "Hello, World!";
str[0] = 'J';

This will result in a runtime error, as the string literal “Hello, World!” is stored in read-only memory.

  • –> To modify a string in C, you must declare an array of characters and initialize it with a string literal, like so:
// create an array of characters
char str[] = "Hello, World!";
str[0] = 'J';

This code creates an array of characters called str, and initializes it with the contents of the string literal “Hello, World!”. The array can be modified at runtime because it is stored in writable memory.

In summary, in C programming language, a string literal is a sequence of characters enclosed in double quotes, and it is stored in read-only memory.

To modify a string in C, you must create an array of characters initialized with a string literal, and modify the array elements individually.

Char pointer in C

A char pointer in C is a pointer variable that is specifically designed to point to a memory location that stores a sequence of characters. In C, strings are typically represented as arrays of characters terminated by a null character (‘\0’).

Char pointers are commonly used to manipulate these arrays.

Here’s an example to illustrate this:

#include <stdio.h>

int main() {
    char str[] = "Hello, world!";
    char *ptr = str;
    printf("%s\n", ptr);
    return 0;
}

–> In this example, we have a character array str that contains the string “Hello, world!”. We then create a char pointer ptr and set it equal to the memory address of the first element of str using the address-of operator (&). Finally, we print out the value of ptr using the %s format specifier, which is used to print out strings.

–> str is a pointer to the beginning of the character array.

When we run this program, we get the following output:

Hello, world!

As we can see, the char pointer ptr is able to point to the beginning of the str array and print out the contents of the string.

Char pointers are also commonly used in functions that manipulate strings. For example, the strlen function takes a char pointer as a parameter and returns the length of the string that the pointer points to:

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

int main() {
    char str[] = "Hello, world!";
    int len = strlen(str);
    printf("Length of string: %d\n", len);
    return 0;
}

In this example, we use the strlen function to calculate the length of the str array, which is 13. The strlen function takes a char pointer as a parameter, so we pass it str, which is a pointer to the beginning of the array.

Overall, char pointers are an important part of working with strings in C, and are used extensively in string manipulation and other operations that involve working with character data.