0% found this document useful (0 votes)
35 views22 pages

Constants, Operators

Uploaded by

mahdialhasan170
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)
35 views22 pages

Constants, Operators

Uploaded by

mahdialhasan170
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/ 22

Constant, Operator Expressions

CSE 1111: Structured Programming Language


Charles Aunkan Gomes
Lecturer
Dept of CSE, UIU

1
Variable vs Constant
 Variable: A variable is nothing but a name given to a storage area.
 A variable may take different values at different times during execution.
Variable declaration: data_type variable_name;
data_type variable_name=value;
 Constant: fixed value that do not change during the execution of a
program.
Constant declaration: #define constant_name value
Rules for Defining Constant
 Naming rule is the same as naming a variable.
 #define must be written before defining a constant

 At least one blank space between #define and symbolic name

and between the symbolic name and constant.

 #define statement must not end with a semicolon

 After defining the symbolic name should not be assigned any other value in the program

 #define statement may appear anywhere in the program before use.


Valid or Invalid
 #define A=21 invalid

 #define A 20; invalid

 #Define A 15 invalid

 #define A 11 valid
Example Code to Define a Constant

#include<stdio.h>
 Output?
#define PI 3.142
Take input of radius:
int main(){
float radius, area; 3
printf(“Take input of radius:");
scanf("%f",&radius); Area of circle=28.278000

area =PI*radius*radius;
Area of circle=28.278
printf(“Area of circle=%f\n",area);

printf(“Area of circle=%0.3f\n",area);
}
Operators
 Operator: Operator is a symbol that tells the computer to perform certain
mathematical or logical manipulations.
 C operators can be classified into a number of categories:
 Arithmetic operators
 Relational operators
 Logical operators
 Assignment operators
 Increment and decrement operators
 Bitwise operators
 Special Operators
Arithmetic Operators

Arithmetic C Operation Example Possible


Operator operands
+ Addition 5+6, a+b, a+4 Any number
- Subtraction 6.5-5.0, a-b Any number
* Multiplication 6*5.5, a*b Any number
/ Division 6/2, a/b Any number
% Modulus 14%3 Only Integer
number
Operator Precedence
 When multiplication and
division exist in the same
operation, as both are of the
same precedence, they are
performed left to right.
 Which means, the expression
6/3*2 is equivalent to the
expression (6/3)*2
Operator Precedence
 Any operations enclosed in parentheses are performed first.
 Any multiplication, division and modulus performed next from left to
right
 Then addition, subtraction performed from left to right
Relational Operators

Relational Operator C Operation Example


> Greater Than x>y
< Less Than x<y
>= Greater Than or Equal x >= y
<= Less Than or Equal x <= y
== Equal x == y
!= Not Equal x != y
Logical Operators
Logical C Structure Example
Operator Operation
&& Logical Condition1 && Condition2 x > 1 && y > 5
AND (both condition must be true)
|| Logical OR Condition1 || Condition2 x > 1 || y > 5
(atleast one condition must be true)
! NOT !(x>1)

 Relational and Logical operators return value 1 if the expression is true


 And return value 0 if the expression is false
Relational & Logical Operators

Expression True/False Return Value


9>7 true 1
9<7 false 0
9>7 && 9!=7 true 1
9>7 || 9!=7 true 1
9<7 && 9!=7 false 0
9<7 || 9!=7 true 1
!(9==7) true 1
Assignment Operator

Operator Name sign example Meaning in C


Assignment = A = 5+6; Assigns the value of the right
A =x+y; expression or Variable to the
A = 5; left operand
A = x;
Increment & Decrement Operators
 Let X=5;
Operator Type example Use in C Meaning in C
Name program
Increment Prefix ++X A = ++X 1. Increments the value of X by 1
2. Assigns the value to A
Postfix X++ A = X++ 1. Assigns the current value of X to A
2. Increment the value of X by 1
Decrement Prefix --X A = --X 1. Decrements the value of X by 1
2. Assigns the value to A
Postfix X-- A = X-- 1.Assigns the current value of X to A
2.Decrement the value of X by 1
Bitwise Operators
 It is used for Operator Name Example
manipulating data at
bit level ~ Not ~x
& AND x&y
 Used for testing bits
or shifting them right | OR x|y
or left. ^ XOR x^y
<< Left shift x << 1
>> Right shift x >> 1
Bitwise Operators Example Code
Special Operator
 Sizeof operator:
 It returns the number of bytes the operand occupies.
 i=sizeof(int);
Special Operator
Type Cast
 Converting one datatype into another is known as type casting or, type-
conversion. Two types of conversion:
 Implicit conversion : If the operands are of different type the lower type is
automatically converted to the higher type before the operation proceeds.
The result is of higher type. Example: A float type variable will be
converted to double type automatically
 Explicit conversion : Conversion from higher type to lower type requires
explicit conversion. Example: Double to float conversion/ Float to in
conversion
 You can convert the values from one type to another explicitly using
the cast operator : (type_name) expression
Implicit Type Conversion Example
Type Cast
Example Action
X = (int) 3.5 3.5 is converted to integer
X = (int)(a+b) The result of (a+b) is converted to integer
X = (int)a+b a is converted to integer and then added with b
X = (int)a/ (int)b a is converted to int, b is converted to int, then division
operation performed
X = (int)20.5 / 20.5 converted to floor int(20), 4.33 converted to floor
(int)4.33 int(4), then division operation performed

You might also like