It depends on what you mean by "empty". If you just want a zero-length string, then your example will work.

This will also work:

buffer[0] = '\0';

If you want to zero the entire contents of the string, you can do it this way:

memset(buffer,0,strlen(buffer));

but this will only work for zeroing up to the first NULL character.

If the string is a static array, you can use:

memset(buffer,0,sizeof(buffer));
Answer from Mysticial on Stack Overflow
🌐
Quora
quora.com › How-do-I-check-if-a-string-is-empty-in-C
How to check if a string is empty in C - Quora
Answer (1 of 10): Many of the answers I have seen here are well-intentioned, but also use outdated methods. Assuming that all strings are ANSI formatted is an incomplete answer. Well, first of all, C does not have a string type. A string in C is essentially a set of characters in contiguous mem...
Discussions

How to check if C string is empty - Stack Overflow
I'm writing a very small program in C that needs to check if a certain string is empty. For the sake of this question, I've simplified my code: #include #include in... 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
string variable unexpectedly becomes an empty string
Both of your scanf calls, as well as your final printf call, are invalid. When reading the name you should use: scanf("%s", name); name will decay to a pointer to its first element. That's a pointer to a char. The %s specifier requires a pointer to a char. The types match — good! When reading the age you should use: scanf("%hu", &age); &age is a pointer to an unsigned short. The %hu specifier requires a pointer to an unsigned short. The types match — good! When printing the age you should use: printf("You are %hu years old\n", age); age is an unsigned short. The %hu specifier requires an unsigned short. The types match — good! It's very important that the types expected by your format specifiers match the types of the arguments you actually pass. If they don't match, the code is invalid. (There are certain cases when you get away with the "wrong" specifier in printf, but it's always best to use the correct one. And scanf isn't so lenient — the types must match there.) More on reddit.com
🌐 r/cprogramming
10
0
February 27, 2024
[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
April 26, 2014
🌐
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.
🌐
Northern Illinois University
faculty.cs.niu.edu › ~winans › CS501 › Notes › cstrings.html
C Strings
The individual characters that ... part of the string, and their contents are irrelevant. A "null string" or "empty string" is a string with a null character as its first character:...
Find elsewhere
🌐
C For Dummies
c-for-dummies.com › blog
Null Versus Empty Strings | C For Dummies Blog
August 12, 2017 - The name array is null string. That doesn’t mean that it has a null character ('\0') at element zero. It means that name hasn’t been assigned a value. Had it been assigned a value, and the contents removed or replaced by a null character at element zero, then it becomes an empty string.
🌐
Cplusplus
cplusplus.com › reference › string › string › empty
std::string::empty
Returns whether the string is empty (i.e. whether its length is 0). This function does not modify the value of the string in any way. To clear the content of a string, see string::clear.
🌐
Reddit
reddit.com › r/cprogramming › string variable unexpectedly becomes an empty string
r/cprogramming on Reddit: string variable unexpectedly becomes an empty string
February 27, 2024 -

For some reason, the name variable becomes empty even though I gave it an input.

Code:

#include <stdio.h>

int main(){
    
    char name[16];
    short unsigned int age;

    printf("What is your name? ");
    scanf("%s", &name);

    printf("How old are you? ");
    scanf("%u", &age);

    printf("Hello, %s.\n", name);
    printf("You are %u years old\n", age);

    return 0;
}

Terminal:

What is your name? Momus
How old are you? 99
Hello, .
You are 99 years old

I seems that the value for name was changed in the part somewhere in the part that prints "How old are you? " and the scanf() for the value of age because it works when I do this.

Code:

#include <stdio.h>

int main(){
    
    char name[25];
    short unsigned int age;

    printf("What is your name? ");
    scanf("%s", &name);
    printf("Hello, %s.\n", name);

    printf("How old are you? ");
    scanf("%u", &age);
    printf("You are %u years old\n", age);

    return 0;
}

Terminal:

What is your name? Momus
Hello, Momus.
How old are you? 99
You are 99 years old

Does anyone know what happened? How do I make it so that the first one will show the input? Thanks!

🌐
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?

🌐
MathWorks
mathworks.com › polyspace bug finder › review analysis results › complete list of polyspace bug finder results › defects › performance defects
Expensive use of string functions from the C standard library - String functions from the C standard library are used inefficiently - MATLAB
Instead of using the string functions, it is more efficient to check for empty C-strings by comparing the first character of the string to null or '\0'. This method reduces the function overhead and makes the code more efficient.
🌐
7-Zip Documentation
documentation.help › C-Cpp-Reference › empty.html
empty - C/C++ Reference Documentation
The empty() function returns true if the string has no elements, false otherwise. ... string s1; string s2(""); string s3("This is a string"); cout.setf(ios::boolalpha); cout << s1.empty() << endl; cout << s2.empty() << endl; cout << s3.empty() << endl;
🌐
Medium
medium.com › nerd-for-tech › representing-the-empty-string-in-c-b1e850a47be8
The Empty String in C#. It’s about readability. | by Michael Moreno | Nerd For Tech | Medium
December 16, 2023 - It’s placed under the category of “Readability Rules”. Other Readability Rules are concerned with whether a comment contains text, or whether a parameter follows a comma. These, and other rules of this type, check that “code is well-formatted and readable”. So fine, StyleCop must be telling us that string.Empty is more readable, because this is a Readability Rule, after all.
🌐
Cppreference
en.cppreference.com › w › cpp › string › basic_string › empty.html
std::basic_string<CharT,Traits,Allocator>::empty
October 18, 2024 - Checks if the string has no characters, i.e. whether begin() == end(). (none) true if the string is empty, false otherwise · Constant. Run this code ·
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › check-if-string-is-empty-in-cpp
How to Check if a String is Empty in C++? - GeeksforGeeks
July 23, 2025 - To check for an empty string we can use the std::string::empty() function because what the empty function does is it checks for the length of a string and if the string is empty, it returns true, otherwise, it returns false.
🌐
Cprogramming
cboard.cprogramming.com › c-programming › 74027-how-test-if-string-empty.html
How to test if a string is empty
January 1, 2006 - if (string[0] == '\0') Unnoticeably... and you couldn't possibly argue that it's impractical to check string length. Besides... it give him a heads up on how to handle it when he, perhaps, has to deal with strings that don't terminate with a null. Besides comparing to two empty quotes.
🌐
Sabe
sabe.io › blog › c-check-string-empty
How to Check if a String is Empty in C | Sabe
March 19, 2022 - The best way to check if a string is empty is to use the strlen() function.
🌐
Quora
quora.com › What-is-the-meaning-of-string-Empty-in-C
What is the meaning of string.Empty() in C#? - Quora
Answer (1 of 4): String.Empty. The string.Empty field is an empty string literal. It is slightly different from an empty string literal constant "". There is a subtle difference—but one that could be significant in some scenarios. It changes the meaning of a program.