Thread: operator precedence in C

  1. #1
    Registered User
    Join Date
    Feb 2016
    Posts
    3

    operator precedence in C

    hi,i am new to C programming and I am a little confused about operator precedence. I wrote a program in which


    x=3
    z=x<10
    i also tried with z=x>10
    for both I am getting z=0 as output.
    why so?
    as far as I know > has precedence and x<10 so a true statement and z must be =1. can I have more details plz?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I did a quick test with this program:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int x = 3;
        int z = x < 10;
        printf("%d\n", z);
        return 0;
    }
    The output that I got:
    Code:
    1
    So you would need to prove otherwise by showing the program that you actually tested.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2016
    Posts
    11
    you are right the output should be 1, maybe the compiler you are using does things differently.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Stark88
    you are right the output should be 1, maybe the compiler you are using does things differently.
    Possible, but if so, dimps' (or your) compiler has a serious bug because according to the language rules, the output that I received is correct. Did you compile and run the code that I posted? What output did you get?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Feb 2016
    Posts
    3
    I use an online compiler. Here is the program I was trying to execute:

    Code:
    #include <stdio.h>
    
    int main()
    {
         int x=3,y,z;
         y=x=10;
         z=x<10;
         printf("x=%d\t y=%d\tz=%d\n",x,y,z);
    }
    and the output shows:
    x=10 y=10 z=0
    x=10 y=10 z=0
    sh-4.3$ ^C
    Last edited by dimps; 02-24-2016 at 06:19 AM.

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    The output is 100% correct then. It says so right there, x is 10. 10 is neither smaller nor larger than 10...
    Devoted my life to programming...

  7. #7
    Registered User
    Join Date
    Feb 2016
    Posts
    3
    You are so right.thank you all

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. remembering operator precedence
    By Satya in forum C Programming
    Replies: 5
    Last Post: 02-21-2012, 10:51 PM
  2. operator precedence
    By ardavirus in forum C Programming
    Replies: 6
    Last Post: 05-20-2011, 06:45 AM
  3. Operator overloading and precedence
    By Sharke in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2009, 11:15 PM
  4. Operator Precedence?
    By cpjust in forum C++ Programming
    Replies: 53
    Last Post: 05-04-2008, 06:43 PM
  5. Operator Precedence
    By alex1067 in forum C Programming
    Replies: 7
    Last Post: 03-17-2008, 01:38 AM

Tags for this Thread