You are on page 1of 40

May 6, 2021

BFF/BHM 2003: LECTURE 3


BFF/ BHM 2003
COMPUTER
PROGRAMMING
1
LECTURE 3
INTRODUCTION TO THE C
LANGUAGE

May 6, 2021
BY END OF THIS CLASS, YOU SHOULD BE ABLE
TO:

BFF2003: LECTURE 3
 Use and construct various variable types and constant
declarations for all sizes of numeric data and strings.

2
IDENTIFIERS
 allow us to name data & other objects in the program.

May 6, 2021
 Each identified object in the computer is stored at a

BFF2003: LECTURE 3
unique address.

 Different programming languages use different


syntactical rules to form identifiers.

 The rules for identifiers in C are very simple.

3
May 6, 2021 BFF2003: LECTURE 3
4
IDENTIFIERS
IDENTIFIERS
 The C language contains 37 keywords:

May 6, 2021
Auto extern short
_Bool float signed

BFF2003: LECTURE 3
break for sizeof
case goto static
char if struct
_Complex _Imaginary switch
const inline typedef
continue int union
default long unsigned
do register void
double restrict volatile
else return while 5

enum
IDENTIFIERS
 Themacros and C++ keywords also should be treated as

May 6, 2021
keywords:

BFF2003: LECTURE 3
and compl or
and_eq complex OR_EQ
bitand imaginary struct
bitor not xor_eq
bool not_eq

6
IDENTIFIERS
 Good identifier names are descriptive but short

May 6, 2021
(including use abbreviations).
student  stdnt

BFF2003: LECTURE 3
C allows name to be up to 63 characters long (if longer
than that, only the first 63 used)
BOSTON: Pegawai pentadbiran kerajaan 'dengan malu' terpaksa mengaku papan tanda nama Tasik
Chargoggagoggmanchauggagoggchaubunagungamaugg di Massachusetts, nama paling
panjang bagi sebuah tasik di wilayah itu, tersilap eja sejak bertahun-tahun lamanya.

Kesilapan ejaan nama tasik mengandungi 45 huruf itu didedahkan akhbar tempatan Worcester Telegram & Gazette yang sudah
mengesan kesilapan ejaan berkenaan sejak 2003.

Akhbar itu melaporkan ejaan Chargoggagoggmanchauggagoggchaubunagungamaugg adalah tepat manakala ejaan


Chargoggagoggmanchaoggag-oggchaubunaguhgamaugg pada papan tanda itu salah kerana menggantikan u dengan o pada huruf ke-
20 dan n dengan h pada huruf ke-38.
7
May 6, 2021 BFF2003: LECTURE 3
8
IDENTIFIERS
May 6, 2021 BFF2003: LECTURE 3
9
IDENTIFIERS
IDENTIFIERS

May 6, 2021
BFF2003: LECTURE 3
Note
An identifier must start with a letter or underscore:
it may not have a space or a hyphen.

C is a case-sensitive language.

num Num NUM


10
are three different identifiers
May 6, 2021 BFF2003: LECTURE 3
11
IDENTIFIERS
TYPES
A type defines a set of values and a set of operation that
can be applied on those value.

May 6, 2021
BFF2003: LECTURE 3
 Divide into 4:

12
TYPES: VOID TYPE
 Designated by keyword void

May 6, 2021
 Has no value and no operations but very useful data type.

BFF2003: LECTURE 3
 E.g.use to designate that a function has no parameters as
in the main function.

 Used to define that a function has no return value and


define a pointer to generic data.

13
TYPES: INTEGRAL TYPE
 Has3 integral types:

May 6, 2021
Boolean
Character

BFF2003: LECTURE 3
Integer

 Integral
types cannot contain a fraction part; they are
whole numbers.

14
TYPES: BOOLEAN TYPE
 Represent only two values: true false

May 6, 2021
C used integers to represent the Boolean value:

BFF2003: LECTURE 3
A nonzero number (+ve or –ve) true
A zero numberfalse

 The Boolean type, which is referred to by the keyword


bool, is stored in memory as 0 (false) and 1(true).

15
TYPES: CHARACTER TYPE
 Evenwe think of characters as the letters of the alphabet,
a computer has another definition.

May 6, 2021
BFF2003: LECTURE 3
A character any value that can be represented in the
computer’s alphabet or character set.

 The C standard provide two character type:

16
TYPES: CHARACTER TYPE
 Most computer use the American Standard Code for
Information Interchange (ASCII) alphabet.

May 6, 2021
BFF2003: LECTURE 3
 You do not need to memorize this alphabet but you will
learn many of the special value by using them. (ASCII
code included in Appendix A).

 Computer use 1 byte to store the char data types.

