Latest web development tutorials

C library functions - memset ()

C standard library - <string.h> C standard library - <string.h>

description

The first ncharacters C library functionsvoid * memset (void * str,int c, size_t n) Copies charactersc (anunsigned character) to the argumentstrpoints to the string.

statement

Here is the memset () function's declaration.

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

parameter

  • str - point to fill the memory block.
  • c - the value to be set.This value is passed as an int, but the function when the memory block is filled using the form unsigned char values.
  • n - the number of bytes to be set to that value.

return value

This value returns a pointer to a pointer to the storage area str.

Examples

The following example demonstrates the memset () function is used.

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

int main ()
{
   char str[50];

   strcpy(str,"This is string.h library function");
   puts(str);

   memset(str,'$',7);
   puts(str);
   
   return(0);
}

Let's compile and run the above program, which will result in the following:

This is string.h library function
$$$$$$$ string.h library function

C standard library - <string.h> C standard library - <string.h>