why getchar() function work different in a loop?
I take you to be asking why getchar() works differently than you expect, as opposed to differently than in other contexts. If you in fact meant the latter then the answer would be "it doesn't."
But of course reading a character from a stream, whether via getchar() or some other I/O function, removes it from the stream. getchar() would not be very useful if it did not do that. Therefore, if you call it repeatedly, you read (and remove) each character in turn until and unless all available characters are consumed. You can test this by replacing the loop in your program with several getchar() calls in row.
And, of course, your loop does call it repeatedly. The loop-control expression, (c=getchar()) != EOF, is evaluated before each iteration of the loop, and that involves calling getchar() (as opposed to using a value previously returned by that function).
On a completely separate note, do be aware that getchar() returns a result of type int, exactly so that it can return at least one value, EOF, that is outside the range of type unsigned char. If you convert the result to type char then either there is one real input value that you will mistake for EOF, or you will never detect EOF, depending on whether char is signed or unsigned. To reliably and portably detect the end of the file, you must handle that return value as an int, not a char.
cmust be of typeint. - Paul Ogilviegetcharandputcharwork just the same regardless of the loop, so it is unclear why you would think that they work differently. Perhaps you are confused about what the loop does: it calls bothgetcharandputcharrepeatedly, so the first calls read and printoin youromarexample, the second calls printm, etc. (Meanwhile changechar ctoint cand search for a FAQ to explain why.) - Arkkuogetcharwill receive theoit will be checked againstEOFandputcharwill print theothen the loop will return togetchar. This will then happen formandaandr- liamcomp