C wmemset() Function

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

Definition

In C, wmemset() fills a wide-character block with one value. It belongs to the wide-character tools declared in <wchar.h>.

Use wchar_t for wide characters and prefix wide literals with L, as in L"Hello". The examples below use simple C Programming Tutorial patterns and print wide strings with %ls.

Syntax

The following concrete call shows the argument shape before the formal declaration.

C
wmemset(buffer, L'*', 5);
CallWhat it does
wmemset(buffer, L'*', 5)Fills a wide-character block with one value in a typical call.

Include <wchar.h>. Add only the supporting headers actually used by your program.

Library declaration
wchar_t *wmemset(wchar_t *dest, wchar_t wc, size_t count);

Parameters

ParameterDescription
destWritable destination with enough room for the requested result.
wcThe wide character to write, find, fill, or convert.
countMaximum number of wchar_t elements to inspect, copy, append, or transform.

Return Value

Return ValueDescription
Destination pointerThe same destination address passed to the function.

C Examples

Example 1 — Process a block

This example uses wmemset() to process a block, then exposes the result so you can connect the call with its output.

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

int main(void) {
    wchar_t buf[6];
    wmemset(buf, L'*', 5);
    buf[5] = L'\0';
    wprintf(L"%ls\n", buf);
    return 0;
}

How It Works

  1. Set up this case. The program prepares the exact input needed to process a block.
  2. Process a counted block. wmemset(buffer, L'*', 5) uses a count of wchar_t elements rather than bytes.
  3. Read the normal result. The displayed output shows the direct result of the call for this input.
  4. Interpret the output. ***** confirms the behavior demonstrated by this example.

Example 2 — Respect the count

This example uses wmemset() to respect the count, then exposes the result so you can connect the call with its output.

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

int main(void) {
    wchar_t buf[6];
    wmemset(buf, L'*', 5);
    buf[5] = L'\0';
    wprintf(L"%ls\n", buf);
    return 0;
}

How It Works

  1. Choose the edge case. This example changes the input so you can see how wmemset() behaves for respect the count.
  2. Process a counted block. wmemset(buffer, L'*', 5) uses a count of wchar_t elements rather than bytes.
  3. Inspect the status or pointer. The program checks what the function returned before relying on output data.
  4. Interpret the output. ***** confirms the behavior demonstrated by this example.

Example 3 — Add termination when needed

This example uses wmemset() to add termination when needed, then exposes the result so you can connect the call with its output.

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

int main(void) {
    wchar_t buf[6];
    wmemset(buf, L'*', 5);
    buf[5] = L'\0';
    wprintf(L"%ls\n", buf);
    return 0;
}

How It Works

  1. Prepare a practical variation. The data is arranged specifically to add termination when needed.
  2. Process a counted block. wmemset(buffer, L'*', 5) uses a count of wchar_t elements rather than bytes.
  3. Notice the safety rule. The example keeps the count, destination capacity, stream state, and terminator requirements visible.
  4. Interpret the output. ***** confirms the behavior demonstrated by this example.

Frequently Asked Questions

wmemset() fills a wide-character block with one value. It is part of the standard wide-character library.
Its parameters are dest, wc, count. dest: Writable destination with enough room for the requested result. wc: The wide character to write, find, fill, or convert. count: Maximum number of wchar_t elements to inspect, copy, append, or transform.
Destination pointer means the same destination address passed to the function.
Include <wchar.h> for wmemset(). Include only the supporting headers used by your program, such as <stdio.h> for FILE or <stdarg.h> for va_list.
The count is measured in wchar_t elements, not bytes. Counted memory functions do not automatically append L'\0'.

Next: C wprintf() Function

Continue learning C wide-character functions.

Next 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