C Strings

What are Strings in C?

A string in C is a collection of characters stored in a character array and terminated by a null character (‘\0’).

Strings are used to store words, names, sentences, and other text data.

In C Programming, strings are declared using the char data type.

Example

char name[] = “SkillovaHub”;

Explanation

• name is the string variable.

• “SkillovaHub” is stored as a character array.

• The compiler automatically adds the null character (‘\0’) at the end of the string.

• Strings are always stored as arrays of characters.

Why Strings are Important?

Strings are important because they:

• Store text data such as names, addresses, and messages.

• Make it easy to display and manipulate text.

• Are widely used in user input and output operations.

• Help manage character data efficiently.

• Are essential for building real-world C applications.

Types of Strings in C

Strings in C can be created in different ways.

The most common types of string declarations are:

• String using Character Array

• String using Pointer

Both methods are widely used in C Programming depending on the application.

String using Character Array

A character array is the most common way to store strings in C.

Each character is stored in consecutive memory locations, and the string ends with a null character (‘\0’).

Example

char name[] = “SkillovaHub”;

Explanation

• name is a character array.

• “SkillovaHub” is stored as a sequence of characters.

• The compiler automatically adds the null character (‘\0’).

• Character arrays are easy to modify.

String using Pointer

A string can also be stored using a character pointer.

In this method, the pointer stores the address of a string literal.

String pointers are commonly used when the string does not need to be modified.

Example

char *name = “SkillovaHub”;

Explanation

• name is a character pointer.

• It points to the string literal “SkillovaHub”.

• The pointer stores the memory address of the string.

• String literals should not be modified using a character pointer.

Advantages of Strings

• Strings are used to store text data efficiently.

• They make programs easier to read and understand.

• Strings simplify input and output operations.

• They are widely used in real-world applications.

• Strings support many built-in library functions.

Example Program

Program

#include <stdio.h>

int main()
{
char name[] = “SkillovaHub”;

printf(“String = %s”, name);

return 0;
}

Output

String = SkillovaHub

Explanation

• char name[] declares a character array to store a string.

• “SkillovaHub” is assigned to the string variable name.

• %s is the format specifier used to display a string.

• printf() prints the complete string on the screen.

• return 0; terminates the program successfully.

Key Points

• A string is a collection of characters stored in a character array.

• Every string in C ends with a null character (‘\0’).

• Strings can be declared using a character array or a character pointer.

• The %s format specifier is used to display strings.

• Strings are widely used to store names, messages, and text data.

• String library functions simplify string manipulation.

• Strings are one of the most important data types in C Programming.