C Programming
C Operators and Expressions
Operators
- Individual constants, variables can be joined together by various operators to form expressions.
- The data items that operators act upon are called operands.
- Some operators require two operands, while other act upon only one operand.
Types of Operators
- Arithmetic
- Unary
- Relational and Logical
- Assignment
- Conditional
- Bitwise
- Special
Arithmetic Operators:
There are 5 arithmetic operators in C. They are:
Operator | Purpose |
+ | Addition |
– | Subtraction |
* | Multiplication |
/ | Division |
% | Remainder after integer division |
Note:
- The % operator is sometimes referred to as the modulus operator.
- Suppose that v1 and v2 are floating-point variables whose values are 12.5 and 2.0, respectively. Several arithmetic expressions involving these variables are shown below, together with their resulting values:
Expression |
Value |
v1 + v2 | 14.5 |
v1 – v2 | 10.5 |
v1 * v2 | 25.0 |
v1 / v2 | 6.25 |
- The operation v1 % v2 is illegal and reported as error by the compiler.
Unary Operators:
- C includes a class of operators that act upon a single operand to produce a new value. Such operators are known as unary operators.
- Unary operators usually precede their single operands, though some unary operators are written after their operands.
- Perhaps the most common unary operation isunary minus, where a numerical constant, variableor expression is preceded by a minus sign.
- Some programming languages allow a minus sign to beincluded as a part of a numeric constant. In C, however, all numeric constants are positive.
- Thus, a negative number is actually an expression, consisting of the unary minus operator, followed by apositive numeric constant.
- Here are several examples which illustrate the use of the unary minus operation,
-743 | -0X7FFF | -0.2 |
-root1 | -(x + y) | -3 * ( x + y ) |
There are two other commonly used unary operators:
- The increment operator , ++, and the decrement operator , —.
- The increment operator causes its operand to be increased by 1, whereas the decrement operator causes its operand to be decreased by 1.
- The operand used with each of these operators must be a single variable.
Example:
- Suppose that i is an integer variable that has been assigned a value of 5. The expression ++i, which is equivalent to writing i = i + 1, causes the value of i to be increased to 6.
- Similarly, the expression –i, which is equivalent to i = i – 1, causes the (original) value of i to be decreased to 4.
- The increment and decrement operators can each be utilized in two different ways, depending on whether the operator is written before or after the operand. i.e ++i or i++.
Relational and Logical Operators:
There are 4 relational operators in C. They are:
Operator | Meaning |
< | less than |
<= | less than or equal to |
> | greater than |
>= | greater than or equal to |
- These operators all fall within the same precedence group, which is lower than the arithmetic and unary operators.
- The associativity of these operators is left to right.
Operator | Meaning |
== | equal to |
!= | not equal to |
- The equality operators fall into a separate precedence group, beneath the relational operators.
- These operators also have a left-to-right associativity.
Note:
- These 6 operators are used to form logical expressions, which represent conditions that are either true or false.
- The resulting expression will be of type integer, since true is represented by the integer value 1 and false is represented by the value 0.
Example:
Suppose that i, j and k are integer variables whose values are 1, 2 and 3, respectively . Several logical expressions involving these variables are shown below :-
Expression | Interpretation | Value |
i < j | true | 1 |
( i + j) >= k | true | 1 |
( j + k ) > ( i + 5 ) | false | 0 |
k ! = 3 | false | 0 |
j == 2 | true | 1 |
- In addition to the relational and equality operators. C contains two logical operators ( also called logical connectives). They are:
Operator | Meaning |
&& | and |
|| | or |
- These operators are referred to as logical and and logical or, respectively.
Example:
- Suppose that i is an integer variable whose value is 7, f is floating-point variable whose value is 5.5, and c is a character variable that represents the character ‘w’. Several complex logical expressions that make use of these variables are shown below:
Expression | Interpretation | Value |
( i >= 6 ) && ( c == ‘w’ ) | true | 1 |
( i >= 6 ) || ( c == 119 ) | true | 1 |
( f < 11 ) && ( i > 100 ) | false | 0 |
( c != ‘p’) || (( i + f ) <= 10) | true | 1 |
- C also includes the unary operator ! that negates the value of a logical expression; i.e., it causes an expression that is originally true to become false, and vice versa.
- This operator is referred to as the logical negation ( or logical not ) operator
Example:
Suppose that i is an integer variable whose value is 7, f is floating-point variable whose value is 5.5. Several logical expressions that make use of these variables and the logical negation operator are shown below:
Expression | Interpretation | Value |
f > 5 | true | 1 |
! ( f > 5 ) | false | 0 |
i <= 3 | false | 0 |
! ( i <= 3 ) | true | 1 |
Assignment Operators ( = ):
- Assignment expressions that make use of this operator are written in the form:
Identifier = expression
- Where identifier generally represents a variable, and expression represents a constant, a variable or a more complex expression.
Example:
- Here are some typical expressions that make use of the = operator.
- The first assignment expression causes the integer value 3 to be assigned to the variable a, and the second assignment causes the value of y to be assigned to x. In the third assignment, the floating point value 0.001 is assigned to delta. The last two assignments each result in the value of an arithmetic expression to a variable.
a = 3 |
x = y |
delta = 0.001 |
sum = a + b |
area = length * width |
- Remember that the assignment operator = and the equality operator == are different.
- The assignment operator is used to assign a value to an identifier, whereas the equality operator is used to determine if two expressions have the same value.
- These operators cannot be used in the place of one another.
Conditional Operators :
- Simple conditional operations can be carried out with the conditional operator ( ? : ).
- An expression that makes use of the conditional operator is called a conditional expression.
- Such an expression can be written in place of the more traditional if-else statement.
- A conditional expression is written in the form:
expression 1 ? expression 2 : expression 3 |
- If expression 1 is true ( i.e., if its value is nonzero), then expression 2 is evaluated and this becomes the value of the conditional expression.
- However, if expression 1 is false (i.e., if its value is zero), then expression 3 is evaluated and this becomes the value of the conditional expression.
Bitwise Operators :
These operators perform bit-level operations on integer operands.
Operator | Meaning |
& | Bitwise AND |
| | Bitwise OR |
~ | Bitwise COMPLIMENT |
^ | Bitwise XOR |
<< | Bitwise SHIFT – LEFT |
>> | Bitwise SHIFT – RIGHT |
Special Operators :
Operator |
Purpose |
sizeof | Returns number of bytes allocated to given value or data-type. |
& | Returns the address of given variable. |
(type) | Explicit Type Casting |
. | Member Access ( Dot Operator ) |
-> | Member Access through pointer ( Arrow Operator ) |
C is a general-purpose, imperative computer programming language, supporting structured programming, lexical variable scope and recursion, while a static type system prevents many unintended operations. By design, C provides constructs that map efficiently to typical machine instructions, and therefore it has found lasting use in applications that had formerly been coded in assembly language, including operating systems, as well as various application software for computers ranging from supercomputers to embedded systems.
C was originally developed by Dennis Ritchie between 1969 and 1973 at Bell Labs, and used to re-implement the Unix operating system. It has since become one of the most widely used programming languages of all time, with C compilers from various vendors available for the majority of existing computer architectures and operating systems. C has been standardized by the American National Standards Institute (ANSI) since 1989 and subsequently by the International Organization for Standardization (ISO).
Course Features
- Lectures 19
- Quizzes 0
- Duration 50 hours
- Skill level All levels
- Language English
- Students 2
- Certificate No
- Assessments Self