EXPRESSIONS

1) What is an expression?

 

In any programming language, if we want to perform any calculation or to frame any condition etc., we use a set of symbols to perform the task. These set of symbols makes an expression.
In the C programming language, an expression is defined as follows.

An expression is a collection of operators and operands that represents a specific value.

In the above definition, an operator is a symbol that performs tasks like arithmetic operations, logical operations, and conditional operations, etc.
Operands are the values on which the operators perform the task. Here operand can be a direct value or variable or address of memory location.

Expression Types in C

 

In the C programming language, expressions are divided into THREE types. They are as follows...

1.     Infix Expression

2.     Postfix Expression

3.     Prefix Expression

The above classification is based on the operator position in the expression.

Infix Expression

 

The expression in which the operator is used between operands is called infix expression.
The infix expression has the following general structure.

Operand1 Operator Operand2

Example

http://www.btechsmartclass.com/c_programming/cp_images/infix-expression.png

 

 

Postfix Expression

 

The expression in which the operator is used after operands is called postfix expression.
The postfix expression has the following general structure.

Operand1 Operand2 Operator

Example

http://www.btechsmartclass.com/c_programming/cp_images/postfix-expression.png

 

 

Prefix Expression

 

The expression in which the operator is used before operands is called a prefix expression.
The prefix expression has the following general structure.

Operator Operand1 Operand2

Example

http://www.btechsmartclass.com/c_programming/cp_images/prefix-expression.png

2) C Operator Precedence and Associativity

 

What is Operator Precedence?

 

Operator precedence is used to determine the order of operators evaluated in an expression. In c programming language every operator has precedence (priority). When there is more than one operator in an expression the operator with higher precedence is evaluated first and the operator with the least precedence is evaluated last.

 

What is Operator Associativity?

 

Operator associativity is used to determine the order of operators with equal precedence evaluated in an expression. In the c programming language, when an expression contains multiple operators with equal precedence, we use associativity to determine the order of evaluation of those operators.

 

In the above table, the operator precedence decreases from top to bottom and increases from bottom to top.

3) C Expression Evaluation

In the C programming language, an expression is evaluated based on the operator precedence and associativity. When there are multiple operators in an expression, they are evaluated according to their precedence and associativity. The operator with higher precedence is evaluated first and the operator with the least precedence is evaluated last.

An expression is evaluated based on the precedence and associativity of the operators in that expression.

To understand expression evaluation in c, let us consider the following simple example expression...

10 + 4 * 3 / 2

In the above expression, there are three operators +, * and /. Among these three operators, both multiplication and division have the same higher precedence and addition has lower precedence. So, according to the operator precedence both multiplication and division are evaluated first and then the addition is evaluated. As multiplication and division have the same precedence they are evaluated based on the associativity. Here, the associativity of multiplication and division is left to right. So, multiplication is performed first, then division and finally addition. So, the above expression is evaluated in the order of * / and +. It is evaluated as follows...

4*3====>12
12/2===>6
10+6===>16
the expression is evaluated to 16.

4) Type Casting and Conversion in C

In a programming language, the expression contains data values of the same datatype or different data types. When the expression contains similar datatype values then it is evaluated without any problem. But if the expression contains two or more different datatype values then they must be converted to the single datatype of destination datatype. Here, the destination is the location where the final result of that expression is stored. For example, the multiplication of an integer data value with the float data value and storing the result into a float variable. In this case, the integer value must be converted to float value so that the final result is a float datatype value.
In a c programming language, the data conversion is performed in two different methods as follows...

1.     Type Conversion

2.     Type Casting

Type Conversion

The type conversion is the process of converting a data value from one data type to another data type automatically by the compiler. Sometimes type conversion is also called implicit type conversion. The implicit type conversion is automatically performed by the compiler.
For example, in c programming language, when we assign an integer value to a float variable the integer value automatically gets converted to float value by adding decimal value 0. And when a float value is assigned to an integer variable the float value automatically gets converted to an integer value by removing the decimal value. To understand more about type conversion observe the following...

 

Int i = 10 ;
float x = 15.5 ;
char ch = 'A' ;

i = x ; =======> x value 15.5 is converted as 15 and assigned to variable i

x = i ; =======> Here i value 10 is converted as 10.000000 and assigned to variable x

i = ch ; =======> Here the ASCII value of A (65) is assigned to i

 

Typecasting

 

Typecasting is also called an explicit type conversion. Compiler converts data from one data type to another data type implicitly. When compiler converts implicitly, there may be a data loss. In such a case, we convert the data from one data type to another data type using explicit type conversion. To perform this we use the unary cast operator. To convert data from one type to another type we specify the target data type in parenthesis as a prefix to the data value that has to be converted. The general syntax of typecasting is as follows.

 

(TargetDatatype) DataValue

Example

int totalMarks = 450, maxMarks = 600 ;
float average ;

average = (float) totalMarks / maxMarks * 100 ;

In the above example code, both totalMarks and maxMarks are integer data values. When we perform totalMarks / maxMarks the result is a float value, but the destination (average) datatype is a float. So we use type casting to convert totalMarks and maxMarks into float data type.

5) C Input Output (I/O)

 

C language has standard libraries that allow input and output in a program. The stdio.h or standard input output library in C that has methods for input and output.

 

scanf()

 

The scanf() method, in C, reads the value from the console as per the type specified.

Syntax:

scanf(“%X”, &variableofXType);                                                                                                                           

 

%X is the format specifier in C. It is a way to tell the compiler what type of data is in a variable and & is the address operator in C, which tells the compiler to change the real value of this variable, stored at this address in the memory.

printf()

 

The printf() method, in C, prints the value passed as the parameter to it, on the console screen.

Syntax:

printf(“%X”, variableOfXType);

%X is the format specifier in C. It is a way to tell the compiler what type of data is in a variable.

6) Pre-processing Command

 

Pre-processing commands are used to include header files and to define constants. We use the #include statement to include the header file into our program. We use a #define statement to define a constant. The pre-processing statements are used according to the requirements. If we don't need any header file, then no need to write #include statement. If we don't need any constant, then no need to write a #define statement.

7) Header File

 

A header file is a file with extension .h which contains C function declarations and macro definitions to be shared between several source files. There are two types of header files: the files that the programmer writes and the files that comes with your compiler.

You request to use a header file in your program by including it with the C pre-processing directive #include, like you have seen inclusion of stdio.h header file, which comes along with your compiler.

Including a header file is equal to copying the content of the header file but we do not do it because it will be error-prone and it is not a good idea to copy the content of a header file in the source files, especially if we have multiple source files in a program.

 

Include Syntax

 

Both the user and the system header files are included using the pre-processing directive #include. It has the following two forms −

#include <file>

This form is used for system header files. It searches for a file named 'file' in a standard list of system directories. You can prepend directories to this list with the -I option while compiling your source code.

#include "file"

This form is used for header files of your own program. It searches for a file named 'file' in the directory containing the current file. You can prepend directories to this list with the -I option while compiling your source code.