Sunteți pe pagina 1din 50

Chapter 2

INTRODUCTORY CONCEPTS
Electrical Engineering Department
MOHD NASRI BIN HASHIM

Faculty of Information Technology and Multimedia, 2008/2009
Learning Outcomes:
Understand Constants and Variables
Understand Data Types
Understand Operators and Expressions


EC201 Fundamental Programming
Faculty of Information Technology and Multimedia, 2008/2009
Keyword
int
The acronym for integer
void
Refer to the function that will not
return any value
case default switch break

for continue float double

return while if do int
Example:
EC201 Fundamental Programming
Constants and Variables (Identifier)
Faculty of Information Technology and Multimedia, 2008/2009
Identifier
Representing particular name in programming
Store values to be used in programming
Refers to the storage in computer
Standard
identifier
User-defined
identifier
Type
EC201 Fundamental Programming
Constants and Variables (Identifier)
Faculty of Information Technology and Multimedia, 2008/2009
Special built-in words
Referred as function name
which will called from C library
Standard
identifier
printf() scanf()
puts() gets()
Identifier
EC201 Fundamental Programming
Constants and Variables (Identifier)
Faculty of Information Technology and Multimedia, 2008/2009
Example..
#include <stdio.h>
#include <conio.h>
#define PI 3.142 //define the constant value, PI=3.142

void main()
{
float radius, area;

printf("\nEnter radius: ");
scanf("%f",&radius);
area = PI * radius * radius;
printf("Area = %.2f \n",area);
printf("Press a key to finish.\n");
getch(); //to handle the screen
}
EC201 Fundamental Programming
Faculty of Information Technology and Multimedia, 2008/2009
Name given to the declaration of
data to be used in program
Refer to the storage name
Store data values/result/output
User-defined
identifier
Constant Variable
Type
Identifier
EC201 Fundamental Programming
Constants and Variables (Identifier)
Faculty of Information Technology and Multimedia, 2008/2009
User-defined
identifier

Identifiers name can only consists of name, number
and underscore
Identifiers name cannot be started with numbers
Symbol cannot be used in identifier name
Cannot contains spaces between two identifiers name
Identifiers name should be unique
Identifiers is not case sensitive

RULES
Identifier
EC201 Fundamental Programming
Constants and Variables (Identifier)
Faculty of Information Technology and Multimedia, 2008/2009
UThM DIT1064 Seven_eleven integer
Valid identifiers
8Century BIT 1033 Two*four

Sixsense void
Invalid identifiers
Identifier
WHY?
WHY?
WHY? WHY?
WHY?
EC201 Fundamental Programming
Constants and Variables (Identifier)
Faculty of Information Technology and Multimedia, 2008/2009
Name which used to store data value
Refer to name of one cell in computer
storage
Contants value is fixed
Constant
Identifier
How to give name to a
constant value?
Follow identifiers rules
EC201 Fundamental Programming
Constants and Variables (Identifier)
Faculty of Information Technology and Multimedia, 2008/2009
const data_type const_name = const_value;
Declaration format:

const float pi = 3.142;
Reserved word
1
Constant
Value
EC201 Fundamental Programming
Constants and Variables (Identifier)
Faculty of Information Technology and Multimedia, 2008/2009
#define const_name const_value;
Declaration format:

#define pi 3.142;
Reserved word
2
EC201 Fundamental Programming
Constants and Variables (Identifier)
Faculty of Information Technology and Multimedia, 2008/2009
Example..
EC201 Fundamental Programming
Faculty of Information Technology and Multimedia, 2008/2009
#define minimum 0;
#define MAX 100;

const int counter = 100;
const char alphabet = J;
const float value = 4.5;

Example of constant:
EC201 Fundamental Programming
Constants and Variables (Identifier)
Faculty of Information Technology and Multimedia, 2008/2009
data_type variable_name;
Name which used to store data/input
value
Refer to the name of one cell in computer
storage
Variables value can be modified/changed
during execution
Variable
Declaration Format:
Identifier
EC201 Fundamental Programming
Constants and Variables (Identifier)
Faculty of Information Technology and Multimedia, 2008/2009
Declaration Example

int number;

float weight;

char alphabet;
Declaration of a variable number of
integer data type.
Declaration of a variable weight of
floating point data type.
Declaration of a variable alphabet
of character data type.
EC201 Fundamental Programming
Constants and Variables (Identifier)
Faculty of Information Technology and Multimedia, 2008/2009
Variable/constant declaration example
//Variable and constant declration
#include <stdio.h>

