×

Expressions in C

Expressions in C: In the C programming language, an expression defines a formula in which the operands are linked to each other by using operators to compute the value. The operand might be a variable, a function reference or a constant, or an array element. 

Expression is a combination of operators and operands that reduces a single value from a more complex one. The operation is performed on the data item, which is created as an operand. 

An operator indicates the operations that will be performed on the data. An expression in the C standard is also defined as two or more operands connected by one operator and are said to perform an operation that a programmer will define. 

The operator is represented using symbols such as “+,” “-”, “/”, “*”, and so on. The C language form operators join separate individual constants, array elements, and present variables. 

In other words, an expression is a sequence of operands and operators that defines a computation. It is provided by evaluating the expression, such as the evaluation of 2 + 2 yields the result 4. 

The result may generate side effects as sometimes, user-defined print functions give the standard output stream and designate functions and objects. Value categories classify expression by their values and order of evaluation of arguments, and subexpression specifies the order in which intermediate results are obtained.

For example, “ a + b ”, in this expression, the addition character (+) is an operator, while the characters ‘a’ and ‘b’ are the operands.

Types of Expression in C 

There are four types of expression in the C standard, which are as discussed below:

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

All these expressions take specific types of operands and then use a specific set of operators. The evaluation of the particular expression produces a specific value. For instance, “ x = 9 / 2 + a * b ”, is not an expression. The part after the equal symbol is considered as an expression.

?     Arithmetic Expression

An arithmetic expression is an expression that contains operands and arithmetic operators. It computes a value of type int, float, and double. When the expression consists of only the integral operands, it is known as a pure integer expression.

When it contains the real operands, it is referred to as pure, authentic expression, and when it consists of both the integral operands and real operands, it is referred to as mixed-mode expression. An arithmetic expression is evaluated by performing one operation at a time. 

The precedence and associativity of the operators decide the order of the evaluation of the individual operations.

The Addition (+), Subtraction (-), Multiplication (*), Division (/), Modulus (%), Increment (++), and Decrement (–) operators are referred to as the “Arithmetic expressions”. 

Syntax:

A + B;

A - B;

A / B;

A * B;

A % B;

A++;

A--;

?     Relational expression

The relational expression is an expression that is used to compare the two operands. It is referred to as a condition used to decide whether the action should be undergone. A

numeric value cannot be compared to a string value in the relational expression. 

The result of the relational expression can be either a zero or a non-zero value. Hence, the zero value is the equivalent to a false, and a non-zero value is always equivalent to a true value.

“ == , !=, >, <, >=, <=” are referred to as relational expression. 

Syntax:

A == B;

A != B;

A > B;

A < B;

A >= B;

A <= B;

?     Logical Expression

The logical expression is an expression that computes either a zero or a non-zero value. A logical expression is considered to be a complex test condition while taking a decision.

Logical AND (&&), logical OR (||), and Logical NOT (!) are the operators that come under logical operators. 

Each of the logical operators falls into its precedence groups. A ‘logical AND’ has higher precedence than ‘logical OR’. Both the precedence groups are lower than the group containing the equality operators. 

The associativity is left to right. The C language also includes the unary operator that negates the value of the logical expression. The associativity of the negation operator is right to left.

Syntax:

A && B;

A || B;

A ! B;

?     Conditional Expressions

A conditional expression is an expression that always returns ‘1’ if the condition specified is true. Else will return ‘0’ if the condition is false. A conditional operator is also known as a ternary operator. It is used to perform the conditional check.

Syntax:

expression 1 ? expression 2 : expression 3

Where,

 expression 1, expression 2, and expression 3 are the three expressions specified.

 It is evaluated based on the value of the expression 1.

Suppose the condition of the expression 1 satisfies and is valid. In that case, the final condition is represented by the expression 2; else will be represented by expression 3.

“ ?, :” are considered to represent the conditional expressions.  

E.g.:

