UNIT 4 EXPRESSIONS AND OPERATORS
-------------------------------------------------------------------------------
4.0 Introduction
4.1 Objectives
4.2 Assignment Statements
4.3 Arithmetic Operators
4.4 Relational Operators
4.5 Logical Operators
4.6 Comma and Conditional Operators
4.7 Type Cast Operator
4.8 Size of Operator
4.9 C Shorthand 1
4.10 Priority of Operators
4.11 Summary
4.12 Solutions / Answers
4.13 Further Reading
FOR MORE DETAILS ABOUT OPERATORS - READ THE ENTIRE BOX BELOW :
------------------------------------------------------------------
4.0 INTRODUCTION
------------------------------------------------------------------
-----------------------------------------
4.1 OBJECTIVES :
-----------------------------------------
4.2 ASSIGNMENT STATEMENT :
-------------------------------------------------In the previous unit, we have seen that variables are basically memory locations and they can hold certain values. But, how to assign values to the variables ?
C provides an assignment operator for this purpose. The function of this operator is to assign the values of values in variables on right hand side(RHS) to variables on Left Hand Side(LHS).
The syntax of the assignment expression is as follows :
The data type of the variable on LHS should match the data type of constant/variable/expression on RHS with a few exceptions where automatic type conversions are possible. Some examples of assignment statements are as follows :
b = a; /* b is assigned the value of a */
b = 5; /* b is assigned the value 5 */
b = a+5 /* b is assigned the value of expr a+5 */
The EXPRESSION on the RHS of the ASSIGNMENT STATEMENT can be :
-------------------------------------------------------------------------------
4.0 Introduction
4.1 Objectives
4.2 Assignment Statements
4.3 Arithmetic Operators
4.4 Relational Operators
4.5 Logical Operators
4.6 Comma and Conditional Operators
4.7 Type Cast Operator
4.8 Size of Operator
4.9 C Shorthand 1
4.10 Priority of Operators
4.11 Summary
4.12 Solutions / Answers
4.13 Further Reading
FOR MORE DETAILS ABOUT OPERATORS - READ THE ENTIRE BOX BELOW :
------------------------------------------------------------------
4.0 INTRODUCTION
------------------------------------------------------------------
In the previous unit we have learnt variables, constants, data types and how to declare them in C programming. The next step is to use those variables in expressions. For writing an expression we need OPERATORS along with VARIABLES. [for More about Operators and Expressions CLICK HERE] An EXPRESSION is a sequence of OPERATORS and OPERANDS that does not or a combination of the following:
- Specifies the computation of a value
- Designates an object for Function.
- Generates Side Effect.
An operator performs an operation (evaluation) on one or more operands. An operand is a sub-expression on which an operator acts.
This unit focuses on different types of operators available in C including the syntax and use of each operator and how they are used in C.
A computer is different from calculator in a sense that it can solve logical expressions also. Therefore, apart from arithmetic operators, C also contains logical operators.
-----------------------------------------
4.1 OBJECTIVES :
-----------------------------------------
- to Write and Evaluate Arithmetic Expression;
- to Express and Evaluate Relational Expressions;
- to Write and evaluate Logical Expressions;
- to Write and solve computer complex expressions (containing Arithmetic, Relational and Logical Operators), and
- to Check Simple Conditions using Conditional Operators;
4.2 ASSIGNMENT STATEMENT :
-------------------------------------------------
C provides an assignment operator for this purpose. The function of this operator is to assign the values of values in variables on right hand side(RHS) to variables on Left Hand Side(LHS).
The syntax of the assignment expression is as follows :
Variable=constant/ variable/ expression;
The data type of the variable on LHS should match the data type of constant/variable/expression on RHS with a few exceptions where automatic type conversions are possible. Some examples of assignment statements are as follows :
b = a; /* b is assigned the value of a */
b = 5; /* b is assigned the value 5 */
b = a+5 /* b is assigned the value of expr a+5 */
The EXPRESSION on the RHS of the ASSIGNMENT STATEMENT can be :
- AN ARITHMETIC EXPRESSION;
- A RELATIONAL EXPRESSION;
- A LOGICAL EXPRESSION;
- A MIXED EXPRESSION;
The above mentioned expression are different in terms of the type of operators connecting the variables and constants on the right hand side of the variable. Arithmetic operators, relational operators and logical operators are discussed in the following sections :
for example :
int a;
float
avg = (b+c)/2 ---------------------- Arithmetic expression
a=b&&c; ------------------------- Logical expression
a=(b+c)&&(b<c); ------------------- Mixed expression
-------------------------------------------------
4.3 ARITHMETIC OPERATORS :
-------------------------------------------------
The basic arithmetic operators in C are the same as in most other computer languages, and correspond to our usual mathematical / algebric symbolism. The following arithmetic operators are present in C :
OPERATOR MEANING
+ Addition
- Subtraction
* Multiplication
/ Division
% Modular Division
Some of the examples of algebric expressions and their C notation are given below :
EXPRESSION C NOTATION
b*g (b *g)/d
d
d
a3+cd (a*a*) + (c*d )
The Arithmetic Operators are all Binary Operators i.e. all the Operators have two Operands. The integer division yields the integer result.
For example,
----- The expression 10/3 --- evaluates to ----- 3
----- and the expression 15/4 --- evaluates to ----- 3
C provides the MODULUS OPERATOR, (In computing, the modulo operation finds the remainder of division of one number by another.); %, which yields (उत्पन्न करना, पैदावार, प्रतिफल, बताना) the reminder after integer division. The modulus operator is an integer operator that can be used only with integer operands. The expression x%y yields (उत्पन्न करना, पैदावार, प्रतिफल, बताना) the reminder after x is divided by y. Therefore, 10%3 yields((उत्पन्न करना, पैदावार, प्रतिफल, बताना)) 1 and 15%4 yields(प्रतिफल) 3.
An attempt to divide by zero is undefined on computer system and generally results in a run-time error. Normally, Arioghmetic Expression in C are written in straight-line from. Thus 'a divided by b' is written as a/b.
The operands in arithmetic expressions can be of integer, float, double type. In order to effectively develop C programs, it will be necessary for you to understand the rules that are used for implicit conversation of floating point and integer values in C.
They are mentioned below :
e.g. :
For example,
----- The expression 10/3 --- evaluates to ----- 3
----- and the expression 15/4 --- evaluates to ----- 3
C provides the MODULUS OPERATOR, (In computing, the modulo operation finds the remainder of division of one number by another.); %, which yields (उत्पन्न करना, पैदावार, प्रतिफल, बताना) the reminder after integer division. The modulus operator is an integer operator that can be used only with integer operands. The expression x%y yields (उत्पन्न करना, पैदावार, प्रतिफल, बताना) the reminder after x is divided by y. Therefore, 10%3 yields((उत्पन्न करना, पैदावार, प्रतिफल, बताना)) 1 and 15%4 yields(प्रतिफल) 3.
An attempt to divide by zero is undefined on computer system and generally results in a run-time error. Normally, Arioghmetic Expression in C are written in straight-line from. Thus 'a divided by b' is written as a/b.
The operands in arithmetic expressions can be of integer, float, double type. In order to effectively develop C programs, it will be necessary for you to understand the rules that are used for implicit conversation of floating point and integer values in C.
They are mentioned below :
- An arithmetic operator between an integer and integer always yields(प्रतिफल) an integer result.
- Operator between float and float yields a float result.
- Operator between integer and float yields a float result.
e.g. :
No comments:
Post a Comment