int number;
float weight;

void main()
{
const float pi =3.142;

int bilangan;
float berat;
char abjad;
}
Constant declaration
Variable declaration
Variable declaration
EC201 Fundamental Programming
Constants and Variables (Identifier)
Faculty of Information Technology and Multimedia, 2008/2009
Variable and constant declaration example:
//Variable and constant declaration
#include <stdio.h>

const float pi=3.142;

void main()
{
int bilangan, bil, bilang;
float berat, kg;
char A;
}
EC201 Fundamental Programming
Constants and Variables (Identifier)
Faculty of Information Technology and Multimedia, 2008/2009
Assigning value to variables
#include <stdio.h>

void main()
{
int number = 10;
float weight;
weight = 60.00;

printf(Enter the value of number :);
scanf(%d,&number);

number = 50.00;
}

Initialize a variable
Interactive
Initialize a variable
EC201 Fundamental Programming
Constants and Variables (Identifier)
Faculty of Information Technology and Multimedia, 2008/2009
Represents types of data can be stored in computer.
Types of data to be stored and used in programming should be
informed to the compiler/system

Types
Integer
Floating
point
Character
Data Types
EC201 Fundamental Programming
Data Types
Faculty of Information Technology and Multimedia, 2008/2009
Data Types
Represents any round number with +/-
values.
Divided into short and long integer.
Reserved word for integer int
Valid until 5 places of integer number.
Integer
Example:
age is used to represent the age of students between
18 and 25 years old. The declaration for the
variable is as follow:

int age;
EC201 Fundamental Programming
Data Types
Faculty of Information Technology and Multimedia, 2008/2009
Data Types
Represents any floating point numbers +/-
Reserved word double /float
Floating
number
Example:
height is used to represent the students height
between 150 cm and 180 cm. The declaration for the
variable is as follow:

float height;
EC201 Fundamental Programming
Data Types
Faculty of Information Technology and Multimedia, 2008/2009
Data Types
Represents character data.
Reserved word char
Character
Example:
gender is used to represent the gender of a student.
The declaration for the variable is as follow:
char gender;
EC201 Fundamental Programming
Data Types
Faculty of Information Technology and Multimedia, 2008/2009
Determine whether the following identifiers is valid or
invalid. Give reason for invalid cases.
1) Parit Raja
2) 20thCentury
3) int
4) INTEGER
5) _BMW2003
6) Reservedword
7) BIT1033
8) markah_pelajar
9) jam*kredit
10) printf

EC201 Fundamental Programming
Example..
Faculty of Information Technology and Multimedia, 2008/2009
Write a suitable variable declaration for each of the following
statement:

i. Salary of an employee
ii. Students mark for programming subject
iii. ATM pin number
iv. Phone number
v. Price of one item
vi. Bus seat number
vii. Student name

EC201 Fundamental Programming
Example..
Faculty of Information Technology and Multimedia, 2008/2009
Given the value of x is 10 and a is 12,
find the result of the following equation:

y = 2x + a - 6
Based on the following problem, determine the appropriate
variables can be declared:
EC201 Fundamental Programming
Example..
Faculty of Information Technology and Multimedia, 2008/2009
Mrs Leeya needs to determine her students grade for
programming subject based on the mark scored during
final examination. The A grade will be given if the mark
scored is between 85 to 100. If a student has scored 90
marks, what is the grade should Mrs Leeya give to the
student?

Based on the following problem, determine the appropriate
variables can be declared:
EC201 Fundamental Programming

Example..

Faculty of Information Technology and Multimedia, 2008/2009
Based on the following problem, determine the appropriate
variables can be declared:
A box has height, width and length.
Calculate the volume of a box.

EC201 Fundamental Programming
Example..
Faculty of Information Technology and Multimedia, 2008/2009
Uncle Degawan wants to buy 5 tins of paint from
Cindas shop. The price of each tin of the paint is
RM 15.60. Calculate the price which Uncle Degawan have
to pay for all the tin of paints he bought.


Based on the following problem, determine the appropriate
variables can be declared:
EC201 Fundamental Programming
Example..
30
Operators and
Expressions
Faculty of Information Technology and Multimedia, 2008/2009
Expressions
Combination of Operators and Operands

