You are on page 1of 74

Prepared by:

Norhamreeza Abdul Hamid


1. Program Development Environment

2. General Form of C

3. Parts of C Programming

4. Special Characters

5. Program Block

6. Basic Syntax of Programming

7. Common Programming Errors


2 Prepared by Norhamreeza Abdul Hamid
Involves translating high-level language (programming
language such as C,C++, Java, PHP,Visual Basic, C#,etc.)

WHY? Because computers do NOT


understand high level language!

Translated to

00011101 01010101 11011111


3 Prepared by Norhamreeza Abdul Hamid
Typical program development environment consist of six
phases to be executed.

4 Prepared by Norhamreeza Abdul Hamid


Phase 1: Creating a Program
 Programmer types or creates program in an editor.
 Makes corrections if necessary.
 Saves or stores program on disk such as C:\
or F:\ etc.
Editor?
Editor or text editor is a type
of program used for editing
plain text files.

5 Prepared by Norhamreeza Abdul Hamid


Dev-C++
editor
(free)

6 Prepared by Norhamreeza Abdul Hamid


Online tools

7 Prepared by Norhamreeza Abdul Hamid


Phase 2: Preprocessing

 Programmer gives command to compile the program.


 Preprocessor program executes automatically and process
the program code.
 The preprocessor obeys commands from preprocessor
directives.
 Preprocessing occurs before a program is compiled.

8 Prepared by Norhamreeza Abdul Hamid


Phase 3: Compiling a Program

 When compiled, compiler translates program into machine


language code and creates object code.
Programmer click
 The object code will be stored in disk.
Compile button

Dialog box in
Dev-C++ editor
shows
compiling
process.
9 Prepared by Norhamreeza Abdul Hamid
Phase 3: Compiling a Program

 The object code will be only created if the translation


process into machine code is successful.
 Otherwise, if unsuccessful, error messages will be displayed
in the compiling dialogue box.
 Programmer must fix all the errors before proceed to the
next phase.
 The process of correcting errors is called debugging.

10 Prepared by Norhamreeza Abdul Hamid


Phase 4: Linking

 A linker links the object code with the libraries.


 A linker will creates an executable file and stores it on disk
if the program compiles and links correctly.
 A linker might name the executable file
with .exe file extension depending on
type of programming language used.

11 Prepared by Norhamreeza Abdul Hamid


Phase 5: Loading

 Before a program can be executed, the program must first


be placed in memory.
 Loader takes the stored program from disk and puts in
memory.
 Additional components from shared libraries that support
the program are also loaded.

12 Prepared by Norhamreeza Abdul Hamid


Phase 6: Executing

CPU takes each instructions and executes it.


Results or output will be displayed.

13 Prepared by Norhamreeza Abdul Hamid


Terms Description
Machine language Binary number codes understood by a specific
CPU.

High-level Machine-independent programming language


language that combines algebraic expressions and
English symbols.

Source file File containing a program written in a high-


level language; the input for compiler

Compiler Software that translates a high-level language


program into machine language.

Linker Software that combines object files and create


an executable machine language program.

14 Prepared by Norhamreeza Abdul Hamid


C is a programming language developed in the
1970's alongside the UNIX operating system.
C provides a comprehensive set of features for
handling a wide variety of applications, such as
systems development and scientific computation.

15 Prepared by Norhamreeza Abdul Hamid


// Program description or comment
#include directives
int main()
{
constant declarations
variable declarations
executable statements
return 0;
}
16 Prepared by Norhamreeza Abdul Hamid
Comment
// sample C program
Preprocessor
#include <stdio.h> directive

int main() Beginning


of function
{ Beginning named main Output
of block for statement
main

printf(“Ye! Ye! I got it!”);


return 0; String
literal
} End of
block for
main
17 Prepared by Norhamreeza Abdul Hamid
Character Name Meaning
// Double slash Beginning of a comment
# Pound sign Beginning of preprocessor
directive
< > Open / close Enclose filename in #include
brackets
( ) Open / close Used when naming a function
parentheses
{ } Open / close brace Encloses a group of statements
" " Open / close Encloses string of characters
quotation marks
; Semicolon End of a programming statement

18 Prepared by Norhamreeza Abdul Hamid


19 Prepared by Norhamreeza Abdul Hamid
Preprocessor Directive
 Inserts the contents of another file into the program.
 Utility program which link files from compiler library to the
program code.
 This is preprocessor directive, not part of C language.
 Must be included in the first line of a computer program.
 Must be started with the symbol #, otherwise syntax errors
will be occurred.
 Two types of common preprocessor directive: #include
and #define.
 DO NOT semicolon at the END of #include line.

20 Prepared by Norhamreeza Abdul Hamid


Preprocessor Directive
Format:
#include<header file> or #include“user defined files”

Example
#include<stdio.h>
#include<conio.h>
#include “jam.h”

21 Prepared by Norhamreeza Abdul Hamid


Preprocessor Directive
Example:
Called from
standard library

 A directive to the C preprocessor


 Lines beginning with # are processed by the preprocessor
before the program is compiled.
 The above code line tells the preprocessor to include the
contents of stdio.h (standard input/output header)

22 Prepared by Norhamreeza Abdul Hamid


 Consists of built-in functions.

Standard  Functions contains standard


Library instructions.

 Function will be called and linked to


program via header file.

List of header file and its function


Header file List of functions
stdio.h scanf, printf, etc.
conio.h clrscr(), putch(), getch(), etc
math.h sqrt(),pow(), log(),etc

23 Prepared by Norhamreeza Abdul Hamid


 Contain functions defined by
User-defined programmer.
Library
 Developed by expert programmers.

List of header file and its function


List of user-defined
Header file
functions
utama.h cetak(), baca(), papar(), etc.

kira.h tambah(),tolak(), darab(),etc.

24 Prepared by Norhamreeza Abdul Hamid


Preprocessor Directive
Format:
#define“filename” or #define constant_name constant_value

Example

#define MAX 100


#define “jam.h”

25 Prepared by Norhamreeza Abdul Hamid


Program Body
 The part in which the program code will be started to
execute.
 Consists of main function, C statements and identifiers.
 Use { to start the program code and } to end the
program code.

Format: main function


{
//identifiers
//C statements
}
26 Prepared by Norhamreeza Abdul Hamid
Main Function

void main( )
{ …………..}

int main( ) main( )


{ {
return 0; return 0;
Main function
} }

27 Prepared by Norhamreeza Abdul Hamid


Write the most basic structures of C
programming.

28 Prepared by Norhamreeza Abdul Hamid


C Statement
 Instruction to be executed by computers
 Every statements must be ended with semicolon

Function
Statement

Declaration Control
Statement Statement

Types

Input /
Compound
Output
Statement
Statement

29 Prepared by Norhamreeza Abdul Hamid


Reserved Words / Key Words
 Standard / special word in standard library
 Contain special meaning in C which understood by compiler
 Cannot be used for any other purpose

Rules
Case –sensitive
Cannot be used as identifier
Must be written in
or variables
small case

30 Prepared by Norhamreeza Abdul Hamid


Reserved Words / Key Words
int
Example: The acronym for
void integer
Refer to the function that will not
return any value

31 Prepared by Norhamreeza Abdul Hamid


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

floating
point

integer
boolean

character

Data Types
32 Prepared by Norhamreeza Abdul Hamid
Data Types
•Represent any round numbers with +/- values

Integer •Divided into short and long integer


•Reserved word for integer – int
•Ex: int age;

Floating •Represent any floating point numbers +/- values


•Reserved word - double / float
number •Ex: float weight;

•Represents character data


Character •Reserved word – char
•Ex: char gender;

Boolean •Represent true or false


•Reserved word - bool

33 Prepared by Norhamreeza Abdul Hamid


Integer Data Types
 Integer variables can hold whole numbers such as 550, 32, 4, -9

Data Type Size Range


short 2 bytes -32, 768 to +32, 767
unsigned short 2 bytes 0 to +65, 535
int 4 bytes -2, 147, 483, 648 to +2, 147, 483, 647
unsigned int 4 bytes 0 to 4, 294, 967, 295
long 4 bytes -2, 147, 483, 648 to +2, 147, 483, 647
unsigned long 4 bytes 0 to 4, 294, 967, 295

34 Prepared by Norhamreeza Abdul Hamid


Floating number Data Types
 The floating-point data types are:
float
double
long double
 They can hold real numbers such as:
12.45 -3.8
 Stored in a form similar to scientific notation
 All floating-point numbers are signed

Data Type Key Word Description


Single precision float 4 bytes. Numbers between ±3.4E-38 and
±3.4E38
Double precision double 8 bytes. Numbers between ±1.7E-308 and
±1.7E308
Long double precision long double 8 bytes. Numbers between ±1.7E-308 and
35 Prepared by Norhamreeza Abdul Hamid
±1.7E308
Character Data Types
 Used to hold characters or very small integer values
 Usually 1 byte of memory
 Numeric values of character from the character set is stored in
memory:

CODE: MEMORY:
char letter; letter
letter = 'C';
67

36 Prepared by Norhamreeza Abdul Hamid


Character String Data Types
 A series of characters in consecutive memory locations:
"Hello"
 Stored with the null terminator, \0, at the end
 Comprised of the characters between the " "

H e l l o \0

37 Prepared by Norhamreeza Abdul Hamid


Boolean Data Types

 Represents values that are true or false


 bool variables are stored as small integers
 false is represents by 0, true by 1:
bool allDone = true;
bool finished = false;

allDone finished

1 0

38 Prepared by Norhamreeza Abdul Hamid


Identifier
 Representing particular name in programming
 Store values to be used in programming
 An identifier is a programmer-defined name for some part of a
program: variables, functions, etc.
 You cannot use any of the C reserved words / key words as an
identifier. These words have reserved meaning.

39 Prepared by Norhamreeza Abdul Hamid


Identifier

40 Prepared by Norhamreeza Abdul Hamid


Identifier (Standard)

 Displays output on the computer screen


printf(“Programming is fun!”);

41
Prepared by Norhamreeza Abdul Hamid
Identifier (Standard)

 Read input entered from user


scanf(“%d”, &integer);

42
Prepared by Norhamreeza Abdul Hamid
Identifier (Standard)
#include <stdio.h>

int main()
{
int h;
printf(“Please enter a number”);
scanf(“%d”, &h);
h = h*5;
printf(“The answer is %d”, h);
return 0;
}

43
Prepared by Norhamreeza Abdul Hamid
Identifier (Standard)

44
Prepared by Norhamreeza Abdul Hamid
Identifier (User-defined)

45
Prepared by Norhamreeza Abdul Hamid
Identifier (User-defined)
Valid identifiers

UThM BIC10204 Seven_eleven integer

WHY?
Invalid identifiers
8Century BIC 1033 Two*four

‘Sixsense’ void
WHY?
WHY? WHY?
46 Prepared by Norhamreeza Abdul Hamid
Identifier (User-defined)

 Name which used to store data value


 Refer to name of one cell in computer
storage
 Constants value is fixed

How to give Follow identifiers


name to a
constant value? rules

47 Prepared by Norhamreeza Abdul Hamid


Identifier (User-defined)

const data_type const_name = const_value;

Constant
Reserved word const float pi = 3.142;
Value

48 Prepared by Norhamreeza Abdul Hamid


Identifier (User-defined)

#define const_name = const_value;

Constant
Reserved word #define pi = 3.142;
Value

49 Prepared by Norhamreeza Abdul Hamid


Identifier (User-defined)

#define minimum 0;
#define MAX 100;

const int counter = 100;


const char alphabet = ‘J’;
const float value = 4.5;

50 Prepared by Norhamreeza Abdul Hamid


Identifier (User-defined)
 Name which used to store data / input
value
 Refer to name of one cell in computer
storage
 Variable’s value can be modified/changed
during execution
 Must be defined before it can be used

data_type variable_name;

51 Prepared by Norhamreeza Abdul Hamid


Identifier (User-defined)

 Variables of the same type can be defined


 On separate lines:
int length;
int width;
unsigned int area;
 On the same line:
int length, width;
unsigned int area;
 Variables of different types must be in different definitions

52 Prepared by Norhamreeza Abdul Hamid


Identifier (User-defined)

int number;

float weight;

char alphabet;

53 Prepared by Norhamreeza Abdul Hamid


Identifier (User-defined)

Constant declaration

Variable declaration

54 Prepared by Norhamreeza Abdul Hamid


Identifier (User-defined)

 Assign value to variable during


 Input/enter data through
declaration.
input devices
 Assign value to variable.
 Use input statement (scanf)
 Use assign symbol, “=”

55 Prepared by Norhamreeza Abdul Hamid


Identifier (User-defined)

 To initialize a variable means to assign it a value when it is


defined:
int length = 18;
 Can initialize some or all variables:
int length = 18, width = 5, area;

56 Prepared by Norhamreeza Abdul Hamid


Identifier (User-defined)

 An assignment statement uses the “=” operator to store a value


in a variable.
item = 55;
 This statement assigns the value 55 to the item variable.
 The variable receiving the value must appear on the left side of
the “=” operator.
 This will NOT work:
//ERROR!!
55 = item;

57 Prepared by Norhamreeza Abdul Hamid


Identifier (User-defined)

Initialize
variable

Interactive
variable

58 Prepared by Norhamreeza Abdul Hamid


Comment
 Statement in program code that will be ignored by
compiler
 Differs in terms of colour : blue

59 Prepared by Norhamreeza Abdul Hamid


Comment
 Intended for persons reading the source code of the
program:
 Indicate the purpose of the program
 Describe the use of variables
 Explain complex sections of code
 Comprise of:
 Single Line
 Multi Line

60 Prepared by Norhamreeza Abdul Hamid


Single Line Comments
 Begin with // through to the end of line:
int length = 18; // length in inches
int width = 4; // width in inches
int area; // calculated area

calculate rectangle area


area = lentgh * width

61 Prepared by Norhamreeza Abdul Hamid


Multi Line Comments
 Begin with /* end with */
 Can span multiple lines:
/* this is a multi-line
comment
*/
 Can begin and end on the same line:
int area; /* calculated area */

62 Prepared by Norhamreeza Abdul Hamid


Common Programming Errors

63 Prepared by Norhamreeza Abdul Hamid


1 Syntax Error
Error occurred during compilation
normally due to syntax problem

 Misplaced else.
 Declaration sytanx error
 Undefined symbol ‘_main’ in module.
 Statement missing in function main()

64 Prepared by Norhamreeza Abdul Hamid


2 Logic Error
Error occurred due to inappropriate output.
Programming mistake.
Not detected during compilation.

65 Prepared by Norhamreeza Abdul Hamid


3 Run-time Error
Error occurred due to wrong user input.
User’s mistake.
System would either display alert message
or hang.

66 Prepared by Norhamreeza Abdul Hamid


67 Prepared by Norhamreeza Abdul Hamid
Determine whether the following identifiers is valid or invalid.
Give reason for invalid cases.
1) Parit Raja
2) 20thCentury
3) int
4) INTEGER
5) copy.jer
6) Reservedword_10
7) totalSales$
8) markah_pelajar
9) jam*kredit
10) printf
68 Prepared by Norhamreeza Abdul Hamid
Write a suitable variable declaration for each of the following statement:
i. Salary of an employee
ii. Student’s mark for programming subject
iii. ATM pin number
iv. Phone number
v. Price of one item
vi. IC number with – e.g: 990930-14-9999
vii. Student name
viii. Your email password

69 Prepared by Norhamreeza Abdul Hamid


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

70 Prepared by Norhamreeza Abdul Hamid


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

Mrs Sue 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 Sue gives to the
student?

71 Prepared by Norhamreeza Abdul Hamid


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.

72 Prepared by Norhamreeza Abdul Hamid


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

Uncle Rahman wants to buy 5 tins of paint from


Maria’s shop. The price of each tin of the paint is
RM 15.60. Calculate the price which Uncle Rahman have
to pay for all the tin of paints he bought.

73 Prepared by Norhamreeza Abdul Hamid

You might also like