Thread: Howz this equality operator working?

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    47

    Howz this equality operator working?

    Code:
    #include<stdio.h>
    main()
    {
    	int k=35;
    	printf ("%d %d %d", k==35, k=50, k>25);
    	
    }

    Code:
    Output:
    0 50 1
    I thought it was just comparing. Like it would return a 1 if k was 35 and a 0 if k was not not 35! But as it happens, even though I change the value of k, I can't see any change in the output. (It still remains 0)

    Please clarify.

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    I'm not a pro C programmer, I'm more of a C++ programmer, but if I remember, printf() uses va_lists to let you have as many parameters as you want and these va_lists are a bunch of macros, if I recall. You might want to surround your expressions with parentheses.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Maybe fix your program?
    http://c-faq.com/expr/seqpoints.html

    You have NO control over when the assignment happens, and when the comparison is performed between the two sequence points which bound that particular statement.
    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
    Jan 2006
    Posts
    47
    Quote Originally Posted by Salem View Post
    Maybe fix your program?
    http://c-faq.com/expr/seqpoints.html

    You have NO control over when the assignment happens, and when the comparison is performed between the two sequence points which bound that particular statement.
    Still not sure what that means!

    Well, this was an assignment problem and I blew my brains out pondering over this thing.

    Please explain how I don't have control when the assignment happens.
    Just wanted to know how printf sees the equality operator.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    In the same way that
    printf( "%d %d\n", i++, i++ );
    is undefined.

    Yours may degenerate into

    k = 50;
    printf ("%d %d %d", k==35, k, k>25);

    or

    printf ("%d %d %d", k==35, k, k>25);
    k = 50;


    Both are correct as far as valid C code is concerned, and the compiler writer is free to choose which one is best given the circumstances. The problem for you is that when you start pushing on the assumptions of the behaviour, sometimes you get bitten.

    Remember, C is a compiled language, so all sorts of things happen between sequence points. What most certainly doesn't happen is the code is executed in the strict L->R order like it might be by a simplistic interpreter.

    Fixing a couple of trivial problems, we get
    Code:
    #include<stdio.h>
    int main()
    {
    	int k=35;
    	printf ("%d %d %d", k==35, k=50, k>25);
    	return 0;
    }
    
    $ gcc -W -Wall -ansi -pedantic -O2 foo.c
    foo.c: In function `main':
    foo.c:5: warning: operation on `k' may be undefined
    foo.c:5: warning: operation on `k' may be undefined
    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.

  6. #6
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Quote Originally Posted by Salem View Post
    printf ("%d %d %d", k==35, k, k>25);
    k = 50;
    You mean
    Code:
    printf ("%d %d %d", k==35, 50, k > 25);
    k = 50;
    There are 10 types of people in this world, those who cringed when reading the beginning of this sentence and those who salivated to how superior they are for understanding something as simple as binary.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by Rashakil Fol View Post
    You mean
    Code:
    printf ("%d %d %d", k==35, 50, k > 25);
    k = 50;
    that's also legal.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dereferencing and Equality?
    By dnguyen1022 in forum C++ Programming
    Replies: 17
    Last Post: 03-06-2009, 01:54 AM
  2. Replies: 5
    Last Post: 02-24-2009, 05:29 PM
  3. string equality testing
    By Bladactania in forum C Programming
    Replies: 2
    Last Post: 02-17-2009, 06:24 AM
  4. Test of Equality for String Macro - Invalid?
    By Jesdisciple in forum C Programming
    Replies: 28
    Last Post: 01-25-2009, 04:47 PM
  5. testing 2 strings for equality
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 11-12-2001, 04:55 AM