cs24-23fa memset

Introduction to Computing Systems (Fall 2023)


memset - fill memory with a constant byte


#include <string.h>

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


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


#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()