Example 2 * y + 5
Operands
Operators
EC201 Fundamental Programming
Faculty of Information Technology and Multimedia, 2008/2009

a) Operand
A quantity or function upon which a mathematical or logical operation is
performed

EC201 Fundamental Programming
Operators and Expressions
Faculty of Information Technology and Multimedia, 2008/2009
b) Operator
An operator is a symbol which helps the user to command the computer to do a
certain mathematical or logical manipulations. Operators are used in C language
program to operate on data and variables. C has a rich set of operators which
can be classified as



EC201 Fundamental Programming
Operators and Expressions
Arithmetic
Relational
Logical
Assignment
Increment and Decrement
Faculty of Information Technology and Multimedia, 2008/2009
i. Arithmetic
- All the basic arithmetic operations can be carried out in C. All the operators
have almost the same meaning as in other languages. Both unary and binary
operations are available in C language. Unary operations operate on a singe
operand, therefore the number 5 when operated by unary will have the value
5
EC201 Fundamental Programming
Operators and Expressions
Operator Meaning
+ Addition or Unary Plus
Subtraction or Unary Minus
* Multiplication
/ Division
% Modulus Operator
Faculty of Information Technology and Multimedia, 2008/2009
Example : There are two integer variables a and b.
If the starting value of a=7 and b=2, the result will
be:
Expression Value
a b 5
a + b 9
a * b 14
a / b 3
a % b 1
Operators and Expressions
EC201 Fundamental Programming
Faculty of Information Technology and Multimedia, 2008/2009 EC201 Fundamental Programming
Operators and Expressions
Integer Arithmetic
When an arithmetic operation is performed on two whole numbers or
integers than such an operation is called as integer arithmetic. It always
gives an integer as the result. Let x = 27 and y = 5 be 2 integer numbers.
Then the integer operation leads to the following results.

x + y = ?
x y = ?
x * y = ?
x % y = ?
x / y = ?

In integer division the fractional part is truncated

32
22
11
5
2

Faculty of Information Technology and Multimedia, 2008/2009 EC201 Fundamental Programming
Operators and Expressions
ii. Relational Operator
Often it is required to compare the relationship between operands and bring out
a decision and program accordingly. This is when the relational operator come
into picture. C supports the following relational operators.

Operator Meaning
< is less than
<= is less than or equal to
> is greater than
>= is greater than or equal to
== is equal to
!= is not equal to
Faculty of Information Technology and Multimedia, 2008/2009 EC201 Fundamental Programming
Operators and Expressions
Given below is a list of examples of relational expressions and evaluated values.


6.5 <= 25 TRUE
-65 > 0 FALSE
10 < 7 + 5 TRUE

** The result of evaluation of a relational operation is either True
(represented by 1) or false (represented by 0).

For example, if a = 7 and b = 5, then a < b yields 0
and a != b yields 1.
Faculty of Information Technology and Multimedia, 2008/2009 EC201 Fundamental Programming
Operators and Expressions
iii. Logical Operator
C has the following logical operators, they compare or evaluate logical and
relational expressions.
Operator Meaning
&& AND
|| OR
! NOT
Faculty of Information Technology and Multimedia, 2008/2009 EC201 Fundamental Programming
Operators and Expressions
Logical AND (&&)

This operator is used to evaluate 2 conditions or expressions with relational
operators simultaneously. If both the expressions to the left and to the right of the
logical operator is true then the whole compound expression is true.
Example

a > b && x = = 10

The expression to the left is a > b and that on the right is x == 10 the whole
expression is true only if both expressions are true i.e., if a is greater than b and
x is equal to 10.

Logical OR (||)
The logical OR is used to combine 2 expressions or the condition evaluates to
true if any one of the 2 expressions is true.
Example

a < m || a < n

The expression evaluates to true if any one of them is true or if both of them are
true. It evaluates to true if a is less than either m or n and when a is less than
both m and n.
Faculty of Information Technology and Multimedia, 2008/2009 EC201 Fundamental Programming
Operators and Expressions
Logical NOT (!)
The logical not operator takes single expression and evaluates to true if the
expression is false and evaluates to false if the expression is true. In other words
it just reverses the value of the expression.

For example

! (x >= y) the NOT expression evaluates to true only if the value of x is neither
greater than or equal to y
Faculty of Information Technology and Multimedia, 2008/2009
The result of logical operation on a and
b are summarized as below:-
Operators and Expressions
EC201 Fundamental Programming

