BIC 10204 Chapter 3 Basic Structure in Programming
BIC 10204 Chapter 3 Basic Structure in Programming
CHAPTER 3
Basic Structure in
Programming
Basic Structure in
Chap 3
Programming
CHAPTER 3
Data Types
Variable
Declarations
Basic Structure in
Chap 3
Programming
Translated to
Preprocess
Compile
Link
Load
Execute
Basic Structure in
Chap 3
Programming
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 A:\ etc.
Editor?
Editor or text editor is a type
of program used for editing
plain text files.
Basic Structure in
Chap 3
Programming
Turbo C
editor
(free)
Basic Structure in
Chap 3
Programming
Vim
editor
(free)
Basic Structure in
Chap 3
Programming
Phase 2: Preprocessing
Basic Structure in
Chap 3
Programming
Phase 3: Compiling a Program
Dialog box in
Turbo C editor
shows
compiling
process.
Basic Structure in
Chap 3
Programming
Phase 3: Compiling a Program
Basic Structure in
Chap 3
Programming
Phase 4: Linking
Basic Structure in
Chap 3
Programming
Phase 6: Executing
Terms Description
Machine language Binary number codes understood by a specific
CPU.
Basic Structure in
Chap 3
Programming
Common Programming Errors
Error
(bugs)
Run-time
Errors
Syntax Errors
Logic Errors
Basic Structure in
Chap 3
Programming
Common Programming Errors
1 Syntax Error
Error occurred during compilation
normally due to syntax problem
Misplaced else.
Declaration syntax error
Undefined symbol ‘_main’ in module.
Statement missing in function main()
Basic Structure in
Chap 3
Programming
Common Programming Errors
2 Logic Error
Basic Structure in
Chap 3
Programming
Computer Program
WHAT
HOW
UNDERSTAND THE
SYNTAX AND FORMAT
STRUCTURE OF
PROGRAMMING
Basic Structure in
Chap 3
Programming
C Basic Structure
Program block
C preprocessor directive
components:
1. Preprocessor
main function
directive
{
2. Program body
//Identifiers/Variables
3. Main function
//C statements
4. Identifiers/Variable
}
5. C statements
6. Comment
Basic Structure in
Chap 3
Programming
Preprocessor Directive
Basic Structure in
Chap 3
Programming
Preprocessor Directive
Format:
#include <header file> or #include “user defined files”
Example
#include <stdio.h>
#include <conio.h>
#include “jam.h”
Basic Structure in
Chap 3
Programming
Preprocessor Directive
Example:
Called from
#include <stdio.h> standard library
Basic Structure in
Chap 3
Programming
Standard
Library Functions contains standard instructions
conio.h clrscr(),putch().getc().dll
math.h sqrt(),pow(), log(),dll
Basic Structure in
Chap 3
Programming
Basic Structure in
Chap 3
Programming
Preprocessor Directive
Format:
Example
#define MAX 100
#define “jam.h”
Basic Structure in
Chap 3
Programming
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 }
Basic Structure in
Chap 3
Programming
Main function
void main( )
{ …………..}
#include <stdio.h>
void main()
{
}
Basic Structure in
Chap 3
Programming
C Statement
Instructions to be executed by computers
Every statements must be ended with semicolon
Function Declaration
statement statement
Control Types
Input/Output
statement statement
Compound
statement
Basic Structure in
Chap 3
Programming
Comment
Statement in program code that will be ignored by compiler
Differs in terms of colour : grey
To increase
program readability To document
a program
Function
As a future
To provide references
additional
information
Basic Structure in
Chap 3
Programming
Reserved Word
Standard/special word in standard library
Contain special meaning understood by compiler
Rules
Case –sensitive
Cannot be used as identifier
Must be written in
or variables
small case
Basic Structure in
Chap 3
Programming
Reserved Word
Example:
void int
Refer to the function that will not
The acronym for integer
return any value
Basic Structure in
Chap 3
Programming
Identifier
Standard User-defined
identifier Type identifier
Basic Structure in
Chap 3
Programming
Identifier
printf() scanf()
puts() gets()
Basic Structure in
Chap 3
Programming
Identifier
Name given to the declaration of
User-defined data to be used in program
Refer to the storage name
identifier Store data values/result/output
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
Basic Structure in
Chap 3
Programming
Identifier
Valid identifiers
‘Sixsense’ void
WHY?
WHY? WHY?
Basic Structure in
Chap 3
Programming
Identifier
Basic Structure in
Chap 3
Programming
Declaration format: 1
const data_type const_name = const_value;
Constant
Reserved word const float pi = 3.142; Value
Basic Structure in
Chap 3
Programming
Declaration format: 2
#define const_name const_value;
#define pi 3.142;
Reserved word
Basic Structure in
Chap 3
Programming
Example of constant:
#define minimum 0;
#define MAX 100;
Variable
Refer to the name of one cell in computer
storage
Variable’s value can be modified/changed
during execution
Declaration Format:
data_type variable_name;
Basic Structure in
Chap 3
Programming
Declaration Example
void main()
{
const float pi =3.142;
Constant declaration
int bilangan;
float berat;
char abjad;
Variable declaration
}
Basic Structure in
Chap 3
Programming
Variable and constant declaration example:
void main()
{
int bilangan, bil, bilang;
float berat, kg;
char A;
}
Basic Structure in
Chap 3
Programming
Method to give/assign value to variable
Interactive Initialization
Basic Structure in
Chap 3
Programming
Assigning value to variables
#include <stdio.h>
void main()
{
Initialize a variable
int number = 10;
float weight;
weight = 60.00; Interactive
printf(“Enter the value of number :”);
scanf(“%d”,&number);
Types
Integer Character
Floating
point
Basic Structure in
Chap 3
Programming
Data Types
Represents any round number with +/-
values.
Integer Divided into short and long integer.
Reserved word for integer – int
Valid until 5 places of integer number.
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;
Basic Structure in
Chap 3
Programming
Data Types
Floating Represents any floating point numbers +/-
Reserved word– double /float
number
Example:
height is used to represent the student’s height
between 150 cm and 180 cm. The declaration for the
variable is as follow:
float height;
Basic Structure in
Chap 3
Programming
Data Types
Example:
gender is used to represent the gender of a student.
The declaration for the variable is as follow:
char gender;
Basic Structure in
Chap 3
Programming
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
Basic Structure in
Chap 3
Programming
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. Bus seat number
vii. Student name
Basic Structure in
Chap 3
Programming
y = 2x + a - 6
Basic Structure in
Chap 3
Programming
Basic Structure in
Chap 3
Programming