Your First C Progra5
Your First C Progra5
C Programming Operators
C Arithmetic Operators
* multiplication
/ division
#include <stdio.h>
int main()
int a = 9,b = 4, c;
c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
C OPERATOR
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
return 0;
Run Code
Output
a+b = 13
a-b = 5
a*b = 36
a/b = 2
The operators +, - and * computes addition, subtraction, and multiplication respectively as you might
have expected.
It is because both the variables a and b are integers. Hence, the output is also an integer. The
compiler neglects the term after the decimal point and shows answer 2 instead of 2.25.
The modulo operator % computes the remainder. When a=9 is divided by b=4, the remainder is 1.
The % operator can only be used with integers.
a/b = 2.5
a/d = 2.5
c/b = 2.5
c/d = 2
Increment ++ increases the value by 1 whereas decrement -- decreases the value by 1. These two
operators are unary operators, meaning they only operate on a single operand.
#include <stdio.h>
int main()
return 0;
Run Code
Output
++a = 11
--b = 99
++c = 11.500000
--d = 99.500000
Here, the operators ++ and -- are used as prefixes. These two operators can also be used as postfixes
like a++ and a--. Visit this page to learn more about how increment and decrement operators work
when used as postfix.
C Assignment Operators
An assignment operator is used for assigning a value to a variable. The most common assignment
operator is =
C OPERATOR
Operator Example Same as
= a=b a=b
+= a += b a = a+b
-= a -= b a = a-b
*= a *= b a = a*b
/= a /= b a = a/b
%= a %= b a = a%b
#include <stdio.h>
int main()
int a = 5, c;
c = a; // c is 5
c += a; // c is 10
c -= a; // c is 5
c *= a; // c is 25
c /= a; // c is 5
c %= a; // c = 0
C OPERATOR
printf("c = %d\n", c);
return 0;
Run Code
Output
c=5
c = 10
c=5
c = 25
c=5
c=0
C Relational Operators
A relational operator checks the relationship between two operands. If the relation is true, it returns
1; if the relation is false, it returns value 0.
== Equal to 5 == 3 is evaluated to 0
int main()
int a = 5, b = 5, c = 10;
return 0;
Run Code
Output
5 == 5 is 1
5 == 10 is 0
5 > 5 is 0
5 > 10 is 0
5 < 5 is 0
5 < 10 is 1
5 != 5 is 0
5 != 10 is 1
5 >= 5 is 1
C OPERATOR
5 >= 10 is 0
5 <= 5 is 1
5 <= 10 is 1
C Logical Operators
An expression containing logical operator returns either 0 or 1 depending upon whether expression
results true or false. Logical operators are commonly used in decision making in C programming.
Logical AND. True only if all operands are If c = 5 and d = 2 then, expression ((c==5) && (d
&&
true equals to 0.
Logical OR. True only if either one If c = 5 and d = 2 then, expression ((c==5) || (d>
||
operand is true equals to 1.
! Logical NOT. True only if the operand is 0 If c = 5 then, expression !(c==5) equals to 0.
#include <stdio.h>
int main()
return 0;
Run Code
Output
(a == b) && (c > b) is 1
(a == b) && (c < b) is 0
(a == b) || (c < b) is 1
(a != b) || (c < b) is 0
!(a != b) is 1
!(a == b) is 0
C Bitwise Operators
During computation, mathematical operations like: addition, subtraction, multiplication, division, etc
are converted to bit-level which makes processing faster and saves power.
C OPERATOR
Bitwise operators are used in C programming to perform bit-level operations.
| Bitwise OR
^ Bitwise exclusive OR
~ Bitwise complement
Other Operators
Comma Operator
Comma operators are used to link related expressions together. For example:
int a, c = 5, d;
The sizeof is a unary operator that returns the size of data (constants, variables, array, structure, etc).
#include <stdio.h>
int main()
int a;
float b;
double c;
char d;
C OPERATOR
printf("Size of int=%lu bytes\n",sizeof(a));
return 0;
Run Code
Output
Other operators such as ternary operator ?:, reference operator &, dereference operator * and
member selection operator -> will be discussed in later tutorials.