Computer for SEE and NEB

It is a complete SEE and NEB solution for computer science. It includes Computer Fundamentals, Database (SQL), Programming in C QBASIC, CSS, JavaScript, and PHP for beginners.

Breaking

Post Top Ad

Your Ad Spot

Saturday, July 2, 2022

Operators and Expressions in C programming for SEE and NEB

Operators and Expressions in C

 programming for SEE and NEB


An operator is a special symbol which helps to perform specific mathematical and logical operations.  Different types of operators are as her under.

i) Arithmetic operator

Arithmetic operator is a symbol used for basic mathematical calculations. It usually takes two operands and returns the result of the mathematical calculations. They are listed in following table

Suppose,  the value of a = 10 and b = 3, then

Operator

Meaning

Expression

Result

+

Addition

a + b

10 + 3 = 13

-

Subtraction

a - b

10 - 3 = 7

*

Multiplication

a * b

10 * 3 = 30

/

Division (Real)

a / b

3 (Quotient)

%

Modulus Division

a % b

1 (Remainder)

 

ii) Relational operator

Relational operator is a symbol that determines the relationship between two different operands. Relational operators always give the final result in terms of true and false after its operations.

Suppose, a = 10 and b = 3, then

Operator

Meaning

Expression

Remarks

> 

Greater than

a > b

TRUE

< 

Less than

a < b

FALSE

> =

Greater than or equals to

a > = b

TRUE

< =

Less than or equals to

a < = b

FALSE

= =

Equals to

a = = b

FALSE

! =

Not equals to

a != b

TRUE

 

iii) Logical operator

Logical operator is a symbol that logically connects the logical expression It combines two or more relational expressions. It is also known as compound relational expression. They are:

Operator

Meaning

Expression

Remarks

&&

Logical AND

a && b

Results TRUE value if and only if all the given inputs are true, otherwise results FALSE value

||

Logical OR

a || b

Results TRUE value if any one of the given inputs are true, results FALSE value, if all the inputs are false.

!

Logical NOT

a ! b

Results complement value of the given input.

 

iv) Increment / Decrement operator

These operators are also called unary operators as they act upon single operand.  The increment operator (++) adds the value 1 to the current value of the operand and the decrement operator (- -) subtracts the value 1 from the current value of the operand. These operators are mostly used in iteration or looping structure.  Following are the different types of increment/decrement operators.

Postfix increment (operand ++)

Prefix increment (++operand)

Postfix decrement (operand - -)

Prefix increment (- -operand)

 

Postfix increment:

Syntax: operand++;

Example:        a++; which is similar to a = a + 1;

Let us consider following expressions.

a = 10; // the value 10 is assigned to variable a

b = a++;

Here, the value of b will be 10, then only the value of is increased by 1 and it becomes 11.

 

Prefix increment:

Syntax: ++operand;

Example: ++a; which is also similar to a = a + 1;

Let us consider following expressions.

a = 20; // the value 20 is assigned to variable a

b = ++a;

Here, the value of a will be 21, then the new value of x (21) is assigned to variable b. so y will be 21

 

Postfix decrement:

Syntax: operand- -;

Example: a- -; which is similar to a = a - 1;

Let us consider following expressions.

a = 10; // the value 10 is assigned to variable a

b = a- -;

Here, the value of b will be 10, then only the value of a is decreased by 1 and it becomes 9.

 

Prefix decrement:

Syntax: - -operand;

Example: - -a; which is also similar to a = a - 1;

Let us consider following expressions.

a = 20; // the value 20 is assigned to variable a

b = - -a;

Here, the value of a will be 19, and then the new value of a (19) is assigned to variable a. so b will be 19

 

v) Assignment operator (=)

It is used to assign value to an operand, the result of an expression to variable, as well as is used to copy value of an operand to another operand. = is the most commonly used assign operator.  Assignment expressions that make use of this operator are written in the following form.

Syntax: identifier=expression

Example:

a = 100; // value 100 is assigned to variable a

b = a;   // the value of a is copied or assigned to variable b

c = a + b; // first the value of a and b are added, then only the added value is assigned to variable c.

The assignment operator (=) can also be used with arithmetic operators as listed below:

Operator

Purpose

Example

+=

Add and assign

a+=b is the same as a=a+b

-=

Subtract and assign

a-=b is the same as a=a-b

*=

Multiply and assign

a*=b is the same as a=a*b

/=

Divide and assign

a/=b is the same as a=a/b

%=

Takes modulus and assign (only for integer division

a%=b is the same as a=a%b

 

vi) Conditional operator (? :)

C offers a conditional operator (? : ) that stores value depending upon a condition.  It is denoted by ? and : symbol.

Syntax:            test_condition ? True_expression : False_expression

Example:

Result=(marks>=35) ? ‘p’ : ‘f’;

The identifier result will have value ‘p’ if the test expression marks>=50 evaluates to true otherwise result will have value ‘f’.

 

vii) Bitwise Operators

C has ability to support that manipulation of data at the bit level. Bitwise operators operate on integers only. The bitwise operators are as-

Bitwise operator

Meaning

&

bitwise AND

|

bitwise OR

~

one’s complement

<< 

left shift

>> 

right shift

^

bitwise XOR

 

Special operators

C supports some special operators such as: comma operator, sizeof() operator, pointer operators (& and *), and member selection operators (. and ->).

sizeof Operator

It is a compile time unary operator that returns the number of bytes the operand occupies. For example, to determine the number of bytes occupied by the integer, we can use sizeof operator as:

int x;

printf(“size of integer=%d”, sizeof(x));

 

Comma Operator

C allows us to put multiple expression in the same statement, separated by a comma. It is also used in loops. The expressions are works for left-to-right order.

For example:

int x=5, y=89;

int x,y,z;

for(m=0,n=10;m<n;m++)

Consider this expression-

a = 8, b = 7, c = 9, a + b + c

Here we have combined 4 expressions. Initially 8 is assigned to the variable a then 7 is assigned to the variable b, 9 is assigned to variable c and after this a + b + c is evaluated which becomes the value of whole expression. So the value of the above expression is 24.

 

Hierarchy of Operators in C:

            Operator symbol

Highest precedence

( ) !

* / %

+  -

<  <=  >  >=

==  !=

&&

||

Lowest precedence

Brackets can be used to change precedence, as everything enclosed within brackets is always evaluated first.  For example, 2*4+3 returns 11 because 2*4 is 8, and 8+3 is 11.  On the other hand, 2*(4+3) returns 14 because 4+3 is 7, and 2*7 is 14.

 

 Expression in C

An expression is a combination of variable, constant, operators, punctuators and functions written according to the syntax of C language. The C expressions are not a statement but they are the basic building blocks of statements. Some examples are as follows:

Algebraic Expression

C Expression

xy-c

x*y-c

abc

a*b*c


I=(p*t*r)/100

4x2+3x+1

4*x*x+3*x+1

 

No comments:

Post a Comment

Post Top Ad

Your Ad Spot

Pages