C strnset() Function

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

Definition

In C, strnset() fills the first n characters of a writable C-string with one byte and leaves the rest alone. The terminator stays in place when n stays inside the string. It is an extension, not ISO C.

You pass the string, the fill byte, and the count. All three are required. Prefer a portable loop when you need standard C.

Syntax

Pass a writable string, the fill character, and the count.

C
strnset(word, '*', 3);
CallWhat it does
strnset(word, '*', 3)Replaces the first 3 characters with '*'.

Not ISO C. Never pass a string literal. On Microsoft compilers the name is often _strnset.

Library declarations
C
char *strnset(char *str, int c, size_t n);
char *_strnset(char *str, int c, size_t n);

Parameters

ParameterDescription
strRequired. A writable C-string. Do not pass a string literal.
cRequired. The fill byte, often a character in single quotes.
nRequired. How many characters to overwrite.

Return Value

Return ValueDescription
str pointerThe same pointer you passed in.

C Examples

Example 1 — Portable Prefix Fill

ISO C approach: overwrite up to n characters, stop at '\0'.

C
#include <stdio.h>

void str_nset(char *s, char c, int n) {
    int i;
    for (i = 0; i < n && s[i] != '\0'; i++) {
        s[i] = c;
    }
}

int main(void) {
    char s[] = "Hello";
    str_nset(s, '*', 3);
    printf("%s\n", s);
    return 0;
}

How It Works

  1. Writable array. char s[] can be modified.
  2. Loop with two stops. End after n writes or at '\0'.
  3. Replace three chars. H e l become * * *.
  4. Rest unchanged. lo and the terminator remain.

Example 2 — Using strnset (Where Available)

On MSVC and some older compilers, call strnset / _strnset directly.

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

int main(void) {
    char s[] = "Code";

    /* MSVC: prefer _strnset(s, '-', 2); */
    strnset(s, '-', 2);
    printf("%s\n", s);
    return 0;
}

How It Works

  1. Start with "Code". Four letters plus '\0'.
  2. Fill two positions. First two become '-'.
  3. Result. Buffer prints as --de.
  4. Toolchain check. If strnset is missing, use Example 1.

Example 3 — When n Exceeds Length

If n is larger than the string length, only characters before '\0' are changed—the terminator is not filled.

C
#include <stdio.h>

void str_nset(char *s, char c, int n) {
    int i;
    for (i = 0; i < n && s[i] != '\0'; i++) {
        s[i] = c;
    }
}

int main(void) {
    char s[] = "Hi";
    str_nset(s, '#', 10);
    printf("%s\n", s);
    return 0;
}

How It Works

  1. String is short. "Hi" has length 2.
  2. n = 10. The loop still stops at '\0' after two writes.
  3. Result. Both letters become '#'; the terminator remains.
  4. vs memset. memset(s, '#', 10) would write past the string—dangerous here.

Frequently Asked Questions

strnset() fills the first n characters of a writable C-string with one byte.
All three are required. str is the string, c is the fill byte, and n is the count.
The same str pointer you passed in.
No. It is an extension. Prefer a portable loop for ISO C.
No. String literals must not be modified.

Next: strpbrk()

Find the first character in a string that matches any in a set.

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