cs24-21fa memset

Introduction to Computing Systems (Fall 2021)

Name

memset - fill memory with a constant byte

Synopsis

#include <string.h>

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

Description

The memset() function fills the first n bytes of the memory area pointed to by s with the constant byte c.

Example

#include <assert.h>
#include <string.h>

int main() {
    char s[7] = "hello!";
    
    // Fill string s with null bytes, making it an empty string.
    memset(s, '\0', sizeof(s));
    assert(strcmp(s, "") == 0);
}

Return Value

Upon completion, the memset() function returns a pointer to the memory area s.

See Also

memcpy(), memmove(), strcpy(), strncpy()