C Programming Language – Operators and Expressions

By
Last updated:

Key Terms : C Programming Language

Operator, Expression, Integer expression, Real arithmetic, Relational operators, Logical operators, Assignment operators, Bitwise operators, Arithmetic Operations. C programming language.

Introduction to C Programming Language – Operators and Expressions

Programming language C supports a rich set of built-in operators. We have already used several of them, such as =, +, -, *, & and <. An operator is a symbol that tells the computer to perform certain mathematical or logical manipulations. Operators are used in programs to manupulate data and variables. They usually form a part of the mathematical or logical expressions.

Programming language C operators can be classified into a number of categories.

    • Arithmetic operators
    • Relational operators
    • Logical operators
    • Assignment operators
    • Increment and decrement operators
    • Conditional operators
    • Bitwise operators
    • Special operators

An expression is a sequence of operands and operators that reduces to a single value.

For example 10+15 is an expression whose value is 25. The value can be any type other than void.

Arithmetic Operators – Language C

Programming language C provides all the basic arithmetic operators. They are listed in next programm. The operators +, -, *, and / all work the same way as they do in other languages. These can operate on any built-in data type allowed in C language. The unary minus operator, in effect, multiples its single operand by-1. Therefore, a number preceded by a minus sign changes its sign.

Integer division trncates any fractional part. The modulo division operation produces the remainder of an integer division. Examples of use of arithmetic operators are:

a-b     a+b
a*b     a/b
 a%b     -a*b

Here a and b are variables and are known as operands. The modulo division operatoe % cannot be used on floating point data. Note that C does not have an operator for exponentiation. Older versions of programming language C dose not support unary plus but ANSI C supports it.

Integer Arithmetic C Programming Language

When both the operands in a single arithmetic expression such as a+b are intergers, the expression is called an integer expression, and the operation is called integer arithmetic. Integer arithmetic always yields an integer value. The largest integer value depends on the machine, as pointed out earlier. In the above examples, if a and b are integers, than for a = 14 and b = 4 we have the following results:

a-b = 10
a+b = 18
a*b = 56
                        a/b = 3 (decimal part truncated)
                       a%b = 2 (remainder of division)

During interger division, if both the operands are the same sign, the result is truncated towards zero. If one of them is negative, the direction of trunction is implementation dependent. That is, 6/7=0 and -6/-7=0 but -6/7 may be zero or -1.

Program
     main()
     {
       int months, days ;

       printf("Enter days\n") ;
       scanf("%d", &days) ;

       months = days / 30 ;
       days = days % 30 ;
       printf("Months = %d Days = %d", months, days) ;
     }
Output
     Enter days
     265
     Months = 8 Days = 25
     Enter days
     364
     Months = 12 Days = 4
     Enter days
     45
     Months = 1 Days = 15

The variables months and days are declared as integers. Therefore, the statement months = days/30; truncates the decimal part and assign the integer part to months. Similarity, the statement days = days%30; assigns the remainder part of the days. Thus the given number of days is converted into are equivalent number of months and days and the result is printed as shown in the output.

C Programming Language – Real Arithmetic

An arithmetic operation involving real operands is called real arithmetic. A real operand may assumed value either in decimal or exponential. Since floating point values are rounded to the number of significant digits permissible, the final value is an approxmation of the correct result. If x,y, and z are floats, then we will have

x = 6.0/7.0 = 0.857143

y =1.0/3.0 = 0.333333

z = -2.0/3.0 = -0.666667

The operator % cannot be used with real operands.

Mixed mode Arithmetic

When one of the operands is real and the other is integer, the expression is called a mixed mode arithmetic expression. If either operand is of the real type, then only the real operation is performed and the result is always a real number. Thus 15/10.0 = 1.5 where as 15/10 = 1 More about mixed operations will be discussed later when we deal with the evaluation of expressions.

Relational Operators

C Programming Language, We often compare two quantities and depending on their relation, take certain decision.

