C memset() Function

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.
memset(buf, 0, sizeof buf);
| Call | What 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
void *memset(void *ptr, int value, size_t n);
Parameters
| Parameter | Description |
|---|
| ptr | Required. The memory to fill. It must hold at least n bytes. |
| value | Required. The byte that is repeated, often 0 or a character in single quotes. |
| n | Required. How many bytes to write. |
Return Value
| Return Value | Description |
|---|
| ptr pointer | The 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).
#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
- Size with sizeof.
sizeof buf is 8, so all eight bytes are cleared. - Write zeros. Each byte becomes
0, including a natural '\0'. - Print as a string.
%s sees an empty C-string and prints nothing between the quotes. - 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.
#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
- Fill five bytes.
memset(buf, '*', 5) writes '*' into indices 0–4. - Add the terminator.
buf[5] = '\0' makes a safe C-string. - Print with %s. Output is five stars; leftover bytes after index 5 are unused.
- 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.
#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
- Pass the address.
&p is the start of the struct in memory. - Size with sizeof.
sizeof p covers every byte, including padding. - Fields become zero. After the call,
p.x and p.y are both 0. - 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.
About the author
Developer, cloud engineer, and technical writer
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