C programming memset – memset C Library Function

C programming memset: 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

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

C program using memset function

Memset function in c: The following program shows the use of memset function to fill characters inside string.

memset C Library Function

#include <stdio.h>
#include <string.h>
#include <conio.h>
 
int main()
{
   char string[100];
 
   printf("Enter a string\n");
   gets(string);
    
   memset(string,'*', strlen(string)/2);
   printf("String after doing memset\n%s", string);
 
   getch();
   return(0);
}

Output

Enter a string
TechCrashCourse
String after doing memset
*******shCourse