hey I've just been using the comma operator and it is not producing the expected results
Code:char c; while(scanf("%c",&c),c=='y') { printf("Yes"); }
hey I've just been using the comma operator and it is not producing the expected results
Code:char c; while(scanf("%c",&c),c=='y') { printf("Yes"); }
the above code just goes through the loop once and does not ask for any more input.
now if i replace %c with %d and make that variable an int and do c==1 then the program works as desired.
Well what did you expect?
You type in "y\n"
The second time around the loop, c == '\n' and the test fails, exit loop.
This is pretty poor use of the comma operator BTW.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
all right here is the modified code but it still produces strange results. It doesn't work the first time you press y\n but the second time onwards it works
Code:char c; while(scanf("%c\n",&c),c=='y') { printf("Yes"); }
By the way, i believe that using the comma isn't a good programming practice in most of the case.
Someone knows if there's an equivalent to the comma in others programming language like Java or Basic ?
As in
while ( scanf("%c\n",&c) == 1 && c == 'y' )
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Java has only a restricted version of the comma operator, which can be used in the initialization and increment sections of for loops. It cannot be applied in while loops, do-while loops, or the conditional section of for loops -- boolean logic like || or && must be applied there. The designers of Java thought that allowing the comma operator anywhere else allowed programmers to create code that is too hard to read.Someone knows if there's an equivalent to the comma in others programming language like Java or Basic ?
Personally I use the comma operator occasionally, usually when I repeat the same simple operation on multiple variables:
Code:if(lower_right()) x ++, y ++;
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
I mean either of the above snippets do not work in the first loop. The i/o is like thisCode:char c; while(scanf("%c\n",&c)==1 && c=='y') printf("Hello"); //or char c; while(scanf("%c\n",&c), c=='y') printf("Hello");
y
y
Hello
You see my point.
and I have one more thing to discuss.
I read somewhere the precise syntax of a for loop is
for(condition 1;condition 2;expression)
code
and i happened to test it by writing code like
and it worked.Code:int i=0; for(i==0;i<5;i++) printf("Hello");
but the strange part is I believe an assignment statement return the r value for eg. i=1 returns 1 and according to this i=0 should return 0 and so our traditional
shouldn't work. Well how does it work then??Code:for(i=0;i<5;i++) code
Last edited by greenberet; 07-20-2007 at 05:50 AM.
why don't you fgetc(stdin) ?
> I mean either of the above snippets do not work in the first loop.
That's because \n doesn't mean what you think it means.
It does NOT mean read the next character (and check it is newline), it means absorb any arbitrary sequence of whitespace characters. So "\n" and "\n\n\n \t\t\t\t\n" would both be matched entirely by a single \n in the conversion. What is more, it can't return until it sees a character which isn't whitespace, which explains why nothing happens until you type in the second 'y'.
while(scanf("%c ",&c)==1 && c=='y')
has exactly the same behaviour.
Learn to read all input with fgets(), then examine the buffer in memory. Learning all the intricacies of just scanf is about as hard as learning the rest of C.
> I read somewhere the precise syntax of a for loop is
> for(condition 1;condition 2;expression)
Well the first one is wrong, it should be initialisation.
> for(i==0;i<5;i++)
Which would be the same as
for( ; i<5 ; i++ )
You compare i with 0, but since there is nothing to assign or test the result, it is simply thrown away.
But since you've already assigned i = 0 outside the loop, all you really have is
for ( i = 0 ; i < 5 ; i++ )
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
thanks Salem now i got a clearer picture of scanf.