std::fputc,std::putc(3)

๐Ÿ“ฆ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

๐Ÿ’– NAME ๐Ÿ’–

std::fputc,std::putc โ€” bestie, the NAME section is literally std::fputc,std::putc again, and honestly? Iconic behavior ๐Ÿ’…โœจ

๐ŸŒธ Synopsis ๐ŸŒธ

Defined in header <cstdio>:

int fputc( int ch, std::FILE* stream );
int putc( int ch, std::FILE* stream );

So here's the tea: this writes a character ch to the given output stream stream. ๐Ÿซถ

Internally, that character gets converted to unsigned char just before it's written. No cap, that's the whole conversion moment right before the write.

In C, putc() may be implemented as a macro, which is disallowed in C++. Therefore, calls to std::fputc() and std::putc() always have the same effect. Periodt. โœจ

๐ŸŽ€ Parameters ๐ŸŽ€

  • ch โ€” the character to be written ๐Ÿ’–
  • stream โ€” the output stream ๐ŸŒŠ

๐Ÿ”ฅ Return value ๐Ÿ”ฅ

  • On success, it returns the written character. Slay. โœ…
  • On failure, it returns EOF and sets the error indicator (see std::ferror()) on stream. ๐Ÿ˜ญ๐Ÿ’€

๐Ÿฆ‹ Example ๐Ÿฆ‹

The page says // Run this code, so literally go run it bestie:

#include <cstdio>

int main()
{
    for (char c = 'a'; c != 'z'; c++)
        std::putc(c, stdout);

    // putchar's return value is not equal to the argument
    int r = 0x102A;
    std::printf("\nr = 0x%x\n", r);

    r = std::putchar(r);
    std::printf("\nr = 0x%x\n", r);
}

๐Ÿ”ฎ Possible output

abcdefghijklmnopqrstuvwxy
r = 0x102A
*
r = 0x2A

๐ŸŒˆ See also ๐ŸŒˆ

  • putchar writes a character to stdout (function) ๐Ÿ‘‘
  • C documentation for fputc, putc ๐Ÿ“š

Signing off with source/version cppreference.com and date 2024.06.10 for page std::fputc,std::putc(3) โ€” bye bestie, go write some characters ๐Ÿ’œ๐Ÿงšโœจ

๐Ÿ“ฆ where this page comes from