0% found this document useful (0 votes)
37 views15 pages

Ch#9 Lecture#8

The document discusses various topics related to elements of C language including identifiers, variables, data types, operators, expressions, assignment statements, logical operators, operator precedence, comments and type casting. Code examples are provided to explain different concepts.

Uploaded by

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

Ch#9 Lecture#8

The document discusses various topics related to elements of C language including identifiers, variables, data types, operators, expressions, assignment statements, logical operators, operator precedence, comments and type casting. Code examples are provided to explain different concepts.

Uploaded by

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

COMPUTER

SCIENCE
SUBJECT TEACHER:
12 RASHAD MAHMOOD

CMA (Managerial) MBA-IT & Business


(MS Access and C) Cisco Certified Network Administrator
Microsoft Certified System Engineer
Linux Operating System Certified
CHAPTER 9: Elements of C Language
Comptia Computer Hardware Certified

Copyright @ Superior College Kharian Rashad Mahmood (IT-Specialist)


Topic
s
• Identifier and Types of Identifiers
• Keywords
• Variable
• Rules for Naming Variable
• Variable Declaration
• Variable Initialization
• Constant
• Data Types ( Integer, Float & Character)
• Overflow and Underflow
• Problems with floating point numbers
• Exponential Notation
Copyright
Copyright @
@IT
Superior
Series College Kharian Rashad Mahmood (IT-Specialist)
www.itseries.com.pk
Topics (continued)

• Range and Precision


• Operator and Expression
• Unary and Binary Operators
• Arithmetic Operator and Expression
• Data type of an expression
• Assignment Statement
• Lvalue and Rvalue
• Compound Assignment Statement
• Compound Assignment Operators
• Increment and Decrement Operators
• Relational Operators and Expression
Copyright
Copyright @
@IT
Superior
Series College Kharian Rashad Mahmood (IT-Specialist)
www.itseries.com.pk
Topics(continued
)
• Compound Condition
• Logical Operators ( AND, OR & NOT)
• Operator Precedence and Associativity of Operators
• Comments
• Type Casting ( Implicit and Explicit)

Copyright
Copyright @
@IT
Superior
Series College Kharian Rashad Mahmood (IT-Specialist)
www.itseries.com.pk
Q24 Explain Compound condition with the help of
example
A type of comparison in which more than one conditions are evaluated is called compound condition
 Example Program inputs two numbers to displays OK if one number is greater than 50 and second
number is less than 100.
 Compound condition is executed by using logical operators.

Relational Operator

(Number > 50 && Number < 100)

Logical Operator Relational Operator

Copyright @ Superior College Kharian Rashad Mahmood (IT-Specialist)


Q25 What are logical operators? Discuss different
logical operators in C language.
The logical operators are used to evaluate compound conditions.
Types of logical operators
&& and
|| or
! not
AND Operator (&&)
 Used to evaluate two conditions
 Produces true result if both conditions are true
produces false result if any one condition is false
Example
Two variables A = 100 and B = 50
 Compound condition (A>10) && (B>10) is true
 Compound condition (A>50) && (B>50) is false

Copyright @ Superior College Kharian Rashad Mahmood (IT-Specialist)


OR Operator (| |)
Used to evaluate two conditions
 Produces true result if either conditions is true
Produces false result if both conditions are false
Example
Two variables A = 100 and B = 50
 Compound condition (A>10) | | (B>10) is true
 Compound condition (A>50) | | (B>50) is true
 Compound condition (A>200) | | (B >100) is
false

Copyright @ Superior College Kharian Rashad Mahmood (IT-Specialist)


NOT Operator (!)
Reverse the result of a condition
 Produces true result if conditions is false
Produces false result if condition is true
Example
Two variables A = 100 and B = 50
Result of condition (A==B) is false but NOT operator converts it into true
 Condition !(A==B) is true.
 Condition !(A>B) is false.
Result of condition (A>B) is true but NOT operator converts it into false

Copyright @ Superior College Kharian Rashad Mahmood (IT-Specialist)


Q26 Explain operator precedence and
associativity
The order in which different types of operators in an expression are evaluated is known as operator
precedence.
 also known as hierarchy of operators
 Any expression given in parentheses is evaluated first
 In case of parentheses within parentheses,
the expression of the inner parentheses will be evaluated first

Copyright @ Superior College Kharian Rashad Mahmood (IT-Specialist)


Example
Operator precedence Operator associativity

* and / have equal


precedence. These
expressions will be
evaluated from left to right.
Similarly, + and - also have
equal precedence.

 First of all, the expression 5–2 will be evaluated. It gives a value 3


 Secondly, 24 will be divided by the result of last line i.e. 24 / 3 giving value 8
 Thirdly, 10 will be multiplied by 8 i.e. giving a result 80
 Finally, 80 will be added in 13 and the last result will be 93.

Copyright @ Superior College Kharian Rashad Mahmood (IT-Specialist)


Q27 Describe comments and their use in C
prohrams.
 Explanatory lines of a program that help user to understand the source code.
 Make programs easy to read and modify
 Are ignored by the compiler.
 Can be added anywhere in the program.
 Comments can be added in programs in two ways:
1. Sing-line Comments
2. Multi-line Comments

Copyright @ Superior College Kharian Rashad Mahmood (IT-Specialist)


 Begin with // and continue to the end of line.

// Program 2.5: Finding the area of rectangle


int length = 12; // length in inches
int width = 15; // width in
inches int area; // calculated
area
// Calculate rectangle area
area = length * width;

Copyright @ Superior College Kharian Rashad Mahmood (IT-Specialist)


 Begin with /* and end with */
 Can span multiple lines.
/* Program 2.7
Description: Finding the area of circle
Written by: Abdullah */
 Can also be used as single-line
comments.
int area; /* Calculated area */

Copyright @ Superior College Kharian Rashad Mahmood (IT-Specialist)


Q28 What is Type Casting?
 Way of changing a value of one type to a value of
another type.
 Two types of casting: It is always
recommended to
1. Implicit Type Casting convert the lower
value to higher for
 Performed automatically by the C++ compiler avoiding data loss.
 when value of one type is automatically changed to
another type
Example
 Operations are performed between operands of the int x=20;
same type.
char y=‘a’
 If the data types of operands are different, the value
with lower data type is converted into higher data // y
type. implicitly
converte
d to int.
ASCII
// value of 'a' is 97
x = x + y;
Copyright @ Superior College Kharian Rashad Mahmood (IT-Specialist)
printf(“x= %d” , x);
2. Explicit Type Casting
 Explicit casting is performed by programmer
 C allows the programmer to convert the type of an expression
 It is performed by using cast operator
Syntax
(type) expression;
 Placing the desired type in parentheses before the expression
 When casting from double to int,
the decimal portion is just truncated – not rounded.
Example
double x=3.6;
int sum;
// Explicit conversion from double to int
sum = (int) x + 1;
printf("sum = %d", sum);

Copyright @ Superior College Kharian Rashad Mahmood (IT-Specialist)

You might also like