Thread: Subtraction in C

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    347

    Subtraction in C

    Hi,
    i am little confused with subtraction
    Code:
    #include <stdio.h>
    int main(void)
    {
      unsigned char test1;
      unsigned char test2;
     if(test1 - test2 > somevalue)
    {
      // Take some action
    }
    }
    The values test1, test2 can change dynamically and sometimes test2 is greater than test1. My doubt is if(test1 - test2 > somevaue) is correct way of writing and works for any value of test1 and test2?

    Thanks

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    You should search for "C Operator Precedence".

    If you find the expression confusing, you can always add extra (`(test1 - test2) > somevalue') parentheses.

    You can pretty easily overdo extra grouping with parentheses, but you should be fine explicitly grouping expressions you find semantically ambiguous because you don't know the precedence rules by heart.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer subtraction
    By bhrugu in forum C Programming
    Replies: 10
    Last Post: 08-07-2010, 02:04 PM
  2. subtraction of two pointers..
    By transgalactic2 in forum C Programming
    Replies: 5
    Last Post: 04-11-2009, 08:32 AM
  3. Division By Subtraction
    By blacksnake in forum C++ Programming
    Replies: 19
    Last Post: 01-13-2007, 10:17 AM
  4. Long subtraction
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 12-01-2005, 07:23 PM
  5. Comparison by subtraction
    By Thantos in forum C++ Programming
    Replies: 13
    Last Post: 10-05-2004, 08:24 PM

Tags for this Thread