Thread: Logic evaluation of an expression

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    5

    Logic evaluation of an expression

    I am having a little problem with a logic evaluation of a simple expression,
    perhaps I need a nudge in the right direction:
    Given an assignment x = 9,
    I am trying to evaluate the following - (x%2==0) and (x%3==0).

    The output gives
    0 and 1 respectively
    as in
    (x ==0) is evaluated as : 0
    and
    (x ==0) is evaluated as : 1
    Looks strange to me, probably needs some explanation if correct

    Thanks!

    Actual Code:
    Code:
    /*Q8.4.C:  Evaluating an  operand */ 
    #include <stdio.h>
    
      main() 
    {
         int x;
             x = 9;
         printf("Given x = 9, \n");
         printf("(x%2==0) is evaluated as :  %i \n ", (x%2==0) );
            printf("and \n");
         printf("(x%3==0) is evaluated as :  %i \n ", (x%3==0) );
         return 0; 
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    What is the result of: 9%2

  3. #3
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Quote Originally Posted by jaymax View Post
    Given an assignment x = 9,
    I am trying to evaluate the following - (x%2==0) and (x%3==0).

    The output gives

    as in
    and

    What do you expect? What do you even want? Of course 9%2 is 1, of course 9%3 is 0. What else would the result be?

  4. #4
    Registered User
    Join Date
    Mar 2014
    Posts
    5
    Thanks! Matticus and Yanin, your replies made it clear: I had been stuck thinking of "(x%2==0) and (x%3==0)" in terms of some 'unknown to me' format specifier and not of the % being the modulus or remainder operator.
    Thanks again.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by jaymax View Post
    Thanks! Matticus and Yanin, your replies made it clear: I had been stuck thinking of "(x%2==0) and (x%3==0)" in terms of some 'unknown to me' format specifier and not of the % being the modulus or remainder operator.
    Thanks again.
    Keep in mind, format specifiers only apply to printf and scanf families of functions, and then only to the format string itself, not to other parameters. Technically, there are other functions that use similar format specifiers, but those are the two common ones that exist in all C implementations.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Evaluation of postfix expression
    By Prateekr10 in forum C Programming
    Replies: 3
    Last Post: 12-29-2009, 09:50 AM
  2. order of evaluation of C-expression
    By Pranav kant in forum C Programming
    Replies: 1
    Last Post: 11-22-2009, 08:24 AM
  3. Replies: 6
    Last Post: 11-11-2009, 02:27 PM
  4. expression evaluation
    By lazy_hack in forum C Programming
    Replies: 11
    Last Post: 08-21-2009, 09:37 AM
  5. sizeof and Expression Evaluation
    By Dave_Sinkula in forum C Programming
    Replies: 2
    Last Post: 08-11-2003, 07:11 PM