C memset_explicit() Function

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

Definition

In C23, memset_explicit() stores a byte value throughout a memory block, like memset(). Its special purpose is to ensure that the writes are performed even when the compiler sees that the memory is never read again.

This makes it useful for clearing passwords, keys, and other sensitive bytes before storage is released or reused. It is a C23 library feature, so older C libraries may not declare or implement it.

Syntax

Start with this concrete call, then adjust the values for your buffer.

C
memset_explicit(secret, 0, sizeof secret);
CallWhat it does
memset_explicit(secret, 0, n)Writes zero into each of the n bytes and preserves the wipe.

Include <string.h> and compile against a C23 library that provides this function.

Library declaration
C
void *memset_explicit(void *s, int c, size_t n);

Parameters

ParameterDescription
sRequired. Pointer to the writable memory block.
cByte value to store. It is converted to unsigned char.
nNumber of bytes to overwrite.

Return Value

Return ValueDescription
sReturns the same pointer that was passed as the destination.

C Examples

Example 1 — Wipe a Password Buffer

Use the function after the program has finished checking a password.

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

int main(void) {
    char password[16] = "open-sesame";

    printf("used password length = %zu\n", strlen(password));
    memset_explicit(password, 0, sizeof password);
    printf("first byte after wipe = %d\n", password[0]);
    return 0;
}

How It Works

  1. Store the temporary secret. The writable array holds the password while it is needed.
  2. Finish using it. The example prints only its length, not the secret itself.
  3. Overwrite every slot. sizeof password wipes all 16 bytes, including unused capacity.
  4. Keep the store observable. C23 requires the explicit wipe to happen even if the buffer is not used later.

Example 2 — Clear a Binary Key

Sensitive data is often binary, so clear the whole object rather than using strlen().

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

int main(void) {
    unsigned char key[8] = {0x21, 0x48, 0x90, 0xAB,
                            0xCD, 0xEF, 0x12, 0x34};

    printf("key byte before = %u\n", key[0]);
    memset_explicit(key, 0, sizeof key);
    printf("key byte after  = %u\n", key[0]);
    return 0;
}

How It Works

  1. Use an unsigned byte array. Every possible key byte value can be represented directly.
  2. Measure storage, not text. sizeof key is eight even if the data contains zero bytes.
  3. Fill with zero. The call overwrites each element of the key array.
  4. Avoid a string length. strlen would be wrong because a binary key is not a C-string.

Example 3 — Wipe a Sensitive Structure

A whole structure can be cleared through its address and size.

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

struct Login {
    char user[12];
    char token[24];
};

int main(void) {
    struct Login login = {"Ada", "temporary-token"};

    printf("user = %s\n", login.user);
    memset_explicit(&login, 0, sizeof login);
    printf("cleared = %s\n", login.user[0] == '\0' ? "yes" : "no");
    return 0;
}

How It Works

  1. Group related fields. The structure stores a user name and temporary token together.
  2. Pass its address. &login points to the first byte of the object.
  3. Cover the complete object. sizeof login includes both arrays and any padding.
  4. Confirm the visible field. After the wipe, the first user-name byte is zero.

Frequently Asked Questions

It fills n bytes and guarantees the operation is performed even when the memory is not read afterward.
An optimizer may remove an ordinary memset used only to wipe dead data; memset_explicit is intended not to be removed.
It is a C23 library function declared in string.h when the implementation supports it.
It returns the same destination pointer passed as its first argument.
No. It wipes only the specified memory block, not copies in other buffers, registers, logs, or backups.

Next: strcasecmp()

Compare two C-strings without treating letter case as different.

strcasecmp() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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