Skip to content

C_Strings.c

ani9977 edited this page Sep 11, 2024 · 1 revision

Here's the updated C code with added documentation and an explanation:

#include <stdio.h>

/**
 * @brief The main function demonstrates basic string handling in C.
 * 
 * This function initializes a character array with a string and prints it.
 * 
 * @return int Returns 0 to indicate successful execution.
 */
int main() {
    char str[] = "Hello, World!";  /**< Character array initialized with the string "Hello, World!". */

    // Print the string
    printf("String: %s\n", str);

    return 0;   /**< Return 0 to indicate successful execution. */
}

Explanation

  1. String Initialization:

    • char str[] = "Hello, World!"; declares a character array str and initializes it with the string "Hello, World!". In C, string literals are automatically null-terminated, which means the array will include a null character '\0' at the end.
  2. Printing the String:

    • printf("String: %s\n", str); prints the string stored in the str array. The %s format specifier is used for printing strings in C.
  3. Return Statement:

    • return 0; indicates that the program has executed successfully. The value 0 is returned to the operating system.

Clone this wiki locally