Tutorials
C LanguageIn this tutorial you will learn about Operators, Arithmetic operators, Relational Operators, Logical Operators, Assignment Operators, Increments and Decrement Operators, Conditional Operators, Bitwise Operators and Special Operators.
Sponsored Links
An operator is a symbol which helps the user to command the computer to do a certain mathematical or logical manipulations. Operators are used in C language program to operate on data and variables. C has a rich set of operators which can be classified as
1. Arithmetic operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Increments and Decrement Operators
6. Conditional Operators
7. Bitwise Operators
8. Special Operators
All the basic arithmetic operations can be carried out in C. All the operators have almost the same meaning as in other languages. Both unary and binary operations are available in C language. Unary operations operate on a singe operand, therefore the number 5 when operated by unary – will have the value –5.
|
Operator |
Meaning |
|
+ |
Addition or Unary Plus |
|
– |
Subtraction or Unary Minus |
|
* |
Multiplication |
|
/ |
Division |
|
% |
Modulus Operator |
Examples of arithmetic operators are
x + y
x - y
-x + y
a * b + c
-a * b
etc.,
here a, b, c, x, y are known as operands. The modulus operator is a special operator in C language which evaluates the remainder of the operands after division.
Example
| . #include void main() //tell the compiler the start of the program { int numb1, num2, sum, sub, mul, div, mod; //declaration of variables scanf (“%d %d”, &num1, &num2); //inputs the operands sum = num1+num2; //addition of numbers and storing in sum. printf(“\n Thu sum is = %d”, sum); //display the output sub = num1-num2; //subtraction of numbers and storing in sub. printf(“\n Thu difference is = %d”, sub); //display the output mul = num1*num2; //multiplication of numbers and storing in mul. printf(“\n Thu product is = %d”, mul); //display the output div = num1/num2; //division of numbers and storing in div. printf(“\n Thu division is = %d”, div); //display the output mod = num1%num2; //modulus of numbers and storing in mod. printf(“\n Thu modulus is = %d”, mod); //display the output } . |
When an arithmetic operation is performed on two whole numbers or integers than such an operation is called as integer arithmetic. It always gives an integer as the result. Let x = 27 and y = 5 be 2 integer numbers. Then the integer operation leads to the following results.
x + y = 32
x – y = 22
x * y = 115
x % y = 2
x / y = 5
In integer division the fractional part is truncated.
When an arithmetic operation is preformed on two real numbers or fraction numbers such an operation is called floating point arithmetic. The floating point results can be truncated according to the properties requirement. The remainder operator is not applicable for floating point arithmetic operands.
Let x = 14.0 and y = 4.0 then
x + y = 18.0
x – y = 10.0
x * y = 56.0
x / y = 3.50
When one of the operand is real and other is an integer and if the arithmetic operation is carried out on these 2 operands then it is called as mixed mode arithmetic. If any one operand is of real type then the result will always be real thus 15/10.0 = 1.5
Often it is required to compare the relationship between operands and bring out a decision and program accordingly. This is when the relational operator come into picture. C supports the following relational operators.
|
Operator |
Meaning |
|
< |
is less than |
|
<= |
is less than or equal to |
|
> |
is greater than |
|
>= |
is greater than or equal to |
|
== |
is equal to |
|
!= |
is not equal to |
It is required to compare the marks of 2 students, salary of 2 persons, we can compare them using relational operators.
A simple relational expression contains only one relational operator and takes the following form.
exp1 relational operator exp2
Where exp1 and exp2 are expressions, which may be simple constants, variables or combination of them. Given below is a list of examples of relational expressions and evaluated values.
6.5 <= 25 TRUE
-65 > 0 FALSE
10 < 7 + 5 TRUE
Relational expressions are used in decision making statements of C language such as if, while and for statements to decide the course of action of a running program.
C has the following logical operators, they compare or evaluate logical and relational expressions.
|
Operator |
Meaning |
|
&& |
Logical AND |
|
|| |
Logical OR |
|
! |
Logical NOT |
This operator is used to evaluate 2 conditions or expressions with relational operators simultaneously. If both the expressions to the left and to the right of the logical operator is true then the whole compound expression is true.
Example
a > b && x = = 10
The expression to the left is a > b and that on the right is x == 10 the whole expression is true only if both expressions are true i.e., if a is greater than b and x is equal to 10.
The logical OR is used to combine 2 expressions or the condition evaluates to true if any one of the 2 expressions is true.
Example
a < m || a < n
The expression evaluates to true if any one of them is true or if both of them are true. It evaluates to true if a is less than either m or n and when a is less than both m and n.
The logical not operator takes single expression and evaluates to true if the expression is false and evaluates to false if the expression is true. In other words it just reverses the value of the expression.
For example
! (x >= y) the NOT expression evaluates to true only if the value of x is neither greater than or equal to y
The Assignment Operator evaluates an expression on the right of the expression and substitutes it to the value or variable on the left of the expression.
Example
x = a + b
Here the value of a + b is evaluated and substituted to the variable x.
In addition, C has a set of shorthand assignment operators of the form.
var oper = exp;
Here var is a variable, exp is an expression and oper is a C binary arithmetic operator. The operator oper = is known as shorthand assignment operator
Sponsored Links
Example
x + = 1 is same as x = x + 1
The commonly used shorthand assignment operators are as follows
Shorthand assignment operators
|
Statement with simple |
Statement with |
|
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 |
Example for using shorthand assignment operator
| . #define N 100 //creates a variable N with constant value 100 #define A 2 //creates a variable A with constant value 2 main() //start of the program { int a; //variable a declaration a = A; //assigns value 2 to a while (a < N) //while value of a is less than N { //evaluate or do the following printf(“%d \n”,a); //print the current value of a a *= a; //shorthand form of a = a * a } //end of the loop } //end of the program . |
Output
2
4
16
Next Page: C Programming - Operators - Page 2
| you made the understanding so easy.especially your examples |
|
ur work on c operators was astounding helped me a lot wid ma project thank you |
| This is work well done on operators It has helped me alot in my studies in computer science. Thanks |
| I would like to have clearly explanation and examples about BITWISE. Please, help me. |
| Hi,, i want to know one program on ~ operator in c programming.. can u pls help me |
| Hi your illustration is easier to understand and to make other know about the operators of c. thanks buddy. |
| if u could have given all operators in one table with precedence it would be more helpful. |
|
Hi, I am new to this site. The way of expalination is excellent.From two days onwards i am visiting this site and i am learning C language.Today i have gone through "C Programming - Operators", Could you please explain Bitwise operators with some examples? |
| What is the meaning of this sign <> |
| helpful in my studies |
|
hi this is me mukesh . i am new to c programming so help me giving some example. |
| what is the meaning of" -= " |
|
What is meaning of == operator it checks what address or string? If I have("john"=="john") it returns false where as true in Java. |
| What is the meaning of a=b=c; |
| There are several operators more like bitwise unary ternary binary comma dot conditional etc. |
|
@Pacifique M lemme give u an example : #include <stdio.h> #include <string.h> int main() { int a=12; a-=5; //a=a-5 // printf("%d n",a); return 0; } OUTPUT: 5 |
|
@Nikhil: == operator checks the operands on the left hand side and right hand side i.e the value only and not the address.but if u need to compare the string,u have to use it in this way given below.strcmp is a built-in function to compare the strings. #include <stdio.h> #include <string.h> int main() { char a[]="John"; char b[]="John"; int x; x=strcmp(a,b); if(x==0) printf("the strings are equal n"); else printf("the strings are not equal n"); return 0; } |
| What does the following operator do; "->"? |
| -> means indirect or pointer member selector |
| tell me the meaning of '^' operator in c |
|
can u tell me that which operator operates upon the integer values only |
| '^' this operator is called as Bitwise Exclusive. |
|
helped me greatly with studying, more so the examples showing differences with c = ++a – b; and d = b++ + a; Have been trying to work out why i got different results from my teacher. NOW i KNOW (Thanks) |
| When I join this site for the first time,my hesitation about programing is reduced,please provide me best examples for me to become a best programmer.thanks |
| What is the difference between Logical AND & Bitwise AND |
| how the work :: operator |
|
I will give an example of different statement #include<stdio.h> #include<conio.h> int main() { int i=3,j=1,res; res=++i+j+++--i+--j; printf("the result of the statement is %d",res); return 0; } see the output in different operating systems ur,chinnu(venki_ou) |
|
if we have both increment and decrement operators with only a single variable then how that statement is executed ex: ++i*i++*--i*i--. venki_ou |
|
char decision[3]; which coding is correct mentioned below (Ex: if(decision==yes) or if(decision=='yes') or if{decision=="yes") |
|
char decision[3]; if (strcmp ("yes",decision) == 0) |
| what is use of else in C? |
| "strcmp" is user for?... |
| Why are you using header files in C language? |
|
#define N 100 //creates a variable N with constant value 100 #define A 2 //creates a variable A with constant value 2 main() //start of the program { int a; //variable a declaration a = A; //assigns value 2 to a while (a < N) //while value of a is less than N { //evaluate or do the following printf(“%d n”,a); //print the current value of a a *= a; //shorthand form of a = a * a } //end of the loop } //end of the program . you wrote above example and it is very much clear while a is less than N then it will evaluate following code but if a is greater than N then program will wait here to be condition true or go some where else please explain. |
|
Please help me; what is the other statement that used to test values other than if and switch? |
| use conditional operator |
|
#include <stdio.h> #include <conio.h> void main () { int i=-3,j=-2,k=-1, x; x = ++ k && ++j && ++i; printf("%d%d%d%d",x,i,j,k); getch(); } The answer is 0 -3 -2 0. I want to know the working principle of the evaluation of the expression. |
|
I would like to have clearly explanation and examples about BITWISE. Please, help me. |
|
Can any one tell me the meaning for ~ in C program? I saw a program like main() { int bit; bit=~bit; |
|
Thanks It was very useful for me once again thanks so much. |
| Very Nice write the C programming operator. |
|
kindly solve this im understand it well please help me about this: CONSTRUCT ALGORITHM AND FLOW CHART OF THE FOLLOWING: A. algorithm that will display all prime numbers from 1 up to the number 10. B. algorithm that will compute the factorial c. algorithm that will compute the sum o all odd numbers from 1 up to the numbers please help me with this matter it will help me a lot thanks. GOD BLESS YOU |
|
@sheik: "strcmp" is used to comparing the strings. Example program: #include<stdio.h> main() { char a[8]="Nithya"; char b[8]="Nithya"; int c; c=strcmp(a,b); if(c==0) printf("The strings are equaln"); else printf("the strings are not equaln"); getch(); } OUTPUT will be: The strings are equal |
|
@Virshali the meaning of a=b=c is All the 3 variables rea having the same values. Which means , the value of c is assinging to the variable b , and the same value will also assinging to the variable a. for example: c=6. then a=b=c will be a=6, b=6, c=6. a=b=c will have 6=6=6 |
|
@Vijay R ~ which means one's complement operator. |
|
Thanks this is an easy way for me to understand C programming. I regularly visiting this site. Please make more examples by using those operations.. more power |
| Can you please write me a simple program using "+=" |
| I am very useful of this website. |
|
Can someone explain following operators with example.. "a = b"? "a &= b"? |
|
I'm teaching myself C and using a book that has used the operator '+=' I can't find a definition for it. What is it? An exampe is: total += array[x]; |
| what is the purpose of high level language? |
| i would like to know the result of using a <<,>> and >>>,<<< operations...and do they require spl header file? |
| I want to know the using of post and pre increment operators |
| Its very easy to understanding... |
| Could you please say me the significance of >>> |
| Provide me logic of calculating factorial of a number. |
|
All the operators in C is hidden in this Statement LARA IS Best crickter L=Logical A=Arithmatich R=Relational A=Assignment B=Bitwise Special I=Increment/Decrement |