This program output

S

Saurav Bhasin

Hi

In this program from K&R:
#include <stdio.h>
main()
{
int c;
while(c=(getchar() != EOF))
{
printf("%d\n",c);
}
}

The output using GCC compiler is :
../a.out

1
1
1

2
2
2

why is the output 1 being printed 2 times, Since there is only 1
printf statement, should not it be printed only once?
Thanks,
 
J

James Dow Allen

In this program from K&R:
#include <stdio.h>
main()
{
      int c;
      while(c=(getchar() != EOF))
       {
            printf("%d\n",c);
        }

}

This looks like a submission to the new Obfuscated C Subdivision:
Rearrange the parentheses in a K&R example program amusingly.
Good work!
The output using GCC compiler is :
./a.out

1
1
1

2
2
2

This is easy to explain. You never compiled your Obfuscated C entry.
This "./a.out" was built by some other inmate.

Hope this helps.
James
 
S

Saurav Bhasin

In




It seems to me that K&R wouldn't make that particular mistake. Got a
page reference?

The code should of course read:

  while((c = getchar()) != EOF))


There is one printf call per loop iteration, and one loop iteration
per keypress. How many keypresses did you perform when typing 1 ?

No, don't say 1. Do the experiment, and count as you go.

If that doesn't help, try an input of, say, SAURAV. Type it all as one
word. Then hit ENTER and see what output you get. Then think about
how a line buffer might work.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999
Sig line vacant - apply within

This program is from K&R page no 17, Exercise 1-6.

I enter 1 and then press <ENTER> in my keyboard. So that takes 2
inputs I am assuming?.

If I press 1 and then press CTRL-D , It prints the output only ONCE.

So I am assuming that <ENTER> is read as a keystroke in getchar()
function?.

Thanks for the reply.
 
S

Saurav Bhasin

This program is from K&R page no 17, Exercise 1-6.

I enter 1 and then press <ENTER> in my keyboard. So that takes 2
inputs I am assuming?.

If I press 1 and then press CTRL-D , It prints the output only ONCE.

So I am assuming that <ENTER> is read as a keystroke in getchar()
function?.

Thanks for the reply.

Hi Guys,

Sorry to bother, but I am a bit noobie in programming. Should I assume
that what I thought is correct?. The man page for getchar() in Unix is
saying that read the input till EOF is encountered. It converts an
Unsigned char to an Int before displaying. So <ENTER> would have been
taken as an unsigned char?.

Thanks
 
A

Antoninus Twink

Not quite. The keystroke is read by your hardware, and your shell
stuffs the input into a line buffer, where it is represented as '\n'
(probably 0x0A on your system). It is that line buffer from which
getchar() gets its value.

Danger, Will Robinson! I see no mention of "hardware", "shells" or "line
buffers" in the ISO C Standard.

You seem to be assuming that the OP is programming for a *nix'y desktop.
As you know better than anyone, the standing assumption here is that
posters to this group are coding either for microchips embedded in a
refridgerator or else for a mainframe from the 1970s, even if they
specifically say otherwise. In both cases, your answer is completely
wrong.

HTH.
 
A

Anand Hariharan

In




It seems to me that K&R wouldn't make that particular mistake. Got a
page reference?

The code should of course read:

  while((c = getchar()) != EOF))

Syntax error.
 
R

Ralf Damaschke

Antoninus said:
I see no mention of "hardware", "shells" or "line
buffers" in the ISO C Standard.

Which only shows that you didn't look hard enough.

-- Ralf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top