Muhamed Cicak Muhamed Cicak - 1 year ago 105
C Question

C getchar() misunderstanding

Can someone explain me the code ?? Wouldn't

be always equal to
? I guess I don't really get this
function.Why isn't
always equal to `c ?


void test(int c);

int main(void) {
int c;
while ((c = getchar()) != EOF) {
return 0;

void test(int c) {
int d;
if (c == '/') {
d = getchar();
printf("%c", d);





Answer Source

No, not really. As mentioned in C11, chapter ยง7.21.7.6, The getchar function, (emphasis mine)

The getchar function returns the next character from the input stream pointed to by stdin. [...]

So, each call to getchar() will give you the next character input present in the input stream. So, when c == '/' condition is met, it will read the next entry and store into d, it need not be the same as c, anyway.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download