
- C - Home
- C - Overview
- C - Features
- C - History
- C - Standards
- C - Environment Setup
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Basic Syntax
- C - User Input
- C - printf Function
- C - Format Specifiers
- Lexical Elements in C
- C - Tokens
- C - Keywords
- C - Identifiers
- Variables and Constants
- C - Variables
- C - Constants
- C - Const Qualifier
- C - Linkage
- Data Types and Type Conversions
- C - Data Types
- C - Literals
- C - Escape sequences
- C - Booleans
- C - Integer Promotions
- C - Character Arithmetic
- C - Type Conversion
- C - Type Casting
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Unary Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Miscellaneous Operators
- Decision Making & Control Statements
- C - Decision Making
- C - if statement
- C - if...else statement
- C - if...else if Ladder
- C - Nested if statements
- C - Switch statement
- C - Nested switch statements
- C - Switch Case Using Range
- Loops in C
- C - Loops
- C - For Loop
- C - While Loop
- C - Do...while Loop
- C - For Loop vs While Loop
- C - Nested Loop
- C - Infinite Loop
- C - Break Statement
- C - Continue Statement
- C - Goto Statement
- Functions in C
- C - Functions
- C - Function Prototype
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- C - Predefined Identifier __func__
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- C - Dynamic Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Character Arrays
- C - Special Characters
- Structures and Unions in C
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Dot (.) Operator
- C - Lookup Tables
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Function Pointers
- C - Array of Function Pointers
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Pointer to an Array
- C - Pointers vs. Multi-dimensional Arrays
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Const Pointers & Pointer to Const
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Restrict Keyword
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Initialization of Pointer Arrays
- Storage Classes and Qualifiers
- C - Storage Classes
- Memory Management in C
- C - Memory Management
- C - Memory Address
- Preprocessors in C
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- File Handling in C
- C - File I/O (File Handling)
- C - Input & Output
- Constants and Literals in C
- C - Macros
- C - Header Files
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
- C Programming Resources
- C - Questions & Answers
- C - Quick Guide
- C - Cheat Sheet
- C - Useful Resources
- C - Discussion
- C Online Compiler
Character Array in C
In C, characters and strings are stored differently. A character array stores a sequence of characters, like text. We use single quotes (' ') to define a single character (e.g., 'A') and double quotes (" ") to define a string or a character array (e.g., "A", "Hello").
The main difference is that a single character is stored as its ASCII value, while a string is stored as a sequence of characters ending with a null terminator (\0). In this chapter, we will see single and double quoted character arrays.
Understanding Chracter Arrays in C
A character array is a collection of characters stored in consecutive memory locations, which means each character occupies the next memory slot one after the another. In C, we use character arrays to represent strings, which are enclosed in double quotes and a null terminator (\0) is added at the end of each string to mark its end.
Example of a Character Array
In this example, we declare a character array str[] with 6 elements and print it using %s. Each character is stored one after the other in memory, so the program prints them one by one until it reaches the null terminator \0.
#include <stdio.h> int main() { char str[] = "Hello"; // Creating a character array of 6 chracters printf("%s", str); // Printing the string return 0; }
Below you can see the output of the above program, which displays the string stored in the character array.
Hello
Single Quotes in C
Single quotes (' ') in C are used to represent single character. We can also use them to create a character array by listing characters one by one inside braces {}. Each character is stored internally as its ASCII value.
For example, 'A' represents the character A, but in memory it is stored as the number 65, which is the ASCII value of A.
Note that we cannot use single quotes to store multiple characters together like 'Hi' because that will cause an error.
Example of Single Quoted Character Array
Below is an example showing a character array using single quotes. Here, we declare a character array arr to store the characters 'H', 'i', and '!'. We then print each character one by one in a loop.
#include <stdio.h> int main() { char arr[] = {'H', 'i', '!'}; // Creating a single-quoted character array int i; for(i = 0; i < 3; i++) { printf("%c ", arr[i]); // Prints each character } return 0; }
Below is the output of the above program, which displays each character of the array one by one.
H i !
Double Quotes in C
Double quotes (" ") in C are used to represent string literals, which are stored as character arrays. A string in C always ends with a null character (\0) so that the program knows where the string ends.
For example, "A" may look like a single character, but in memory, it is stored as two characters: 'A' followed by '\0'.
Note: Unlike single quotes, double quotes can store multiple characters as a string.
Example of Double Quoted Character Array
Below is an example showing the use of double quotes. Here, we define a string str with the value "Hi". So, "Hi" is stored as three characters: 'H', 'i', and the null terminator '\0'. That is why the size of the array becomes 3.
#include <stdio.h> int main() { // storing the string "Hi" in a character array (includes '\0' automatically) char str[] = "Hi"; printf("String: %s\n", str); printf("Size of str: %lu\n", sizeof(str)); return 0; }
Following is the output of the above program, which displays the given string and also shows the size of the array (including the null terminator) −
String: Hi Size of str: 3
Common Mistakes
When working with characters and strings in C, it's easy to get confused when to use single quotes (' ') and double quotes (" "). Here are two common mistakes to avoid −
- Using double quotes for a single character −
char c = "A"; // Wrong: "A" is a string (character array), not a single character char c = 'A'; // Correct: 'A' is a single character
Always use single quotes when you want to store just one character.
- Using single quotes for strings −
char str[] = 'Hello'; // Wrong: single quotes can only hold one character char str[] = "Hello"; // Correct: double quotes are used for strings
Always use double quotes when you want to store multiple characters (a string).
Difference between Single and Double Quotes in C
The following table shows the key differences between single quotes and double quotes in C −
Feature | Single Quotes (' ') | Double Quotes (" ") |
---|---|---|
Represents | A single character | A sequence of characters (string) |
Example | 'A' | "A" |
Data Type | char (or integer constant) | char[] (character array) |
Memory Size | 1 byte (for a single character) | Number of characters + 1 (for \0) |
Null Terminator | Not added | Automatically added at the end |
Usage | Individual letters, digits, or symbols | Words, sentences, or multiple characters |
Stored As | ASCII value of the character | Array of ASCII values with \0 at the end |
Conclusion
In this chapter, we learned that single quotes are used to store a single character as its ASCII value, while double quotes store multiple characters as a string ending with a null terminator. We also looked at their differences in character arrays.