C Programming Language Tutorial

Variables and Data Types

Input/Output

Looping and Selection Structures

Array

Functions

Preprocessing Command

Pointer

Structure

File Operations

Important Knowledge

Character Arrays And Strings in C Programming Language

In the C programming language, strings are represented using character arrays, which are sequences of characters terminated by a null character ('\0'). In this tutorial, we will explore character arrays and strings in C.

  • Initializing character arrays and strings

Character arrays can be initialized by specifying the characters one by one, or by using a string literal, which automatically appends a null character ('\0') at the end of the string.

Example:

#include <stdio.h>

int main() {
    char str1[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
    char str2[] = "Hello";

    printf("str1: %s\n", str1); // Output: str1: Hello
    printf("str2: %s\n", str2); // Output: str2: Hello
    return 0;
}
  • Reading strings using scanf

To read a string from the user, you can use the scanf function. Note that the %s format specifier is used to read strings.

#include <stdio.h>

int main() {
    char name[30];

    printf("Enter your name: ");
    scanf("%s", name); // Note: No '&' is required, as the array name itself is a pointer to the first character

    printf("Hello, %s!\n", name);
    return 0;
}

Keep in mind that using scanf with %s can lead to buffer overflow if the input is longer than the array size. Consider using safer alternatives like fgets to avoid such issues.

  • Reading strings using fgets

The fgets function reads a line from the specified input stream (such as stdin for user input) and stores it in the character array.

#include <stdio.h>

int main() {
    char name[30];

    printf("Enter your name: ");
    fgets(name, sizeof(name), stdin); // Read a line from stdin with a maximum length of 30 characters

    printf("Hello, %s!\n", name);
    return 0;
}
  • String manipulation functions

The C standard library (<string.h>) provides a set of functions for string manipulation, such as calculating the string length, concatenating strings, and comparing strings.

Example:

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

int main() {
    char str1[] = "Hello";
    char str2[] = "World";
    char str3[20];

    // Calculate the string length
    printf("Length of str1: %zu\n", strlen(str1)); // Output: Length of str1: 5

    // Concatenate strings
    strcpy(str3, str1); // Copy str1 to str3
    strcat(str3, " ");
    strcat(str3, str2); // Append str2 to str3
    printf("Concatenated string: %s\n", str3); // Output: Concatenated string: Hello World

    // Compare strings
    int cmp = strcmp(str1, str2);
    printf("Comparison result: %d\n", cmp); // Output: Comparison result: -15 (non-zero means str1 and str2 are different)
    
    return 0;
}

This tutorial covered the basics of character arrays and strings in C, including initialization, reading strings, and basic string manipulation using the C standard library. To effectively work with strings in C, it's essential to understand the concepts of character arrays and the null terminator, as well as the various string manipulation functions provided by the standard library.

  1. Character array initialization in C:

    #include <stdio.h>
    
    int main() {
        char greeting[] = "Hello, World!";
        printf("Greeting: %s\n", greeting);
    
        return 0;
    }
    
    • Demonstrates the initialization of a character array (string) in C.
  2. C program examples with character arrays:

    #include <stdio.h>
    #include <string.h>
    
    int main() {
        char name[20];
        printf("Enter your name: ");
        scanf("%s", name);
    
        printf("Hello, %s!\n", name);
    
        return 0;
    }
    
    • Provides a simple C program example using a character array to store and display a name.
  3. Concatenating strings in C programming:

    #include <stdio.h>
    #include <string.h>
    
    int main() {
        char str1[20] = "Hello";
        char str2[] = ", World!";
        strcat(str1, str2);
    
        printf("Concatenated String: %s\n", str1);
    
        return 0;
    }
    
    • Illustrates how to concatenate strings using the strcat function in C.
  4. String input and output in C:

    #include <stdio.h>
    
    int main() {
        char sentence[50];
    
        printf("Enter a sentence: ");
        fgets(sentence, sizeof(sentence), stdin);
    
        printf("You entered: %s\n", sentence);
    
        return 0;
    }
    
    • Demonstrates string input and output using fgets in C.
  5. Comparing strings in C language:

    #include <stdio.h>
    #include <string.h>
    
    int main() {
        char str1[] = "apple";
        char str2[] = "banana";
    
        int result = strcmp(str1, str2);
    
        if (result < 0) {
            printf("%s comes before %s\n", str1, str2);
        } else if (result > 0) {
            printf("%s comes after %s\n", str1, str2);
        } else {
            printf("%s is equal to %s\n", str1, str2);
        }
    
        return 0;
    }
    
    • Shows how to compare strings using the strcmp function in C.
  6. Common pitfalls with character arrays and strings in C: