Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
8 views7 pages

C Operators

The document outlines the structure of a C programming language, detailing its components such as preprocessor commands, functions, variables, statements, and comments. It also explains various types of operators in C, including arithmetic, relational, logical, and bitwise operators, along with their precedence and associativity. Additionally, it provides examples of how to use these operators in code.

Uploaded by

rahab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views7 pages

C Operators

The document outlines the structure of a C programming language, detailing its components such as preprocessor commands, functions, variables, statements, and comments. It also explains various types of operators in C, including arithmetic, relational, logical, and bitwise operators, along with their precedence and associativity. Additionally, it provides examples of how to use these operators in code.

Uploaded by

rahab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

PROGRAM STRUCTURE

Structure is the collection of variables of different types under a single name for better handling
Format of a structured programming language

Structured programs basically consists of the following parts −

 Preprocessor Commands
 Functions
 Variables
 Statements & Expressions
 Comments

Let us look at a simple code that would print the words "Hello World" −

#include <stdio.h>

int main() {
/* my first program in C */
printf("Hello, World! \n");

return 0;
}

Let us take a look at the various parts of the above program −

 The first line of the program #include <stdio.h> is a preprocessor command, which tells
a C compiler to include stdio.h file before going to actual compilation.
 The next line int main() is the main function where the program execution begins.
 The next line /*...*/ will be ignored by the compiler and it has been put to add additional
comments in the program. So such lines are called comments in the program.
 The next line printf(...) is another function available in C which causes the message
"Hello, World!" to be displayed on the screen.
 The next line return 0; terminates the main() function and returns the value 0.
C PROGRAMMING OPERATORS

Operator is the symbol which operates on a value or a variable (operand). For example: + is an
operator to perform addition.

C programming language has a wide range of operators to perform various operations. For better
understanding of operators, these operators can be classified as:

OPERATORS IN C PROGRAMMING

1. Arithmetic Operators
2. Increment and Decrement Operators
3. Assignment Operators
4. Relational Operators
5. Logical Operators
6. Conditional Operators
7. Bitwise Operators
8. Special Operators

ARITHMETIC OPERATORS
Assume variable A holds 10 and variable B holds 20 then
Operator Description Example
+ Adds two operands A + B will give 30
- Subtracts second operand from the first A - B will give -10
* Multiplies both operands A * B will give 200
/ Divides numerator by de-numerator B / A will give 2
% Modulus Operator - remainder of after an B % A will give 0
integer division

Note: % operator can only be used with integers.

INCREMENT AND DECREMENT OPERATORS – Unary Operators

In C, ++ and -- are called increment and decrement operators respectively. Both of these
operators are unary operators, i.e, used on single operand. ++ adds 1 to operand and -- subtracts
1 to operand respectively. For example:

Let a=5
a++; //a becomes 6
a--; //a becomes 5
++a; //a becomes 6
--a; //a becomes 5

Difference between ++ and -- operator as postfix and prefix

When i++ is used as prefix(like: ++var), ++var will increment the value of var and then return it
but, if ++ is used as postfix(like: var++), operator will return the value of operand first and then
increment it. This can be demonstrated by an example:

#include <stdio.h>
int main(){
int c=2;
printf("%d\n",c++); /*this statement displays 2 then, only c incremented by 1 to 3.*/
printf("%d",++c); /*this statement increments 1 to c then, only c is displayed.*/
return 0;
}

Output
20
4

ASSIGNMENT OPERATORS – Binary Operators

The most common assignment operator is =. This operator assigns the value in the right side to
the left side. For example:

var=5 //5 is assigned to var


a=c; //value of c is assigned to a
5=c; // Error! 5 is a constant.
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
NB/ += means Add and Assign etc.

RELATIONAL OPERATORS - Binary Operators


Relational operators check relationship between two operands. If the relation is true, it returns
value 1 and if the relation is false, it returns value 0. For example:

a>b

Here, > is a relational operator. If a is greater than b, a>b returns 1 if not then, it returns 0.

Relational operators are used in decision making and loops in C programming.

Operator Meaning of Operator Example


== Equal to 5= =3 returns false (0)
> Greater than 5>3 returns true (1)
< Less than 5<3 returns false (0)
!= Not equal to 5!=3 returns true(1)
>= Greater than or equal to 5>=3 returns true (1)
<= Less than or equal to 5<=3 return false (0)

LOGICAL OPERATORS - Binary Operators

Logical operators are used to combine expressions containing relational operators. In C,


there are 3 logical operators:

Meaning of
Operator Example
Operator
If c=5 and d=2 then,((c= =5) && (d>5))
&& Logical AND
returns false.
If c=5 and d=2 then, ((c= =5) || (d>5))
|| Logical OR
returns true.
! Logical NOT If c=5 then, !(c= =5) returns false.

The following table shows the result of operator && evaluating the expression a&&b:

&& OPERATOR (and)


a b a && b
true true true
true false false
false true false
false false false
CONDITIONAL OPERATOR – Ternary Operators

Conditional operator takes three operands and consists of two symbols ? and : . Conditional
operators are used for decision making in C. For example:

d=(c>0)?10:-10;

If c is greater than 0, value of c will be 10 but, if c is less than 0, value of c will be -10.

BITWISE OPERATORS

Bitwise operators work on bits and performs bit-by-bit operation.

PRECEDENCE OF OPERATORS

If more than one operator is involved in an expression then, C language has a predefined rule of
priority of operators. This rule of priority of operators is called operator precedence.
Here, operators with the highest precedence appear at the top of the table, those with the lowest
appear at the bottom. Within an expression, higher precedence operators will be evaluated
first.

Category Operator Associativity


Postfix () [ ] ++ - - Left to right

Multiplicative */% Left to right


Additive +- Left to right
Relational < <= > >= Left to right
Equality = = != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %=>>= <<= &= ^= |= Right to left
Comma , Left to right

ASSOCIATIVITY OF OPERATORS
Associativity indicates in which order two operators of same precedence (priority) executes. Let
us suppose an expression:
a= =b!=c

Here, operators == and != have the same precedence. The associativity of both == and != is left
to right, i.e., the expression in left is executed first and execution take pale towards right. Thus,
a==b!=c equivalent to :
(a= =b)!=c
Operators may be left-associative (meaning the operations are grouped from the left), right-
associative (meaning the operations are grouped from the right)
Operator Name Associativity Operators

Multiplicative left to right * / %

Additive left to right + -

Bitwise Shift left to right << >>

Relational left to right < > <= >=

Equality left to right == !=

Bitwise AND left to right &

Bitwise Exclusive OR left to right ^

Bitwise Inclusive OR left to right |

Logical AND left to right &&

Logical OR left to right ||

Conditional right to left ?:

Assignment right to left = += -= *= /= <<= >>= %= &= ^= |=

Comma left to right ,

You might also like