Operator in c programming part-I



OPERATORS IN C PROGRAMING:
      An operator operates on variables and performs an action in a program. It consist of words or sysmbols. For instance, the arithmetic operators (+) and (-) cause us to add or subtract two numbers respectively.





      We need opertors to do arithmetic operations such as addition, subtratin, etc. Similarly, to perform relational comparison, we need relational operators like <, >, <=,>=, etc. The programming language C supports the bitwise operations and increment/decrement operations. We shall see howto use these operators to form expressions and how to use them in various stayements. Operators in C language may be classified as:

  • Arithmetic operators 
  • Relational operators 
  • Logical operators 
  • Assignment operators 
  • Pointer opertaors 
  • Bitwise operators 
  • Special operators

1.  Arithmetic Operators:
      An arithmetic operator is a symbol which performs an arithmetic operation namely addition, subtraction, etc. The data on which such operations are carried out may be variable or constant.
      The operator thus operates on an operand. For instance, in the expression:
8 +  4 – 3   the + and – symbols are operators and the constants 8, 4, 3 are the operands. C support the arithmetic operators as given below:
Opertor
Symbol
Form
Operation
Multiplication
*
x * y
x times y
Division
/
x / y
X divided by y
Remainder
%
x % y
remainder of x divided by y
Addition
+
x + y
y is added to x
Subtraction
-
x – y
y is subtracted from x
Increment operator
++
a++
++ means “add 1” (a + 1)
Decrement operator
--
a--
-- means “subtract 1” (a - 1)

FOR EXAMPLE:
// Increment and decrement operations
#include <stdio.h>
main()  {
int a, b, c;
a = b = c = 0;
printf("Initial value of a,b,c %d %d %d\n",a,b,c);
a = ++b + ++c;
printf("a = ++b + ++c = %d %d %d\n",a,b,c);
a = b++ + c++;
printf("a = b++ + c++ = %d %d %d\n",a,b,c);
a = ++b + c++;
printf("a = ++b + c++ = %d %d %d\n",a,b,c);
a = b-- + c--;
printf(" a= b-- + c-- = %d %d %d\n",a,b,c);   }

Output:







2. Relational and Logical Operators:
        Relational operators such as greater than (>) or less than (<) are used to compare values between two variables and thus form relational expressions.
        The logical operators such as AND (&&), OR (||), etc. are used to connect relational expressions together using the rules of Boolean Algebra. Both these type of expressions give TRUE or FALSE results. Here, FALSE is zero, whiole any nonzero number is TRUE.

Relational Operators:
Operator
Action
Greater than
>=
Greater then or equal
Less than
<=
Less than or equal
==
Equal
!=
Not equal

Logical Operators:
Operator
Action
&&
AND
||
OR
!
NOT

FOR EXAMPLE:
// Check whether a year is leap year or not
#include <stdio.h>
main()  {
int year;
char leap;
printf("Enter the year: ");
scanf("%d",&year);
leap = (year %4 == 0 && year %100 != 0) || (year %400 ==0) ? 'Y' :'N';
printf("This is leap year: %c\n",leap);  }
Output:
Operator in c programming part-I Operator in c programming part-I Reviewed by vijay pratap singh on August 12, 2017 Rating: 5

No comments:

Powered by Blogger.