In addition to Will Dean's version, the following are common for whole buffer initialization:

char s[10] = {'\0'};

or

char s[10];
memset(s, '\0', sizeof(s));

or

char s[10];
strncpy(s, "", sizeof(s));
Answer from Matt Joiner on Stack Overflow
🌐
Quora
quora.com › How-do-you-initialize-char*-to-an-empty-string-in-C
How to initialize char* to an empty string in C - Quora
An empty string in C - meaning one that would be a legal-formed string that would be regarded as a string of zero-length by the string.h string functions and other functions that operate on strings - is simply "". It is an array of char with a element, and the value of that element is (char) 0 or '\0'; Confusingly, this character in the ASCII and UTF-8 character sets is also called NUL, which is different from the C NULL used for pointers. So, you could initialize a char pointer to the empty string by this
Discussions

c - Initializing a string with the empty string - Stack Overflow
Also is my use of strcmp to compare word to "" ok? ... I just put that in so the if statement is false and doesn't execute, so the string returned is the empty string. Basically I'm wondering if it's ok to initialize an empty string like that and return it in the function. More on stackoverflow.com
🌐 stackoverflow.com
C string initialization - undersized | The FreeBSD Forums
I am trying the following problem on FreeBSD 8.1-RELEASE with gcc 4.2.1. I try to initialize a string, that's correct format according to C guides is: char abcd[5] = "abcd"; In general if I initialize a string that is undersized I get a compiler warning: char abcd[3] = "abcd"; warning... More on forums.freebsd.org
🌐 forums.freebsd.org
May 17, 2012
[Caesar]How to declare an empty string array in C?
First, you should realize that the caesar and vigenere problems don't require you to create an encrypted string for later printing. You can print the result of encrypting each character as you go. Your declaration of c_txt is trying to create an array of i strings. What you could do is compute the length (call it p_len, for example) of the p_txt string, and then define an array whose element type is char and whose dimension is p_len + 1. The extra element is to allow for a null character ('\0') after the encrypted string. That null terminating character is needed if you intend to print the string using printf. More on reddit.com
🌐 r/cs50
2
3
May 29, 2014
Proper way to initialize a string in C - Stack Overflow
In general, if you want to play it safe, it's good to initialize to NULL all pointers; in this way, it's easy to spot problems derived from uninitialized pointers, since dereferencing a NULL pointer will yield a crash (actually, as far as the standard is concerned, it's undefined behavior, but on every machine I've seen it's a crash). However, you should not confuse a NULL pointer to string with an empty ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Unstop
unstop.com › home › blog › strings in c | initialization and string functions (+examples)
Strings In C | Initialization and String Functions (+Examples)
May 30, 2025 - In the programming language C, ... string by creating a new variable that is a character array with no specified length, then giving it no value except for two quotes side-by-side, symbolizing blank space....
🌐
Cprogramming
cboard.cprogramming.com › cplusplus-programming › 12230-setting-empty-string.html
Setting an empty string
March 3, 2002 - so char string[] = ""; // creates ... way but it depends on what you want to do as to which one you should use. A truly "empty" string would be char *pString = NULL; If a tree falls in the forest, and no one is around to see it, do the other trees make fun of it?...
🌐
Northern Illinois University
faculty.cs.niu.edu › ~winans › CS501 › Notes › cstrings.html
C Strings
A "null string" or "empty string" is a string with a null character as its first character: The length of a null string is 0. ... This declaration creates an unnamed character array just large enough to hold the string "Karen" (including room for the null character) and places the address of the first element of the array in the char pointer name:
🌐
GeeksforGeeks
geeksforgeeks.org › c language › strings-in-c
Strings in C - GeeksforGeeks
2 weeks ago - char str[] = "Geeks"; This line declares a character array str and initializes it with the string "Geeks". Internally, this creates an array like: { 'G', 'e', 'e', 'k', 's', '\0'} The null character '\0' is automatically added at the end to ...
Find elsewhere
🌐
University of Chicago
classes.cs.uchicago.edu › archive › 2020 › winter › 15200-1 › lecs › notes › Lec10Strings.html
Strings
Allocated but uninitialized strings have undetermined length - they have an allocated portion, but if there is no '\0' within that allocated portion, then the length will be considered longer than the allocated portion. So be careful! ... Initializing a variable is placing the first values into that variable.
🌐
C For Dummies
c-for-dummies.com › blog
Null Versus Empty Strings | C For Dummies Blog
August 12, 2017 - By the way, this function wouldn’t ... must be initialized before you assault them with a function. Though C doesn’t really have official typing for empty and null strings, you can still tell the difference between them: compare. That’s the secret, should the need ever arise. You must be logged in to post a ...
Top answer
1 of 2
5

Well it's sort of possible, but it won't behave the way you expect. And in more recent versions of C, it's undefined behaviour.

What you did was allocate memory and then throw that pointer away, thus leaking that memory. You replaced the pointer with a non-constant pointer to a string literal (which should make your compiler emit a warning or an error).

This happened to work in your case. Fortunately you didn't try to write to that memory. If you did, chances are that bad stuff will happen.

2 of 2
3

Uh, oh! You allocate memory for a string and store the handle to the allocated memory in w:

char* w = malloc(100*sizeof(char));

In the next line, you overwrite that handle with an immutable string literal:

w = "";

That means that (1) you can no longer free w as you should after using it and (2) that w now points to a string in read-only memory whose modification will lead to undefined behaviour, most likely a crash.

The dynamically allocated memory behaves like an array. C strings are character arrays that contain the valid characters of the string up to a null terminator, '\0'. There fore, setting the first character to the null character will give you an empty string:

*w = '\0';

or

w[0] = '\0';

In the dead branch, you want to fill the character array with the contents of a string, but you assign a read-only literal, too. You can use the function strcpy from <string.h> to fill a character array with a string:

strcpy(w, "Not empty anymore");

You must make sure, however, that the array is big enough to hold the string plus the terminating null character.

🌐
FreeBSD
forums.freebsd.org › development › userland programming and scripting
C string initialization - undersized | The FreeBSD Forums
May 17, 2012 - Size: 6 Why 6? Why not 5? Because the array not has a null character at the end, the function of strlen(3) misses and goes into the not initialized memory. С has not strings, you can use a pointer to a memory.
🌐
Python Examples
pythonexamples.org › c › how-to-create-an-empty-string
How to Create an Empty String in C
In C, you can create an empty string by initializing a character array with a null terminator.
🌐
DEV Community
dev.to › biraj21 › empty-strings-and-zero-length-arrays-how-do-we-store-nothing-1jko
Empty Strings and Zero-length Arrays: How do We Store... Nothing? - DEV Community
June 25, 2024 - When an IntArray is initialized, memory is allocated based on the capacity in bytes, and the address of the allocated memory is stored in the data pointer. The length field is then set to zero. This process is analogous to what higher-level languages like JavaScript do internally when you create an empty array. Knowing that an empty string in C is just a pointer that points to '\0' character feels good.
🌐
Quora
quora.com › How-do-you-define-an-empty-string-in-C-and-the-usage-of-const-empty-strings
How to define an empty string in C++ and the usage of const empty strings - Quora
Answer (1 of 4): const char *str=””; const char str[]=”” as well, except You directly have an array, but You could address str[n] still when it is a pointer, but all values except 0 within the brackets are memory addressing critical, ...
🌐
Log2Base2
log2base2.com › C › string › declaration-and-initialization-of-string-in-c.html
Declaration and initialization of string in c
There are multiple ways we can initialize a string. If we assign string directly with in double quotes, no need to bother about null character. Because compiler will automatically assign the null character at the end of string. ... To store "india", string size 5 is enough but we should give ...
🌐
Reddit
reddit.com › r/cs50 › [caesar]how to declare an empty string array in c?
r/cs50 on Reddit: [Caesar]How to declare an empty string array in C?
May 29, 2014 -

How do I initialize it to anything other than zero or NULL? I just want an empty string array, which I can use to store text with the help of a for loop. I dont want to assign it the function of GetString. If I declare the array in between an operation like.

string c_txt[i] = (p_txt[i] + key) % 26;

where c_txt is the array containing enciphered text, p_txt is array containing plain text, the console pops me an error saying "variable-sized object may not be initialized".

Also, string c_txt = NULL; desent work for obvious reasons. So how do I declare this string array?

P.S : I have a really bad feeling that this is a really dumb question and I`m making a fool of myself in front of the staff. What am I missing?

Top answer
1 of 11
15

You're supposed to set it before using it. That's the only rule you have to follow to avoid undefined behaviour. Whether you initialise it at creation time or assign to it just before using it is not relevant.

Personally speaking, I prefer to never have variables set to unknown values myself so I'll usually do the first one unless it's set in close proximity (within a few lines).

In fact, with C99, where you don't have to declare locals at the tops of blocks any more, I'll generally defer creating it until it's needed, at which point it can be initialised as well.

Note that variables are given default values under certain circumstances (for example, if they're static storage duration such as being declared at file level, outside any function).

Local variables do not have this guarantee. So, if your second declaration above (char *str;) is inside a function, it may have rubbish in it and attempting to use it will invoke the afore-mentioned, dreaded, undefined behaviour.

The relevant part of the C99 standard 6.7.8/10:

If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. If an object that has static storage duration is not initialized explicitly, then:

  • if it has pointer type, it is initialized to a null pointer;
  • if it has arithmetic type, it is initialized to (positive or unsigned) zero;
  • if it is an aggregate, every member is initialized (recursively) according to these rules;
  • if it is a union, the first named member is initialized (recursively) according to these rules.
2 of 11
6
I'm wonder, what is the proper way of initializing a string?

Well, since the second snippet defines an uninitialized pointer to string, I'd say the first one. :)

In general, if you want to play it safe, it's good to initialize to NULL all pointers; in this way, it's easy to spot problems derived from uninitialized pointers, since dereferencing a NULL pointer will yield a crash (actually, as far as the standard is concerned, it's undefined behavior, but on every machine I've seen it's a crash).

However, you should not confuse a NULL pointer to string with an empty string: a NULL pointer to string means that that pointer points to nothing, while an empty string is a "real", zero-length string (i.e. it contains just a NUL character).

char * str=NULL; /* NULL pointer to string - there's no string, just a pointer */
const char * str2 = ""; /* Pointer to a constant empty string */

char str3[] = "random text to reach 15 characters ;)"; /* String allocated (presumably on the stack) that contains some text */
*str3 = 0; /* str3 is emptied by putting a NUL in first position */
🌐
Sololearn
sololearn.com › en › Discuss › 1772085 › are-c-strings-initialized-to-empty-during-initialization
Are C++ Strings Initialized to Empty during Initialization?
Sololearn is the world's largest community of people learning to code. With over 25 programming courses, choose from thousands of topics to learn how to code, brush up your programming knowledge, upskill your technical ability, or stay informed about the latest trends.
🌐
GameDev.tv
community.gamedev.tv › unreal courses › talk
No need to initialize string (i.e. string Guess = "";) - Talk - GameDev.tv
April 29, 2017 - (I code in C++ professionally, but am watching the videos with my kids) String variables are already initialized to an empty string, so the suggested initialization in lecture 17 is a no-op. In some ways I guess it’s good practice (as a type like ‘int’ will no be so initialized and if ...