Arithmetic Expressions in C | C Expressions

Expression:

An expression is a formula which combines operands with operators to produce a result. The values produce by these expressions can be stored in variables, or used as a part of even larger expressions. C can handle any complex mathematical expression. Expressions are evaluated using an assignment statement.


An operand in C may be a variable, a constant or a function reference.


For example, in the following expressions,

a+b

Here, + is an operator and a , b are operands.


An expression represents a single data item such as a number or character or a constant or a variable or an array element. An expression may also consist of some combination of such single items connected by one or more operators.


For example, a=b or a+b or a=a+b or ++a


There are four types of expressions exist in C:

  • Arithmetic expressions
  • Relational expressions
  • Logical expressions
  • Conditional expressions

 

Arithmetic Expressions:

An arithmetic expression is an expression that consists of operands and arithmetic operators, such as addition, subtraction, and so on. These combinations of operands and operators should be mathematically meaningful, otherwise, they can not be considered as an Arithmetic expression in C.

An arithmetic expression computes a value of type int, float or double. An arithmetic expression contains only arithmetic operators and operands.

When an expression contains only integral operands, then it is known as pure integer expression when it contains only real operands, it is known as pure real expression, and when it contains both integral and real operands, it is known as mixed mode expression.

 

Evaluation of Arithmetic Expressions:

Expressions are evaluated using an assignment statement of the form:

variable = expression;

  • Variable is any valid C variable name.
  • When the statement is encountered, the expression is evaluated first and the result then replaces the previous value of the variable (on the left-hand-side).
  • All variables used in the expression must be assigned values before evaluation is attempted.


Examples of Evaluation Statement:

x= a*b-c;

y=b/c*a;

z=a-b/c+d;

The expressions are evaluated by performing one operation at a time. The precedence and associativity of operators decide the order of the evaluation of individual operations.



Programming example:


Program to input marks of 5 subjects of a student and calculate total, average and percentage of all subjects. (Assume that maximum marks obtained by a student in each subject is 100).



Output:




Post a Comment

0 Comments