OPERATORS AND EXPRESSION | C Language Notes - Computer Times

Latest

Friday 24 August 2018

OPERATORS AND EXPRESSION | C Language Notes



Operators-: Operators are used to operate the variables and constants.

1. Arithmetic operators in C language ( +, -, *, /, % ) -: 


    The five arithmetical operations supported by the C language are: + addition , - subtraction,  * multiplication , / division,  % modulo

2.    Increase and decrease (++, --) -:

 The increase operator (++) and the decrease operator (--) increase or reduce by one the value stored in a variable. They are equivalent to +=1 and to -=1, respectively. Thus:
c++;
c+=1;
c=c+1;
are all equivalent in its functionality: the three of them increase by one the value of c.

Example 1  
B=3;
A=++B;
// A contains 4, B contains 4

Example 2
B=3;
A=B++;
// A contains 3, B contains 4

3.    Relational and equality operators ( ==, !=, >, <, >=, <= ) -:

Relation operators are used to compare the variables and constants. these are -
== Equal to                                                                                                                     
!= Not equal to
>  Greater than
<    Less than
>= Greater than or equal to
<= Less than or equal to
Here there are some examples:
(7 == 5) // evaluates to false.
(5 > 4)  // evaluates to true.
(3 != 2)  // evaluates to true.
(6 >= 6)  // evaluates to true.
(5 < 5)    // evaluates to false.
Of course, instead of using only numeric constants, we can use any valid expression, including variables. Suppose that a=2, b=3 and c=6,
(a == 5) // evaluates to false since a is not equal to 5.
(a*b >= c) // evaluates to true since (2*3 >= 6) is true.
(b+4 > a*c) // evaluates to false since (3+4 > 2*6) is false.
((b=2) == a) // evaluates to true.

  v  Important : 
The operator = (one equal sign) is not the same as the operator == (two equal signs), the first one is an assignment operator (assigns the value at its right to the variable at its left) and the other one (==) is the equality operator that compares whether both expressions on the two sides of it are equal to each other.

4.    Logical operators ( ! , &&, || ) : -

The logical operators && and || are used when evaluating two expressions to obtain a single relational result.
                                             && OPERATOR
  A
B
A && b
True
true       
true
True
false
false
False
true
False
False
false
false
                                                      ||  OPERATOR
A
B
A || b
True
true       
true
True
false
true
False
true
True
False
false
false
For example:
( (5 == 5) && (3 > 6) )    // evaluates to false ( true && false ).
( (5 == 5) || (3 > 6) )      // evaluates to true ( true || false ).

      5.    Conditional operator ( ? ) -:


The conditional operator evaluates an expression returning a value if that expression is true and a different one if the expression is evaluated as false. Its format is:
condition  ?  result1  :  result2
If condition is true the expression will return result1, if it is not it will return result2.
a=(7==5) ? 4 : 3         // a = 3, since 7 is not equal to 5.
b=(7==5+2) ? 4 : 3     // b= 4, since 7 is equal to 5+2.
d=(5>3) ? a : b          // returns the value of a, since 5 is greater than 3.
e=(a>b )? a : b         // returns whichever is greater, a or b.

     6.    Comma operator ( , ) -:

The comma operator (,) is used to separate two or more expressions that are included where only one expression is expected. When the set of expressions has to be evaluated for a value, only the rightmost expression is considered.
For example, the following code:
a = (b=3 , b+2) ;
Would first assign the value 3 to b, and then assign b+2 to variable a. So, at the end, variable a would contain the value 5 while variable b would contain value 3.

     7.    Explicit type casting operator- :

Type casting operators allow you to convert a datum of a given type to another. There are several ways to do this in C. The simplest one, which has been inherited from the C language, is to precede the expression to be converted by the new type enclosed between parentheses (()):
int i;
float f = 3.14;
i = (int) f;
The previous code converts the float number 3.14 to an integer value (3), the remainder is lost. Here, the
typecasting operator was (int).

     8.    sizeof() -:

This operator accepts one parameter, which can be either a type or a variable itself and returns the size in bytes of that type or object:
a = sizeof (char) ;

Arithmetic Expressions in c language-:
 An arithmetic expressions is a combinations of variables , constant, and operators. Expression are using the assignment statement of the form is -:
                       
                                                Variable = Expression;  

                        Expression table-
Algebraic Expressions
(i)
 A
___  +  C
B

(ii)
2 x3  +3y +5

 C Expressions
(i)
(A / B ) + C

(ii)
(2 * x * x )+ (3 *y )+ 5




CLanguageCoachinginjalandhar

#C++LanguageCoachinginjalandhar

#JavaLanguageCoachinginjalandhar

#ComputerScienceCoachinginjalandhar

Best Digital Marketing Jalandhar

Website Design Jalandhar

SEO SERVICES IN JALANDHAR

SMO Services Jalandhar

Maths Coaching Jalandhar

Digital Jalandhar, Online Coaching,Website Development,SEO and SMO Services



No comments:

Post a Comment