Use memset function to initialize a string

Syntax

C memset function has the following syntax.

void *memset(void *buf, int ch, size_t count);

C memset function is from header file string.h.

Description

C memset function copies ch into the first count characters of *buf and returns buf.

The most common use of memset() is to initialize a region of memory to some value.

Example

Use C memset function to initialize a string.


#include <string.h>
#include <stdio.h>
/*from  www .  j a  va  2  s.  co  m*/
int main(void){
  char buf[100];
  memset(buf, '\0', 100);
  memset(buf, 'X', 10);
  printf(buf);
}

The code above generates the following result.





















Home »
  C Language »
    Function Reference »




assert.h
ctype.h
math.h
setjmp.h
signal.h
stdio.h
stdlib.h
string.h
time.h
wctype.h