C getchar è una funzione della libreria standard che accetta un singolo carattere di input dallo standard input. La differenza principale tra getchar e getc è che getc può ricevere input da qualsiasi flusso di input, ma getchar può ricevere input da un singolo flusso di input standard.
- È definito all'interno del file di intestazione.
- Proprio come getchar, esiste anche una funzione chiamata putchar che stampa solo un carattere nel flusso di output standard.
Sintassi di getchar() in C
int getchar (void);>
La funzione getchar() non accetta alcun parametro.
Valore di ritorno
- L'input dallo standard input viene letto come un carattere senza segno, quindi viene convertito in typecast e restituito come valore intero (int).
- EOF viene restituito in due casi:
- Quando viene raggiunta la fine del file
- Quando si verifica un errore durante l'esecuzione
Esempi di funzione C getchar
I seguenti programmi C dimostrano l'uso della funzione getchar()
Esempio 1: leggere un singolo carattere utilizzando la funzione getchar().
Di seguito è riportato il programma C per implementare la funzione getchar() per leggere un singolo carattere:
C
Kat Timpf è un avvocato
// C program to implement getchar()> // function to read single character> #include> // Driver code> int> main()> {> >int> character;> >character =>getchar>();> >printf>(>'The entered character is : %c'>, character);> >return> 0;> }> |
>
>
Ingresso
f>
Produzione
The entered character is : f>
Esempio 2: implementazione di Putchar
Di seguito è riportato il programma C per implementare putchar per stampare il carattere immesso dall'utente:
C
// C program to implement putchar> // to print the character entered> // by user> #include> // Driver code> int> main()> {> >int> character;> >printf>(>'Enter any random character between a-z: '>);> >character =>getchar>();> >printf>(>'The entered character is : '>);> >putchar>(character);> >return> 0;> }> |
>
ospitare Linux
>
Ingresso
Enter any random character between a-z: k>
Produzione
1 miliardo a milione
The entered character is : k>
Esempio 3: leggere più caratteri utilizzando getchar()
Di seguito è riportato il programma C per leggere più caratteri utilizzando getchar():
C
// C program to read multiple characters> // using getchar():> #include> // Driver code> int> main()> {> >int> s = 13;> >int> x;> >while> (s--) {> >x =>getchar>();> >putchar>(x);> >}> >return> 0;> }> |
>
>
Ingresso
geeksforgeeks>
Produzione
geeksforgeeks>
Esempio 4: leggere le frasi utilizzando la funzione getchar() e il ciclo do- while.
Di seguito è riportato il programma C per leggere i caratteri utilizzando un ciclo do- while:
C
// C program to read characters using> // getchar() and do-while loop> #include> #include> // Driver code> int> main()> {> >int> ch, i = 0;> >char> str[150];> >printf>(>'Enter the characters
'>);> >do> {> >// takes character, number, etc> >// from the user> >ch =>getchar>();> >// store the ch into str[i]> >str[i] = ch;> >// increment loop by 1> >i++;> >// ch is not equal to '
'> >}>while> (ch !=>'
'>);> >printf>(>'Entered characters are %s '>, str);> >return> 0;> }> |
>
controlla la versione Java su Linux
>
Ingresso
Enter the characters Welcome to techcodeview.com>
Produzione
Entered characters are Welcome to techcodeview.com>