[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

getchar confusion

Krishna

5/3/2011 8:28:00 PM

Hi,

I missed one of the parenthesis in the following code and got the
following result, I am not able to figure it out can any one explain.
Thanks

mian(void){
int c;
while(c=getchar()!=EOF)
putchar(c);
}

Result: (Entered 1)
1
??
6 Answers

Angel

5/3/2011 9:24:00 PM

0

On 2011-05-03, Krishna <tkmallik@gmail.com> wrote:
> Hi,
>
> I missed one of the parenthesis in the following code and got the
> following result, I am not able to figure it out can any one explain.
> Thanks
>
> mian(void){
> int c;
> while(c=getchar()!=EOF)
> putchar(c);
> }
>
> Result: (Entered 1)
> 1
> ??????

The != operator has a higher precedence than the = operator, so c gets
assigned the value of the expression (getchar()!=EOF), which is not what
you want. Put parentheses around (c=getchar()).

https://secure.wikimedia.org/wikipedia/en/wiki/Operators_in_C_a...


--
The perfected state of a spam server is a smoking crater.
- The Crater Corollary to Rule #4

Keith Thompson

5/3/2011 9:24:00 PM

0

Krishna <tkmallik@gmail.com> writes:
> I missed one of the parenthesis in the following code and got the
> following result, I am not able to figure it out can any one explain.
> Thanks
>
> mian(void){
> int c;
> while(c=getchar()!=EOF)
> putchar(c);
> }
>
> Result: (Entered 1)
> 1

That's not your actual code. If it were, it wouldn't have compiled
because you mispelled "main", and you would never have gotten
any output. Please copy-and-paste the exact code you compiled;
don't try to re-type it.

Here's a corrected version of your program. I've added a pair of
parentheses; they don't change the semantics, but they do help
clarify what's going on. I've also added "#include <stdio.h>";
even though you might get away with omitting it, it's not optional.

#include <stdio.h>

int main(void) {
int c;
while (c = (getchar() != EOF)) {
putchar(c);
}
return 0;
}

The condition on the while loop evaluates the subexpression
(getchar() != EOF) and assigns the result to c. The "!=" operator
yields 1 if its operands are unequal, 0 if they're equal. If you
enter the character '1', getchar() will return the value of '1'
converted to int (probably 49) which clearly is not equal to EOF
(typically -1). So c gets the value 1, which is then passed to
putchar().

The loop terminates when the condition is false (0), which happens
when getchar() returns EOF, i.e., when it reaches end-of-file.
So, just by luck, the termination condition happens to be correct.

On most systems, the character value 1 is control-A, a non-printable
control character. Your program is printing that character
once for each input character it sees, but you're not seeing it.
(You're seeing the '1' you typed because your terminal echoes it.)
The final 0 value assigned to c is not displayed because the loop
terminates at that point.

Depending on your system, you may be able to see the non-printable
characters. On Unix-like systems, for example, you can use "cat -A":

% gcc c.c -o c
% ./c | cat -A
1234
^A^A^A^A^A%
%

The real solution, as I'm sure you know, is to fix the code.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Fred

5/3/2011 9:28:00 PM

0

On May 3, 1:27 pm, Krishna <tkmal...@gmail.com> wrote:
> Hi,
>
> I missed one of the parenthesis in the following code and got the
> following result, I am not able to figure it out can any one explain.
> Thanks
>
> mian(void){
>   int c;
>   while(c=getchar()!=EOF)
>      putchar(c);
>
> }
>
> Result: (Entered 1)
> 1
> ??

Operator precedence: '!=' has precedence over '='
c=getchar()!=EOF is eauavilent to:
int x = (getchar() != EOF);
c = x;


Seebs

5/4/2011 12:35:00 AM

0

On 2011-05-03, Krishna <tkmallik@gmail.com> wrote:
> I missed one of the parenthesis in the following code and got the
> following result, I am not able to figure it out can any one explain.

Er.

> mian(void){

It is completely obvious that you are not posting your actual code, but...

> int c;
> while(c=getchar()!=EOF)

Think this through. What did you actually write? What are the operators,
and what are their operands?

You are assigning something to c. What *exactly* is the expression whose
value is assigned to c?

-s
--
Copyright 2011, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net
http://www.seeb... <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/...(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.

Keith Thompson

5/4/2011 2:01:00 AM

0

Seebs <usenet-nospam@seebs.net> writes:
> On 2011-05-03, Krishna <tkmallik@gmail.com> wrote:
>> I missed one of the parenthesis in the following code and got the
>> following result, I am not able to figure it out can any one explain.
>
> Er.
>
>> mian(void){
>
> It is completely obvious that you are not posting your actual code, but...
>
>> int c;
>> while(c=getchar()!=EOF)
>
> Think this through. What did you actually write? What are the operators,
> and what are their operands?
>
> You are assigning something to c. What *exactly* is the expression whose
> value is assigned to c?

My assumption was that Krishna knew what he had done wrong (he even said
that he "missed one of the parenthesis"), and likely how to fix it, but
was curious about the odd results he was getting from the syntactically
valid but logically incorrect code. But that's only a guess.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Seebs

5/4/2011 3:01:00 AM

0

On 2011-05-04, Keith Thompson <kst-u@mib.org> wrote:
> My assumption was that Krishna knew what he had done wrong (he even said
> that he "missed one of the parenthesis"), and likely how to fix it, but
> was curious about the odd results he was getting from the syntactically
> valid but logically incorrect code. But that's only a guess.

Yeah. It seemed to me that it would be a short step once you figure out
what it's doing.

-s
--
Copyright 2011, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net
http://www.seeb... <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/...(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.