Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Sem I 2010/11
Lecture Notes
Computer Programming
BTI 1022
Lecturer:
Winardi Sani, Dipl.-Ing.
Tutors:
Imran bin Razali
Muhamad Zaini bin Yunos
Sharifah Z.R. Bt Syed Ahmad
Faizul Amin Anuar
Table of Content
1 Introduction
1.1 Lesson Plan . . . . . . . . . . . . .
1.2 Computer system . . . . . . . . .
1.3 Computer programming . . . .
1.4 Software development method
1.5 Algorithm and Pseudo code . .
1.6 Flow chart . . . . . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
1-1
1-1
1-1
1-2
1-2
1-3
1-5
2 Structure of C Programs
2.1 Program Development . . . .
2.2 Compiler . . . . . . . . . . . . .
2.3 Background . . . . . . . . . . .
2.4 Structures of a C Program . . .
2.5 Identifiers . . . . . . . . . . . . .
2.6 Data types . . . . . . . . . . . .
2.6.1 Basic data types . . . .
2.6.1.1 Qualifiers . . . .
2.7 Variables . . . . . . . . . . . . .
2.7.1 Variable definition . . .
2.8 Constants . . . . . . . . . . . .
2.8.1 Integer constants . . . .
2.8.2 Floating point constants
2.8.3 Character constants . .
2.8.4 String constants . . . . .
2.8.5 Defined constants . . .
2.8.6 Memory constants . . .
2.8.7 A program example . .
2.9 Formatted Input/Output . . . .
2.9.1 printf() . . . . . . . . . .
2.9.2 Output examples . . . .
2.10 Formatted input . . . . . . . . .
2.10.1 Input examples . . . . .
2.11 Self exercises . . . . . . . . . . .
2.12 Keywords . . . . . . . . . . . . .
2.13 ASCII Code Table . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
2-1
2-1
2-1
2-2
2-3
2-3
2-4
2-4
2-4
2-5
2-5
2-6
2-6
2-6
2-7
2-7
2-8
2-8
2-8
2-9
2-9
2-11
2-11
2-12
2-12
2-13
2-14
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
Table of Content
3.2
3.3
3.4
3.5
3.6
3.7
3.8
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
3-3
3-3
3-3
3-3
3-4
3-6
3-6
3-6
3-7
3-7
3-8
3-8
3-9
3-10
3-11
3-11
3-13
4-1
4-1
4-1
4-2
4-4
4-4
4-6
4-7
4-8
4 Selections
4.1 Statements and Block . . . . . . . . . .
4.2 if statement . . . . . . . . . . . . . . .
4.3 if - else statement . . . . . . . . . .
4.4 switch statement . . . . . . . . . . . .
4.4.1 Evaluation of switch statement
4.5 else - if statement . . . . . . . . . .
4.6 Conditional Expressions . . . . . . . . .
4.7 Exercises . . . . . . . . . . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
5 Repetition
5.1 Concept of a loop . . . . .
5.2 Pretest and PostTest Loop
5.3 Loops in C . . . . . . . . . .
5.4 while Loop . . . . . . . . .
5.5 do..while Loop . . . . . . .
5.6 for Loop . . . . . . . . . . .
5.7 continue Statement . . . .
5.8 Exercises . . . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
5-1
. 5-1
. 5-1
. 5-3
. 5-3
. 5-5
. 5-6
. 5-9
. 5-10
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
6 Functions
6.1 Top-Down Design . . . . . . . . .
6.2 Functions in C . . . . . . . . . . .
6.2.1 Advantages of Functions
6.3 User-Defined Functions . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
6-1
6-1
6-1
6-2
6-2
Table of Content
6.4 Recursive . . . . . . . .
6.5 Parameter Passing . .
6.5.1 Pass by Value .
6.5.2 Pass by Address
6.6 Math Library . . . . . .
6.7 Exercises . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
. 6-6
. 6-7
. 6-7
. 6-8
. 6-9
. 6-10
CHAPTER 1
Introduction
Weight
Where to do
30%
On computer
30%
Final Examination
40%
Examination Hall
Ran cangan Pengajaran dan Pembelajaran
1-1
1 Introduction
1. Operating system: it provides services such as a user interface, files and
database access and communication systems.
2. System support software: System utilities such as disk format.
3. System development softwares: Interpreters/compilers that convert programs into machine language. Assembler, C, C++, Basic, Java, etc.
Application software: word processors, DB management systems, CAD/CAE.
1-2
1-3
1 Introduction
Example 2
Computing Weekly Wages: Gross pay depends on the pay rate and the number
of hours worked per week. However, if you work more than 40 hours, you get paid
time-and-a-half for all hours worked over 40. Pseudo-code the task of computing
gross pay given pay rate and hours worked.
1. start
2. get hours worked
3. if hours worked 40 then
a) gross pay = pay rate * hours worked
4. else
a) gross pay = pay rate * 40 + 1.5 * pay rate *(hours worked - 40)
5. display gross pay
6. halt/end
Example 3
Computing a Quiz Average: Pseudo-code a routine to calculate your quiz average.
1. start
2. get number of quizzes
3. sum = 0
4. count = 0
5. while count < number of quizzes
a) get quiz grade
b) sum = sum + quiz grade
c) count = count + 1
6. average = sum / number of quizzes
7. display average
8. halt/end
Pseudo code construction
Computation/Assignments
"variable" = "expression"
1-4
1-5
1 Introduction
Flowchart constructs
The flowchart equivalents for the structured programming constructs are shown
below:
Selection
Sequence
single (if)
Flowchart constructs,cont
1-6
double (if-else)
multiple (switch-case)
Repetition
while
do..while
for
x if x 0
f (x) =
x if x < 0
1-7
1 Introduction
Write a pseudo-code and the relevant algorithm to solve the function.
4. Follow the instruction given by the tutor for more exercises and assignments.
1-8
CHAPTER 2
Structure of C Programs
The compiler translates it into a machine code (.obj). If any syntax errors
exists, their must be fixed (compiling time).
Together with other object codes, if any, the linker generates an executable
code.
During program execution, error may exist (run time), and correction must
be done at this stage (debugging).
The cycle of modifying the source code, compiling, linking, and running
continues until the program is complete and free of errors.
2.2 Compiler
In this course, Turbo C is used as the compiler and it works under MS-DOS. It can
be downloaded in internet for free (hopefully). Turbo C supports an Integrated
Development Environment (IDE) and compiler for the C programming language
from Borland.
2-1
2 Structure of C Programs
2.3 Background
Year
Language
Remarks
1960
ALGOL(Algorithmic
Language)
1967
Compiler writing
Programming
guage
Lan-
1970
1972
1978
Standard traditional C
1989
ANSI/ISO C
1990
ISO C
Standard for C
Features of C:
very efficient and fast. This is due to its variety of data types and powerful
operators. It is many time faster than BASIC
a powerful and flexible language which helps system developers to deliver
various complex tasks with ease. C is used for diverse projects as operating
systems, word processors, graphics, spreadsheets, and even compilers for
other languages.
C is popular among professional programmers for programming. Matlab is
an example.
C is highly portable language.
Writing C program with user-defined functions makes program more simple
and easy to understand. Breaking a problem in terms of functions makes
program debugging, maintenance and testing easier.
its ability to extend itself. A C program is basically a collection of various
function supported by C library (also known as header files). We can also
add our own functions to the C library.
2-2
2.5 Identifiers
Purposes of an identifier in any programming language:
1. To name data and other objects. Every data in a computer is stored at
a unique address. The identifier is also a symbolic representation of data
location.
2. To allow the compiler keep track of the data using the identifier to locate
where the data is stored inside the computer memory. Rule of an identifier:
2-3
2 Structure of C Programs
a) First character must be alphabetic or underscore.
b) It must consist of alphabetic characters (A . . . Z, a . . . z, digits (0 . . . 9), or
underscore (_).
c) The first 31 characters are significant. C is case-sensitive.
d) It can not duplicate a reserved words (keywords).
Valid identifier
a
student_name
_force
Invalid identifier
Name
Comment
$sum
$ is illegal
2name
int
Reserved word
2-4
2.7 Variables
Type
Range of values
Declaration
Integer
int
2 (16)
-32,768 . . . 32,767
int a;
char
1 (8)
-128 . . . 127
char c;
float
4 (32)
231 . . . 231 1
float f;
double
8 (64)
263 . . . 263 1
double d;
void
Generic type
void main()
Character
Floating Point
Double precision floating Point
Void
Memory allocation/Size
Data range
short int
2 Bytes
-32,768 . . . 32,767
long int
4 Bytes
231 . . . 231 1
int
2 Bytes
-32,768 . . . 32,767
unsigned int
2 Bytes
0 . . . 65,535
unsigned char
1 Byte
0 . . . 255
Example
short int a
long
int b
int a;
unsigned int a;
unsigned char a;
2.7 Variables
A variable is a symbolic name for a certain memory location in a computer machine. It has a type and consequently a memory size and a set of operations to
manipulate the data.
2.7.1 Variable definition
Each variable must be first declared and defined before use. Every variable
has a data type for the memory allocation and a set of operations applied
on it.
Defining a variable means creating an object. Examples:
2-5
2 Structure of C Programs
We can initialize a variable at the definition time by an initial value using an
assignment (=) operator.
2.8 Constants
Constants are data values that cannot be changed during the program execution. Every constant has a data type.
2.8.1 Integer constants
Eventhough integers are stored in the binary form, they are simply coded in decimal form. Types of an integer constant are
signed integer, signed long
unsigned integer, unsigned long
Literal
Value
Type
Example
+123
123
int
-123
-123
int
-12345L
123,45
long int
1234LU
1,234
Omitting the suffix on a literal, it defaults to int. The Suffix L or l is for a long type,
and u or U for the unsigned type.
2.8.2 Floating point constants
Floating point constants are number with decimal parts and stored in memory as
two parts: the significand and the exponent. The default form for float constant
is double. The Suffix F or f is used for a float type, and L or l for long double.
2-6
Literal
Value
Type
Example
0.
0.0
double
double sum = 0.
.0
0.0
double
int sum = .0
-2.0f
-2.0
float
3.1415926L
3.1415926
long double
2.8 Constants
2.8.3 Character constants
A character constant is enclosed in two single quotes (apostrophes) such as y.
The value of a character constant is the numeric value of the character in the
machines character set. For example, in the ASCII (American Standard Codes
for Information Interchange) character set the character constant 0 has the
value 48, which is unrelated to the numeric value 0.
Special characters that begin with a backslash (\) are used for special purposed (escape sequences).
Symbolic name
ASCII char.
Symbolic name
ASCII char.
\0
null
\a
alert (bell)
\b
backspace
\t
horizontal tab
\n
newline
\v
vertical tab
\r
carriage return
\\
backslash
The program below will show the output of a character constant in different formats.
1
2
3
4
5
6
7
#include <stdio.h>
void main (){
char c;
c=d;
printf (\nChar %c in ASCII code
is a %d \n,c,c);
}
The
program
will
display:
Char d in ASCII code is a 100.
The same character constant will
be displayed in different output
formats (%c and %d). There are 255
characters defined in ASCII code.
2-7
2 Structure of C Programs
2.8.5 Defined constants
Another way to designate a constant is to use the preprocessor command define.
Example:
2-8
2.9.1 printf()
printf(format string, data list)
The formatted output printf() requires two parameters:
1. format string
it contains any text data to be printed and instructions for formatting. The
format string is enclosed in a set of double quotation marks (text and field
specification). The field specification for formatting purpose has the syntax:
%<flag><minimum width><precision><size>conversion-code
2-9
2 Structure of C Programs
Size
Code
Type
Example
char
%c
short int
%hd
int
%d
long int
%ld
float
%f
double
%f
long double
%Lf
%e
le
%le
%E
width:
It is used to specify the minimum number of positions in the
output. It is useful to align output in column. For a floating point number, the number of decimal places can be specified using a precision
modifier with the format:
.m
where m is number of decimal digits. If width and precision are used, the
width must be larger enough to contain the integral value, the decimal
point, and the number of digits. Examples:
%2hd
%4d
%8ld
%7.2f
%10.3Lf
/*
/*
/*
/*
/*
flag: If the flag is zero and there is a width specification, then a number will be printed with leading zeros. If the flag is a minus sign (-). then
the data is formatted left justified.
%-8d
%08d
2. data list
The second parameter is the data list to be printed on the monitor with the
format set by the first parameter in the field specification. The data list may
be a constant, variable name, or a combination of them.
2-10
z
A
4.1
53.6
5. printf(\%8c
%d\,h,23)
Display:
23
2-11
2 Structure of C Programs
scanf(format string, address list)
To indicate an address you must prefix the variable name with an address operator, the ampersand (&).
The following rules are applied for using scanf():
1. The conversion operation continues until:
End of file is reached.
The maximum number of characters have been processed.
A whitespace character is found after a digit in a numeric application.
An error is detected.
2. There must be a field specification for each variable to be read.
3. There must be a variable address of the proper type for each field specification.
4. Any character in the format string other than whitespace or a field specification must be exactly matched by the user during the input, otherwise
scanf stops.
2.10.1 Input examples
The following examples show the use of scanf.
1. Input: 214 156 14 z
scanf(%d %d %d %c, &a, &b, &c, &d);
2. Input: 214 156 2.14
scanf(%d %d %d %f, &a, &b, &c);
3. Input: 14/26 15/77
scanf(%2d/2%d %2d/%2d, &a, &b, &c,&d);
4. Input: 20-7-2010
scanf(%d-%d-%d, &a, &b, &c);
2-12
2.12 Keywords
c) a floating point variable, product, initialized to 1.
2. Write a C program that calculates the perimeter, the surface area, and
the volume of a cylinder. The input data: the radius and the height of the
cylinder. Use proper data types for each variable, and write on the screen
the value of radius, height, surface area, perimeter, and the volume. The
data output shall be in convenient format.
3. What is the difference between the data type of short, int, and long int,
with respect to:
a) Memory allocation
b) Type format, and
c) The lower and upper data limit?
4. Write a program to verify your answer in question 3 through an addition of
two different integer numbers.
5. To write a constant, C provides two alternatives. You can use a keyword
const or define the constant in the precompiler part using #define. Explain
the difference between them.
2.12 Keywords
The C language contains several keywords or reserved words that cannot be
used for functions, variables, and named constants. They are shown in table 2.2.
Table 2.2: Keywords of C language
auto
extern
sizeof
static
struct
char
switch
continue
if
union
do
long
void
else
while
signed
break
case
float
const
goto
default
int
unsigned
double
register
return
volatile
enum
short
define
main
for
typedef
2-13
2 Structure of C Programs
2-14
CHAPTER 3
Operators and Expressions
Operators are symbols which take one or more operands or expressions and perform arithmetic or logical computations of data. An expression is a sequence of
operands and operators that reduces to a single value: Example of an expression:
2 + 7
Plus sign (+) is the operator for addition and 2 and 7 are the operands that
receive an action of the operator for an addition. This expression reduces to a
single value (9). C language has a set of operators including:
1 Arithmetic Operators
2 Relational and Logical Operators
3 Assignment Operators
4 Increments and Decrement Operators
5 Conditional Operators
6 Bit Operators
There is no limit to the number of operator and operand sets that may be combined to form an expression. The only rule is that, when they have been evaluated the result is a single value that represents the expression.
3-1
Operator
Use for
Example
(x = 27, y = 5)
Result/Remark
Addition
x+y
32
Subtraction
x-y
22
Multiplication
x*y
135
Division
x/y
Modulo
(Remainder of an integers
division)
x%y
2 (2 = 27 5 5).
Program example:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include<stdio.h>
#include<conio.h>
/* tell the compiler the start of the program */
void main(){
/* declaration of variables */
int numb1, num2, sum, sub, mul, div, mod;
clrsc();/* make screen clear */
scanf (%d %d
T, &num1, &num2); /*inputs the operands */
sum = num1+num2; /*addition of numbers and storing in sum.*/
printf(\n The sum is = %d,sum);/*display the output */
sub = num1-num2; /*subtraction of numbers and storing in sub.*/
3-2
3-3
<
<=
>
Meaning
is less than
is less than or equal to
is greater than
than
or
Example
(x = 1, y = 2)
Result/ Remark
1 < 2
TRUE
1 <= 2
TRUE
1 > 2
FALSE
1 >= 2
FALSE
>=
is greater
equal to
==
is equal to
1 == 2
FALSE
!=
is not equal to
1 != 2
TRUE
#include<stdio.h>
#include<conio.h>
/* tell the compiler the start of the program
int main(){
//declaration of variables
int x,y,z;
x = 2; y = 20;
3-4
*/
AND
x AND y
false
false
false
false
true
false
true
false
true
true
Operator
x OR y
false
false
false
false
true
true
false
true
false
true
true
true
true
true
OR
Operator
NOT
NOT x
false
true
true
false
Expr1
Expr2
a < b
10
20
x == y
a < b && x == y
1 && 0
a < b || x == y
Value
1 || 0
8
9
10
11
12
13
14
15
16
17
18
19
3-5
printf(
S\n %d < %d == %d
T, x,y,z); /*display x == y = z*/
getch();/* hold on screen before enter */
return 0;
/* end of program */
The output of the program example is:
2 + 20 = 22
2 < 20 = 1
2 == 20 = 0
z = x * y;
i = i + 1;
The left-hand side operand in an assignment expression must be a single variable. So, it is not allowed to write x * y = z, why?.
3.3.2 Compound Assignment
Compound assignment is a shorthand notation for a simple assignment. It requires that the left operand be repeated as part of the right expression. There
are five compound assignment operators defined in C.
Table 3.5: Compound Assignments
3-6
Compound Assignment
x *= y
x = x * y
x /= y
x = x / y
x += y
x = x + y
x -= y
x = x - y
x %= y
x = x % y
Meaning
Prefix Form
Meaning
a++
++a
a--
--a
If a compound assignment is used with a binary expression, the binary expression is evaluated first. Example:
x *= y - 2
is evaluated as:
x *= x * (y - 2)
3-7
x *= j * j++;
If j equals 3, what will be the value of x?. It is advisable not to use such an
expression. Examples:
i
j
i
j
=
=
=
=
k--;
l++;
--k;
++l;
/*
/*
/*
/*
Stores the
Stores the
Decrements
Increments
value of k in
value of l in
k then stores
l then stores
i then decrements k. */
j then increments l. */
the new value of k in i. */
the new value of l in j. */
The following program demonstrates the uses both prefix and postfix increment
and decrement operators:
1 #include <stdio.h>
2 int main(){
3
int j = 5, k = 5, l = 5, m = 5;
4
printf("j: %d\t k: %d\n", j++, k--);
5
printf("j: %d\t k: %d\n", j, k);
6
printf("l: %d\t m: %d\n", ++l, --m);
7
printf("l: %d\t m: %d\n", l, m);
8
return 0;
9 }
The output of the program is as follows:
j:
j:
l:
l:
5
6
6
6
k:
k:
m:
m:
5
4
4
4
The output shows that the initial values of j and k are used in the first printf().
They also show that l and m are incremented and decremented, respectively,
before the third printf() call.
3-8
Bit of Operand2
AND Relation
Result
0 AND 0
0 AND 1
1 AND 0
1 AND 1
53
212 /* 53 shifted left 2 bits */
13 /* 53 shifted right 2 bits */
3-9
Operand
Bit Representation
53
00 11 01 01
50
00 11 00 10
53 & 50
00 11 00 00
Bit Representation
53
00 11 01 01
50
00 11 00 10
53 | 50
00 11 01 11
Bit Representation
53
00 11 01 01
50
00 11 00 10
53 ^ 50
00 00 01 11
Bit Representation
53
00 11 01 01
~53
11 00 10 10
3-10
-502.2, f3 = 7.35e9;
3
3
3
-502 in decimal fffffe0a in hex
4294966794 in decimal
10 in decimal 0a in hex
run-time error
run-time error
run-time error
3-11
Operators
Grouping
primary
() [] ->
L to R
unary
(type casting)
sizeof
& (address of)
* (dereference)
- (reverse sign)
~
!
++
--
R to L
multiplicative
* / %
L to R
additive
L to R
shift
<<
relational
equality
==
L to R
>>
left to right
L to R
=!
bitwise AND
&
L to R
logical AND
&&
L to R
logical OR
||
L to R
conditional
?:
R to L
assignment
=
/=
<<=
comma
3-12
+=
%=
&=
-=
>>=
^=
*=
R to L
|=
R to L
3.8 Exercises
3.8 Exercises
1 Relational operators
Given the following declaration:
int j = 0, m = 1, n = 1;
float x = 2.5, y = 0.0;
Expression
Equivalent Expressions
j > m
m / n < x
Result
j > m
(m / n) < x
j <= m >= n
j <= x == m
-x + j == y > n > m
x += (y >= n)
++j == m != y * 2
Replace the ? sign in each row with the equivalent expression and evaluate
it to to get the right result.
2 Write a program that coverts and prints a user-supplied measurement in
inches into:
(a) foot (12 inches)
(b) yard (36 inches)
(c) centimeter (25.4/inch)
(d) meter (39.37 inches)
3 Given the pseudocode below, write a C program that executes it. Use
floating-point data types for all values.
1
2
3
4
5
6
7
read x
read y
compute
compute
compute
display
display
p = x
s = x
total
x and
total
* y
+ y
= s^2 + p * (s - x) * (p + y)
y
3-13
Equivalent Expressions
Result
(m + n) || (!j)
x * 5 && 5 || m / n
!x || !n || m+n
x * y < j + m || n
(x > y) + !j || n++
(j || m) + (x || ++n)
Replace the ? sign in each row with the equivalent expression and evaluate
it to to get the right result.
6 Write a C program code for the question 5 to verify your answer.
3-14