retagged by
821 views
0 votes
0 votes

#include <stdio.h>

int main ()
{
  int c;
  while ((c = getchar ()) != EOF)
    {
      putchar (c);
      printf ("*");
    }

  
  return 0;
}

when i run this program, the output i get is as

qwerty                                                                                                                         

q * w * e * r * t * y *                                                                                                        

 * 

i'm not getting how this last "*" is getting printed.

it has something to do with the return of putchar(). so how does putchar() function actually returns. i know that it returns after EOF is reached, so in that case , it won't print anything and printf("*") will get executed. but the thing is, why the last * is getting printed in the next line. is it like putchar() returns and shift the printing pointer to new line?

please clear my doubt.. 

 

 

retagged by

1 Answer

0 votes
0 votes

The terminal always buffers the input characters until an EOF or newline character is reached, i.e. the terminal won't send any info to the C program until that happens. Once EOF or newline character is reached, only then the entered info is sent to the C program (along with the EOF/newline char)

For your case, you can test the output by providing the input in a file.

Create input.txt  which contains: qwerty

Assuming you are running windows, run the following to get the output: 

gcc program.c -o output.exe
output.exe < input.txt

 

Related questions

0 votes
0 votes
1 answer
2
dixit bishwash asked Mar 31, 2018
1,173 views
The Correct answer is c but how?int main(){ int a=210,b=120,c; c=a>b?1,2,3:2,5,6,7; printf("%d",c);}ans:-a) 1b) 2c) 3d) Error.