C Programming Operators
This tutorial is all about C programming operators. Along with a detailed answer of operators in C, you will also come to know the answer for the following:
What is & and * operators in c?
What is operators and its types?
What are different types of operators in c?
Let's start by knowing the actual meaning of operators and what the operators are?
Operators are the symbols that are used to perform some operations. Suppose we are given a statement a+b*c. Here, symbols like +, * are operators and the alphabets like a, b, c are operands.
When operands and operators combine to perform any operations then this is called expression.
Before knowing more about c programming operators, let us first understand some basic fundamentals. You may know that any programs written in any language are made up of using some basic elements.
- Alphabets
- Digits
- Blank spaces
- Special characters
What are the alphabets used in writing a program?
"A" to "Z" and "a" to "z".
Here, you should remember that C is a case-sensitive language that means that ' a ' and ' A' both are different in C.
What are the digits used in writing a program?
"0" to "9".
What are the blank spaces used in writing a program?
'\n' , '\t' , '\r' , and so on.
What are the special characters used in writing a program?
Here is the list of the special symbols or characters that are used in writing a program.
- . period
- , comma
- : colon
- ; semi-colon
- _ underscore
- % percentile
- - hyphen or minus
- + plus
- * asterisk
- & ampersand
- ! exclamation mark
- ? question mark
- # number sign
- / forward slash
- \ backward slash
- ' single quote
- " double quotes
- ~ tilde
- ( ) parenthesis or small brackets
- { } curly braces or middle brackets
- [ ] big brackets
- < > angular brackets
- | pipe
What are the keywords used in C language?
C language has some reserved words that cannot be used in naming any variables, functions, etc. They are the special because C compiler does not need any introduction before being applied in a program. They are only 32 words in c language which are keywords in C language and they are:
- auto
- break
- case
- char
- const
- continue
- default
- do
- double
- else
- num
- extern
- float
- for
- goto
- if
- int
- long
- register
- return
- short
- signed
- sizeof
- static
- struct
- switch
- typedef
- union
- unsigned
- void
- volatile
- while
What are identifiers in C?
Identifiers are the words that are formed by the programmers to give names to the variables, functions, array, pointers, etc so that it becomes very easier to recognize them in the future by its unique name. However, there are some specific rules which are to be known in our mind before creating any identifiers.
Rules to create Identifiers in C
- No keywords can be used as an identifier because they are special and already known to the compiler.
- The maximum length of the identifiers should not exceed 32 characters and almost length of identifier is 32.
- Only the following character sets can be used for creating identifiers. 'A' to 'Z', 'a' to 'z', '0' to '9', and ' _' (underscore).
- The starting character of an identifier can either be an alphabet or underscore.
- No two identifiers can be similar or identical in a single block.
Note that C is a case sensitive language where upper and lower case characters are important and pretend to be different from each other. for example, we can take Sum, sum, sUm, suM,... etc as identifiers. All of them represent different identifiers.
What are operators in C programming?
Operators are the symbol used to perform some operations. In C, operators are defined in two ways.
- On the basis of operands they required.
- On the basis of the type of operation they perform.
C programming operators On the basis of operands required:
- Unary operators
- Binary operators
- Ternary operators
Unary operators in C
The operator of this type requires one operand to complete its operation. for example
- ++ increment
- -- decrement
- + unary plus
- - unary minus
- ! not
Here, let me tell you that what is mean by unary plus and minus. Consider a statement -4, +5, the sign that is used before a number as a prefix only, then it is called unary plus or minus.
Let say -5-7: Here, the minus sign before 5 is called unary minus but the minus sign in between 5 and 7 is called subtraction.
Binary operators in C
The operators of this kind requires two operands to complete the operation. for example
- + plus or addition
- - minus or subtraction
- * multiplication
- / division
- % modulus
- < lesser than
- > greater than
- = is equal to or equals to, and so on.
Ternary operators in C
The operators of this type need three operands to complete its operations. for example '' ? : " is a ternary operator and is followed in the conditional statements.
C programming operators on the base of the type of operation they show:
- Arithmetic operators or Mathematical operators
- Relational operators
- Logical operators
- Assignment operators
- Increment and Decrement operators
- Bitwise operators
- Size of operators [sizeof ()]
- Comma ( , ) operators
Arithmetic or Mathematical operators in C
- + Addition
- - Subtraction
- * Multiplication
- / Division
- % Modulus
These are the arithmetic or mathematical operators. They are also binary operators because they require exactly two operands to do an operation.
Example: Arithmetic operators in c
#include<stdio.h> #include<conio.h> void main() { printf("%d \n", 11+8); printf("%d \n", 11-8); printf("%d \n", 11*8); printf("%d \n", 11/8); printf("%d \n", 11%8); }
Output
19 3 88 1 3
Explanation:
1. When we add 11 and 8, we get 19.
2. When we subtract 8 from 11, we get 3.
3. When we multiply 8and 11, we get 88.
4. When we divide 11 and 8, we get 1 as quotient and 3 as remainder.
5. The symbol ' / ' gives us quotient. So, the answer is 1.
When we divide 11 and 8, we get 1 as quotient and 3 as remainder.
The symbol ' % ' gives us the remainder. So, the answer is 3.
Relational operators in C
- < lesser than
- > greater than
- < = lesser than equals to
- > = greater than equals to
- = = is equal to
- ! = is not equal to
These are called relational operators because they all show some relations between the operands. They are binary operators too because they require two operands for performing operations.
Example: Relational operators in c
#include<stdio.h> #include<conio.h> void main() { printf("%d \n", 10>7); printf("%d \n", 10==7); printf("%d \n", 10!=7); }Output
1 0 1
Explanation:
This type of question shows either 1 or 0 as the output on the screen.
- 10 is greater than 7. So, the answer is 1.
- 10 is not less than 7. So, the answer is 0.
- 10 is neither lesser nor equals to 7. So, the answer is 0. If anyone of the condition is true, the answer will be true (1).
- 10 is greater than 7 but is not equals to 7. Here, one condition is true So, the answer will be 1 (True).
- 10 is not equal to 7. So, the answer will be 0.
- 10 is not equal to 7. So, the answer will be 1.
Logical operators in C
- && AND Operator
- | | OR Operator
- ! NOT Operator
Logical operators are used to join two or more relations inside any program. An expression containing a logical operator returns 0 (0 means False) or 1(1 means True) depending upon the expression results. It is practiced for decision making in C language.
&& - Logical AND - It implies True only if all the operands are True.
|| - Logical OR - It implies True if either of the one operand is True.
! - Logical NOT - It implies True only if the operand denotes False.
&& - Logical AND - It implies True only if all the operands are True.
|| - Logical OR - It implies True if either of the one operand is True.
! - Logical NOT - It implies True only if the operand denotes False.
Assignment operators in C
Assignment operators are the type of operators that manipulates the value of the variables. There are two types of assignment operators.
- Constructive assignment operators
- Destructive assignment operators
Destructive assignment operators overwrite the old value of the variable with the new one whereas constructive assignment operators update the old value of the variable with the new one.
This can be addition, subtraction, multiplication depends upon the operators that are being used.
Destructive assignment operator is: " = "
Constructive assignment operators are: += , -= , *= , /= , %= , >>= , <<=
Example: Assignment operators in c
#include<stdio.h> void main() { int i; // It is an example of destructive assignment operator. i = 0; i = 2; i = 11; printf("It is an example of destructive assignment operator. \n"); printf("%d", i); }Output
It is an example of destructive assignment operator. 11
Example: Assignment operators in c
#include<stdio.h> void main() { int i = 2; // It is an example of constructive assignment operator. i += 1; i = 3; i *= 2; printf("An example of constructive assignment operator. \n"); printf("%d", i); }Output
An example of constructive assignment operator. 6
Increment / Decrement operator in C
++ is increment operator and -- is decrement operator. They can be used with variables only. They can be prefixed or postfixed with the variables.
Example: Increment/Decrement operators in c
#include<stdio.h> void main() { int i = 4; i++; i += 2; i+4; // i+4 does not do anything. ++i; i *= i; printf("%d", i); }Output
64
Explanation:
first, we take variable 'i' and initialize it with 4. At the next step, it got incremented by 1 and 'i' become 5.
Then, the addition of 5 and 2 (that is 7) is updated in 'i' now i becomes 7.
Here, i+4 does mean nothing because no sign of '=' present here. So, it does nothing with i.
Now in the next step, i is again incremented by 1 and becomes 8.
Finally, the multiplication of i*i that is (8*8=64) and 64 is stored into i and 64 is printed by the printf statement as an output on the screen.
Bitwise operator in C
- & Bitwise AND
- | Bitwise OR
- ^ XOR (Exclusive OR)
- ~ 1's Complement.
- >> Right shift
- << Left shift
Bitwise operators operate on the binary value of an operand.
Sizeof operator in C [sizeof ()]
The sizeof operator in c is used to determine the memory consumption of any variable and data types.
Example: Sizeof operators in c
#include<stdio.h> void main() { int a; printf("%d", sizeof(a)); // prints size of a. printf("%d", sizeof(int)); // prints size taken by int datatype. }Output
4 4
Comma operator in C
It is used as a separator between similar types of declaration. When the programmer wants to declare multiple variables of the same data types. Then, they make use of the comma operator as a separator between the declared variables.
Example: Comma operators in c
#include<stdio.h> int main() { int a, b, c, e, f; char m, z, j, g; float n, k, s; printf("Comma operator executed successfully"); return 0; }Output
Comma operator executed successfully
Conditional operator in C
The conditional operator is the only operator in C language which requires three operands to complete its operations.
It is a replacement of if-else statement i.e., the programs that are made in multiple lines using an if-else statement can be made in a simple one-line using conditional operator.
Conditional operator is formed by the combination of two symbols ( ? and :).
It works exactly like an if-else statement where it tests for some condition first and after its evaluation executes either expression 1 or expression 2 as shown below in the syntax.
Syntax of Conditional operator in C
(True Condition) ? expression1 : expression2;
Example: Conditional operators in c
#include<stdio.h> void main() { int a, b, c; printf("Enter two values \n"); scanf("%d%d", &a, &b); c = (a>b) ? a : b; printf("Greatest number = %d", c); }Output
Enter two values 105 10 Greatest number = 105
Example: Conditional operators in c
/* programs to find the greatest among three numbers using the conditional operator or conditional statement */ #include<stdio.h> void main( { int a, b, c, big; printf("Enter three values \n"); scanf("%d%d%d", &a, &b, &c); big = a > b ? (a > c ? a : c) : (b > c ? b : c) ; printf("Greatest number = %d", big); }Output
Enter three values 5 120 13 Greatest number = 120
Operator precedence associativity or rank associativity in C
When there are multiple operators present in an expression then their priority of execution is determined by its precedence also known as rank.
According to the rule, the operator with the least rank operates first in the expression but when there is more than one operator of the same rank is present in an expression.
Then their priority of execution is determined by their associativity that can be left to right or right to left.
Following are the lists of operators rank associativity:
Rank: 1
Operator: ( ), [ ], ++ (prefix), -- (prefix)
Direction: from left to right.
Rank: 2
Operator: sizeof( ), + (unary), - (unary), & (pointer), * (pointer)
Direction: from right to left.
Rank: 3
Operator: *, /, %
Direction: from left to right.
Rank: 4
Operator: + (add), - (subtract)
Direction: from left to right.
Rank: 5
Operator: >>, <<
Direction: from left to right.
Rank: 6
Operator: >, <, >=, <=
Direction: from left to right.
Rank: 7
Operator: = =, !=
Direction: from left to right.
Rank: 8
Operator: &
Direction: from left to right.
Rank: 9
Operator: ^
Direction: from left to right.
Rank: 10
Operator: |
Direction: from left to right.
Rank: 11
Operator: &&
Direction: from left to right.
Rank: 12
Operator: | |
Direction: from left to right.
Rank: 13
Operator: (Condition) ? :
Direction: from right to left.
Rank: 14
Operator: =, +=, -=, /=, %=
Direction: from right to left.
Rank: 15
Operator: ,