Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 50

Electrical Engineering Department MOHD NASRI BIN HASHIM

Chapter 2
INTRODUCTORY CONCEPTS

Learning Outcomes:

Understand Constants and Variables Understand Data Types Understand Operators and Expressions

Faculty of Information Technology EC201 Fundamental and Multimedia, Programming 2008/2009

Constants and Variables (Identifier)


Keyword
Example:
Refer to the function that will not return any value

void

The acronym for integer

int

case

default

switch

break

for

continue
return while

float
if

double

do int

Faculty of Information Technology EC201 Fundamental and Multimedia, Programming 2008/2009

Constants and Variables (Identifier)


Identifier
Representing particular name in programming Store values to be used in programming Refers to the storage in computer

Standard identifier

Type

User-defined identifier

Faculty of Information Technology EC201 Fundamental and Multimedia, Programming 2008/2009

Constants and Variables (Identifier)


Identifier
Standard identifier
Special built-in words Referred as function name which will called from C library

printf() scanf() puts() gets()


Faculty of Information Technology EC201 Fundamental and Multimedia, Programming 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

Faculty of Information Technology EC201 Fundamental and Multimedia, Programming 2008/2009

Constants and Variables (Identifier)


Identifier
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

Type

Variable

Faculty of Information Technology EC201 Fundamental and Multimedia, Programming 2008/2009

Constants and Variables (Identifier)


Identifier
User-defined identifier
RULES
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
Faculty of Information Technology EC201 Fundamental and Multimedia, Programming 2008/2009

Constants and Variables (Identifier)


Identifier
Valid identifiers
UThM DIT1064 Seven_eleven integer WHY? Two*four void WHY?

Invalid identifiers
8Century BIT 1033 Sixsense WHY? WHY?

Faculty of Information Technology EC201 Fundamental and Multimedia, Programming 2008/2009

Constants and Variables (Identifier)


Identifier
Constant Name which used to store data value Refer to name of one cell in computer storage Contants value is fixed

How to give name to a constant value?

Follow identifiers rules

Faculty of Information Technology EC201 Fundamental and Multimedia, Programming 2008/2009

Constants and Variables (Identifier)


Declaration format:

const data_type const_name = const_value;

Reserved word

const

float pi = 3.142;

Constant Value

Faculty of Information Technology EC201 Fundamental and Multimedia, Programming 2008/2009

Constants and Variables (Identifier)


Declaration format:

#define const_name const_value;

Reserved word

#define pi 3.142;

Faculty of Information Technology EC201 Fundamental and Multimedia, Programming 2008/2009

Example..

Faculty of Information Technology EC201 Fundamental and Multimedia, Programming 2008/2009

Constants and Variables (Identifier)


Example of constant:
#define minimum 0; #define MAX 100; const int counter = 100; const char alphabet = J; const float value = 4.5;

Faculty of Information Technology EC201 Fundamental and Multimedia, Programming 2008/2009

Constants and Variables (Identifier)


Identifier
Variable
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

Declaration Format: data_type variable_name;

Faculty of Information Technology EC201 Fundamental and Multimedia, Programming 2008/2009

Constants and Variables (Identifier)


Declaration Example
Declaration of a variable number of integer data type.

int number; float weight; char alphabet;


Declaration of a variable weight of floating point data type. Declaration of a variable alphabet of character data type.

Faculty of Information Technology EC201 Fundamental and Multimedia, Programming 2008/2009

Constants and Variables (Identifier)


Variable/constant declaration example

//Variable and constant declration #include <stdio.h> int number; float weight;

Variable declaration Constant declaration Variable declaration

void main() { const float pi =3.142;

int bilangan; float berat; char abjad; }

Faculty of Information Technology EC201 Fundamental and Multimedia, Programming 2008/2009

Constants and Variables (Identifier)


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; }

Faculty of Information Technology EC201 Fundamental and Multimedia, Programming 2008/2009

Constants and Variables (Identifier)


Assigning value to variables

#include <stdio.h> void main() { int number = 10; float weight; weight = 60.00;

Initialize a variable Interactive

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

Initialize a variable

Faculty of Information Technology EC201 Fundamental and Multimedia, Programming 2008/2009

Data Types
Data Types
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
Faculty of Information Technology EC201 Fundamental and Multimedia, Programming 2008/2009

Character

Data Types
Data Types
Integer
Represents any round number with +/values. Divided into short and long integer. Reserved word for integer int Valid until 5 places of integer number.

age is used to represent the age of students between 18 and 25 years old. The declaration for the variable is as follow:

Example:

int age;

Faculty of Information Technology EC201 Fundamental and Multimedia, Programming 2008/2009

Data Types
Data Types
Floating number
Represents any floating point numbers +/Reserved word double /float

height is used to represent the students height between 150 cm and 180 cm. The declaration for the variable is as follow:

Example:

float height;

Faculty of Information Technology EC201 Fundamental and Multimedia, Programming 2008/2009

Data Types
Data Types
Character
Represents character data. Reserved word char

gender is used to represent the gender of a student. The declaration for the variable is as follow:

Example:

char gender;

Faculty of Information Technology EC201 Fundamental and Multimedia, Programming 2008/2009

Example..
Determine whether the following identifiers is valid or invalid. Give reason for invalid cases.