( x + 2 = 10) ? ‘True’ : ‘False’

This expression makes use of the conditional operator. A conditional expression frequently appears on the right-hand side of a simple assignment statement. The resulting value of the conditional expression is assigned to the identifier on the left.

The conditional operator has its precedence, just above the assignment operator. The associativity is right to left.


Related Topics

Macros in C

In the C programming language, the macro is defined as a segment of the code replaced by the macro defined value at the beginning. ‘#define’ is the directive that defines...

3 minutes read.

C Math Library

C Math Library The <math.h> header defines various mathematical functions and one macro. Many functions are available in this library to take double as an argument and then return double as...

4 minutes read.

File Operations in C

Why do I need the file? All data will be lost when the program exits. Saving data to a file keeps it safe even if the program stops working. If there...

6 minutes read.

How to create a node in C?

A node is a small concept taken from the graph theory, or a node is a fundamental unit of a data structure, like a tree data structure. Every graph contains...

3 minutes read.

Errors in C

Errors in C Errors are nothing but problems or faults that pretty much occur in all the programming languages. Errors make the behavior of the program seem abnormal, and even the...

4 minutes read.

Error handling in C

Error handling in C: The C programming standard does not provide direct convenience for handling the errors. However, being a system programming language, it definitely will give access to handling...

4 minutes read.

Pi() Function in C

Pi: Mathematic constants are not defined in the c or c++ programming languages. To use the Mathematical constants firstly, we have to define the _USE_MATH_DEFINES and then declare the cmath and...

3 minutes read.

Factorial Program in C using For Loop

Before move on the factorial program. We have to know about the for loop in C programming language. The syntax of the For Loop is: for (initialization statement; test expression; an increment...

3 minutes read.

Flexible Array Members in a Structure in C

There is flexibility to declare array from c99 generation onwards that declaration of the collections can be made possible without even mentioning its dimension, which means that the displays are...

4 minutes read.

Compound Interest Program in C

Interest on loans and deposits is compound interest. This is the most commonly used concept in everyday life. Compound interest on an amount is based on principal and interest earned...

3 minutes read.

Pointer to pointer in C

A pointer to another pointer is another type of multiple indirections and a chain of many pointers. Generally, a pointer consists of the address of the variable. Once a pointer to...

4 minutes read.

Pointers in C

Pointers in C: In the C programming language, a pointer is a pointer variable that points to the address of the other variable. It also stores the address of the...

4 minutes read.

Write a program that produces different results in C and C++

In this tutorial, we'll explore several programs that, depending on whether they are developed using C or C++ compilers, will produce varying outputs. There are numerous similar programs, but we will just...

2 minutes read.

What is the main in C?

In the C programming language, "main" is a predefined term or function. It is the primary function in every C program and is in charge of beginning and terminating the...

6 minutes read.

Control statement in C

What is a control statement? A control statement helps us to control the flow of the program. The control statement helps us to execute the program's instructions in a user-defined order....

8 minutes read.

Attributes in C

Attributes are one of modern C++ key features. It is the process by which initiator can add more information to the language instead of introducing new keywords for every attribute....

4 minutes read.

User Defined Functions in C

C Functions: A function is defined as a block of code that is used to perform some specific task. In functions, we use flower brackets ({}) which contain the set of programming...

5 minutes read.

C program to Store Information of Students Using Structure

What is the Structure in C? User-defined data types include structures. Structures aid your ability to combine things of various categories into a single group. Like arrays, it operates similarly. A...

3 minutes read.

Data Structures And Algorithms in C

C language is one of the most flexible and simple languages. So, it is always better to learn any concept in C language compared to other languages. Let us look...

13 minutes read.

Hexadecimal to Binary in C

What is hexadecimal? Hexadecimal defines a sequence of numbers centered on-16, i.e., before assigning the next number to a new location, it describes a numbering scheme that includes 16 sequential numbers as basic...

2 minutes read.