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

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

or

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

or

Copychar 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
Answer (1 of 8): 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 [code ]""[/code]. It is an array of [code ]char [/code]with a element,...
๐ŸŒ
Cprogramming
cboard.cprogramming.com โ€บ cplusplus-programming โ€บ 12230-setting-empty-string.html
Setting an empty string
so char string[] = ""; // creates ... 50 characters with the initial string of \0. You are right either 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 ...
๐ŸŒ
Northern Illinois University
faculty.cs.niu.edu โ€บ ~winans โ€บ CS501 โ€บ Notes โ€บ cstrings.html
C Strings
Since char is a built-in data type, no header file needs to be included to create a C string. The C library header file <cstring> contains a number of utility functions that operate on C strings. Here are some examples of declaring C strings as arrays of char: char s1[20]; // Character array - can hold a C string, but is not yet a valid C string char s2[20] = { 'h', 'e', 'l', 'l', 'o', '\0' }; // Array initialization char s3[20] = "hello"; // Shortcut array initialization char s4[20] = ""; // Empty or "null" C string of length 0, equal to the string literal ""
๐ŸŒ
University of Chicago
classes.cs.uchicago.edu โ€บ archive โ€บ 2020 โ€บ winter โ€บ 15200-1 โ€บ lecs โ€บ notes โ€บ Lec10Strings.html
Strings
char str1[] = "Hello world"; // allocate 12, initialize to "Hello world" char str2[50] = "Hello world"; // allocate 50, initialize to "Hello world" char str3[] = {'H','e','l','l','o',' ','w','o','r','l','d', '\0'}; // allocate 12, initialize to "Hello world" char str4[12] = {'H','e','l','l','o',' ','w','o','r','l','d', '\0'}; // allocate 12, initialize to "Hello world" char str5[20] = "" // allocate 20, but initialize to empty string ยท Strings are much like arrays, except for two constraints: ... The length is determined by the location of '\0' This changes what the loop looks like to step through the string. Let's find the length of the string (the number of characters before '\0'): unsigned int stringlength(char s[]) { int length = 0; for (length = 0; s[length] != '\0'; length++) ; // all of the work is being done in the for loop syntax!
๐ŸŒ
C For Dummies
c-for-dummies.com โ€บ blog
Null Versus Empty Strings | C For Dummies Blog
The string is empty. The strlen() returns an unpredictable value for the null[] array because itโ€™s not initialized. Therefore the string length is wherever the next random null character is found in memory.
๐ŸŒ
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....
๐ŸŒ
Python Examples
pythonexamples.org โ€บ c โ€บ how-to-create-an-empty-string
How to Create an Empty String in C
#include <stdio.h> int main() { char emptyString[] = "\0"; printf("Empty string created: '%s'", emptyString); return 0; }
Find elsewhere
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.

๐ŸŒ
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?
April 26, 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?

๐ŸŒ
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, even negative indexing seems to be possible here, also in arr...
๐ŸŒ
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.
๐ŸŒ
Guru99
guru99.com โ€บ home โ€บ c programming โ€บ strings in c: how to declare & initialize a string variables in c
Strings in C: How to Declare & Initialize a String Variables in C
August 8, 2024 - In string3, the NULL character must be added explicitly, and the characters are enclosed in single quotation marks. โ€˜Cโ€™ also allows us to initialize a string variable without defining the size of the character array.
๐ŸŒ
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.
๐ŸŒ
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 the read operation on cin fails, an int variable will continue to be uninitialized).
๐ŸŒ
Log2Base2
log2base2.com โ€บ C โ€บ string โ€บ declaration-and-initialization-of-string-in-c.html
Declaration and initialization of string in c
If we assign character by character , we must specify the null character '\0' at the end of the string. ... All four methods are perfectly valid. ... Interactive pattern-based practice for Bank, SSC, Railway & Defence exams โ€” completely free.