STRMAXCPY(3pub)
π STRMAXCPY(3pub) π
okay bestie, welcome to the C Programmer's Manual β¨ this one lives in section 3pub and it's a lil string-copying icon, no cap.
πΈ NAME πΈ
strmaxcpy β copy at most a given number of characters of string π«Ά
β¨ SYNOPSIS β¨
#include <publib.h>
char *strmaxcpy(char *tgt, const char *src, size_t n);
π DESCRIPTION π
so basically, strmaxcpy copies up to n-1 characters from the beginning of src over into tgt, and then it tacks on a '\0' like the responsible queen it is. π
n has got to be at least 1. that's the floor, bestie, no going below.
and your target string? it must be large enough to hold the result. like, actually big enough. it's giving "bring your own buffer" energy, and if you don't, that's on you. π
now here's the part that makes her different from strncpy(3) β and this is the moment, fr fr:
- she always terminates the result with
'\0'. always. every single time. periodt. - she does not fill the result with a bunch of extra
'\0'characters. none of that padding nonsense. she's efficient. she's clean. she ate. π
π RETURN VALUE π
strmaxcpy returns its first argument. that's it, that's the whole vibe. π
π¦ EXAMPLE π¦
say you wanna print out the first 69 characters of a string. here's how you might do it (tho ngl, being comfy with printf's format string might be more useful in this case π):
#include <stdio.h>
#include <publib.h>
void print42(const char *string) {
char copy[43]; /* 42 + '\0' */
puts(strmaxcpy(copy, string, sizeof(copy)));
}
π SEE ALSO π
π AUTHOR π
Lars Wirzenius (lars.wirzenius@helsinki.fi) β shoutout to the legend himself π
ok that's the whole page bestie!! this was brought to you by Publib and the C Programmer's Manual, filed under STRMAXCPY(3pub) β go forth and copy responsibly, mwah ππβ¨
π¦ where this page comes from
- debian bookworm: publib-dev 0.40-3+b1 Β· BSD-2-Clause
- debian trixie: publib-dev 0.40-4 Β· BSD-2-Clause
- ubuntu jammy: publib-dev 0.40-3build1 Β· BSD-2-Clause
- ubuntu noble: publib-dev 0.40-3build1 Β· BSD-2-Clause
- ubuntu questing: publib-dev 0.40-4
- ubuntu resolute: publib-dev 0.40-4build1 Β· BSD-2-Clause
- ubuntu stonking: publib-dev 0.40-4build1 Β· BSD-2-Clause
- Copyright (c) 1994-2006 Lars Wirzenius. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms