Read a single character - C Data Type

C examples for Data Type:char

Description

Read a single character

Demo Code

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

int main(){/* w  w  w .  j a  va2s . c o  m*/
    int i;
    char msg[25];

    printf("Type up to 25 characters and then press Enter...\n");
    for (i = 0; i < 25; i++)
    {
        msg[i] = getchar(); //read a single character
        if (msg[i] == '\n')
        {
            i--;
            break;
        }
    }

    putchar('\n'); // One line break after the loop is done.
    for (; i >= 0; i--)
    {
        putchar(msg[i]);
    }
    putchar('\n');

    return(0);
}

Result


Related Tutorials