C String Functions - C memset






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

Copies the character c (an unsigned char) to the first n characters of the string str.

The argument str is returned.

Prototype

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

Parameter

This function has the following parameter.

ptr
Pointer to the block of memory to fill.
value
Value to be set.
num
Number of bytes to be set to the value.

size_t is an unsigned integral type.

Return

ptr is returned.

Example


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

int main (){
  char str[] = "this is a test!";
  memset (str,'-',6);
  puts (str);
  return 0;
}        

The code above generates the following result.





Example 2


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
 //ww w .  j  av a2s .c om
int main(void)
{
    char str[] = "this is a test";
    puts(str);
    memset(str,'a',5);
    puts(str);
}

The code above generates the following result.