×

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

Assignment Operator Program in C

An assignment operator is a symbol used to assign a value to a variable in a programming language. The assignment operator is often the equal symbol (=) in programming languages....

12 minutes read.

Sum of N numbers in C using For loop

Before we move on the program of sum of N numbers, first we have to know about the For Loop statement. The syntax of ‘for’ loop in C programming language is...

3 minutes read.

How to initialize array to zero in C

In this article, you will learn how to initialise an array to 0 in C In C, an array is declared as: char ZEROARRAY[2022]; The global scope changes at runtime to all zeros....

3 minutes read.

Dangling pointers in C

Dangling pointers in C: A pointer is a variable that stores the memory address of other variables. The pointers may also store the address of other’s memory items. They are...

4 minutes read.

Booleans in C

Introduction to Boolean With all the complexities of programming, it can take time to understand the basics. One concept, in particular, that confuses many beginners is the use of Booleans in...

6 minutes read.

Bank management system in C

This is a mini project that is constructed completely using the C language. The bank management system is a beginner friendly project. To construct this user only needs to know...

12 minutes read.

How to Find Square Free Numbers in C?

Introduction Square-free number program is a typical interview and academic question in C Programming. In this section, we will define square-free integers, construct algorithms and C program to print the nth...

11 minutes read.

Comma Operator in C

What is a comma operator? The comma operator in the C programming language has the least priority. The comma operator is essentially a binary operator that operates on the first operand...

4 minutes read.

Array to function in C

Array to function in C We can create functions that receive an array as an argument. To pass an array into a function, we need to write the name of the...

4 minutes read.

Projects on C language in 2024

Introduction Most aspiring programmers learn the C language as their first high-level programming language. The most flexible language utilized in every industry is, without a doubt, C. Even after 50 years...

9 minutes read.

What is required in each C Program?

Each C program must require one function, i.e., main() function. It is because when we execute the C program, C compiler looks for the main() function, and from here only...

5 minutes read.

Exponential() in C

Introduction: In C language, there are available many types of functions. The exponential() function is one of them. It is a mathematical function. This article describes using the pow() property to...

3 minutes read.

Pseudo Code in C

Pseudo code in C can be referred to as a simple way or method to write programming code or program algorithm in English. A pseudocode is an informal representation of...

4 minutes read.

What is 2s Complement in C?

The complement of 2s in C is generated from the 1s in C. As we know, the 1s complement of a binary number is generated by converting bit 1 to...

4 minutes read.

Java Array vs ArrayList

As we all know, arrays are linear data structures that allow you to add elements to them continuously in memory address space, whereas ArrayList is a Collection framework class. Despite...

4 minutes read.

What are linker and loader in C

Linker and loader are utility programs that have a significant role in executing a program. Linker: A linker is a program that joins the object files produced by the assembler/ compiler...

3 minutes read.

Assert() Function in C

Assert (): In C, the statements are executed with the exit statement. In C language declare, and it tests the condition parameters. If the statement executed will be false, it shows...

4 minutes read.

How to move a text in C

A text can be moved to the desired location in the output based on the coordinates provided in the gotoxy() function. What is gotoxy() function? In C language, gotoxy() is a pre-defined...

3 minutes read.

Bigint (BIG INTEGERS) in C with Example

The maximum number of digits that a long, long int can have in C/C++ is 20. The issue is how to store the 22-digit number, which is difficult to do...

9 minutes read.

getch() function in C

getch() is a pre-define or built-in function present in the conio.h library. It returns the given character immediately without waiting for the enter key to be entered. By using getch()...

3 minutes read.