logical operation

This is a discussion on logical operation within the C Programming forums, part of the General Programming Boards category; Code: main() { int i=4,j=7; j = j || i++ && printf("YOU CAN"); printf("%d %d", i, j); } Answer: 4 ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    32

    Lightbulb logical operation

    Code:
    main()
    {
    	int i=4,j=7;
    	j = j || i++ && printf("YOU CAN");
    	printf("%d %d", i, j);
    }	
    
    Answer:
    4 1

    Actually && has highest priority than ||. But compiler first evaluates || of above program. How is it?

  2. #2
    Registered User gaurav9991's Avatar
    Join Date
    Oct 2010
    Location
    Pune, Maharashtra, India
    Posts
    69
    Quote Originally Posted by siperi View Post

    Actually && has highest priority than ||. But compiler first evaluates || of above program. How is it?
    how do you know that it first evaluates || ?

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,269
    Quote Originally Posted by siperi
    Actually && has highest priority than ||. But compiler first evaluates || of above program. How is it?
    Grouping of subexpressions and order of evaluation are different, though related, issues.

    Quote Originally Posted by gaurav9991
    how do you know that it first evaluates || ?
    The (i++ && printf("YOU CAN")) subexpression never gets evaluated because the left hand side, j, with a value of 7, is evaluated as true, and due to lazy/short-circuit evaluation that means that the right hand side of the || is not evaluated.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User gaurav9991's Avatar
    Join Date
    Oct 2010
    Location
    Pune, Maharashtra, India
    Posts
    69
    using which compiler we will come to know which expression is evaluated first?
    I am using Pelles C IDE, is it possible to find this in it?

    if yes, how?

    thanks

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,269
    Quote Originally Posted by gaurav9991
    using which compiler we will come to know which expression is evaluated first?
    I am using Pelles C IDE, is it possible to find this in it?

    if yes, how?
    Aside from special cases as in siperi's question, you should just generate assembly output and check what the compiler actually generated.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by laserlight View Post
    Aside from special cases as in siperi's question, you should just generate assembly output and check what the compiler actually generated.
    In PellesC you can do this by turning off debugging in the Project Options, compile then debut... you will see the assembly code and you can execute it step by step to see what it does.

    (Laserlight... One of PellesC's nicer features that nobody seems to know about)

  7. #7
    Registered User gaurav9991's Avatar
    Join Date
    Oct 2010
    Location
    Pune, Maharashtra, India
    Posts
    69
    so knowing of assembly language is must ( atleast for this problem ) ?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,269
    Quote Originally Posted by gaurav9991
    so knowing of assembly language is must ( atleast for this problem ) ?
    Read the text of the 1999 edition of the C standard:
    Quote Originally Posted by C99 Clause 6.5 Paragraph 3
    The grouping of operators and operands is indicated by the syntax. Except as specified later (for the function-call (), &&, ||, ?:, and comma operators), the order of evaluation of subexpressions and the order in which side effects take place are both unspecified.
    Now, "unspecified" means that the behaviour is implementation defined, i.e., it is up to the compiler. Therefore, if you want to know what exactly is the behaviour, then you need to examine what the compiler did, i.e., you need to look at its output. Of course, you might be able to deduce what it did based on say, printf statements, or observing the values in a debugger, but chances are it would not be as clear as looking at assembly output.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by gaurav9991 View Post
    so knowing of assembly language is must ( atleast for this problem ) ?
    It would help.

    Adding a thought to what laserlight has said, keep in mind that "unspecified" also means that unless you are examining this on the same compiler the OP is using, your results may differ.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by gaurav9991 View Post
    so knowing of assembly language is must ( atleast for this problem ) ?
    Not at all.

    j=j will ALWAYS be true, and that exits that line of code.

    That's the end of it, except to mention that j is now 1.

    That would not change, on any C compiler. No assembly examination needed.

    If you need to check subexpressions, and what their precedence is, THEN you need further testing and/or examination.

    Not at all necessary for this code.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,269
    Quote Originally Posted by Adak
    j=j will ALWAYS be true, and that exits that line of code.
    As I stated in post #3, it is not j=j, but just j, which does not necessarily evaluate to true, though it does in this case. The assignment operators have a lower precedence compared to the logical or operator. Consequently, you are correct to say that the value of j will then be 1; if that left hand subexpression was really j=j, then j would still have a value of 7.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Thank you, laserlight - I see what you're saying.

    I'm going to have to learn to tilt my head over when I read these weird lines of code.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Adak View Post
    Thank you, laserlight - I see what you're saying.

    I'm going to have to learn to tilt my head over when I read these weird lines of code.
    I generally find it helpful to hold them up to the mirror on my ceiling...


  14. #14
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    Quote Originally Posted by siperi View Post
    Actually && has highest priority than ||. But compiler first evaluates || of above program. How is it?
    The short circuit evalation of operators takes effect.

    [Edit] nevermind, laserlight's already mentioned it.
    Last edited by itCbitC; 11-08-2010 at 10:31 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. Replies: 5
    Last Post: 12-04-2008, 08:15 PM
  3. arithmatic vs logical shift operation
    By onebrother in forum C Programming
    Replies: 2
    Last Post: 02-21-2008, 04:21 AM
  4. logical operation functions in c; And, Or, Xor, Not etc..
    By Jasonx521 in forum C Programming
    Replies: 6
    Last Post: 10-03-2006, 01:24 AM
  5. Logical Operation Sequence ?!?
    By Halo in forum C Programming
    Replies: 2
    Last Post: 04-16-2002, 01:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21