memset C Library Function

The function void *memset (void *ptr, int c, size_t n); copies the character c to the first n bytes of the block of memory pointed by ptr.

Function prototype of memset

void *memset (void *ptr, int c, size_t n);
  • ptr : This is pointer to the block of memory to fill.
  • c : This is the value to be set in first n bytes of block of memory. The value is passed as an int, but the function populates the block of memory using it's character equivalent.
  • n : This is the number of bytes to be filled.

Return value of memset

It returns a pointer to the block of memory after populating it.

C program using memset function

The following program shows the use of memset function to fill characters inside string.

#include <stdio.h>
#include <string.h>
#include <conio.h>

int main()
   char string[100];

   printf("Enter a string\n");
   memset(string,'*', strlen(string)/2);
   printf("String after doing memset\n%s", string);


Enter a string
String after doing memset