Movatterモバイル変換


[0]ホーム

URL:


man7.org > Linux >man-pages

Linux/UNIX system programming training


strncat(3) — Linux manual page

NAME |LIBRARY |SYNOPSIS |DESCRIPTION |RETURN VALUE |ATTRIBUTES |STANDARDS |HISTORY |CAVEATS |BUGS |EXAMPLES |SEE ALSO |COLOPHON

strncat(3)               Library Functions Manualstrncat(3)

NAME        top

       strncat - append non-null bytes from a source array to a string,       and null-terminate the result

LIBRARY        top

       Standard C library (libc,-lc)

SYNOPSIS        top

#include <string.h>char *strncat(size_t ssize;char *restrictdst, const charsrc[restrictssize],size_tssize);

DESCRIPTION        top

       This function appends at mostssize non-null bytes from the array       pointed to bysrc, followed by a null character, to the end of the       string pointed to bydst.dst must point to a string contained in       a buffer that is large enough, that is, the buffer size must be at       leaststrlen(dst) + strnlen(src, ssize) + 1.       An implementation of this function might be:           char *           strncat(char *restrict dst, const char *restrict src, size_t ssize)           {               #define strnul(s)  (s + strlen(s))               stpcpy(mempcpy(strnul(dst), src, strnlen(src, ssize)), "");               return dst;           }

RETURN VALUE        top

strncat() returnsdst.

ATTRIBUTES        top

       For an explanation of the terms used in this section, seeattributes(7).       ┌──────────────────────────────────────┬───────────────┬─────────┐       │InterfaceAttributeValue│       ├──────────────────────────────────────┼───────────────┼─────────┤       │strncat()                            │ Thread safety │ MT-Safe │       └──────────────────────────────────────┴───────────────┴─────────┘

STANDARDS        top

       C11, POSIX.1-2008.

HISTORY        top

       POSIX.1-2001, C89, SVr4, 4.3BSD.

CAVEATS        top

       The name of this function is confusing; it has no relation tostrncpy(3).       If the destination buffer does not already contain a string, or is       not large enough, the behavior is undefined.  See_FORTIFY_SOURCE       infeature_test_macros(7).

BUGS        top

       This function can be very inefficient.  Read about Shlemiel the       painter        ⟨https://www.joelonsoftware.com/2001/12/11/back-to-basics/⟩.

EXAMPLES        top

       #include <err.h>       #include <stdio.h>       #include <stdlib.h>       #include <string.h>       #define nitems(arr)  (sizeof((arr)) / sizeof((arr)[0]))       int       main(void)       {           size_t  n;           // Null-padded fixed-size character sequences           char    pre[4] = "pre.";           char    new_post[50] = ".foo.bar";           // Strings           char    post[] = ".post";           char    src[] = "some_long_body.post";           char    *dest;           n = nitems(pre) + strlen(src) - strlen(post) + nitems(new_post) + 1;           dest = malloc(sizeof(*dest) * n);           if (dest == NULL)               err(EXIT_FAILURE, "malloc()");           dest[0] = '\0';  // There's no 'cpy' function to this 'cat'.           strncat(dest, pre, nitems(pre));           strncat(dest, src, strlen(src) - strlen(post));           strncat(dest, new_post, nitems(new_post));           puts(dest);  // "pre.some_long_body.foo.bar"           free(dest);           exit(EXIT_SUCCESS);       }

SEE ALSO        top

string(3),string_copying(7)

COLOPHON        top

       This page is part of theman-pages (Linux kernel and C library       user-space interface documentation) project.  Information about       the project can be found at        ⟨https://www.kernel.org/doc/man-pages/⟩.  If you have a bug report       for this manual page, see       ⟨https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/tree/CONTRIBUTING⟩.       This page was obtained from the tarball man-pages-6.15.tar.gz       fetched from       ⟨https://mirrors.edge.kernel.org/pub/linux/docs/man-pages/⟩ on       2025-08-11.  If you discover any rendering problems in this HTML       version of the page, or you believe there is a better or more up-       to-date source for the page, or you have corrections or       improvements to the information in this COLOPHON (which isnot       part of the original manual page), send a mail to       man-pages@man7.orgLinux man-pages 6.15            2025-06-28strncat(3)

Pages that refer to this page:pmstrncat(3)pmstrncpy(3)string(3)wcsncat(3)feature_test_macros(7)signal-safety(7)string_copying(7)



HTML rendering created 2025-09-06 byMichael Kerrisk, author ofThe Linux Programming Interface.

For details of in-depthLinux/UNIX system programming training courses that I teach, lookhere.

Hosting byjambit GmbH.

Cover of TLPI


[8]ページ先頭

©2009-2025 Movatter.jp