Learning Sections show
Strings in C
Example
#include<stdio.h> // Include the standard input-output library
int main() {
// String declaration and initialization
char str[] = "Hello, World!";
// Print the string
printf("%s\n", str);
return 0;
}
Explanation:
Strings in C are arrays of characters terminated by a null character ('\0'
). They are used to store and manipulate text.
In this example:
#include<stdio.h>
: Includes the standard input-output library which allows us to use theprintf
function.char str[] = "Hello, World!";
: Declares a character array (string) namedstr
and initializes it with "Hello, World!".printf("%s\n", str);
: Prints the string to the console.return 0;
: Ends the main function and returns 0 to the operating system, indicating that the program ended successfully.
In summary, strings in C are essential for handling text. They provide a way to store and manipulate sequences of characters efficiently.