Variable Expression
A B A && B A || B !A
T T T T F
T F F T F
F T F T T
F F F F T
Faculty of Information Technology and Multimedia, 2008/2009 EC201 Fundamental Programming
Operators and Expressions
4. Assignment Operators
C provides several assignment operators for abbreviating assignment
expressions. For example the statement
c = c + 3;
can be abbreviated with the addition assignment operator += as
c += 3;
The += operator adds the value of expression on the right of the operator to the
value of the variable on the left of the operator and stores the result in the
variable on the left of the operator. Any statement of the form
Variable = variable operator expression;
Where operator is one of the binary operators +, -, *, / or %, can be written in the
form
Variable operator = expression;
Thus the assignment c += 3 adds 3 to c. Table shows the arithmetic assignment
operators, sample expressions using these operators and explanations.

Faculty of Information Technology and Multimedia, 2008/2009 EC201 Fundamental Programming
Operators and Expressions
Assignment
operator
Sample
expression
Explanation Assigns
Assume: int c = 3, d = 5, e = 4, f = 6, g = 13
+= c += 7 c = c + 7 10 to c
-= d -= 4 d = d 4 1 to d
Assignment
operator
Sample
expression
Explanation Assigns
*= e *= 5 e = e * 5 20 to e
/= f /= 3 f = f / 3 2 to f
%= g %= 9 g = g % 9 3 to g
Faculty of Information Technology and Multimedia, 2008/2009 EC201 Fundamental Programming
Operators and Expressions
5. Increments and Decrement Operators
Operator Sample
expression
Explanation
++ ++a Increment a by 1 then use the new
value of a in the expression in which
a resides
++ a++ Use the current value of a in the
expression in which a resides, the
increment a by 1 (Still remain same
number)
-- --b Decrement b by 1 the use the new
value of b in the expression in which
b resides
-- b-- Use the current value of b in the
expression in which b resides, then
decrement b by 1 (Still remain same
number)
Faculty of Information Technology and Multimedia, 2008/2009 EC201 Fundamental Programming
Operators and Expressions
5. Increments and Decrement Operators


#include <stdio.h>
int main(void);
{
int j = 5, k = 5, l = 5, m = 5;
printf("j: %d\t k: %d\n", j, k);
printf("j: %d\t k: %d\n", j++, k--);
printf("l: %d\t m: %d\n", l, m);
printf("l: %d\t m: %d\n", ++l, --m);
}


Output:
j: 5 k: 5
j: 5 k: 5
l: 5 m: 5
l: 6 m: 4

Faculty of Information Technology and Multimedia, 2008/2009
HIERARCHY OF OPERATOR
The hierarchy of operator precedence form
highest to lowest is summarized below:

Operator Category Operator
Unary - - - + +
Arithmetic multiply, devide,
remainder
* / %
Arithmetic add and subtract + -
Relational operators < <= > >=
Equality operators = = ! =
Logical AND &&
Logical OR ||
EC201 Fundamental Programming
Faculty of Information Technology and Multimedia, 2008/2009
Find the answer using the hierarchy of
operator.
Use: a=12, b=2, c=3
a) X = a % b
b) X = a /b
c) X = a % b / c
d) X = a / b % c
EC201 Fundamental Programming
Example..
Faculty of Information Technology and Multimedia, 2008/2009
EXERCISE 1:

int a=5, b=2, c=3, d=4;
float answer;

answer = (a % c)* (d+b) * d / (a-c);
printf("The answer is : %.3f",answer);
printf("\n");


EXERCISE 2:

int m;
int a=100, b=5, c=3, d=2, x=3;

m = a + b * c / d x++;

printf(The answer is : %d",m);
printf(/n);


Example..
EC201 Fundamental Programming
answer=24 m=104
Faculty of Information Technology and Multimedia, 2008/2009
Consider the following example:
2*3+4/2 > 3 AND 3<5 OR 10<9
[2*3+4/2] > 3 AND 3<5 OR 10<9
[[2*3]+[4/2]] > 3 AND 3<5 OR 10<9
[6+2] >3 AND 3<5 OR 10<9
[8 >3] AND [3<5] OR [10<9]
True AND True OR False
[True AND True] OR False
True OR False
True



EC201 Fundamental Programming
Example..

S-ar putea să vă placă și