C strset() Function

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

Definition

In C, strset() fills every character of a writable C-string with one byte and leaves the ending '\0' in place. It is an extension and is not part of ISO C.

You pass the string and the fill byte. Both are required. Prefer a portable loop or memset when you need standard C.

Syntax

Pass a writable string and the fill character. Start with this call.

C
strset(word, '*');
CallWhat it does
strset(word, '*')Replaces every character in word with '*', and keeps the ending '\0'.

Not ISO C. Never pass a string literal. A portable loop or memset works everywhere.

Library declarations
C
char *strset(char *str, int c);

Parameters

ParameterDescription
strRequired. A writable C-string. Do not pass a string literal.
cRequired. The fill byte, often a character in single quotes.

Return Value

Return ValueDescription
str pointerThe same pointer you passed in.

C Examples

Example 1 — Fill With Asterisks

Replace every visible character in a writable array. Same idea as extension strset, done with a portable loop.

C
#include <stdio.h>

char *str_set(char *s, char c) {
    char *start = s;
    while (*s != '\0') {
        *s++ = c;
    }
    return start;
}

int main(void) {
    char buf[] = "Hello";

    printf("before: %s\n", buf);
    str_set(buf, '*');
    printf("after:  %s\n", buf);

    return 0;
}

How It Works

  1. Writable buffer. char buf[] = "Hello" lives in modifiable memory.
  2. Walk until null. The loop stops when it sees '\0'.
  3. Overwrite each char. Every letter becomes '*'.
  4. Length unchanged. The terminator stays, so %s still works.

Example 2 — Mask Sensitive Text

Hide a password-like string for display while keeping the same length (useful for simple UI demos).

C
#include <stdio.h>

char *str_set(char *s, char c) {
    char *start = s;
    while (*s != '\0') {
        *s++ = c;
    }
    return start;
}

int main(void) {
    char secret[] = "p@ssw0rd";

    printf("plain:  %s\n", secret);
    str_set(secret, 'X');
    printf("masked: %s\n", secret);

    return 0;
}

How It Works

  1. Start with real text. secret holds eight characters plus '\0'.
  2. Fill in place. str_set overwrites each byte with 'X'.
  3. Same length. Display still shows eight symbols—good for fixed-width masking.
  4. Not secure wipe. This is for demos; real secrets need careful clearing and never printing plain text.

Example 3 — Portable Fill with memset

Standard C alternative: fill strlen(s) bytes with memset—same effect as strset on a normal C-string.

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

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

    printf("before: %s (len=%zu)\n", buf, strlen(buf));
    memset(buf, '0', strlen(buf));
    printf("after:  %s (len=%zu)\n", buf, strlen(buf));

    return 0;
}

How It Works

  1. Measure first. strlen(buf) is 4—do not include the terminator in the count.
  2. Fill that many bytes. memset(buf, '0', 4) writes four zeros.
  3. Terminator remains. Position 4 stays '\0', so length is still 4.
  4. Prefer this on GCC/Clang. No extension needed; works with strict -std=c11.

Frequently Asked Questions

strset() fills every character of a writable C-string with one byte and keeps the ending '\0'.
Both are required. str is the string, and c is the fill byte.
The same str pointer you passed in.
No. It is an extension. Prefer a portable loop or memset.
No. String literals must not be modified.

Next: strspn()

Count how many leading characters belong to a set.

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