Professional Documents
Culture Documents
CSEB113 PRINCIPLES of
PROGRAMMING CSEB134
PROGRAMMING I
by
Badariah Solemon
BS (May 2012)
Topics
1.
2.
3.
4.
5.
6.
C Tokens
Basic Data Types
Variables
Constants
Reading Data from the Keyboard
Arithmetic Operators and
Expressions
7. Single Character Data
BS (May 2012)
Topic 1
C TOKENS
BS (May 2012)
reserv
ed
words
string
literals
return 0;
punctuatio
n
BS (May 2012)
1. Reserved Words
Keywords that identify language
entities such as statements, data
types, language attributes, etc.
Have special meaning to the
compiler, cannot be used as
identifiers in our program.
Should be typed in lowercase.
MUST NOT be used as identifiers!!
Ex: auto, double, else, void, i
BS (May 2012)
Flow of Controlrelated
Miscellaneous
int
if
enum
BS (May 2012)
2. Identifiers
Words used to represent and reference certain
program entities (variables, constant,
function names, etc).
Also known as programmer-defined words.
Example:
int myName;
variable.
#define TAXRATE 26
myName is program
TAXRATE is a
constant.
void CalculateTotal(int value);
CalculateTotal is a function
name.
BS (May 2012)
Identifiers: Rules
Rules
Example
H2o
Number1; _area
XsquAre
my_num
R*S+T
#@x%!!
struct; printf;
My height
Tax
TaX
TAX
BS (May 2012)
are different
8
3. String Literals
A sequence of any number of characters
surrounded by double quotation marks
.
Example:
This is a string constant.
Hello \John\.
printf(My
name is
Beckham.\n);
Example
of usage
inDavid
C program:
Produces
output:
My name
is David
Beckham.
BS (May 2012)
10
4. Operators
Tokens that result in some kind of
computation or action when applied
to variables or other elements in an
expression.
Example of operators:
* + = - / < >
Usage example:
result = total1 + total2;
BS (May 2012)
11
5. Punctuators
Symbols used to separate different parts of
the C program.
These punctuators include:
[ ] ( ) { } , ; : * #
Usage example:
main()
{
printf(Testing.);
}
To be discussed when we come to the proper language feature
in the coming chapters.
BS (May 2012)
12
Exercise
1. Valid/invalid variables or constant
Variables/
Constant
Valid/
Invalid
?
Variable/
Constant
CO2
const
_prs
Formula #1
2ndDay
Last.Name
First_name
account#
NetPRIce
engine 1
my name
main
index
Main
m*cee
First+Last
mx_2ndcovera
ge
typedef
BS (May 2012)
Valid/
Invalid?
13
Topic 2
BASIC DATA TYPES
BS (May 2012)
14
float
float income =
1234567890.12;
char*
double
To declare floating point variable
of higher precision or higher
range of numbers - exponential
numbers, positive and negative
Ex:
double bigvalue = 12E-3;
//equals to 12x10-3
Single character:
1.Numeric digits: 0 - 9
2.Lowercase/uppercase letters: a
- z and A - Z
3.White space
4.Special characters: , . ; ? / ( ) [
]{}*&%^<>
Ex:
char initial = B;
BS (Feb 2012)
15
Topic 3
VARIABLES
BS (May 2012)
16
17
Declaring Variables
A variable is declared by specifying
the DATA TYPE of the variable name
Ex:
Data Type
Variable Name
int Age;
18
Weight = 60;
Data
Type
Data
Value
Memory Cell
Address*
Age
int
17
ffff8
Weight
int
60
ffff2
BS (Feb 2012)
19
BS (Feb 2012)
20
Example:
int age = 80;
float cgpa = 4.00;
printf(Your age: %d\n, age);
printf(CGPA: %f\n, cgpa);
printf(My CGPA: %.2f\n, cgpa);
BS (Feb 2012)
Your age: 80
CGPA: 4.000000
My CGPA: 4.00
21
Placeholders in printf()
Function
Data Type
%d
int
%f
double/float
%c
char
%s
String
BS (Feb 2012)
Refer Table
12.2, pg 647
(Hanly &
Koffman)
22
printf(FormatString, PrintList);
Example:
#include <stdio.h>
main()
{
int age = 80;
float cgpa = 3.512, weight = 60.4778;
23
Exercise
Do hand-out exercise declare & assign variables
What happen if you try to print an int with
%f or a float with %d?
What is the output for the following program?
#include <stdio.h>
main()
{
int yearS = 4;
float cgpa = 3.88, gpa = 4.00;
double
printf(How long it takes to complete ur study?\n%d\n,yearS);
printf(GPA\t\tCGPA\n);
printf(%.2f\t\t%.2f,gpa, cgpa);
printf(Excellent!\n);
printf(Now, test your skill\n);
printf(CGPA in 1 decimal point = %.1f\n, cgpa);
printf(CGPA = %f\n, cgpa)
}
BS (May 2012)
24
Topic 4
CONSTANTS
BS (May 2012)
25
Constants
Entities that appear in the program
code as fixed values.
Types of operations:
1. Declaring and assigning constant
values
2. Operating constant values
Almost similar treatment like variables but
CANNOT change the values after
declaration!
BS (Feb 2012)
26
Declaring Constants
1. As constant macro created using
a preprocessor directives using
<stdio.h>
define#include
keyword
#define PI 3.14159
Ex:
DATA TYPE?
{
}
BS (Feb 2012)
27
Types of Constants
Integer Constant
Floating-point Constant
Character Constant
Enumeration
#define LETTER n
const char NUMBER = 1;
BS (Feb 2012)
enum Language {
English
Arabic
Mandarin
}
28
29
Topic 6
READING DATA FROM THE
KEYBOARD
BS (May 2012)
30
BS (Feb 2012)
31
Placeholders in scanf()
Function
Data Type
%d
int
%f
float
%lf
double
%c
char
%s
String
BS (Feb 2012)
12.2, pg 647
(Hanly &
Koffman)
32
Example
int Age;
float income;
Address operator
int Age;
double income;
printf(Enter your age and income);
scanf(%d %lf, &Age, &income);
printf(Your age: %d\n, Age);
printf(Your income: %f\n, income);
BS (Feb 2012)
Exercise
Write a simple C program that read two
integer values and print them on the
screen in one same line separated by one
vertical tab. Follow
thisinteger:
sample80output:
Enter first
Enter second integer: 99
80
99
34
Topic 5
ARITHMETIC OPERATORS AND
EXPRESSIONS
BS (May 2012)
35
Arithmetic Expressions
A syntactically correct and meaningful
combination of operators and operands
is known as an Expression.
= is the basic assignment operator
Syntax:
Variable = ArithmeticExpression;
12 is assigned to variable month
Example:
operands
month = 12;
Expression:
arithmetic operator
36
Arithmetic Operators
There are 2 types of arithmetic operators in
C:
operand
37
1. Unary Operators
BS (May 2012)
38
Prefix/Postfix Increment
a++ or ++a is equals to a = a + 1 but they work
differently
Prefix Increment (+
+a)
Postfix Increment
(a++)
Example:
int a=9;
printf(%d\n, a);
printf(%d\n, ++a);
printf(%d, a);
Output:
9
10
10
Example:
int a=9;
printf(%d\n, a);
printf(%d\n, a++);
printf(%d, a);
Output:
9
9
10
BS (May 2012)
39
Prefix/Postfix Decrement
Similarly, --a or a-- is equals to a = a - 1 but
they work differently
Prefix Decrement (--a)
1. Subtract the value of
a by 1
2. Return the value a
Postfix Decrement
(a--)
1. Return the value a
2. Subtract the value of
a by 1
Example:
Example:
int a=9
int a=9;
printf(%d\n, a);
printf(%d\n, a);
printf(%d\n, --a);
printf(%d\n, a--);
printf(%d, a);
printf(%d, a);
Output:
Output:
9
9
8
9
BS (May 2012)
8
8
40
2. Binary Operators
If all operands in an arithmetic expression are of type double/float, the result is also of type double/float except for
modulus operator.
If all operands in an arithmetic expression are of type int, the result is also of type int.
Value of a is 9
BS (May 2012)
41
7 % 2 is 1
7%2
remainder
42
Example
A simple C program that find an
average of two real numbers.
#include <stdio.h>
void main(void)
{
float no1, no2, sum, avg;
printf(Enter three real numbers: );
scanf(%f %f, &no1, &no2);
sum
avg
=
=
no1
sum
+
/
no2;
2;
43
Exercise
Write a simple C program that sum up 4
integer numbers and display the computed
sum.
Covert the pseudocode/flowchart prepared
to solve the following problems (refer slide
39 in Chapter 2) into complete C programs
that:
1. compute and display the average of three
numbers.
2. read the value of the height, width and length
of a box from the user and print its volume.
BS (May 2012)
44
Compound Assignment
Operator
BS (May 2012)
45
Exercise
What is the output of the following
program?
#include <stdio.h>
void main(void)
{
int first;
printf("Enter a value: ");
scanf("%d", &first);
printf("\nNew
printf("\nNew
printf("\nNew
printf("\nNew
value
value
value
value
is:
is:
is:
is:
%d\n\n",
%d\n\n",
%d\n\n",
%d\n\n",
first+=4);
first*=4);
first-=4);
first%=4);
}
BS (May 2012)
46
float
Examples:
5 + 3 is 8
WHY?
5/3 is 1
float
float
5.2 + 3 is 8.2
5.0/3 is 1.6
5/3.0 is 1.6
float
float
int
BS (May 2012)
47
n is 1
q is
1.6
in
t
int/int int
5/3 is 1
floa
t
in
t
int/float
float
5/3 is 1.6
floa
t
BS (May 2012)
48
Type Casting
You can modify the way C uses types in
arithmetic operations by using cast operators.
The syntax:
(type) expression
Recall:
5/3 result in 1
follows: 5/(float)3
(float)5/(float)3
((float)5)/((float)3)
BS (May 2012)
49
avg = sum / 4;
50
= x / y + z
s evaluating
The rules for
expressions are:
1. Parentheses rule: all expressions in () must be evaluated
separately. Nested parentheses innermost first .
Parentheses can be used to control the order of operator
evaluation
2. Precedence rule: specifies which of the operators will be
evaluated first.
3. Associativity rule: specifies which of the multiple
occurrences of the same operators will be evaluated first.
BS (May 2012)
51
Lowest
(evaluated
L
from
last)
Operation
Associativ
ity
[ ] ( )
postfix++ postfix--
unary*
* / %
binary+ binary-
= +=
left to right
-= R
*=/=
%=
from
BS (May 2012)
right to left
52
Example 1
s = x / y + z
The order:
is 1, z = 2):
Step 1: s = 9 / 1 + z
Step 2: s = 9 + 2
Step 3: s = 11
i += j 2
Other option?
Use parentheses
Evaluation steps (assume initial value of i is 1, j is 7):
The order:
Step 1: i += 7 - 2
Step 2: i = 6 (i += 5 i = 1 + 5)
BS (May 2012)
53
Example 2
v = (p2 p1) / (t2 t1)
The order:
1:
2:
3:
4:
v
v
v
v
=
=
=
=
Note:
The expression inside a parentheses will be evaluated
first.
The associativity rule of parentheses is left to right
BS (May 2012)
54
Exercise 1
Find output of the program fragment
...
int i=5, c=10, x;
x = +i++;
printf("a) x = %d, i = %d\n", x, i);
x = ++x;
printf("b) x = %d, i = %d\n", x, i);
x = c++ + i;
printf("c) x = %d, i = %d\n", x, i);
x = --c + --i;
printf("d) x = %d, i = %d\n", x, i);
x = i + -x;
printf("e) x = %d, i = %d\n", x, i);
...
BS (May 2012)
55
Exercise 2
Find output of the program fragment
...
int x, y=5,z=10, s=3, p, q, r;
x = z/3 + -y;
printf("%d\n", x);
x = (x % 2) + (y 3));
printf("%d\n", x);
x = ++z + z * 2;
printf("%d\n", x);
p = y * z s + x /
q = y * (z s) + x
r = y * z s + x /
printf(%d\n, x);
z;
/ z;
z;
...
BS (May 2012)
56
Topic 7
SINGLE CHARACTER DATA
BS (May 2012)
57
Digits
Uppercase A to Z
Lowercase a to z
0 to 9
White Space
Blank space
Horizontal tab
(\t)
Carriage return
(r)
Special Characters
BS (May 2012)
58
C Special Characters
BS (May 2012)
59
Each character is
enclosed in single
quotes
c2 = \n;
c3 = #;
Using printf(%c\n,
printf() function to print characters. Example:
c1);
printf(%c\n, c1);
printf(%c %c, c1, c2);
BS (May 2012)
60
Example:
#include <stdio.h>
main()
{
char myC;
printf(Please type a character: );
myC = getchar();
printf(\nYou have typed this character:
);
putchar(myC);
printf(\n);
To be discussed
further in Chapter 8: Characters and Strings
}
BS (Feb 2012)
61
BS (May 2012)
62
Summary
Basic elements (tokens) in C: reserved words,
identifiers, string literals, operators, punctuators
Working with variables and constants of types
int, float, double, char
Reading data using scanf() function
The C rules when evaluating expression with
multiple mix of operators in arithmetic
expression:
Parentheses rule, precedence rule & associativity rule
63