C memccpy() Function

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

Definition

In C, memccpy() copies bytes from one memory block to another. It stops after copying the first byte equal to c, or after copying n bytes when that byte is not found.

Unlike string-copy functions, memccpy() works with raw bytes and does not stop at '\0' unless zero is the chosen stop byte. It is a POSIX function, not a classic ISO C function available on every compiler.

Syntax

Start with this concrete call, then adjust the values for your buffer.

C
char *after = memccpy(dest, src, ':', sizeof src);
CallWhat it does
memccpy(dest, src, ':', n)Copies through the first colon, but never more than n bytes.

Include <string.h> on systems that provide it. The source and destination must not overlap.

Library declaration
C
void *memccpy(void *restrict dest,
               const void *restrict src,
               int c, size_t n);

Parameters

ParameterDescription
destRequired. Writable destination with room for at least n bytes.
srcRequired. Source memory block containing at least n readable bytes.
cThe stop byte. The value is converted to unsigned char before comparison.
nMaximum number of bytes to examine and copy.

Return Value

Return ValueDescription
Pointer after stop byteWhen c is found, points one byte past its copied position in dest.
NULLReturned when c is not found in the first n source bytes.

C Examples

Example 1 — Copy Through a Delimiter

Copy a name and its colon, then use the returned pointer to finish a C-string.

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

int main(void) {
    const char src[] = "name:value";
    char dest[16];
    char *after = memccpy(dest, src, ':', sizeof src);

    if (after != NULL) {
        *after = '\0';
        printf("%s\n", dest);
    }
    return 0;
}

How It Works

  1. Start at the first byte. memccpy begins copying n, a, m, and e.
  2. Copy the delimiter. The colon is copied into dest before the function stops.
  3. Use the return pointer. after points to the byte immediately after the copied colon.
  4. Terminate the text. Writing '\0' through after makes dest safe for printf.

Example 2 — Stop Byte Not Found

See the NULL result when the requested byte is absent.

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

int main(void) {
    const char src[] = "ABCDE";
    char dest[6] = {0};
    void *result = memccpy(dest, src, 'X', 5);

    printf("result: %s\n", result == NULL ? "NULL" : "found");
    printf("copied: %s\n", dest);
    return 0;
}

How It Works

  1. Set a strict limit. Only the first five bytes of src are examined.
  2. Search while copying. Each byte is copied even though none equals 'X'.
  3. Reach the limit. After five copies, the function cannot continue and returns NULL.
  4. Keep termination separate. The zero-filled destination supplies a terminator after the copied bytes.

Example 3 — Copy a Binary Record

The stop value can be a byte in binary data; no C-string is required.

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

int main(void) {
    unsigned char packet[] = {10, 20, 255, 30, 40};
    unsigned char out[5] = {0};
    unsigned char *after = memccpy(out, packet, 255, sizeof packet);
    size_t copied = after ? (size_t)(after - out) : sizeof packet;

    printf("bytes copied = %zu\n", copied);
    printf("last byte = %u\n", out[copied - 1]);
    return 0;
}

How It Works

  1. Treat data as bytes. The arrays contain numbers, not null-terminated text.
  2. Compare converted values. The stop value 255 matches the third unsigned byte.
  3. Subtract pointers. after - out gives the number of bytes copied.
  4. Include the matched byte. The count is three because the matching byte is copied before stopping.

Frequently Asked Questions

It copies up to n bytes and stops just after copying the first byte equal to c.
It returns a pointer one byte past the copied stop byte, or NULL when that byte is not found.
No. It only copies bytes. Add a terminator yourself if the result must be a C-string.
It is specified by POSIX and is not a classic ISO C function available everywhere.
No. Use memmove() when the memory regions may overlap.

Next: memchr()

Find the first matching byte inside a bounded memory block.

memchr() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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