[go: up one dir, main page]

0% found this document useful (0 votes)
6 views11 pages

(L2) Operators

The document provides an overview of various operators in C programming, including conditional, logical, bitwise, and operator precedence. It explains the syntax and usage of these operators with examples, as well as details on identifiers and the 'typedef' keyword. Additionally, it covers control statements like 'continue' and 'goto', illustrating their functionality with code snippets.
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)
6 views11 pages

(L2) Operators

The document provides an overview of various operators in C programming, including conditional, logical, bitwise, and operator precedence. It explains the syntax and usage of these operators with examples, as well as details on identifiers and the 'typedef' keyword. Additionally, it covers control statements like 'continue' and 'goto', illustrating their functionality with code snippets.
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/ 11

CONDITIONAL OPERATOR

Conditional operator (?:) works upon three arguments. That’s why It is also
known as ternary operator.
• It returns one value if condition is true, returns another value if condition is
false.
SYNTAX: EXPRESSION 1 ? EXPRESSION 2 : EXPRESSION 3
Generally, Expression 1 is Condition, Expression 2 is returned if condition is true
and Expression 3 is returned if condition is false.
For Example,
void main() void main() void main()
{ { {
int a = 10, b; int a=10, b=30, int a = 10, b=40, c=80, large;
b = (a == 10 ? 20 : 30); large; large=(a>b)?(a>c?a:c):(b>c?b:c);
} large = (a>b ? a : b); }
LOGICAL OPERATORS
&& Logical AND void main()
{
|| Logical OR int m1,m2,m3,m4,m5,per ;
printf(“Enter marks of five subjects ”);
scanf(“%d%d%d%d%d”, &m1,&m2,&m3,&m4,&m5);
! Logical NOT per = (m1+m2+m3+m4+m5)/5;
if(per>=60)
printf(“FIRST DIVISION”);
if(per>=50 && per<60)
printf(“SECOND DIVISION”);
if(per>=40&&per<50)
printf(“THIRD DIVISION”);
If(per<40)
printf(“FAIL”);
}
identifier
An identifier is a string of alphanumeric characters that begins with an
alphabetic character or an underscore character.
• Identifiers are used to represent various programming elements such as
variables, functions, arrays, structures, unions and so on.
• Actually, an identifier is a user-defined word.
For Example,
char name[10];
double marks, total;
float average;
Here, name, marks, total and average are identifiers.
char, int, double are keywords.
typedef
typedef: “typedef” is a keyword used in C language to provide some
meaningful names to the already existing variable in the C program.
• It acts as an alias for the existing variable names.
SYNTAX: typedef <Existing_Name> <New_Name>
For Example, typedef int roll_no;
main() main()
{ {
typedef int roll_no; typedef long int lint;
lint a, b, c;
roll_no a, b, c; }
}
BITWISE OPERATORS
Bitwise operators works upon individual bits in C. a = 5 (00000101)
TYPES OF BITWISE OPERATORS: b = 9 (00001001)
& (Bitwise AND): It takes two numbers as operands and apply AND operation on
every bit of two numbers. The result of AND is 1 only if both bits are 1. c=a&b; = 00000001
| (Bitwise OR): It takes two numbers as operands and apply OR operation on
every bit of two numbers. The result of OR is 1 if any of the two bits is 1. c=a|b; = 00001101
^ (Bitwise XOR): It takes two numbers as operands and apply XOR operation on
every bit of two numbers. The result of XOR is 1 if the two bits are different. c=a^b; = 00001100
<< (Left Shift): It takes two operands, left shifts the bits of the first operand, the
second operand decides the number of places to shift. c=a<<1; = 00001010
>> (Right Shift): It takes two operands, right shifts the bits of the first operand,
the second operand decides the number of places to shift.c=a>>1; = 00000010
~ (Bitwise NOT): It takes one number and inverts all bits of it c=~b; = 11110110
OPERATOR PRECEDENCE AND ASSOCIATIVITY
Operator precedence: It defines the order in which various operators will be
evaluated in an expression.
For example, 5 * 4 + 8 = ? 28 or 60
To answer such questions, we need Operator Precedence Table of C.
(High precedence operators will be evaluated before the operators with low precedence.)
Associativity: It determines the direction in which an expression is evaluated.
• It also defines the order in which operators of the same precedence are evaluated
in an expression.
• Associativity can be either from left to right or right to left.
For example, a = b; Another example, a == b != c;
Here value of b is assigned to a, Not Here both “==” and “!=” has same
value of a is assigned to b. precedence and associativity is
This is because “=” has associativity from left to right. So a==b shall be
from right to left. evaluated first.
C OPERATOR PRECEDENCE TABLE & ASSOCIATIVITY OF OPERATORS
Operator Meaning of operator Associativity

() Functional call
[] Array element reference
Left to right
-> Indirect member selection
. Direct member selection
Higher Precedence

! Logical negation
~ Bitwise(1 's) complement
+ Unary plus
- Unary minus
++ Increment
Right to left
-- Decrement
& Dereference (Address)
* Pointer reference
sizeof Returns the size of an object
(type) Typecast (conversion)
* Multiply
/ Divide Left to right
% Remainder
C OPERATOR PRECEDENCE TABLE & ASSOCIATIVITY OF OPERATORS
+ Binary plus(Addition)
Left to right
- Binary minus(subtraction)

<< Left shift


Left to right
Higher Precedence
>> Right shift

< Less than


<= Less than or equal
Left to right
> Greater than
>= Greater than or equal

== Equal to
Left to right
!= Not equal to

& Bitwise AND Left to right

^ Bitwise exclusive OR Left to right

| Bitwise OR Left to right

&& Logical AND Left to right


C OPERATOR PRECEDENCE TABLE & ASSOCIATIVITY OF OPERATORS
|| Logical OR Left to right

?: Conditional Operator Right to left


Higher Precedence

= Simple assignment
*= Assign product
/= Assign quotient
%= Assign remainder
+= Assign sum
-= Assign difference Right to left
&= Assign bitwise AND
^= Assign bitwise XOR
|= Assign bitwise OR
<<= Assign left shift
>>= Assign right shift

, Separator of expressions Left to right


Section B: continue statement in C
“continue statement” is similar to break statement, but it will not jump out of
the loop instead it just skips the remaining statements in the body of the loop
for the current pass.
• Hence, continue is used to skip a particular cycle of loop.
#include <stdio.h>
void main() Output:
{ 1*1=1
int a[5] = {1, -4, 3, -7, 10}; 3*3=9
int i; 10 * 10 = 100
for (i = 0; i < 5; i++ )
{
if (a[i] < 0)
continue;
printf("%d * %d = %d\n", a[i], a[i], a[i] * a[i]);
}
return 0;
}
goto statement in C
“goto statement” is used to transfer the control of execution of the program to
another part of the program where a particular label is defined.
Syntax: goto label; void main()
{
• The label is the identifier which defines where the control should go on the execution
int i;
of 'goto' statement.
for(i = 0; i Output
• label statement block must be written like - label: statement or statement
< 10; i++)
block:
Another Example {
For Example
Use of goto as a loop if(i == 5)
Output:
void main() {
0
1 { goto out;
2 int i = 0; }
3 a: printf("%d\n", i);
4 if(i < 5) }
I will not count now { out:
printf("%d\n", i); printf("I will not count
i++; now\n");
goto a; }

You might also like