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
๐ŸŒ
Cprogramming
cboard.cprogramming.com โ€บ c-programming โ€บ 114330-clear-string.html
clear a string
mystring[0] = '\0'; That will effectively clear your string. There is no need to fill it with null characters. But if you want to... ... There is no need to fill it with null characters. But if you want to... Just in case you don't know why you would want to, virtually all code (including the ...
Top answer
1 of 3
2

In C a string is an array of char and the end of the string is marked with a NUL character (aka '\0') which is nothing else than a byte of value 0.

If you want to have an empty string it is sufficient to do

temp[0] = '\0';

or

*temp = '\0';

which is the same.

If you defined something like

char temp[100];

you could also do

memset(temp, '\0', sizeof temp);

This will overwrite all characters, allowing you to fill in new data character by character without having to care about the terminating '\0'.

With dynamic allocation the answer will be different.

It depends a bit on how you want to assign a new value to temp, but in general it is not necessary to clear the old value before assigning a new value.

2 of 3
0

In C, a string is a sequence of consecutive char variables that is terminated by the representation of zero as a character, '\0'.

This value acts as a sentinel for the end of the string and allows the idiomatic "string processing" loop below:

char *p = <some string>;
while(*p)
{
    dosomething(*p);
    p++;
}

Library functions that process strings (i.e. strlen, strcpy, strcat, etc.) use a construct similar to the code above, and when you write your own code that processes arbitrary strings, you will find the null character check useful; even if a string is stored in a char [] array of known length, it decays to a pointer to its first element when passed to a function, losing information about its length.

So, if you want to blank the string, all that is needed is to set the first element of the string to '\0'. If the first value in the string is the null terminator, the condition *p is false and the loop is never entered:

char *p = "\0someotherstuff";
printf("%zu\n", strlen(p));
// Output: 0
๐ŸŒ
Quora
quora.com โ€บ How-do-you-clear-a-string-variable-in-the-C-programming-language
How to clear a string variable in the C programming language - Quora
Answer (1 of 12): From the answers here you can see that simple things are lessons in pain in C. So many primitive and low-level ways to do a single logical high-level requirement. At the logical level, we should not think about memory at all, just set a string reference to new strings. However, ...
๐ŸŒ
Northern Illinois University
faculty.cs.niu.edu โ€บ ~winans โ€บ CS501 โ€บ Notes โ€บ cstrings.html
C Strings
Array elements after the null character are not 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: The length of a null string is 0. ... This declaration creates an unnamed character array just large ...
๐ŸŒ
Cplusplus
cplusplus.com โ€บ reference โ€บ string โ€บ string โ€บ clear
std::string::clear
Erases the contents of the string, which becomes an empty string (with a length of 0 characters). ... This program repeats every line introduced by the user until a line contains a dot ('.'). Every newline character ('\n') triggers the repetition of the line and the clearing of the current string content. Unspecified, but generally constant. Any iterators, pointers and references related to this object may be invalidated.
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ how-to-empty-a-char-array-in-c
How to Empty a Char Array in C? - GeeksforGeeks
July 23, 2025 - strcpy is the predefined function already existing in the string header file. strcpy function helps us to copy elements of one array into another. ... Here first is the element where we need to copy elements and second is the array from which we are copying the elements. ... // C Program empty char array // Using strcpy to clear // the string #include <stdio.h> int main() { // Creating char array char arr[5] = { 'a', 'b', 'c', 'd', 'e' }; printf("Before: "); for (int i = 0; arr[i] != '\0'; i++) printf("%c ", arr[i]); printf("\n"); // Copying elements of empty array in arr strcpy(arr, ""); printf("After: "); for (int i = 0; arr[i] != NULL; i++) printf("%c ", arr[i]); return 0; }
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ what-is-stdbasicstringclear-in-c-cpp
What is std::basic_string::clear in C/C++?
The first string contains a phrase and, using the clear() function, the string is emptied. Upon printing the same string after applying the clear() function, an empty string is returned.
๐ŸŒ
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,...
๐ŸŒ
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!

๐ŸŒ
C For Dummies
c-for-dummies.com โ€บ blog
Null Versus Empty Strings | C For Dummies Blog
An empty string has a single element, the null character, '\0'. Thatโ€™s still a character, and the string has a length of zero, but itโ€™s not the same as a null string, which has no characters at all. ... 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.
๐ŸŒ
Edaboard
edaboard.com โ€บ digital design and embedded programming โ€บ pc programming and interfacing
make a string empty in C | Forum for Electronics
September 15, 2011 - Perhaps if you were to post the entire routine with a more detailed description of its intend purpose I could make a more helpful suggestion. BigDog ... #include<stdio.h> #include<string.h> #include<stdlib.h> #define length(x) strlen(x) int main(int argc,char* argv[]) { char *string; string=(char *)malloc(20); register char special; register short int i=0,a=0; register short int c; FILE *fp; fp=fopen(argv[1],"r"); while(1) { while(!feof(fp)) { a=0; string=(char *)calloc(20,1); while(!feof(fp))// { c=getc(fp); special=c; if((c>=65 && c<=122)) { string[a]=c; } else if(c>=48 && c<=57) { string[a]=c; } else break; ++a; } i=length(string)-1; while(i>=0) { putchar(string[i]); --i; } if(c=='\n')break; if((c>=0 && c<=126)) putchar(special); } if(c==EOF)break; putchar('\n'); } fclose(fp); return 0; }