Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 17

CSE 191 – COMPUTER PROGRAMMING

Najia Manjur
Lecturer
Department of Computer Science & Engineering (CSE)
Military Institute of Science & Technology (MIST)
Mirpur, Dhaka – 1216, Bangladesh
Scope of variable
#include<stdio.h>

void sum()
{
Same name of the variables.
int x; But different function makes
the scope of the variable
} different.
So, No Conflict!
void main()
Local Variable :
{ • Visible and meaningful only
inside the function in which
int x; they are declared.
• Not known to other functions.
}
2
Scope of variable
#include<stdio.h>
void main()
{
int a = 100;
if(a > 100)
{
int b = 1; // b is known inside this scope
}
//b is not known here
}
// a is not known here
3
Scope of variable
#include<stdio.h>
void main()
{
int x = 15;
if(x == 15)
{ Hides the outer
int x; variable x

x = 99;
printf(“%d”, x); //So, here x = 99
}
//Here x = 15
printf(“%d”, x);
}

4
Scope of variable
#include<stdio.h>
int b = 1; global variable
void main()
{
int a = 100;
if(a > 100)
{
b = 2; // b is known anywhere in the program.
}
//b is known here
}
// b is known here

5
Symbolic Constant
 Structure:
#define name value

name represents a symbolic name

value can be, I) Integer constant


II) Floating point constant
III) Character constant
IV) String constant

6
Symbolic Constant

 Variable declaration

int a = 10

 Symbolic Constant

#define a 10

7
Symbolic Constant
 Example:

#define age 50 //Integer constant

#define pi 3.1416 //floating point constant

#define hello ‘a’ //character constant

#define str “computer” //string constant

8
Regular ASCII Characters (0-127)
Regular ASCII Chart(codes 0-127)

9
Arithmetic Operators
Ari

Operator Name Example


+ Addition 7 + 8, x + y
- Subtraction/unary 17 - 11, x - 6
minus
* Multiplication 12 * 13, x * 3
/ Division 18 / 3, x / y
% Modulus 17 % 3, x % 2

10
Relational Operators

Operator Name Example


> Greater than x> y
< Less than 11 < 17, x < 6
>= Greater than or equal x >= y
<= Less than or equal x <= y
!= Not equal to x != y
== Equal to x == y

11
Logical Operators

Operator Name Example


! NOT !(x > y)
&& AND x < 6 && x > 2
|| OR x >= y || a<=b

12
Bitwise Operators

Operator Name Example


~ bitwise Complement ~var
& bitwise AND var1 & var2
| bitwise OR var1 | var2
^ bitwise XOR var1 ^ var2
<< Left shift var1 << 1
>> Right Shift var2 >> 2

13
Assignment Operators
Operator Name Example Meaning
= Assignment Var = 7; Var  7
++ Increment and var++; var = var + 1;
Assignment
-- Decrement and Var--; var = var – 1;
Assignment
+= Add and assign var += 7; var = var + 7;
–= Subtract and assign var –= var1; var = var – var1;
*= Multiply and assign var *= 7; var = var * 7;
/= Divide and assign var /= var1; var = var / var1;
%= Take modulus and assign var %= var1; var = var % var1;
|= bitwise OR and assign var |= var1; var = var | var1;
&= bitwise AND and assign var &= var1; var = var & var1;
^= bitwise XOR and assign var ^= var1; var = var ^ var1;
<<= Left shift and assign var <<= 2; var = var << 2;
>>= Right shift and assign var >>= var1; var = var >> var1;
14
Ternary Operator

?:
condition ? true result : false result;

1st Part 2nd Part 3rd Part

var1 > var2 ? printf(“var1 Big”) : printf(“var2


Big”);

15
Reference
 Text Books
 Schaum’s Outlines Programming with C (3rd Edition) – Byron Gottfried

Chapter – 2
Chapter – 3 (3.1, 3.2, 3.3, 3.4, 3.5)
All examples (Section 3.2, 3.3,
3.4, 3.5)
End of Slides

You might also like