Strings and Characters in C: A Complete Guide

In C programming, working with characters and strings is one of the most fundamental concepts. Whether you want to store a name, a sentence, or any text-based information, strings and characters play a vital role.

In this blog, we will cover everything from basic character representation to advanced string manipulation. We will also discuss memory representation, functions, and differences to make you a pro in handling strings in C.

What is a Character in C?

A character in C is a single alphabet, digit, or symbol enclosed in single quotes (' '). It is represented using the char data type.

C charcter Example

In the example above:

  • 'A' → Represents a character.
  • '5' → Represents a character (even though it's a digit).
  • '@' → Represents a character (even though it's a symbol).

Note:

  • Each character has a corresponding ASCII value (like A = 65)
  • A character occupies 1 byte of memory.

What is a String in C?

A string in C is a sequence of characters terminated by a null character (\0). In simple terms, a string is nothing but an array of characters.

C String Example

This will be stored in memory like:

Index 0 1 2 3 4 5
Character M o k s h \0
  • The null character (\0) is added automatically when you declare a string using double quotes (" ").
  • This null character tells the compiler where the string ends.

But why is the null character important?

  • Without a null character, the compiler wouldn't know where the string ends
  • It is a key differentiator between a normal character array and a string.

Difference Between Character and String

Feature Character ('A') String ("A")
Definition Represents a single character. Represents multiple characters with a null character (\0).
Declaration char ch = 'A'; char str[] = "A";
Size in Memory 1 Byte 2 Bytes (1 for 'A' + 1 for \0)
Output Format Printed using %c Printed using %s
Null Character Does not exist. Exists (\0)


Memory Representation

Memory Representation of Character ('A')

char ch = 'A'

In memory, 'A' takes only 1 byte and only 1 Byte is allocated.

Data Address
65 (A) 1000

Memory Representation of String ("A")

A string takes space equal to the number of characters plus one (for null character).

char ch[]='A'

"A" is stored as ASCII value 65. \0 (null character) is stored at the next memory location. Total memory occupied = 2 Bytes.

Data Address
65 1000
0 1001

Pointer to Strings in C

A pointer to a string is a more efficient and flexible way to handle strings in C. Instead of using an array to store a string, you can use a pointer to reference the first character of the string.

char *name='Moksh'

This declares a pointer name that points to the first character of the string "Moksh".

Memory Representation of Pointer to String

Data Address
1000 2000
'M' 1000
'o' 1001
'k' 1002
's' 1003
'h' 1004
'\0' 1005

Here:

  • 2000 contains the address of the first character (1000).
  • The pointer simply stores the memory address of the string instead of the value.

Benefits of Using Pointer to String

  • Allows dynamic memory allocation.
  • Easy to pass and return strings from functions.
  • Efficient memory usage as it uses only a pointer instead of a full array.

Example of Using Pointer to String:

String using pointer example

Common String Functions

Here are some commonly used string functions in C:

strlen() - String Length Function

Purpose - Returns the length of the string (excluding \0).

strlen function example

strcpy() - String Copy Function

Purpose: Copies content from one string to another.

strcpy function example

Conclusion

Understanding strings and characters in C is fundamental to working with text-based data. You have now learned the difference between a character and a string, how they are stored in memory, and the power of pointers to strings.

Start practicing string manipulation using different functions like strlen(), strcpy(), and explore advanced functions like strcat() and strcmp() to build practical applications.

Note: Always remember that strings in C are null-terminated (\0) and working with pointers can significantly improve your program’s efficiency.

Post a Comment

0 Comments