The Gateway to Computer Science Excellence
+1 vote
191 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 | 191 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
52,215 questions
60,016 answers
201,243 comments
94,703 users