Memset () function in C

The function void * memset (void * str, int c, size_t n) copies the character c (an unsigned char) to the first n characters of the string pointed to by parameter str.

Declare memset () in C

Below is the declaration for memset () in C:

 void * memset ( void * str , int c , size_t n ) 

Parameters

str - This is a pointer to memory block to fill .

c - This is the value to be set. The value is passed as an int, but the memory fill function uses the unsigned char conversion of this value.

n - This is the number of bytes to be set to the value.

Returns the value

This function returns a pointer to memory str.

For example

The following program C illustrates the usage of memset () in C:

 #include #include int main () { char str [ 50 ]; strcpy ( str , "Hoc C co ban va nang cao tai QTM" ); puts ( str ); memset ( str , '$' , 7 ); puts ( str ); return ( 0 ); } 

Compile and run the above C program to see the results.

According to Tutorialspoint

Previous article: memmove function in C

Next lesson: strcat function in C

4 ★ | 5 Vote

May be interested

  • Strcat function in CPhoto of Strcat function in C
    the char * strcat function (char * dest, const char * src) append the string pointed to by src, at the end of the string pointed to by dest.
  • Strncat () function in CPhoto of Strncat () function in C
    char * strncat (char * dest, const char * src, size_t n) append (string), pointed to by src, at the end of the string pointed to by dest, with a length of up to n characters.
  • The strchr () function in CPhoto of The strchr () function in C
    char * strchr function (const char * str, int c) looks for the first occurrence of the character c (an unsigned char) in the string pointed to by parameter str.
  • The function strcmp () in CPhoto of The function strcmp () in C
    the function int strcmp (const char * str1, const char * str2) compares the string pointed to by sr1 with the string pointed to by srt2.
  • The function strncmp () in CPhoto of The function strncmp () in C
    the function int strncmp (const char * str1, const char * str2, size_t n) compares the first n bytes of str1 and str2.
  • The strcoll () function in CPhoto of The strcoll () function in C
    the function int strcoll (const char * str1, const char * str2) compares strings str1 and str2. the result depends on setting lc_collate.