swab(3)

πŸ’– swab(3) β€” swap adjacent bytes πŸ’–

okay bestie, welcome to the swab(3) page, straight out of the Library Functions Manual section 3 (the "library functions" girlies, you know the ones). this is a tiny little function with big main character energy and honestly? she ate. let's get into it. ✨


🌸 NAME 🌸

swab β€” swap adjacent bytes

like literally that's it, that's the whole gig. adjacent bytes get swapped. boom. πŸ¦‹


πŸ“š LIBRARY πŸ“š

Standard C library (libc, -lc)


πŸŽ€ SYNOPSIS πŸŽ€

#define _XOPEN_SOURCE       /* See feature_test_macros(7) */
#include <unistd.h>

void swab(ssize_t n;
          const void from[restrict n], void to[restrict n],
          ssize_t n);

okay so yes, bestie, you do gotta define _XOPEN_SOURCE before you pull in <unistd.h> β€” feature_test_macros(7) is right there if you wanna know why. πŸ™ˆ


✨ DESCRIPTION ✨

so here's the vibe. swab() copies n bytes from the array that from points at, over to the array that to points at β€” and while it's doing that, it exchanges adjacent even and odd bytes. like it grabs byte 0 and byte 1 and swaps them, then byte 2 and byte 3, and so on. slay.

what is this even for, you ask? it's for exchanging data between machines that have different low/high byte ordering. so if your machines disagree about endianness, this little icon is the mutual friend who smooths things over. 🫢

now the ✨ caveats ✨, and listen closely because this part matters:

  • if n is negative, this function does literally nothing. no cap. it just vibes.
  • if n is positive and odd, it handles n-1 bytes the normal way, and then does something unspecified with that last byte. πŸ’€ unspecified!! which is just a fancy way of saying "we're not telling you, don't ask."
tl;dr β€” n should be even. like, please. just make it even. protect your peace. 🧚

πŸ’… RETURN VALUE πŸ’…

swab() returns no value. nothing. zero. she gives nothing and she's not sorry. 😌


πŸ‘‘ ATTRIBUTES πŸ‘‘

if you wanna know what these terms mean, go peek at attributes(7) β€” it's got the whole glossary.

  • Interface: swab()
    • Attribute: Thread safety
    • Value: MT-Safe

yup, she's MT-Safe. multi-thread approved. go off queen. πŸ”₯


🌈 STANDARDS 🌈

POSIX.1-2008.

certified, standardized, official. periodt. πŸ’…


πŸ¦‹ HISTORY πŸ¦‹

POSIX.1-2001, SVr4, 4.3BSD.

she's been on the scene for a minute β€” an absolute elder stateswoman of byte swapping. respect the icon. πŸ‘‘


πŸ” SEE ALSO πŸ”


and that's the whole page bestie!! go forth and swap those adjacent bytes responsibly (with an even n, fr fr) πŸŒΈπŸ’–βœ¨

this lil dispatch came to you from Linux man-pages 6.20, dated 2026-02-08 β€” mwah, love u, bye!! πŸŽ€πŸ«Ά

πŸ“¦ where this page comes from
  • arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • πŸ“œ the page's license: Linux-man-pages-copyleft
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms