R D H Dulshini Jayaratne
BSc.(hons) in ITB (Gold Medal)–Coventry University –UK
(NIBM)
Lecture - 04
•Any questions before
start?
•Are you ready?
•Are you sure?
•Let’s start….
What is a Computer Program ..?
• A Computer program is a Set of
instructions that tells a computer to
do various task.
• Programs are written to solve
problems or perform tasks on a
computer.
• All programs are written in some
kind of programming language.
3
Computer Program contd..
• Computer programs come in all shapes and sizes,
from the very simplest ones to extremely large,
integrated systems with multiple functionality
• Controlling factories
• Big business accounts
• Banks
• World markets
• Scientific research, etc.
4
Computer Program contd..
• Programming languages can be mainly divided into :
1.Low-level programming languages
▪Machine Language
▪Assembly Language
2.High-level programming languages
▪C , C++ , C# ,Java , Python etc..
Low – Level languages
1.Machine Language
➢Consist of 1 s and 0 s
➢Machine dependent
➢Computer can directly understand its own
machine language
➢Tedious and error prone for programmers
Low – Level languages contd..
2.Assembly Language
➢English-like abbreviations called mnemonics
formed the basis
➢Clearer to humans but incomprehensible to
computers
➢Need to translate to machine language using
translator programs called assemblers
High – Level languages
▪Instructions look almost like English and
mathematical notations
▪Substantial tasks can be accomplished from a single
statement.
▪Easy for humans to understand.
▪Translator programs convert high-level programming
languages into machine language
Program Translators
▪Converting the source-code into object-code.
Object
Source
Translator code
code (Machine
Language code)
Source Code
• Consists of the programming statements that are created by
a programmer with a text editor or a visual programming
tool and then saved in a file.
Object Code / Machine code
• A collection of binary digits or bits that the computer reads
and interprets.
• Machine language is the only language a computer is
capable of understanding.
Program Translators
▪There are three types of computer code translators.
1. Compiler
2. Interpreter
3. Assembler
1. Compiler
➢Compilers convert high-level language programs to
machine language.
2.Interpreter
➢Interpreters execute high-level language programs
line by line.
3.Assembler
➢Assemblers convert assembly language programs to
machine language.
Program Errors
▪There are two types of computer program errors..
1. Syntax errors
2. Run-time errors
Syntax errors
• Occur when the program is not written according to the
rules of the language.
Run-time errors
• Occur with programs that, even though they are
syntactically correct, they might execute instructions that
violate some other rules of the language or some other set
condition.
➢Eg : Divisible by 0
Integrated Development Environment-
IDE
A software program that is designed to help programmers
and developers build software.
Most IDEs include: -
➢A source code editor
➢A compiler
➢Build automation tools
➢A debugger
C language
History of C..
• C language was evolved from two previous
languages, BCPL and B by Dennis Ritchie at
Bell Laboratories in 1972.
• C initially became widely known as the
developing language of the UNIX operating
system.
• C99 and C11 are revised standards for C
programming language that refines and
expands the capabilities of C
C programming language
C is a:
- Procedural language
- Structured Language
- High level Language
- Machine independent
- Easy to learn
- It can be compiled on a variety of computer
platforms.
Alphabets Constants
Digits Variables Instructions Programs
Symbols Keywords
Basic Structure of C program
#include<stdio.h>
int main( )
{
// program instructions
return 0;
}
Basic Structure of C program
#include<stdio.h> Header files
int main( ) Main function
{
// program instructions Statements
Body
return 0; Return value
}
Simple C program
Source Code :
/*This is my first program in C.
This program displays a message for the user.*/
#include<stdio.h>
int main( ) //main function begins Output:
{
Welcome to C
printf(“Welcome to C.”);
return 0;
} //end of the function main
Code Explanation
#include<stdio.h>
• This is a directive to C preprocessor.
• Lines begin with # are processed by the preprocessor
before compiling the program.
• stdio.h is a header file which contains information
about standard input/output library function calls.
Code Explanation contd..
int main( )
• Every C program has this main function, and it begins
executing at main.
• “int” to left of main indicates that the function returns
an integer.
Code Explanation contd..
{}
• Left brace , { , begins the body of every function.
• Functions ends with a corresponding right brace}.
• The portion of the program between the braces is
called a block.
Code Explanation contd..
printf(“Welcome to C”);
• Entire line is called a statement.
• It instructs the computer to perform an action.
• A statement must end with a semicolon(;).
• This statement prints a string of characters marked by
the quotation marks on the screen
Display/ Print command
Syntax used :
printf(“statement to be print”);
Example:
printf(“This is my First program”);
Code Explanation contd..
return 0;
• Included at the end of every main function.
• It is used to exit the main function and the value 0
indicates a successful termination.
Code Explanation contd..
Comments
• Comments are used to document programs and it improves the
program readability.
• C compiler ignores comments.
• Line comments begin with //(single line comment) and continue for
the rest of the line.
• /* … */ multi-line comments can also be used.
• Everything from /* to */ is a comment.
Code Explanation contd..
Single- line comments
//main function begins
Multi- line comments
/*This is my first program in C.
This program displays a message for the user.*/
Save C File
We are saving C file using .c extension.
file_name.c
First_program.c
Let’s code..
Program 1
Write a C program to display as “Hello World”.
Program 1: Code
#include<stdio.h>
int main()
{
printf("Hello World");
return 0;
}
Program 2
Write a C program to display your name.
Program 2 - Code
Write a C program to display your name.
#include<stdio.h>
int main()
{
printf("My name is Rose");
return 0;
}
Program 3
Write a C program to display your name and age.
Program 3 - Code
Write a C program to display your name and age.
#include<stdio.h>
int main()
{
printf("My name is Rose\n");
printf("I'm 26 yrs old");
return 0;
}
Escape character
• The backslash (\) is called escape character.
\n - means newline
\t – horizontal tab
\\ - backslash – insert a backslash character in a
string
\” – Double quote - insert a double-quote
character in a string
Program 3
Write a C program to display below statements.
Welcome to NIBM..!!
My name is …………….(your name)
I am a student of DSE 24.2 batch
Program 3
Write a C program to display below statements.
#include<stdio.h>
int main()
{
printf("Welcome to NIBM..!!\nMy nameis
Rose\nI’m a student of DSE 24.1");
return 0;
}
Data types in C
What is a Data type?
• An attribute associated with a piece of data that
tells a computer system how to interpret its value.
• Declarations of variables.
• There are four basic data types can be used in C.
1. Integer
2. Float
3. Character
4. Double
Data types in C
1.Interger
• Used to store the integer numbers (including both
positive and negative numbers)
• Memory size = 2 byte
Declaration of interger variables ;
int var_name;
int num ;
int num=0;
Data types in C
2.Float
• Used to store floating –point values (decimal values)
• Memory size = 4 byte
Declaration of float variables ;
float var_name;
float salary ;
float salary = 2500.00;
Data types in C
3.Double
• Used to store floating –point values (decimal values)
• Memory size = 8 byte
Declaration of double variables ;
double var_name;
double price;
double price=25.36598;
Data types in C
4.Character
• Used to store only a single character.
• Memory size = 1 byte
Declaration of char variables ;
char var_name;
char grade;
char grade=‘A’;
Defining string variable
In C programming we define string values by using an
array of characters.
char variable_name[size];
Example :
char name[10];
char name[10]=“Dushy”;
Format specifiers of Data types
Data type Format Specifier
int %d
float %f
char %c
double %lf
Char[ ] (string) %s
What is a variable..?
The name of a memory location that we use for storing data.
Data value
Rules for naming a variable
• Must only contain alphabets, digits and underscore.
• Must start with an alphabet or an underscore only. It
cannot start with a digit.
• No whitespaces are allowed within a variable name.
• Variable name must not be any reserved word or a
keyword.
• Variable names are case-sensitive.
• No limit on the length of the variable name.
Syntax of a variable
Declare a variable
Data_type var_name ;
Eg :
int num;
float price;
Syntax of a variable
Assigning a value for a variable
Data_type var_name = value;
Eg :
int num = 2;
float price = 2.36;
char letter =‘Z’;
Note :
When assigning a value for char and string data type the value should be
indicated with in single quotes ( ‘….’)
Activity
Identify related data types and format specifier for
below given data values.
Data value Data type Format Specifier
25
X
23.12
NIBM
23.564789
54
Activity
Identify related data types and format specifier for
below given data values.
Data value Data type Format Specifier
25 int %d
X char %c
23.12 float %f
NIBM string %s
23.564789 double %lf 55
Introduction To C
• Any questions before finish?
• Are you sure?
• Thank you for listening.
56