140 views

#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?

retagged | 140 views
0
I think the new line we enter after character input is considered as input and then the last * is printed.
0
0
No no. It's not a new line, * is pritned in the next line only .Line spacing is there due to gateoverflow formatting.

I had given just one input qwerty and the output is there as shown.
0
In this output too there is a * at the end and plus one more at the end of 1st line of output. I'm not getting what's going on.
0

For I/p : qwerty

O/p will be : q*w*e*r*t*y* only

& If you I/p: qwerty(enter)

O/p will be: q*w*e*r*t*y*

*

0
@Naveen Kumar 3

but how can i make output to get printed without pressing enter?

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

by Junior (677 points)
0