I am new in programming so very less idea about the C libraries.
Problem-I have a string temp
, i want to empty it and store new string in temp
.Is there any built in function in C to clear the string like clear()
in C++?

- 20,656
- 7
- 53
- 85

- 21
- 1
- 1
- 1
-
1Strings in C are just character arrays. Either free (dynamically allocated) or overwrite them. – meowgoesthedog Mar 12 '19 at 18:11
-
You can dynamically allocate the array of char like this `char *str = strdup("");` , but don't forget to free it. You can also initialize it like `char *str = "";` – Krishna Mar 12 '19 at 18:14
-
Something like `string[0] = '\0'` or `realloc(string, 0)`?f – Artyer Mar 12 '19 at 18:15
-
1I tried to answer based on the little information in your question. To get better answers you should add some source code to the question showing how you think it should work. – Bodo Mar 12 '19 at 18:21
3 Answers
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.

- 9,287
- 1
- 13
- 29
-
We oldsters just do `*temp = 0;` and it always worked, but I'm sure someone will tell me that there's a reason that's not ok anymore. ;) – little_birdie Mar 12 '19 at 18:20
-
@little_birdie Of course `*temp` is the same as `temp[0]`, but assigning `0` may result in a compiler warning because `0` is not of type `char`. Although it does work I don't want to propose bad coding style. – Bodo Mar 12 '19 at 18:25
-
2
-
2
-
I just tried it on gcc, t.c: `int main() { char s[100]; *s = 0; }` then `gcc -Wpendantic t.c` compiles with no warning. So yes, `char` is still an integer type in c. Here's a SO on that topic: https://stackoverflow.com/questions/11698535/is-char-a-special-type-of-integer-variable – little_birdie Mar 12 '19 at 18:37
-
@little_birdie I don't want to discuss the types here. It seems to be OK to assign `0` to a char, but in contrast to `0` the `char` literal `'\0'` makes it clear to the human reader that the code should deal with characters. – Bodo Mar 12 '19 at 19:00
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

- 20,656
- 7
- 53
- 85
If you need to „secure erase” the data, memset (stringname, 0, strlen(stringname));
will wipe the current content. But if you already blanked the string by setting the 1st character to 0, it'll not work because string length is already 0.
For wiping password strings in structures etc., it's safer to keep the full string length and always call memset (stringname, 0, fullstringlength);
If you simply want to write new content to temp, you don't even need to clean it first. strcpy (temp, "the_new_content");
will work. But sometimes cleaning is useful:
void AddToLog (char* news)
{
strcat (temp, news); //Append new part
if (strlen (temp) >= worth_saving) //Log is long enough
{
FlushLog(temp); //Write it somewhere
temp[0]=0; //Empty the log
}
}

- 20,656
- 7
- 53
- 85

- 339
- 1
- 9