Strings in C

Strings are an essential part of programming, and understanding how they work is crucial for any C programmer. In C, a string is a sequence of characters enclosed in double quotes, like “Hello, World!”. It is important to note that C does not have a built-in string data type, so strings are represented as arrays of characters.

In C, strings are stored as arrays of characters, with the last character being a null character ‘’ to indicate the end of the string. This null character is automatically added by the compiler when you initialize a string using double quotes.

Let’s take a closer look at how strings are declared and initialized in C:


#include<stdio.h>

int main() {
    char greeting[] = "Hello, World!";
    printf("%sn", greeting);
    return 0;
}

In the example above, we declare a character array called “greeting” and initialize it with the string “Hello, World!”. The size of the array is determined by the number of characters in the string, including the null character.

Once a string is declared and initialized, we can perform various operations on it. Here are some common operations:

String Length

To find the length of a string in C, we can use the strlen() function from the string.h library. This function returns the number of characters in a string, excluding the null character.


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

int main() {
    char greeting[] = "Hello, World!";
    int length = strlen(greeting);
    printf("Length: %dn", length);
    return 0;
}

The output of the above code will be:


Length: 13

String Concatenation

In C, we can concatenate two strings using the strcat() function from the string.h library. This function appends the second string to the end of the first string.


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

int main() {
    char greeting[50] = "Hello, ";
    char name[] = "John";
    strcat(greeting, name);
    printf("%sn", greeting);
    return 0;
}

The output of the above code will be:


Hello, John

String Comparison

To compare two strings in C, we can use the strcmp() function from the string.h library. This function compares two strings and returns an integer value based on the comparison result.


#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 equaln");
    } else if(result < 0) {
        printf("String 1 is less than String 2n");
    } else {
        printf("String 1 is greater than String 2n");
    }
    
    return 0;
}

The output of the above code will be:


String 1 is less than String 2

These are just a few examples of what you can do with strings in C. Strings are a fundamental concept in programming, and mastering them will greatly enhance your ability to work with textual data in C.

Remember, strings in C are represented as arrays of characters, and various string manipulation functions are available in the string.h library to perform operations on them.

Scroll to Top