Saturday, 7 January 2012

Arithmetic Operators

There are five main arithmetic operators in ‘C’. They are ‘+’ for additions,‘-' for subtraction, ‘*’ for multiplication, ‘/’ for division.
Operands can be integer quantities, floating-point quantities or characters.

The division operator (/) requires that the second operand be nonzero.


If division operation is carried out with two floating- point numbers, or with one floating point number. & one integer, the result will be a floating-point quotient.


If we take two variables say x and y and their values are 20 and 10
respectively, and apply operators like addition, subtraction, division,
multiplication and modulus on them then their resulted values will
be as follows:
x+y=30
x-y=10
x*y=200
x/y=2


If one or both operands represent negative values, then the addition, subtraction, multiplication and division operations will result in values whose signs are determined by the usual rules of algebra.


  • If one operand is a floating point type and the other is a char or an int, the char/int will be converted to the floating point type of the other operand and the result will be expressed as such.So, an operation between an int and a double will result in double.
  • If both operands are floating-point types with different precisions, the lower-precision operand will be converted to the precision of the other operand and the result will be expressed in this higher precision.

  1. Float & double -->double
  2. Float & long double -->long double
  3. Double & long double -->long double

  • If neither operand is a floating point type or a long int, then both operands will be converted to int & the result will be int.
  • If neither operand is a floating point type but one is a long int,the other will be converted to long int & the result will be long int.

The operator within C are grouped hierarchically according to their order of evaluation known as precedence.

Arithmetic operators *,/ and % are under one precedence group and +,- are under another precedence group. The operators *, / and % have higher precedence than + and -.
Example
main()
{
int a,b,c;
a=10;
b=20;
c=a+b;
printf("%d",c);
c=x-y;
printf("%d",c);

c=x*y;

printf("%d",c);


c=x/y;

printf("%d",c);

}

No comments:

Post a Comment

host gator coupon