retagged by
603 views
0 votes
0 votes

My question is when I give :

i/p - abcde

how i get output - abcde .

I mean C is a character so shouldn't o/p also be an single character . Why is o/p coming out to be be string cant understand.

#include<stdio.h>

int main(){
    char c;
    while((c = getchar()) != EOF){
        printf("%c",c);
        }
    return 0;
}

Appreciate Help. I know I should use int for c here.

retagged by

2 Answers

Best answer
1 votes
1 votes
1. c = getchar();
2. printf("%c",c);
On abcde, it prints only 'a' .

1. while((c = getchar()) != EOF){
2. printf("%c",c);
3. }

Input: abcde
While loop...
line 1.. c= a ... line 2... printf output is remembered since its inside...
line  1  c=b .... line2...  printf output is remembered since its inside..
........
On enter it will print abcde.
So Why do you think it should only print 'a' ? Printf is executed 5 times you know. So why only 1st printf output will be printed?
Your qsn would look better.. if you have asked why not last one should be printed ..i,e  'e'   ...
Variable 'c' is replaced each time... but why printf output will be replaced... Its stored in buffer..It just wants your enter to be printed.
selected by
1 votes
1 votes

it is in while loop 

so it takes one character at a time 

and then print that one character

and why should u use int u are taking a character value from a string

if u take int it will covert into ASCIi value of character

Related questions