Thread: C comma operator problem

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    9

    C comma operator problem

    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");
    }

  2. #2
    Registered User
    Join Date
    Jul 2007
    Posts
    9

    Angry

    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.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    9
    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");
    }

  5. #5
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    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 ?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Someone knows if there's an equivalent to the comma in others programming language like Java or Basic ?
    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.

    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.

  8. #8
    Registered User
    Join Date
    Jul 2007
    Posts
    9
    Code:
    char c;
    while(scanf("%c\n",&c)==1 && c=='y')
    printf("Hello");
    
    //or
    
    char c;
    while(scanf("%c\n",&c), c=='y')
    printf("Hello");
    I mean either of the above snippets do not work in the first loop. The i/o is like this
    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
    Code:
    int i=0;
    for(i==0;i<5;i++)
    printf("Hello");
    and it worked.

    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
    Code:
    for(i=0;i<5;i++)
    code
    shouldn't work. Well how does it work then??
    Last edited by greenberet; 07-20-2007 at 05:50 AM.

  9. #9
    Registered User
    Join Date
    Dec 2005
    Posts
    167
    why don't you fgetc(stdin) ?

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > 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.

  11. #11
    Registered User
    Join Date
    Jul 2007
    Posts
    9

    Smile

    thanks Salem now i got a clearer picture of scanf.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array (seperate inputs by comma)
    By chuckwag0n in forum C++ Programming
    Replies: 7
    Last Post: 04-03-2009, 01:10 PM
  2. Array (seperate inputs by comma)
    By chuckwag0n in forum C Programming
    Replies: 1
    Last Post: 04-02-2009, 01:01 PM
  3. comma and semi colon
    By studentc in forum C Programming
    Replies: 13
    Last Post: 05-14-2004, 05:49 PM
  4. Comma Operator
    By doraiashok in forum C Programming
    Replies: 2
    Last Post: 12-09-2003, 08:38 AM
  5. Delete last Character (comma) ?
    By Hexxx in forum C Programming
    Replies: 2
    Last Post: 10-19-2003, 12:28 AM