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

Ch#9 Lecture#7

The document discusses elements of the C programming language including identifiers, keywords, variables, data types, operators, expressions, assignment statements, and relational operators. It provides examples and descriptions of increment and decrement operators and how they work as both prefix and postfix operators.

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 PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views15 pages

Ch#9 Lecture#7

The document discusses elements of the C programming language including identifiers, keywords, variables, data types, operators, expressions, assignment statements, and relational operators. It provides examples and descriptions of increment and decrement operators and how they work as both prefix and postfix operators.

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 PDF, TXT or read online on Scribd
You are on page 1/ 15

COMPUTER SCIENCE

12
SUBJECT TEACHER:
RASHAD MAHMOOD
CMA (Managerial) MBA-IT & Business
Cisco Certified Network Administrator
(MS Access and C) Microsoft Certified System Engineer
Linux Operating System Certified
Comptia Computer Hardware Certified

CHAPTER 9: Elements of C Language

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


Topics

• 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
Q19 What is meant by data type of an expression?
 The data type of each variable must be specified in its declaration, but how does C determine the data type of
an expression?
 What is the type of expression x+y when both x and y are of type int?
 The data type of an expression depends on the type(s) of its operands
 The result of expression is evaluated to larger data type in the expression
 If both are of type int, then the expression is of type int.
 If either one or both is of type double,
then the expression is of type double.

 An expression in which operands are of different data types is called mixed-type expression.
 An expressions that has operands of both int and double is a mixed-type expression

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


Q20 What is assignment statement?
A statement that assigns a value to a variable is known as assignment statement.
 When an assignment statement is executed, the expression is first evaluated; then the result is
assigned to the variable to the left side of assignment operator
Syntax
Assignment operator

variable = expression;

Examples
A = 100; A constant, variable or combination of
C = A + B; operands and arithmetical operators
X = C – D + 10;

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


 An lvalue is an operand that can be written on the left side of assignment operator =
 An rvalue is an operand that can be written on the right side of assignment operator =
 Lvalues must be variables
 rvalues can be any expression
Example
 distance = rate * time;
lvalue: "distance"
rvalue: "rate * time"
 All lvalues can be used as rvalues but all rvalues cannot be used as lvalues
 x = 5 is valid but 5 = x is not valid.
 C = A + B is valid but A+B=Cis invalid.

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


Q21 Describe Increment Operator
 Increment operator, ++ is a unary operator and works with single variable.
 Used to increase the value of variable by 1 and is denoted by the symbol ++
A++; is equivalent to A = A + 1;
 Increment operator cannot increment the value of constant and expressions
Example 10++ , (a+b)++ or ++ (a+b) are invalid
Prefix and Postfix Increments
 No difference if ‘alone’ in statement:
 A++; and ++A;  identical result
 The value of the expression (that uses the ++ operators) depends on the position of the operator
 Example - Result of two statements x=a++ and x=++a are different
Postfix increment in Expressions
 Uses or assign current value of variable, THEN increments it
If ++ is after the variable, as in
a++, the increment takes place
after the expression is
evaluated

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


Example:
Variable Initialization

Variable Declaration

Assign value to result

Display the value of result


 This code segment produces the output:
4 If ++ is after the variable, as in
n++, the increment takes place Newline characters move the
3 after the expression is cursor to the next line
evaluated

 Since postfix increment was used

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


Prefix increment in Expressions
 Increments variable first, THEN uses new value
If ++ is before the variable, as in
++a, the increment
takes place before the expression is
evaluated
Example:

 This code segment produces the output:


6
3
 Since pre-increment was used

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


Q22 Describe decrement Operator
 Decrement operator, is a unary operator and works with single variable
 Used to decrease the value of variable by 1 and is denoted by the symbol - -
A--; is equivalent to A = A – 1;
 Decrement operator cannot decrement the value of constant and expressions.
Example - -10 , (a+b) - - or - - (a+b) are invalid
Prefix and Postfix Decrements
 No difference if ‘alone’ in statement: A--; and --A;  identical result
 The value of the expression (that uses the -- operators) depends on the position of the operator.
Example Result of two statements x = a-- and x = --a are different.
Postfix decrement in Expressions
 Uses current value of variable, THEN decrements it

If - - is after the variable, as in a- -, the


increment takes place after the
expression is evaluated

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


Example:
Variable Initialization

Variable Declaration

Assign value to result

Display the value of result

Newline characters move the


 This code segment produces the output: cursor to the next line
4 If -- is after the variable, as in
1 n--, the decrement takes place
after the expression is
evaluated

 Since postfix decrement was used

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


Prefix decrement in Expressions
 Decrements variable first, THEN uses new value
If -- is before the variable, as in --a,
the decrement
takes place before the expression is
evaluated

Example:

 This code segment produces the


output: 2
1
 Since pre-decrement was used

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


Q23 Define relational operators. How many relational operators are
provided in C language? Also describe relational expression.
 Used to specify conditions in programs
 Condition is an expression that is either false (represented by 0) or true (represented by
1). e.g., “grade > 60” is a condition.
 Conditions contain relational operators, and have the following forms
variable relational-operator variable a>b
variable relational-operator constant marks > 75
 Also called comparison operators as they test conditions that are either true or false
Operators Used in Conditions

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


A type of expression that consists of constants, variables and relational operators is called relational
expression.
 Expressions are used to compare the values Number > 50
 The result of a relational expression can be true or false
Examples
Suppose the value of A = 10 and the value of B = 5.
Relation Expression Result
A >B True Relational
A <B False Variable operator Constant
A <= B False
A >= B True

A == B False
A != B True

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

You might also like