1 byte = 8 bits = 256 different value in the char set


17
May 6, 2021 BFF2003: LECTURE 3
18
TYPES: CHARACTER TYPE
May 6, 2021 BFF2003: LECTURE 3
19
TYPES: CHARACTER TYPE
May 6, 2021 BFF2003: LECTURE 3
20
TYPES: CHARACTER TYPE
TYPES: CHARACTER TYPE
 First 32 ASCII & last ASCII character are control
characters: physical devices (monitor, printer). The rest

May 6, 2021
are characters for compose words and sentences.

BFF2003: LECTURE 3
 What make the letter a different from the letter x?

Letter Binary Decimal Value


a 0110 0001 97
 To support non-English & languages that
x 0111 1000 120 don’t use the
Roman alphabet, the C99 standard created the wide
character type ( wchar_t ).
21
TYPES: INTEGER TYPE
 Is a number without a fraction part.

May 6, 2021
C defines these data types so that they can be organized

BFF2003: LECTURE 3
from the smallest to the largest & the size of the field in
which data can be stored.
sizeof (short) ≤ sizeof (int) ≤ sizeof (long) ≤ sizeof (long long)

22
TYPES: INTEGER TYPE
 Each integer size can be a signed or an unsigned integer.

May 6, 2021
 For signed= one bit for a signed (0 is + ; 1 is - ).

BFF2003: LECTURE 3
 Forunsigned = a positive number is 2X as large as the
signed integer of the same size.

23
TYPES: INTEGER TYPE

May 6, 2021
Typical integer size and values for signed integers

BFF2003: LECTURE 3
24
TYPES: FLOATING-POINT
TYPES
 TheC standard recognize three:

May 6, 2021
Real
Imaginary

BFF2003: LECTURE 3
Complex

C has a library, float.h that contains the limit of floating-


point values.

 Real type values are always signed.


25
TYPES: REAL TYPES
 Holds values that consist of an integral and a fractional
part such as: 43.32 .

May 6, 2021
BFF2003: LECTURE 3
 Support 3 different size of real types.

Note
sizeof (float) ≤ sizeof (double) ≤ sizeof (long double)
26
TYPES: IMAGINARY TYPES
 Is used extensively in maths and eng.

May 6, 2021
 Like real number multiplied by 1

BFF2003: LECTURE 3
 Have three different size:
float imaginary

double imaginary

long double imaginary

 theimaginary part is one of the components of the


27
complex type & not a part of the standard.
TYPES: COMPLEX TYPES
 Is implemented by most compilers.

May 6, 2021
 Combination of a real number and imaginary number.

BFF2003: LECTURE 3
 Have a different sizes as real or imaginary types.

28
May 6, 2021 BFF2003: LECTURE 3
29
TYPES
VARIABLES
 Are named memory location that have a types, such as
integer or character, which is inherited from their type.

May 6, 2021
BFF2003: LECTURE 3
 The type determines the values that a variable may
contain the operations that may be used with its values.

30
VARIABLES DECLARATION
 Each variable in your program must be declared and
defined at the same time (but have some exception later)

May 6, 2021

BFF2003: LECTURE 3
 A declaration is used to name an object.

 Definitions are used to create the object.

31
May 6, 2021 BFF2003: LECTURE 3
32
VARIABLES
May 6, 2021 BFF2003: LECTURE 3
33
VARIABLES
VARIABLES INITIALIZATION
 We can initialize a variable at the same time we declare it
by including an initializer.

May 6, 2021
BFF2003: LECTURE 3
 The initializer establishes the 1st value that the variable
will contain.

 The indentifier is followed by the assignment operator


(equal sign, = ) and then the initializer which is the value
the variable is to have when functions starts.

34
May 6, 2021 BFF2003: LECTURE 3
VARIABLES INITIALIZATION

35
VARIABLES INITIALIZATION

May 6, 2021
BFF2003: LECTURE 3
Note
When a variable is defined, it is not initialized.
We must initialize any variable requiring
prescribed data when the function starts.
36
May 6, 2021 BFF2003: LECTURE 3
VARIABLES INITIALIZATION

37
May 6, 2021 BFF2003: LECTURE 3
VARIABLES INITIALIZATION

38
May 6, 2021 BFF2003: LECTURE 3
VARIABLES INITIALIZATION

39
END OF TODAY’S LECTURE
 Thank you for your attention.

May 6, 2021
BFF2003: LECTURE 3
 Now, we were move to the practical work
(the important part of this class).

 Please sit in your own group.

 Please do the assignment NOW. Submit the assignment according


40

to the date on the assignment sheet.

You might also like