C Standard Library

C <string.h> - memset() Function

The C <string.h> memset() function converts value to unsigned char and copies it into each of the first num characters of the object pointed to by ptr.

The behavior is undefined if num is greater than the size of the object pointed to by ptr.


void * memset ( void * ptr, int value, size_t num );                


ptr Specify pointer to the object to fill.
value Specify the fill byte .
num Specify the number of characters to fill.
size_t is an unsigned integer type.

Return Value

Returns ptr.


The example below shows the usage of memset() function.

#include <stdio.h>
#include <string.h>
int main (){
  char str[50] = "Hello World!";

  //displaying str
  printf("str is: %s\n", str);

  //setting first 5 characters of str to $ 
  memset(str, '$', 5);

  //displaying str
  printf("str is: %s\n", str);
  return 0;

The output of the above code will be:

str is: Hello World!
str is: $$$$$ World!

❮ C <string.h> Library