๐Ÿ—ƒ๏ธ C Arrays & Strings
Estimated reading: 3 minutes 275 views

C Strings Overview โ€“ Managing Text with Character Arrays

Introduction โ€“ What Are Strings in C?

In C programming, a string is a one-dimensional array of characters terminated by a null character '\0'. Unlike higher-level languages, C doesnโ€™t have a built-in string typeโ€”strings are handled manually using character arrays and functions from the <string.h> library.

In this guide, youโ€™ll learn:

  • How strings are stored and terminated in C
  • Syntax for declaring and initializing strings
  • How to access and manipulate strings
  • Core functions used for string operations

Declaring and Initializing Strings

Method 1: Character Array with Initialization

char str[6] = {'H', 'e', 'l', 'l', 'o', '\0'};

Method 2: Using String Literal (Preferred)

char str[] = "Hello";

The compiler automatically adds the '\0' null terminator at the end.


Accessing and Modifying Strings

Access individual characters using array indexing:

printf("%c", str[1]);  // Output: 'e'
str[0] = 'M';           // Changes "Hello" to "Mello"

Common String Functions in <string.h>

FunctionPurpose
strlen()Returns length of string (excluding \0)
strcpy()Copies one string to another
strcat()Concatenates two strings
strcmp()Compares two strings
strchr()Finds first occurrence of a character
strstr()Finds substring

Important Notes

  • Strings must be null-terminated ('\0'), or undefined behavior occurs
  • Direct assignment like str1 = "Hello" is invalid after declarationโ€”use strcpy()
  • Use gets() with caution (deprecated). Prefer fgets() for safe input.

Best Practices

  • Always ensure space for the '\0' character
  • Use fgets() for input to prevent buffer overflow
  • Use const char* for immutable string literals
  • Donโ€™t modify string literalsโ€”they are stored in read-only memory

Summary โ€“ Recap & Next Steps

C strings are arrays of characters with a '\0' terminator. While powerful and flexible, they require careful management to avoid memory errors and buffer overflows.

Key Takeaways:

  • Strings are char arrays ending with '\0'
  • Can be initialized with character lists or string literals
  • Use <string.h> functions for safe and common operations
  • Always allocate space for the null terminator

Real-World Relevance:

Strings are used in user input, filenames, file I/O, commands, messaging, and anywhere text processing is required.


Frequently Asked Questions (FAQ)

How is a string stored in memory?

As a contiguous block of characters followed by a null terminator '\0'.


Can I assign a string to a char[] after declaration?

No. Use strcpy() instead:

strcpy(str, "Hello");

What happens if I forget the null terminator?

C will not know where the string endsโ€”may cause buffer overflows or print garbage values.


How do I get string input in C?

Use fgets():

fgets(str, sizeof(str), stdin);

Can I use == to compare strings in C?

No. Use strcmp() from <string.h> to compare content:

if (strcmp(str1, str2) == 0) { /* strings match */ }

Share Now :
Share

๐Ÿงต C Strings Overview

Or Copy Link

CONTENTS
Scroll to Top