C strings are one-dimensional arrays of characters terminated by a null character (\0). Unlike higher-level languages, C does not have a built-in string data type; instead, strings are implemented as arrays of char with a \0 at the end to mark the string's termination.

Key Characteristics:

  • Null-terminated: The \0 character is essential; without it, string functions may behave unpredictably.

  • Declared using char: Strings are declared with char followed by the name and square brackets, e.g., char str[].

  • Initialized in multiple ways:

    • Using a string literal: char str[] = "Hello"; (compiler adds \0 automatically).

    • Character-by-character: char str[] = {'H', 'e', 'l', 'l', 'o', '\0'};.

Common Operations:

  • Printing: Use printf("%s", str); to print a string.

  • Input: Use scanf("%s", str) (stops at whitespace) or fgets(str, size, stdin) (reads entire line, including spaces).

  • Length: Use strlen(str) from <string.h> to get the length (excludes \0).

  • Copying/Comparing: Use strcpy, strcat, strcmp, etc., from <string.h>.

Note: Always ensure the destination array is large enough to hold the string and the null terminator to prevent buffer overflows.

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
What is a literal string & char array in C? - Stack Overflow
Firstly, I included C++ as C++ is just a parent of C, so I'm guessing both answers apply here, although the language I'm asking about and focusing on in this question is C, and not C++. So I began More on stackoverflow.com
🌐 stackoverflow.com
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
Nylon strings to play in Drop C
this doesnt really work. nylon strings arent as responsive/ loud as steel strings, so tuning any set down that far will make the strings very very loose, and they wont play well, or loud enough. however, dont put steel strings on your classical guitar! More on reddit.com
🌐 r/classicalguitar
9
1
December 22, 2018
🌐
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).
🌐
Programiz
programiz.com › c-programming › c-strings
Strings in C (With Examples)
When the compiler encounters a sequence of characters enclosed in the double quotation marks, it appends a null character \0 at the end by default. ... Here, we have declared a string of 5 characters.
🌐
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.
Find elsewhere
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.

Top answer
1 of 2
9

A string literal is an unnamed string constant in the source code. E.g. "abc" is a string literal.

If you do something like char str[] = "abc";, then you could say that str is initialized with a literal. str itself is not a literal, since it's not unnamed.

A string (or C-string, rather) is a contiguous sequence of bytes, terminated with a null byte.

A char array is not necessarily a C-string, since it might lack a terminating null byte.

2 of 2
6

What is a literal string & char array in C?

C has 2 kinds of literals: string literals and compound literals. Both are unnamed and both can have their address taken. string literals can have more than 1 null character in them.

In the C library, a string is characters up to and including the first null character. So a string always has one and only one null character, else it is not a string. A string may be char, signed char, unsigned char.

Copy//          v---v string literal 6 char long
char *s1 = "hello";
char *s2 = "hello\0world";
//          ^----------^  string literal 12 char long

char **s3 = &"hello";  // valid

//        v------------v  compound literal
int *p1 = (int []){2, 4};
int **p2 = &(int []){2, 4};  // vlaid 

C specifies the following as constants, not literals, like 123, 'x' and 456.7. These constants can not have their address taken.

Copyint *p3 = &7; // not valid

C++ and C differ in many of these regards.


A chararray is an array of char. An array may consist of many null characters.

Copychar a1[3];          // `a1` is a char array size 3
char a2[3] = "123";  // `a2` is a char array size 3 with 0 null characters 
char a3[4] = "456";  // `a3` is a char array size 4
char a4[] = "789";   // `a4` is a char array size 4
char a5[4] = { 0 };  // `a5` is a char array size 4, all null characters

The following t* are not char arrays, but pointers to char.

Copychar *t1;
char *t2 = "123";
int *t3 = (char){'x'};  
🌐
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.
🌐
WsCube Tech
wscubetech.com › resources › c-programming › strings
Strings in C Programming (Define, Declare, Initialize, Examples)
August 29, 2025 - Learn how to define, declare, and initialize strings in C programming with clear examples. Understand the basics of working with strings in C. Read now!
🌐
Quora
quora.com › What-is-a-string-in-C-along-with-its-advantages-and-disadvantages
What is a string in C along with its advantages and disadvantages? - Quora
Answer: In c strings are nothing but character array .You can declare a string like- char name[]={‘s’,’o’,’u’,’v’,’n’,’i’,’k’,’\0’}; or char s[] = "souvanik"; OR, char c[50] = "souvanik"; ‘\0’ represent end ...
🌐
Northern Illinois University
faculty.cs.niu.edu › ~mcmahon › CS241 › Notes › cstrings.html
C Strings
However, an array of char is not by itself a C string. A valid C string requires the presence of a terminating "null character" (a character with ASCII value 0, usually represented by the character literal '\0'). Since char is a built-in data type, no header file needs to be included to create ...
🌐
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.
🌐
YouTube
youtube.com › watch
C string functions
Enjoy the videos and music you love, upload original content, and share it all with friends, family, and the world on YouTube.
🌐
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
🌐
W3Schools
w3schools.com › c › c_ref_string.php
C string (string.h) Library Reference
C Examples C Real-Life Examples C Exercises C Quiz C Code Challenges C Compiler C Syllabus C Study Plan C Interview Q&A C Certificate ... The <string.h> library has many functions that allow you to perform tasks on strings.
🌐
Quora
quora.com › What-is-the-syntax-of-a-string-in-C-programming
What is the syntax of a string in C programming? - Quora
Answer: C doesn't directly support string data type directly unlike Java or other languages. Here we use an array of characters which is indirectly an string. As we know an array 8s a collection of homogeneous elements ie if same datatype, here ...
🌐
Unstop
unstop.com › home › blog › strings in c | initialization and string functions (+examples)
Strings In C | Initialization and String Functions (+Examples)
May 30, 2025 - Strings in C are 1D character arrays/ sequences of characters that terminate with a null character. There are 4 ways to initialize them in a C program.
🌐
Yale University
cs.yale.edu › homes › aspnes › pinewiki › C(2f)Strings.html
C/Strings
Because delimited strings are more lightweight, C went for delimited strings. A string is a sequence of characters terminated by a null character '\0'. Note that the null character is not the same as a null pointer, although both appear to have the value 0 when used in integer contexts.
🌐
GeeksforGeeks
geeksforgeeks.org › c language › string-functions-in-c
C String Functions - GeeksforGeeks
July 26, 2025 - C language provides various built-in functions that can be used for various operations and manipulations on strings. These string functions make it easier to perform tasks such as string copy, concatenation, comparison, length, etc.