This documentation is copyright © 1998-2001 Sandro Sigala <sandro@sigala.it>.
All rights reserved.

Released under the GNU General Public License.

Return to index Return to header Previous symbol Next symbol

memset

Prototype

#include <string.h>

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

Description

Fills the first n bytes of the specified buffer dest with the specified character c. Returns the dest parameter.
For example, in the following code we initialize a part of an array:
char buf[9];
memset(buf + 2, 'x', 5);
this is the state of the buf array before and after the call of memset (? means any character):

Implementation

View source
#include <string.h>

void *memset(void *dest, int c, size_t n)
{
    unsigned char *dp = dest;

    while (n-- > 0)
	*dp++ = c;

    return dest;
}

Example

View source
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
    char buf[8];

    memset(buf, 'x', 7);
    memset(buf + 1, 'y', 5);
    buf[7] = '\0';		/* Add the terminating null. */
    printf("%s\n", buf);	/* Prints "xyyyyyx". */

    return EXIT_SUCCESS;
}

References

ISO C 9899:1990 7.11.6.1