The Gateway to Computer Science Excellence
+1 vote
149 views
#include<stdio.h>
main(){
int c;
while(c = getchar() != '\n') 
putchar(c); 
}

why does this program shows wrong output for every input string.. plzz some one help

in Programming by (397 points) | 149 views
0
what u want to do by this.
+4

Precedance of = is right to left.So your program evaluated as

main(){
    int c;
    while(c = (getchar() != '\n'))
        putchar(c);
}

So on some input it will give unexpected result.

If you want the correct output make some changes

main(){
    int c;
    while((c = getchar()) != '\n')
        putchar(c);
}

Now everything works fine.

+1
it should be while((c = getchar()) != '\n');

Please log in or register to answer this question.

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,654 questions
56,166 answers
193,872 comments
94,261 users