For example, we may compare the age of two persons, or the price of two items, and so on, These comparisons can be done with the help of relational operators. We have already used the symbol ‘<‘, meaning less than an expression such as a<b or 1 < 20 containing a relational operator is termed as a relational expression. The value of a relational expression is either one or zero. It is one if the specified relation is true and zero if the relation is false.

For example 10 < 20 is true, but 20 < 10 is false Language C support six relational operators in all. These operators and their meaning are shown in image.

ae-1 and ae-2 are arithmetic expressions, which may be simple constants, variables or combination of them. Give below are some examples of simple relational expressions and their values:

4.5 <= 10 TRUE
4.5 <-10 FALSE
-35 > =0 FALSE
10 < 7+5 TRUE

a+b = c+d TRUE only if the sum of values of a and b is equal to the sum of values of c and d. When arithmetic expressions are used on either side of a relational operator, the arithmetic epressions will be evaluated first and then the results compared. That is, arithmrtic operators have a higher priority over relational operators.

Relational expressions are used in decision statements such as if and while to decide the course of action of a running program. We have already used the while statement in before. Dcision statements are discussed in details in`next post.

Logical Operators > Language C

In addition to the relational operators, C has the following three logical operators.

&&   meaning logical AND
||    meaning logical OR
!          ”       logical NOT

The logical operators && and || are used when we to test more than one condition and make decisions An example is : a>b&&x==10

An expression of this kind, which combines two or more relational expressions, is termed as a logical repression or a compound relational expression. Like the simple relational expressions, a logical expression also yields a value of one or zero, according to the truth table. The logical expression given above is true only if a>b is true and x == 10 is true. If either (or both) of them after false, the expression is false.

Shorthand Assignment Operators

Statement with simple assignment Operator Statement with shorthand operator
a=a+1 a+=1
a=a-1 a-=1
a=a*(n+1) a*=n+1
a=a/(n+1) a/=n+1
a=a%b a%=b

 

The use of shorthand assignment operators has three advantages:

  1. What appears on the left-hand side need not be repeated and therefore it becomes easier to write.
  2. The statement is more concise and easier to read.
  3. The statement is more efficient.

Advantages may be appreciated if we consider a slightly more involved statement like value(5*j-2) = value(5*j-2) + delta;

With the help of the += operator, this can be written as follows: value(5*j-2) + = delta;

It is easier to read and understand and is more efficient because the expression 5*j-2 is evaluated only once.

Special Operators

C supports some special operators of interest such as comma operator, sizeof operator, pointer operators (& and *) and member selection operators (. and ->). The comma and sizeof operators are discussed in this section while the pointer operators are discussed in next chapter. Member selection operators which are used to select members of a structure in chapter upcoming. ANSI committee has introduced two preprocessor operators known as “string-izing” and “token-pasting” operators (# and ##).

The Comma Operator

The comma operator can be used to link the related expressions together. A comma-linked list of expressions are evaluated left to right and the value of right most expression is the value of the combined expression. value = (x = 10, y = 5, x+y);

first assigns the value 10 to x, then assigns 5 to y, and finally assigns 15 to value. Since comma operator has the lowest precedence of all operators, the parentheses are necessary.

In for loops: for ( n = 1, m = 10, n<=m; n++, m++)

In while loops: while (c = getchar(), c != ’10’)

Exchanging values: t = x, x = y, y =t;

The sizeof Operator

The sizeof is a compile time operator and, when used with an operand, it returns the number of bytes the operand occupies. The operand may be a variable, a constant or a data type qualifier.

Examples :     m = sizeof (sum);
               n = sizeof (long int);
               k = sizeof (235L);

The sizeof operators is normally used to determine the lengths of arrays and structures when their sizes are not known to the programmer, It is also used to allocated memory space dynamically to variables during execution of a program.

Read More Topics
The TCP/IP Reference Model
The HyperText Transfer Protocol
The Transport Layer in the Reference Model
The User Datagram Protocol (UDP)
The Transmission Control Protocol (TCP)

Santhakumar Raja

Hello The goal of this blog is to keep students informed about developments in the field of education. encourages pupils to improve as writers and readers.

For Feedback - techactive6@gmail.com

Leave a Comment