C memset() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
string.h

Definition

In C, memset() fills a block of memory with one repeated byte. memset(buf, 0, 10) writes ten zero bytes.

You pass the memory, the byte value, and the count. All three are required. The result is the same pointer you passed in. Include <string.h>.

Syntax

Pass the block, the fill byte, and the count. Start with this call.

C
memset(buf, 0, sizeof buf);
CallWhat it does
memset(buf, 0, sizeof buf)Fills the whole array with zero bytes.

Include <string.h>. Only the low 8 bits of the value are used.

Library declarations
C
void *memset(void *ptr, int value, size_t n);

Parameters

ParameterDescription
ptrRequired. The memory to fill. It must hold at least n bytes.
valueRequired. The byte that is repeated, often 0 or a character in single quotes.
nRequired. How many bytes to write.

Return Value

Return ValueDescription
ptr pointerThe same pointer you passed in.

C Examples

Example 1 — Zero a Buffer

The most common pattern: clear every byte with memset(buf, 0, sizeof buf).

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

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

    memset(buf, 0, sizeof buf);
    printf("buf = \"%s\"\n", buf);
    printf("first byte = %d\n", (unsigned char)buf[0]);

    return 0;
}

How It Works

  1. Size with sizeof. sizeof buf is 8, so all eight bytes are cleared.
  2. Write zeros. Each byte becomes 0, including a natural '\0'.
  3. Print as a string. %s sees an empty C-string and prints nothing between the quotes.
  4. Check the first byte. Printing as a number confirms the fill value is 0.

Example 2 — Fill with a Character

Fill part of a buffer with '*', then add '\0' so it is a valid C-string.

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

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

    memset(buf, '*', 5);
    buf[5] = '\0';
    printf("buf = %s\n", buf);

    return 0;
}

How It Works

  1. Fill five bytes. memset(buf, '*', 5) writes '*' into indices 0–4.
  2. Add the terminator. buf[5] = '\0' makes a safe C-string.
  3. Print with %s. Output is five stars; leftover bytes after index 5 are unused.
  4. Do not fill the whole array. Filling all 8 bytes with '*' would leave no room for '\0'.

Example 3 — Zero a Struct

Clear every byte of a struct before use. Handy when you want all fields (and padding) set to zero.

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

struct Point {
    int x;
    int y;
};

int main(void) {
    struct Point p;

    memset(&p, 0, sizeof p);
    printf("p = (%d, %d)\n", p.x, p.y);

    return 0;
}

How It Works

  1. Pass the address. &p is the start of the struct in memory.
  2. Size with sizeof. sizeof p covers every byte, including padding.
  3. Fields become zero. After the call, p.x and p.y are both 0.
  4. Note on floats. Zero-bytes work for integers and pointers; for floating types, prefer explicit = 0.0 or = {0} when clarity matters.

Frequently Asked Questions

memset() fills n bytes of memory with one repeated byte value.
All three are required. ptr is the memory, value is the byte, and n is how many bytes to write.
The same ptr you passed in.
Include <string.h>.
No. It only writes the repeated byte. Add '\0' yourself if you need a C-string.

Next: strcasecmp()

Compare two strings without caring about letter case.

strcasecmp() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful