0% found this document useful (0 votes)
56 views20 pages

3 Operators and Expressions

The document discusses operators and expressions in C programming. It covers different types of operators like arithmetic, relational, logical, assignment, conditional, and bitwise operators. It also discusses operator precedence, associativity, side effects, and short circuit evaluation. The document provides examples of C programs demonstrating the use of various operators and evaluates the output. It also covers basic data types, format specifiers, type conversions, escape sequences, symbolic constants, and comments in C programming.

Uploaded by

Daniel
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)
56 views20 pages

3 Operators and Expressions

The document discusses operators and expressions in C programming. It covers different types of operators like arithmetic, relational, logical, assignment, conditional, and bitwise operators. It also discusses operator precedence, associativity, side effects, and short circuit evaluation. The document provides examples of C programs demonstrating the use of various operators and evaluates the output. It also covers basic data types, format specifiers, type conversions, escape sequences, symbolic constants, and comments in C programming.

Uploaded by

Daniel
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/ 20

PROGRAMMING IN C

Operators and Expressions

Dr. S. Usha Kiruthika


Department of CSE
NITT.
Operators
• C supports rich set of built in Operators
• Used to manipulate Constants (Data) & Variables
• Part of Mathematical (or) Logical expressions
• Operators vs Operands
• Operator – Definition
• Symbol (or) Special character that instructs the compiler to perform
mathematical (or) Logical operations
Types of Operators
• Arithmetic operators (+, -, *, /, %, ++, --)
• Relational operators (>, <, >=, <=, ==, !=)
• Logical operators (&&, ||)
• Assignment operators (=, +=, *=, /=)
• Conditional operator (? .. :)
• Bitwise operators (&, |, ^, <<, >>, ~)
• Special operators (->, .)
Precedence and
Associativity of Operators
• Precedence of operators determine
the order in which operations are to
be done when evaluating an
expression

• Associativity determines the order of


operations to be done if multiple
operators in an expression have same
precedence
Evaluate These Expressions
• a=-5+10*2-15+8
• a=7==8
• a=5<=8&&6!=5
• a+=3 b*=5 c%=2 (a=2,b=4,c=5)
• a*=b/=c-=d+=6*2 (a=2,b=4,c=16,d=2)
• a=b++*3 (b=5) a=++b*3 (b=5)
• a%=-b++/5 (a=3,b=11)
Find the output of the Program
#include <stdio.h>
int main()
{
int a;
a= 1 > 2 + 3 && 4;
printf("%d",a);
}
Find the output of the Program

int main(void) {
int b=10, d=20, c=1;
int a = b + (c = d / b) - 1;
printf("a: %d",a);
return 0;
}
Find the output of the Program
#include <stdio.h>
int main()
{
int a = 10, b = 20, c =30;

if (c > b > a)
printf("TRUE");
else
printf("FALSE");
return 0;
}
Evaluate the expressions
Side-effect
• a=b++ + ++b (b=3)
• a=b*=c++ + ++d+3 (a=3,b=3,c=2,d=2)
• a=b&&++c (b=0, c=5) a=b||++c (b=1,c=5)

Short circuit evaluation


Bitwise Operators

& bitwise AND


n = n & 0177 | bitwise inclusiveOR
x = x >> 2 ^ bitwise exclusiveOR
y = y << 2 << left shift
x = x & ~077 >> right shift
~ one's complement (unary)
Recap of Data Types
• Basic Data Types
• int
• char
• float
• double
• Qualifiers
• int – short, long, signed, unsigned
• char – signed, unsigned
• double - long
Data Types

Reading: Pg.36 in K&R book


Format Specifiers
Format Format
Type of Output Type of Output
Specifiers Specifiers
%d or %i A decimal integer or signed integer %Lf Long double
%c Signed character %o Octal integer
%f Signed float %u Short unsigned integer
%e A floating-point number %ld Long decimal integer
%s A string or sequence of character %x Hexadecimal integer
%lf double %p Print memory address in the hex form
Find the Output of the Programs

int main()
int main()
{
{
double x = 1.2;
int x = 10;
char y = 'a'; int sum = (int)x + 1;
x = x + y;
float z = x + 1.0f; printf("sum = %d", sum);
printf("x = %d, z = %f", x, z);
return 0;
return 0;
}
}
Type Conversions
• Narrowing Conversion
• Widening Conversion

• Implicit Conversion
• Coercion
• Explicit Conversion
(int) sum
Escape Sequences
Examples
• Write a program to print
“Hello World”
• Write a program to print
\Hello World\
• Write a program to print
The format specifiers: float:%f
integer:%d
Symbolic Constants
• Occurrence of a number or constant can be replaced by a meaningful
name

Syntax:
#define name replacement text
Eg.
#define PI 3.14159
Comments
• Single line comments
eg. //This is a comment
• Multi-line comments
eg. /* This is a
Multi-line comment */

You might also like