Strings in C are sequences of characters terminated by a null character (\0). Unlike higher-level languages, C does not have a built-in string type. Instead, strings are implemented as null-terminated character arrays using the char data type.

Key Characteristics

  • Null Termination: Every string must end with a \0 character, which marks the end of the string. This is essential for functions like strlen(), strcpy(), and printf() to work correctly.

  • Array-Based: A string is declared as a char array. For example:

    char greeting[] = "Hello";

    Here, the compiler automatically adds the \0 at the end, making the array size 6 (5 characters + 1 null terminator).

  • String Literals: Double quotes (") are used to define string literals. The compiler stores them in read-only memory and appends \0.

Declaring and Initializing Strings

  • Using string literals:

    char str[] = "Hello";
  • Character-by-character initialization (must include \0):

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

Important Notes

  • No Assignment: You cannot assign a string literal to an array using the assignment operator after declaration:

    char str[10];
    str = "Hello"; // ❌ Error

    Use strcpy() instead:

    strcpy(str, "Hello");
  • Memory Management: When using pointers or dynamic allocation, ensure sufficient space is allocated for the string and the \0 character.

Common String Functions (from <string.h>)

  • strlen(s) – Returns the length of string s (excludes \0).

  • strcpy(dst, src) – Copies src to dst.

  • strcat(dst, src) – Concatenates src to dst.

  • strcmp(s1, s2) – Compares two strings.

  • strncpy, strncat, strstr, strchr, strtok – Additional useful functions.

Example

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

int main() {
    char name[] = "Alice";
    printf("Name: %s\n", name); // Output: Name: Alice
    printf("Length: %zu\n", strlen(name)); // Output: Length: 5
    return 0;
}

Summary

  • C strings are arrays of char ending with \0.

  • They are not a data type but a convention based on null-terminated arrays.

  • Always ensure proper null termination and memory allocation to avoid undefined behavior.

C does not and never has had a native string type. By convention, the language uses arrays of char terminated with a null char, i.e., with '\0'. Functions and macros in the language's standard libraries provide support for the null-terminated character arrays, e.g., strlen iterates over an array of char until it encounters a '\0' character and strcpy copies from the source string until it encounters a '\0'.

The use of null-terminated strings in C reflects the fact that C was intended to be only a little more high-level than assembly language. Zero-terminated strings were already directly supported at that time in assembly language for the PDP-10 and PDP-11.

It is worth noting that this property of C strings leads to quite a few nasty buffer overrun bugs, including serious security flaws. For example, if you forget to null-terminate a character string passed as the source argument to strcpy, the function will keep copying sequential bytes from whatever happens to be in memory past the end of the source string until it happens to encounter a 0, potentially overwriting whatever valuable information follows the destination string's location in memory.

In your code example, the string literal "Hello, world!" will be compiled into a 14-byte long array of char. The first 13 bytes will hold the letters, comma, space, and exclamation mark and the final byte will hold the null-terminator character '\0', automatically added for you by the compiler. If you were to access the array's last element, you would find it equal to 0. E.g.:

const char foo[] = "Hello, world!";
assert(foo[12] == '!');
assert(foo[13] == '\0');

However, in your example, message is only 10 bytes long. strcpy is going to write all 14 bytes, including the null-terminator, into memory starting at the address of message. The first 10 bytes will be written into the memory allocated on the stack for message and the remaining four bytes will simply be written on to the end of the stack. The consequence of writing those four extra bytes onto the stack is hard to predict in this case (in this simple example, it might not hurt a thing), but in real-world code it usually leads to corrupted data or memory access violation errors.

Answer from dgvid on Stack Overflow
🌐
W3Schools
w3schools.com › c › c_strings.php
C Strings
Unlike many other programming languages, C does not have a String type to easily create string variables. Instead, you must use the char type and create an array of characters to make a string in C:
🌐
GeeksforGeeks
geeksforgeeks.org › c language › strings-in-c
Strings in C - GeeksforGeeks
November 14, 2025 - A string is an array of characters terminated by a special character '\0' (null character). This null character marks the end of the string and is essential for proper string manipulation.
Discussions

Does C have a string type? - Stack Overflow
C doesn't have strings. ... Your strcpy will overflow your char array by the way. you need at least a char array of length 14 (13 chars + nul terminator) ... @Grhm strncmp is the wrong function for two reasons, firstly its a cmp function instead of a cpy function, secondly you should use strlcpy ... More on stackoverflow.com
🌐 stackoverflow.com
Why use C strings in C++? - Stack Overflow
Is there any good reason to use C strings in C++ nowadays? My textbook uses them in examples at some points, and I really feel like it would be easier just to use a std::string. More on stackoverflow.com
🌐 stackoverflow.com
How do strings work in C
First: C has no type for strings. It does not, simply out, have strings. Print functions expect a sequence of contiguous chars with a null (0x00) at the end. The same for strings length and other “string” functions. Second: the two first sentences do the same, they declare a variable as a pointer to a sequence of chars and in the same line it assigns a constant value where each element is one of the chars of the string and add a 0 at the end. So you can do string1[4] and string2[3]. Primitive value array pointers can be seen as an array or as a pointer indistinctly More on reddit.com
🌐 r/C_Programming
46
51
October 15, 2025
New to C. If C does not have strings, then what exactly is printf doing?
C does have strings. It doesn't have a "string data type", but it has strings. A string object in C is not a string because the type system says it's a string, it's a string because of the nature of the bytes that make up the object. Specifically, a C string is "a contiguous sequence of characters terminated by and including the first null character". That definition makes no reference to types at all. This might sound a bit pedantic, but it's actually pretty important. Let's say you have an object declared as follows: char str[100]; Does this object identify a string or not? We cannot answer this unless we know the value being stored in the object. The value might be a string, or it might not be. The type system does not tell us. Somebody just asked me "what is a character in C"... but then they deleted the question. I suspect it was going to lead on to "isn't char a data type?" Yes, char is a data type. But in C a character is given the somewhat more abstract definition "member of a set of elements used for the organization, control, or representation of data", as well as the practical definition of a value stored in a single byte. Essentially I see the notion of a character as being a description of a value, not the type of that value. Characters are often stored in char objects, but they can also be stored in other type objects, like unsigned char and int. For the "character as an element of a string" sense, however, one must think of these characters as being in contiguous bytes in memory. One might typically use a char pointer or an array of char to denote such an object, since they allow you to directly address each character in the string individually. A void pointer would not let you do this so easily, for instance, even though it could just as well point to the first character of a string. More on reddit.com
🌐 r/C_Programming
34
66
January 26, 2022
🌐
Programiz
programiz.com › c-programming › c-strings
Strings in C (With Examples)
In this tutorial, you'll learn about strings in C programming. You'll learn to declare them, initialize them and use them for various I/O operations with the help of examples.
🌐
Systems Encyclopedia
systems-encyclopedia.cs.illinois.edu › articles › c-strings
Strings in C - Systems Encyclopedia
Unlike many higher-level programming languages, C does not feature an explicit string type. While C does allow string literals, strings in C are strictly represented as character arrays terminated with a null byte (\0 or NUL).
Top answer
1 of 7
97

C does not and never has had a native string type. By convention, the language uses arrays of char terminated with a null char, i.e., with '\0'. Functions and macros in the language's standard libraries provide support for the null-terminated character arrays, e.g., strlen iterates over an array of char until it encounters a '\0' character and strcpy copies from the source string until it encounters a '\0'.

The use of null-terminated strings in C reflects the fact that C was intended to be only a little more high-level than assembly language. Zero-terminated strings were already directly supported at that time in assembly language for the PDP-10 and PDP-11.

It is worth noting that this property of C strings leads to quite a few nasty buffer overrun bugs, including serious security flaws. For example, if you forget to null-terminate a character string passed as the source argument to strcpy, the function will keep copying sequential bytes from whatever happens to be in memory past the end of the source string until it happens to encounter a 0, potentially overwriting whatever valuable information follows the destination string's location in memory.

In your code example, the string literal "Hello, world!" will be compiled into a 14-byte long array of char. The first 13 bytes will hold the letters, comma, space, and exclamation mark and the final byte will hold the null-terminator character '\0', automatically added for you by the compiler. If you were to access the array's last element, you would find it equal to 0. E.g.:

const char foo[] = "Hello, world!";
assert(foo[12] == '!');
assert(foo[13] == '\0');

However, in your example, message is only 10 bytes long. strcpy is going to write all 14 bytes, including the null-terminator, into memory starting at the address of message. The first 10 bytes will be written into the memory allocated on the stack for message and the remaining four bytes will simply be written on to the end of the stack. The consequence of writing those four extra bytes onto the stack is hard to predict in this case (in this simple example, it might not hurt a thing), but in real-world code it usually leads to corrupted data or memory access violation errors.

2 of 7
18

To note it in the languages you mentioned:

Java:

String str = new String("Hello");

Python:

str = "Hello"

Both Java and Python have the concept of a "string", C does not have the concept of a "string". C has character arrays which can come in "read only" or manipulatable.

C:

char * str = "Hello";  // the string "Hello\0" is pointed to by the character pointer
                       // str. This "string" can not be modified (read only)

or

char str[] = "Hello";  // the characters: 'H''e''l''l''o''\0' have been copied to the 
                       // array str. You can change them via: str[x] = 't'

A character array is a sequence of contiguous characters with a unique sentinel character at the end (normally a NULL terminator '\0'). Note that the sentinel character is auto-magically appended for you in the cases above.

Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › home › cprogramming › c strings in c programming
C Strings in C Programming
June 10, 2012 - A string in C is a one-dimensional array of char type, with the last character in the array being a "null character" represented by '\0'. Thus, a string in C can be defined as a null-terminated sequence of char type values.
🌐
Codecademy
codecademy.com › docs › strings
C | Strings | Codecademy
April 21, 2025 - Strings in C are one-dimensional arrays of characters terminated by a null character '\0'. They are used to store and manipulate sequences of characters such as words or sentences in C programming.
🌐
Swarthmore College
cs.swarthmore.edu › ~newhall › unixhelp › C_strings.html
Strings in C
A string in C is an array of char values terminated by a special null character value '\0'. For example, here is a statically declared string that is initialized to "hi":
🌐
Wikipedia
en.wikipedia.org › wiki › C_string_handling
C string handling - Wikipedia
December 9, 2025 - The C programming language has a set of functions implementing operations on strings (character strings and byte strings) in its standard library. Various operations, such as copying, concatenation, tokenization and searching are supported.
Top answer
1 of 16
23

The only reasons I've had to use them is when interfacing with 3rd party libraries that use C style strings. There might also be esoteric situations where you would use C style strings for performance reasons, but more often than not, using methods on C++ strings is probably faster due to inlining and specialization, etc.

You can use the c_str() method in many cases when working with those sort of APIs, but you should be aware that the char * returned is const, and you should not modify the string via that pointer. In those sort of situations, you can still use a vector<char> instead, and at least get the benefit of easier memory management.

2 of 16
15

A couple more memory control notes:

C strings are POD types, so they can be allocated in your application's read-only data segment. If you declare and define std::string constants at namespace scope, the compiler will generate additional code that runs before main() that calls the std::string constructor for each constant. If your application has many constant strings (e.g. if you have generated C++ code that uses constant strings), C strings may be preferable in this situation.

Some implementations of std::string support a feature called SSO ("short string optimization" or "small string optimization") where the std::string class contains storage for strings up to a certain length. This increases the size of std::string but often significantly reduces the frequency of free-store allocations/deallocations, improving performance. If your implementation of std::string does not support SSO, then constructing an empty std::string on the stack will still perform a free-store allocation. If that is the case, using temporary stack-allocated C strings may be helpful for performance-critical code that uses strings. Of course, you have to be careful not to shoot yourself in the foot when you do this.

🌐
mtlynch.io
mtlynch.io › notes › zig-strings-call-c-code
Using Zig to Call C Code: Strings · mtlynch.io
December 15, 2023 - Even though strlen says the string has length two, the sizeof function tells me the size of the string in bytes, which is three: ... The reason sizeof returns 3 for a two-character string is that C implicitly added a null terminator at the end of the string.
🌐
Reddit
reddit.com › r/c_programming › how do strings work in c
r/C_Programming on Reddit: How do strings work in C
October 15, 2025 -

There are multiple ways to create a string in C:

char* string1 = "hi";
char string2[] = "world";
printf("%s %s", string1, string2)

I have a lot of problems with this:

According to my understanding of [[Pointers]], string1 is a pointer and we're passing it to [[printf]] which expects actual values not references.

if we accept the fact that printf expects a pointer, than how does it handle string2 (not a pointer) just fine

I understand that char* is designed to point to the first character of a string which means it effectively points to the entire string, but what if I actually wanted to point to a single character

this doesn't work, because we are assigning a value to a pointer:

int* a;
a = 8

so why does this work:

char* str;
str = "hi"
Top answer
1 of 30
65
First: C has no type for strings. It does not, simply out, have strings. Print functions expect a sequence of contiguous chars with a null (0x00) at the end. The same for strings length and other “string” functions. Second: the two first sentences do the same, they declare a variable as a pointer to a sequence of chars and in the same line it assigns a constant value where each element is one of the chars of the string and add a 0 at the end. So you can do string1[4] and string2[3]. Primitive value array pointers can be seen as an array or as a pointer indistinctly
2 of 30
17
if we accept the fact that printf expects a pointer, than how does it handle string2 (not a pointer) just fine Arrays decay to a pointer to their first element when passed as arguments to functions. I understand that char* is designed to point to the first character of a string which means it effectively points to the entire string, but what if I actually wanted to point to a single character You increment the pointer to point to the index within the string. If string1 points to "hello", then string1 + 1 or ++string1 points to "ello". Alternatively, you can use &string[1] - the address of character 1 in the string (0-indexed). The subscripting syntax, [] for arrays is really just pointer arithmetic. this doesn't work, because we are assigning a value to a pointer: Because 8 is not stored anywhere in memory, it's just a constant. In this case you're setting the pointer to address 0x00000008 in memory, which is almost definitely not what you want. Normally you would want to say *a = 8 to set the value at the address of a to 8 - but you first need to allocate some memory to write to. so why does this work: Because string literals do have a location in memory - in the .text or .data section of the program. When you assign str = "hi", the compiler encodes "hi" into the compiled executable, and when the process runs this section gets loaded into memory. str then points to this location - which could either be a fixed location or a section-relative location.
🌐
Jonathan Cook
cs.nmsu.edu › ~jcook › posts › c-strings
C: Strings • Jonathan Cook
January 19, 2025 - In a Unix/Linux terminal, you can do the command man ascii to see an ASCII table, man iso-8859-1 to see an ISO 8859 table, and man utf-8 to learn more about one Unicode format. A C string is a sequence (an array) of non-zero character values that end with a character value of zero.
🌐
W3Schools
w3schools.com › cpp › cpp_strings_cstyle.asp
C++ C-Style Strings
The name comes from the C language, which, unlike many other programming languages, does not have a string type for easily creating string variables. Instead, you must use the char type and create an array of characters to make a "string" in C.
🌐
Reddit
reddit.com › r/c_programming › new to c. if c does not have strings, then what exactly is printf doing?
r/C_Programming on Reddit: New to C. If C does not have strings, then what exactly is printf doing?
January 26, 2022 -

Brand new to C, and I am told that there is no string data type. So I am just curious, if that id the case, then how exactly is something like: printf(“Hello World”) a thing?

Top answer
1 of 8
123
C does have strings. It doesn't have a "string data type", but it has strings. A string object in C is not a string because the type system says it's a string, it's a string because of the nature of the bytes that make up the object. Specifically, a C string is "a contiguous sequence of characters terminated by and including the first null character". That definition makes no reference to types at all. This might sound a bit pedantic, but it's actually pretty important. Let's say you have an object declared as follows: char str[100]; Does this object identify a string or not? We cannot answer this unless we know the value being stored in the object. The value might be a string, or it might not be. The type system does not tell us. Somebody just asked me "what is a character in C"... but then they deleted the question. I suspect it was going to lead on to "isn't char a data type?" Yes, char is a data type. But in C a character is given the somewhat more abstract definition "member of a set of elements used for the organization, control, or representation of data", as well as the practical definition of a value stored in a single byte. Essentially I see the notion of a character as being a description of a value, not the type of that value. Characters are often stored in char objects, but they can also be stored in other type objects, like unsigned char and int. For the "character as an element of a string" sense, however, one must think of these characters as being in contiguous bytes in memory. One might typically use a char pointer or an array of char to denote such an object, since they allow you to directly address each character in the string individually. A void pointer would not let you do this so easily, for instance, even though it could just as well point to the first character of a string.
2 of 8
41
In C, there is not default string type. A C string is an array of characters terminated (last character) by null. Printf changes depending on the platform but for something like your pc it’s just formatting an array of characters terminated by a null and inserting it inside the stdout file stream.
🌐
Reddit
reddit.com › r/programming › c strings and a slow descent to madness
r/programming on Reddit: C Strings and a slow descent to madness
April 6, 2023 - C had no good reason for not doing what freakin Pascal already have done: define a string as a struct of length and a pointer. That’s all they should have done, and that single 32bit field wouldn’t have been a big overhead even back then — especially that due to not having the length information available causes plenty redundant length computation which is a ton of unnecessary work (khm gta load times).
🌐
YouTube
youtube.com › watch
C++ String and C Strings - YouTube
Strings in C++ and C Strings. In this video, you will learn about the difference between a C++ String and a C String. In addition, you will learn about how s...
Published   January 16, 2024
🌐
YouTube
youtube.com › programiz
#21 C Strings | C Programming For Beginners - YouTube
#21 C Strings | C Programming For BeginnersIn this video, we will learn about strings in C. With many examples we will show you how to can create strings. Th...
Published   March 9, 2022
Views   135K
🌐
Wikipedia
en.wikipedia.org › wiki › C_string
C string - Wikipedia
March 1, 2025 - C-string (clothing), a specific type of thong, or a brand of women shorts