How is a string stored in C?

C has no string type. A string is a char array which ends with the null character '\0'.

#include <stdio.h>
#include <string.h>

int main(void) {
    char s[] = "Hi";     // stored as 'H' 'i' '\0'

    printf("%zu %zu\n", strlen(s), sizeof(s));
    return 0;
}

Output:

Output of the above program is

2 3

The length is 2, but the array needs 3 bytes. See C Strings.


Learn more about the similar topics:
Tutorials
C Arrays
C Two Dimensional Arrays
C Passing Arrays to Functions
C Strings
C String Input and Output
C String Functions
C strlen() Function
Exercises & Assignments
Write a C program to find the sum and average of array elements
Write a C program to find the largest element in an array
Write a C program to print the elements of an array in reverse order
Write a C program to sort an array using bubble sort
Write a C program to search an element in an array
Write a C program to add two matrices
Write a C program to find the length of a string without using strlen
Interview Questions & Answers
What is an array in C?
How is a string stored in C?
How to reverse a string in C?
What is the difference between memcpy() and strcpy() in C?