putchar() in C: Displaying One Character

putchar() writes one character to standard output. It is useful for character-by-character output, simple loops, filters and examples that pair naturally with getchar().

Syntax Top ↑

int result = putchar(character);

Print a Character and a Newline Top ↑

#include <stdio.h>

int main(void) {
    putchar('C');
    putchar('\n');

    return 0;
}

Read with getchar() and Write with putchar() Top ↑

#include <stdio.h>

int main(void) {
    int ch;

    printf("Enter one character: ");
    ch = getchar();

    if (ch != EOF) {
        putchar(ch);
        putchar('\n');
    }

    return 0;
}

Change the Letter Case Top ↑

#include <ctype.h>
#include <stdio.h>

int main(void) {
    int ch;

    printf("Enter a letter: ");
    ch = getchar();

    if (ch == EOF) {
        return 1;
    }

    if (islower((unsigned char)ch)) {
        putchar(toupper((unsigned char)ch));
    } else if (isupper((unsigned char)ch)) {
        putchar(tolower((unsigned char)ch));
    } else {
        putchar(ch);
    }

    putchar('\n');
    return 0;
}

The unsigned char cast is used before passing ordinary character values to the <ctype.h> functions.

Print a String Character by Character Top ↑

#include <stdio.h>

int main(void) {
    const char text[] = "plus2net";
    int i = 0;

    while (text[i] != '\0') {
        putchar(text[i]);
        i++;
    }

    putchar('\n');
    return 0;
}

Print A to Z Top ↑

#include <stdio.h>

int main(void) {
    for (int ch = 'A'; ch <= 'Z'; ch++) {
        putchar(ch);

        if (ch < 'Z') {
            putchar(' ');
        }
    }

    putchar('\n');
    return 0;
}




plus2net.com






✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer