C language has different character sets and symbols. There are many special characters with special meaning.
Operators are special symbols which cause to perform mathematical or logical operations. Following are the symbols used for arithmetic operators in C.
% operator is also referred as modulus operator. It generates remainder after the integer division.
The second data supplied for the / and % operator should be non zero. Operators separate identifiers in a statement/expression.
a + b = a a and b operands + operator
Operators act upon data items (variable, constants). The data items upon which operation is performed are called operands. Some operators required a single operand, while others may require two or even three operands.
Operators which take only one operand are called unary (nomadic) operators and operators which take two operands are called binary (dyadic) operators. Examples of operations are
- a + b + takes two operands ‘a” and ‘b’. So it is binary operator
- -b i takes one operand. So it is unary operator
Different types of operators available in C are
It is the basic and common operation in a computer programming language and used to perform mathematical operations. It is known as binary operator since they take two operands to be evaluated.
|%||Remainder||X%y (returns reminder)|
The assignment operator is used to assing a value to a variable. The form is as follows
variable = expression;
When executed, expression is evaluated, and the resulting value is assigned to variable, Some assignment operators are
|+=||x+=y, x=x+y, x+y is equal to x=x+y;|
In a C assignment statement, the right side can be any expression, and the left side must be variable name.
The unary mathematical operators are so named because they take a single operand. Unary operator acts upon single operand to be increased or decreased by one.
|Increment||++||Increment the operand by one||++x, x++|
|Decrement||—||Decrements the operand by one||–x, x–|
are the equivalent of these statements:
They differ in terms of when the increment or decrements is performed;
- When used in prefix mode, the increment and decrements operators modify their operand before it’s used.
- When used in post-fix mode, the increment and decrements operators modify their operand after it’s used.
an example of prefix mode
after these statements are executed, x has the value 11, and y has the value 10. The value of x was assigned to y, and then x was incremented.
In contrast, the following statements result in both y and x having the value 11. x is incremented, and then its value is assigned to y. an example of postfix mode;
Relational operators are used to compare expression , asking questions such as “is x greater than 50?” or “is y is equal to 0?” an expression containing a relational operator evaluates to either true (1) or false (0).
|Greater than or equal to||>=||x>=y|
|Less than or equal to||<=||x<=y|
Logical operators let you combine two or more relational expression into a single expression that evaluates to either true or false.
|AND||&&||Expression1 && Expression2|
|OR||||||Expression1 || Expression2|
The ternary operator is C’s only ternary operator, meaning that it takes three operands,
expression1? expression2 : expression3;
if expression1 evaluates to true (that is, nonzero), the entire expression evaluates to the value of expression2.
If the expression evaluates to false (tat is zero), the entire expression evaluates as the value of expression3. For example, the following statement assigns the value 1 to x if y is true and assigns 100 to x if y is false.
Likewise, to make z equal to the larger of x and y, you could write
perhaps you have noticed that the conditional operator functions somewhat like an if statement. The preceding statement could also be written like this.
The ternary operator can’t be used in all situations in place of an if…else construction, but the conditional operators is more concise.
The ternary operator can also be used in places you can’t use an if statement, such as inside a single printf() statement.
printf("The larger value is %d",((x>y)?x:y));
It permits two different expression to appear in situation where only one expression would ordinarily be used.
c=(a=10, b=20, a+b);
This is a unary operator, which finds out the number of bytes that any object occupies in computers’ memory.
It has the same precedence and associativity as the other unary operators. The syntax for sizeof operator is,
where object refers to variable or data type. for example, the size of data types can be displayed by the following statements.
printf("size of char = %u bytes", sizeof(char));
printf("size of int = %u bytes",sizeof(int));
printf("size of float = %u bytes",sizeof(float));
printf("size of double = %u bytes",sizeof(double));
Similarly, the type of a constant or a variable can be displayed as,
printf("size of char = %u bytes",sizeof(a));
printf("size of int = %u bytes",sizeof(5));
The conditional operator in C is also known as ternary operator ?:. It is called ternary operator because it takes three arguments.
The conditional operator has the following construct.
expr1 ? expr2 : expr3
The expression expr1 is normally a true-false expression. The first expression expr1 is evaluated first if it is nonzero (true) then expr2 is evaluated which is the value of the whole conditional expression.
If expr1 is zero (false) then expr3 is evaluated which is the value of the whole conditional expression.
Thus, a conditional expression can be used to do the work of the if-else statement. For example the following assignment statement.
small = x<y ? x : y;
small is assigned the value of x if x is smaller than y otherwise y is assigned to small. This expression is equivalent to the following if-else statement.
small = x;
small = y;
int a = 5, b = 8;
c = (a < b)? a : b;