Computer Programming C: UNIT I: Introduction to C

C Programming: Expressions

An expression represents data item such as variables, constants and are interconnected with operators as per the syntax of the language.

EXPRESSIONS

 

1. INTRODUCTION

An expression represents data item such as variables, constants and are interconnected with operators as per the syntax of the language. An expression is evaluated using assignment operator.

Syntax: variable=expression;

Description: Any 'c' valid variable and expression.

Example: x=a*b‒c;

In the above statement the expression evaluated first from left to right. After the evaluation of the expression the final value is assigned to the variable from right to left.

Example:

y=(a/b)+c;

z=(a*b)‒d;

The blank space around an operator is optional and adds only to improve readability of the program. The variables that are used in an expression must be declared at the declaration part of the program.

Examples of Algebraic expressions and C expressions


 

2. OPERATOR PRECEDENCE AND ASSOCIATIVITY OF OPERATORS

Precedence is used to determine the order in which different operators in a complex expression are evaluated.

Associativity is used to determine the order in which operators with same precedence are evaluated in a complex expression.

1. Arithmetic Operators Precedence

Usually the Arithmetic Operators are evaluated from left to right using the precedence of operators when the expression is written without the parameters. The Arithmetic Operator precedence


The Arithmetic expression evaluation is carried out using the two phases from left to right through the expression.

First Phase: The highest priority operators are evaluated in the expression.

Second Phase: The lowest priority operators are evaluated in the expression.

Examples:

result = x ‒ y/3 + z * 3 ‒ 1

where, x = 3, y = 9, z = 77

sum = 3 ‒ 9/3 + 77 * 3 ‒ 1


This expression is revalued in two phases, as follows:

First phase :

sum = 3‒3+ 77 * 3 ‒ 1

sum = 3‒3+ 231 ‒ 1

Second phase:

sum = 0 + 231 ‒ 1

sum = 231 ‒ 1

sum = 230

This is illustrated in figure.

However, during the evaluation of expression, the order of evaluation can be changed by putting the parenthesis in an expression.

Example :

(3‒9) / 3 + 77 * (3 ‒1)

‒ 6/3 + 77 * (3 ‒ 1)

‒ 6/3 + 77 * 2

‒ 2 + 154

= 152

2. Rules for evaluation of expression

a) Evaluate the sub expressions from left to right if parenthesized.

b) Evaluate the arithmetic expression from left to right using the rules of precedence.

c) The highest precedence is given to the expressions with in parenthesis.

d) Apply the associative rule, if more operators of the same precedence occurs.

e) Evaluate the inner most sub expression if the parenthesis are nested.

 

3. PRECEDENCE AND ASSOCIATIVITY OF OPERATORS

Every operator in 'C' has a precedence associated with it. The precedence is used to determine how an expression involving more than one operator is evaluated. The operators at the higher level of precedence are evaluated first. The operators of the same precedence evaluated either from left to right or right to left depending on the level is known as associativity.

Hierarchy of operators in 'C' are summarised below.

i) Any expression within parenthesis is first evaluated, if more than one pair of parenthesis are present, the innermost parenthesis is evaluated first.

ii) Unary operators are evaluated first in an expression.

iii) Then priority is given for multiplication and division.

iv) Then subtraction and addition are performed.

v) Then relational operations are performed.

vi) Then equality checking is performed.

vii) Then logical operations are performed.

viii) Then the conditions are checked.

ix) Finally the assignment operation is carried out.

Summary of 'C' operators precedence and associativity


 

Computer Programming C: UNIT I: Introduction to C : Tag: Computer Science : - C Programming: Expressions


Computer Programming C: UNIT I: Introduction to C



Under Subject


Computer Programming C

CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation



Related Subjects


English Essentials I

EN25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


தமிழர் மரபு - Heritage of Tamils

UC25H01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Calculus

MA25C01 Maths 1 M1 - 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Physics I

PH25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Chemistry I

CY25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Makerspace

ME25C04 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Computer Programming C

CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Computer Programming Python

CS25C02 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Fundamentals of Electrical and Electronics Engineering

EE25C03 1st Semester EEE Depart | 2025 Regulation | 1st Semester 2025 Regulation


Introduction to Mechanical Engineering

ME25C03 1st Semester Mechanical Dept | 2025 Regulation | 1st Semester 2025 Regulation


Introduction to Civil Engineering

CE25C01 1st Semester Civil, Agri Departments | 2025 Regulation | 1st Semester 2025 Regulation


Essentials of Computing

CS25C03 1st Semester - AIDS, CSE, CSE(CY), IT Department | 2025 Regulation | 1st Semester 2025 Regulation


Applied Physics I Laboratory

PH25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Applied Chemistry I Laboratory

CY25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Computer Programming C Laboratory

CS25C01 1st Semester EEE, ECE, CSE, CSE(CY), AIDS, IT practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Computer Programming Python Laboratory

CS25C02 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Engineering Drawing

ME25C01 EEE, Mech, Agri, EEE Depts | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electronics and Electrical Engineering

EE25C04 1st Semester ECE Dept | 2025 Regulation | 1st Semester 2025 Regulation


Essentials of Computing - Laboratory

CS25C03 1st Semester AIDS, CSE, CSE(CY), IT Depts | practical Laboratory Manual | 2025 Regulation | 1st Semester 2025 Regulation