Getc and getwc: How do they read stdin?

I'm not quite sure if this is a stupid question, but I think I will find out soon.

I'm having trouble understanding how getcand work getwc. It’s not that I cannot use them, but it seems more like I don’t know exactly what they are doing. intand getcreturn most characters if I printf them ("% c"), including multibyte characters like € or even £.

My question is: how exactly do these functions work, how do they read stdin exactly ? Explanations and good pointers to documents are greatly appreciated.

Change . Please read the comment I left in William's answer. This helps clarify the level of detail that I am after.

+3
source share
3 answers

If you are using a system with 8-bit characters (i.e. UCHAR_MAX == 255), getc () will return one 8-bit character. The reason it returns an int is because the EOF value can be distinguished from any of the possible character values. This is just about any system you are likely to encounter today.

The reason fgetc () seems to work for multi-byte characters for you is because the bytes that make up the multi-byte character are read separately, written separately, and then interpreted as a multi-byte character by your console. If you change your printf to:

printf("%c ", somechar);

(.. ), , , , , ).

+3

. unix- getc . , read() , ( ). , .

+1

, , glibc.

getc() libio/getc.c _IO_getc_unlocked(), libio/libio.h __uflow() libio/genops.c underflow.

, ;)

+1

Source: https://habr.com/ru/post/1713604/


All Articles