In C programming language, strings are represented as arrays of characters. Sometimes, we may need to clear a string value to either reset it or free up memory. Clearing a string means setting all its character elements to null or empty values. In this article, we will explore different methods to clear a string value in C.
Method 1: Using memset()
One way to clear a string in C is by using the memset() function. The memset() function sets a block of memory with a particular value, which can be used to clear the string. Here’s an example:
“`c
#include
#include
int main() {
char str[100] = “Hello, world!”;
// Clear the string using memset()
memset(str, ‘