memset Function

Copies the specified character into the destination buffer.

Include

<string.h>

Prototype

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

Arguments

s
buffer
c
character to put in buffer
n
number of times

Return Value

Returns the buffer with characters written to it.

Remarks

The character c is written to the buffer n times.

Example

See the notes at the beginning of this chapter or section for information on using printf() or scanf() (and other functions reading and writing the stdin or stdout streams) in the example code.

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

int main(void)
{
  char buf1[20] = "What time is it?";
  char buf2[20] = "";
  char ch1 = '?', ch2 = 'y';
  char *ptr;
  int res;

  printf("memset(\"%s\", \'%c\',4);\n", buf1, ch1);
  memset(buf1, ch1, 4);
  printf("buf1 after memset: %s\n", buf1);

  printf("\n");
  printf("memset(\"%s\", \'%c\',10);\n", buf2, ch2);
  memset(buf2, ch2, 10);
  printf("buf2 after memset: %s\n", buf2);
}

Example Output

memset("What time is it?", '?',4);
buf1 after memset: ???? time is it?

memset("", 'y',10);
buf2 after memset: yyyyyyyyyy