Programming with C

⌘K
  1. Home
  2. Docs
  3. Programming with C
  4. Basics of C Programming
  5. Expressions

Expressions

Expressions

An expression in C is made up of one or more operands. Simplest form of an expression consists of a single operand

Example

2 is an expression that consists of a single operand. But such an expression does not specify any operation to be performed and is not meaningful. A meaningful expression consists of one or more operands and operators that specify the operations to be performed on operands.

Example

a = 2+3 is a meaningful expression

Three operands
a, 2, 3
Two operators
= , +

An expression is a sequence of operators and operands that specifies the computation of a value.

Operand

An operand specifies an entity on which an operation is to be performed. An operand can be a variable name, a constant, a function call or a macro name

Example

Consider the expression,

a = printf(“Hello”) +2

The expression has three operands namely:

  • A variable name ➡️ (a)
  • Function call ➡️ (printf(“Hello”)
  • A constant ➡️ (2)

Operators

An operator specifies operation to be applied to its operands

Example

Consider the expression,

a = printf(“Hello”) +2

The expression above has three operator namely,

  • Function call operator ➡️ ( )
  • Arithmetic operator ➡️ +
  • Assignment operator ➡️ =

Types of expressions

Simple expression

An expression that has only one operator is known as simple expression

Example

a+2

Compound expression

An expression that involves more than one operator is called a compound expression

Example

b = 2+3+5

The evaluation of simple expression is easier as it is having only one operator. But in compound expression, one must determine the order in which the operators will operate. The order in which the operators will operate depends upon the precedence and the associativity of operators

Classification of operators

The operators in C are classified based on:

  • The number of operands on which an operator operates
  • The role of an operator

Classification based on number of operands

Unary Operator

A unary Operator operates on only one operand

Example
-3

Here, – is a unary minus operator as it operates on only one operand

Examples of unary operator
  • & ➡️ address of operator
  • sizeof operator
  • ~ ➡️ bitwise negation

Binary Operator

A binary operator operates on two operands. It requires an operand towards its left and right.

Example
2-3

Here – is a binary minus operator as it operates on two operands.

Examples of binary operators

+, -, *, /

Ternary operator

A ternary operator operates on three operands. Conditional operator is the only ternary operator in C

Examples of Conditional operators

(?:)

Classification based on number of operators

Based upon the role, operators are classified as

  1. Arithmetic operators
  2. Relational operators
  3. Logical Operators
  4. Bitwise Operators
  5. Assignment Operators
  6. Miscellaneous operators

Views: 0

Articles

How can we help?

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments