6.18.30 getchar Function

Get a character from stdin.

Include

<stdio.h>

Prototype

int getchar(void);

Return Value

Returns the character read or EOF if a read error occurs or end-of-file is reached.

Remarks

This function is equivalent to fgetc with the argument stdin.

When building with the MPLAB XC8 compiler for PIC MCUs, the getchar() function relies on the getch() function being properly defined to obtain input from the required peripheral or location. Reading will not work as expected until the getch() stub (found in the pic/sources/c99/common/getch.c of your compiler distribution) is completed. See Example code for 8-bit PIC MCUs for information on defining getch() so that text can be read from a file in the MPLAB X IDE simulator.

Example

#include <stdio.h>

int main(void)
{
  char y;

    y = getchar();
    printf("%c|", y);
    y = getchar();
    printf("%c|", y);
    y = getchar();
    printf("%c|", y);
    y = getchar();
    printf("%c|", y);
    y = getchar();
    printf("%c|", y);
}

Example Input

Contents of UartIn.txt (used as stdin input for simulator):

"Short
Longer string"

Example Output

S|h|o|r|t|