1)
2) 3) 4) 5) 6) 7)

Parit Raja
20thCentury int INTEGER _BMW2003 Reservedword BIT1033

8)
9) 10)

markah_pelajar
jam*kredit printf

Faculty of Information Technology EC201 Fundamental and Multimedia, Programming 2008/2009

Example..
Write a suitable variable declaration for each of the following statement: i. ii. iii. iv. v. Salary of an employee Students mark for programming subject ATM pin number Phone number Price of one item

vi.
vii.

Bus seat number


Student name

Faculty of Information Technology EC201 Fundamental and Multimedia, Programming 2008/2009

Example..
Based on the following problem, determine the appropriate
variables can be declared:

Given the value of x is 10 and a is 12, find the result of the following equation:

y = 2x + a - 6

Faculty of Information Technology EC201 Fundamental and Multimedia, Programming 2008/2009

Example..
Based on the following problem, determine the appropriate
variables can be declared:

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?

Faculty of Information Technology EC201 Fundamental and Multimedia, Programming 2008/2009

Example..
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.

Faculty of Information Technology EC201 Fundamental and Multimedia, Programming 2008/2009

Example..
Based on the following problem, determine the appropriate
variables can be declared:

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.

Faculty of Information Technology EC201 Fundamental and Multimedia, Programming 2008/2009

Operators and Expressions

30

Expressions
Combination of Operators and Operands

Operators
Example

2*y+5
Operands

Faculty of Information Technology EC201 Fundamental and Multimedia, Programming 2008/2009

Operators and Expressions


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

Faculty of Information Technology EC201 Fundamental and Multimedia, Programming 2008/2009

Operators and Expressions


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

Arithmetic

Logical

Increment and Decrement

Relational

Assignment

Faculty of Information Technology EC201 Fundamental and Multimedia, Programming 2008/2009

Operators and Expressions


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
Operator + Meaning Addition or Unary Plus Subtraction or Unary Minus

*
/ %

Multiplication
Division Modulus Operator

Faculty of Information Technology EC201 Fundamental and Multimedia, Programming 2008/2009

Operators and Expressions


Example : There are two integer variables a and b. If the starting value of a=7 and b=2, the result will be:
Expression ab a+b a*b a/b a%b Value 5 9 14 3 1

Faculty of Information Technology EC201 Fundamental and Multimedia, Programming 2008/2009

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 = ?32 x y = ?22 x * y = ?11 x % y = ?5 x / y = ?2 In integer division the fractional part is truncated

Faculty of Information Technology EC201 Fundamental and Multimedia, Programming 2008/2009

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 < <= > >= is less than is less than or equal to is greater than

Meaning

is greater than or equal to

==
!=

is equal to
is not equal to

Faculty of Information Technology EC201 Fundamental and Multimedia, Programming 2008/2009

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 EC201 Fundamental and Multimedia, Programming 2008/2009

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 EC201 Fundamental and Multimedia, Programming 2008/2009

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 EC201 Fundamental and Multimedia, Programming 2008/2009

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 EC201 Fundamental and Multimedia, Programming 2008/2009

Operators and Expressions


The result of logical operation on a and b are summarized as below:Variable A T T F F B T F T F A && B T F F F Expression A || B T T T F !A F F T T

Faculty of Information Technology EC201 Fundamental and Multimedia, Programming 2008/2009

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 EC201 Fundamental and Multimedia, Programming 2008/2009

Operators and Expressions


Assignment Sample Explanation operator expression Assume: int c = 3, d = 5, e = 4, f = 6, g = 13 += -= c += 7 d -= 4 c=c+7 d=d4 Assigns

10 to c 1 to d

Assignment operator *= /= %=

Sample expression e *= 5 f /= 3 g %= 9

Explanation e=e*5 f=f/3 g=g%9

Assigns 20 to e 2 to f 3 to g

Faculty of Information Technology EC201 Fundamental and Multimedia, Programming 2008/2009

Operators and Expressions


5. Increments and Decrement Operators Operator ++ Sample expression ++a Explanation Increment a by 1 then use the new value of a in the expression in which a resides Use the current value of a in the expression in which a resides, the increment a by 1 (Still remain same number) Decrement b by 1 the use the new value of b in the expression in which b resides Use the current value of b in the expression in which b resides, then decrement b by 1 (Still remain same number)

++

a++

--

--b

--

b--

Faculty of Information Technology EC201 Fundamental and Multimedia, Programming 2008/2009

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 EC201 Fundamental and Multimedia, Programming 2008/2009

HIERARCHY OF OPERATOR
The hierarchy of operator precedence form highest to lowest is summarized below:
Operator Category Unary Arithmetic multiply, devide, remainder Arithmetic add and subtract Relational operators Equality operators Logical AND Logical OR Operator - -- ++ * / %
+ < <= > >= == !=

&& ||

Faculty of Information Technology EC201 Fundamental and Multimedia, Programming 2008/2009

Example..
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
Faculty of Information Technology EC201 Fundamental and Multimedia, Programming 2008/2009

Example..

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);

answer=24

m=104

Faculty of Information Technology EC201 Fundamental and Multimedia, Programming 2008/2009

Example..
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
Faculty of Information Technology EC201 Fundamental and Multimedia, Programming 2008/2009

You might also like