You are on page 1of 24

CSE 191 – COMPUTER PROGRAMMING

Najia Manjur
Lecturer
Department of Computer Science & Engineering (CSE)
Military Institute of Science & Technology (MIST)
Mirpur, Dhaka – 1216, Bangladesh
Types of Programming language

Machine Language

High Level Language

Special Purpose Language

CSE 191 - Computer Programming 2


Machine Language

 Machine Language – A collection of very detailed,


cryptic instructions that control the computer’s
internal circuitry.
 Natural dialect of the computer.
 Problems:
 It is very cumbersome to work with.
 Every different type of computer has its own unique
instruction set.
 Example: Assembly Language

CSE 191 - Computer Programming 3


High Level Language
High Level Language – Instruction set is more
compatible with human languages and human
thought processes.
Example: C, Pascal, Fortran
Advantages:
 A single instruction in high level language is equivalent to
several instructions in machine language.
 Simple
 Uniform Program written in high level
 Portable (Machine independent) language must be translated
into machine language
before execution.
Disadvantage : Slow !!!

CSE 191 - Computer Programming 4


Compiler and Interpreter

Compiler – Translate the entire program into


machine language before executing any of the
instructions. This process is known is
Compilation.

Interpreter – Proceed through a program by


translating and then executing single
instructions or small groups of instructions.
This process is known is Interpretation.

CSE 191 - Computer Programming 5


Compiler and Interpreter

Program written in
Machine language
High-level
program
language
Input Compiler/Interpreter Output
(A computer program)

• The original high-level program is called the Source program.


• The resulting machine-language program is called the Object
program.

CSE 191 - Computer Programming 6


General Characteristics of C
The general characteristics,
 Structured programming language

 Augmented by certain English keyword (e.g. if, else, for)

 It can be used for System programming (e.g. Operating System)


and Application programming (e.g. Software Development)

 Small instruction set

 Extensive library function

 C compilers are available for computer of all sizes

 Highly portable

CSE 191 - Computer Programming 7


Structure of C program
 Every C program consists of one or more modules
called functions.

 One of the functions must be called main

 Program execution must begin from main function

 Other user defined function must be defined either


ahead of or after main

CSE 191 - Computer Programming 8


Composition of a Function
 A function must contain,
 A function heading consists of function name, then followed by an optional list of
arguments enclosed in parentheses
 A list of argument declarations
 A compound statement Function Argument
Name List

Function
function_name (arg1, ar2 … argn) Heading
{
Statement 1
Statement 2
Compound
return data; Statement
}

CSE 191 - Computer Programming 9


Composition of a Function
 A function must contain,
 A function heading consists of function mane, then followed by an optional
list of arguments enclosed in parentheses
 A list of argument declarations
 A compound statement

function_name (arg1, ar2 … argn) main( )


{ {
Statement 1
Statement 2
printf(“Hello”);
return data; return 0;
} }

CSE 191 - Computer Programming 10


Algorithms
 A set of steps to accomplish a task
 In Computer Science, an algorithm is a set of steps in a
computer program to accomplish a task

Starts with input data

Do complex calculations

Stop when we find answer

CSE 191 - Computer Programming 11


Flowchart

 A flowchart is a diagram that
 represents an algorithm  
 represents the workflow or process,
 shows the order of executing the processes.

CSE 191 - Computer Programming 12


Flowchart

CSE 191 - Computer Programming 13


A Sample Program

#include<stdio.h>
main()
{
int number = 100;
float sum = 57.26 + 100.1
printf(“%d”,number);
printf(“%.2f”,sum);
}
CSE 191 - Computer Programming 14
Variable
 A variable is a data name used to store a data value.
 They can be modified during the program.
 All variables must be declared before they can be used.
 Declaration of Variables:
 It tells the compiler what the variable name is.
 It specifies what type of data the variable will hold.
 Format :
data_type var_name;
data_type var_name1, var_name2, var_name3, ………….. ;

CSE 191 - Computer Programming 15


A Sample Program

#include<stdio.h>
main()
{
int number; Type declarations
Assigning value
number = 100;
float sum = 57.26 + 100.1
printf(“%d”,number);
printf(“%.2f”,sum);
}
CSE 191 - Computer Programming 16
Identifiers
Refers to the name of variables, functions, arrays.

 Must start with letter or ‘_’.


 In an identifier, middle characters are letter, digits, or
underscore( ‘_’).
 Must not match with keywords.
 Maximum 31 characters.
 In an identifier, upper & lowercase are treated as distinct. Ex.
count, Count & COUNT are three separate identifiers.

CSE 191 - Computer Programming 17


Keyword
C Keywords
• have specific meanings
• cannot be changed

C has 32 keywords

Keywords

auto break case char const continue default do

double else enum extern float for goto if

int long register return short signed sizeof static

struct switch typedef union unsigned void volatile while

CSE 191 - Computer Programming 18


Data Types
Data Type

Type Characteristics Typical Minimal Range Examples


size in
Bytes
int Numeric / 2 Byte -32,768 to 32,767 10, -101, 15
integer
float Fractional 4 Byte 1E-37 to 1E+37 with 10.568960
numbers 6 digits of precision.
double Fractional 8 Byte 1E-37 to 1E+37 with 11.57896589
numbers 10 digits of 12
precision.
char Individual letters , 1 Byte -128 to 127 A, Y
symbols, digits.
void No data type
CSE 191 - Computer Programming 19
Format Specifier
Format Specifier

Data Type Format Specifier Use


int %d To input/output Integer value
float %f To input/output Fractional
(float) value
double %lf To input/output Fractional
(double) value
char %c To input/output character

CSE 191 - Computer Programming 20


Backslash Character Constant/
Escape Sequence
quences

Code Meaning
\n New Line
\b Back Space
\t Horizontal Tab
\v Vertical Tab
\0 NULL
\” Double quote
\’ Single quote
\\ Backslash
\a Alert
\? Question mark

CSE 191 - Computer Programming 21


scanf()
 giving values to variables through keyboard.
variable name’s address

scanf(“control string”,&variable1,&variable2,
…); format specifier of data being received

scanf(“%d”,&variable);

CSE 191 - Computer Programming 22


Reference
 Programming with C
 Chapter 1 : 1.4, 1.5(Introduction to C, Structure of a C Program)
 Chapter 2 : 2.1, 2.2, 2.3, 2.4, 2.5(Variables), 2.6, 2.7, 2.8, 2.9

CSE 191 - Computer Programming 23


End of Slides

CSE 191 - Computer Programming 24

You might also like