0% found this document useful (0 votes)
10 views11 pages

Lecture 1 Introduction

The document provides an overview of computer programming, defining key concepts such as algorithms, programming languages, and the process of coding. It discusses the characteristics of the C programming language, types of programming languages, and the role of Integrated Development Environments (IDEs) and language processors. Additionally, it outlines common programming errors and provides a simple example of a C program.

Uploaded by

abrahammwenda083
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views11 pages

Lecture 1 Introduction

The document provides an overview of computer programming, defining key concepts such as algorithms, programming languages, and the process of coding. It discusses the characteristics of the C programming language, types of programming languages, and the role of Integrated Development Environments (IDEs) and language processors. Additionally, it outlines common programming errors and provides a simple example of a C program.

Uploaded by

abrahammwenda083
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

INTRODUCTION

Definitions
A computer program is just a collection of the instructions necessary to solve a specific
problem.
The basic operations of a computer system form what is known as the computer’s instruction
set.
And the approach or method that is used to solve the problem is known as an algorithm.

Programming

known as coding or software development, is the process of creating a set of instructions that
tell a computer how to perform a task. These instructions are written in a programming
language, which is a formalized set of rules and syntax for expressing algorithms and
computations

Key aspects of programming include:

Algorithm Design: Creating a step-by-step plan or set of rules for solving a specific problem.
Algorithms are essential in programming as they outline the logical flow of a program.

Syntax: Writing code according to the rules and syntax of a programming language. Each
programming language has its own set of rules for expressing instructions, and programmers
must follow these rules to create functional and correct programs.

Debugging: Identifying and fixing errors or bugs in the code. Debugging is an essential skill in
programming, as errors can prevent a program from running correctly.

Testing: Verifying that the program behaves as expected under various conditions. Testing
helps ensure that the program meets its specifications and works correctly in different scenarios.

Problem Solving: Programming often involves breaking down complex problems into smaller,
more manageable parts and solving them through code. This requires analytical thinking and
problem-solving skills.

Data Structures and Algorithms: Understanding and implementing data structures (like
arrays, linked lists, trees) and algorithms (methods for solving problems or performing tasks
efficiently) are crucial for effective programming.
Introduction to C

C is a general-purpose programming language created by Dennis Ritchie at the Bell


Laboratories in 1972.

It is a very popular language, despite being old.

C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Characteristics of C

 It is one of the most popular programming language in the world


 If you know C, you will have no problem learning other popular programming languages
such as Java, Python, C++, C#, etc, as the syntax is similar
 C is very fast, compared to other programming languages, like Java and Python
 C is very versatile; it can be used in both applications and technologies
 So
Programming Language?

A programming language is a computer language that is used by programmers (developers)


to communicate with computers. It is a set of instructions written in any specific language ( C,
C++, Java, Python) to perform a specific task.

Types of programming language


1. Low-level programming language

Low-level language is machine-dependent (0s and 1s) programming language. The processor
runs low- level programs directly without the need of a compiler or interpreter, so the programs
written in low-level language can be run very fast.

Low-level language is further divided into two parts -


i. Machine Language

Machine language is a type of low-level programming language. It is also called as machine


code or object code. Machine language is easier to read because it is normally displayed in
binary or hexadecimal form (base 16) form. It does not require a translator to convert the
programs because computers directly understand the machine language programs.

The advantage of machine language is that it helps the programmer to execute the programs
faster than the high-level programming language.

ii. Assembly Language

Assembly language (ASM) is also a type of low-level programming language that is designed
for specific processors. It represents the set of instructions in a symbolic and human-
understandable form. It uses an assembler to convert the assembly language to machine
language.

The advantage of assembly language is that it requires less memory and less execution time to
execute a program.

2. High-level programming language

High-level programming language (HLL) is designed for developing user-friendly software


programs and websites. This programming language requires a compiler or interpreter to
translate the program into machine language (execute the program).

The main advantage of a high-level language is that it is easy to read, write, and maintain.

High-level programming language includes Python, Java, JavaScript, PHP, C#, C++,
Objective C, Cobol, Perl, Pascal, LISP, FORTRAN, and Swift programming language.

A high-level language is further divided into three parts -

i. Procedural Oriented programming language

Procedural Oriented Programming (POP) language is derived from structured programming and
based upon the procedure call concept. It divides a program into small procedures
called routines or functions.

Procedural Oriented programming language is used by a software programmer to create a


program that can be accomplished by using a programming editor like IDE, Adobe
Dreamweaver, or Microsoft Visual Studio.

The advantage of POP language is that it helps programmers to easily track the program flow
and code can be reused in different parts of the program.
The advantage of POP language is that it helps programmers to easily track the program
flow and code can be reused in different parts of the program.

Example: C, FORTRAN, Basic, Pascal, etc.

ii. Object-Oriented Programming language

Object-Oriented Programming (OOP) language is based upon the objects. In


this programming language, programs are divided into small parts called objects. It is used
to implement real-world entities like inheritance, polymorphism, abstraction, etc in the program
to makes the program resusable, efficient, and easy-to-use.

The main advantage of object-oriented programming is that OOP is faster and easier to execute,
maintain, modify, as well as debug.

Note: Object-Oriented Programming language follows a bottom-up approach.

Example: C++, Java, Python, C#, etc.

iii. Natural language

Natural language is a part of human languages such as English, Russian, German, and
Japanese. It is used by machines to understand, manipulate, and interpret human's language. It is
used by developers to perform tasks such as translation, automatic summarization, Named
Entity Recognition (NER), relationship extraction, and topic segmentation.

The main advantage of natural language is that it helps users to ask questions in any subject and
directly respond within seconds.

3. Middle-level programming language

Middle-level programming language lies between the low-level programming language and
high-level programming language. It is also known as the intermediate programming
language and pseudo-language.

A middle-level programming language's advantages are that it supports the features of high-
level programming, it is a user-friendly language, and closely related to machine language and
human language.

Example: C, C++, language


Integrated Development Environments (IDE)
The process of editing, compiling, running, and debugging programs is often
managed by a single integrated application known as an Integrated Development
Environment, or IDE for short.
An IDE is a windows-based program that allows us to easily manage large software programs,
edit files in windows, and compile, link, run, and debug programs.
Popular IDE's include Code::Blocks, Eclipse, and Visual Studio. These are all free, and they can
be used to both edit and debug C code.

Language Processors: Assembler, Compiler and Interpreter


Language Processors –
Assembly language is machine dependent yet mnemonics that are being used to represent
instructions in it are not directly understandable by machine and high Level language is
machine independent.
A computer understands instructions in machine code, i.e. in the form of 0s and 1s. It is a
tedious task to write a computer program directly in machine code. The programs are written
mostly in high level languages like Java, C++, Python etc. and are called source code.
These source codes cannot be executed directly by the computer and must be converted into
machine language to be executed.
Hence, a special translator system software is used to translate the program written in high-level
language into machine code is called Language Processor and the program after translated into
machine code (object program / object code).
The language processors can be any of the following three types:
1. Compiler –
The language processor that reads the complete source program written in high level
language as a whole in one go and translates it into an equivalent program in machine
language is called as a Compiler.
Example: C, C++, C#, Java
In a compiler, the source code is translated to object code successfully if it is free of
errors. The compiler specifies the errors at the end of compilation with line numbers
when there are any errors in the source code. The errors must be removed before the
compiler can successfully recompile the source code again.>
Source code
(high level language)- compiler-object code (machine language)

2. Assembler –
The Assembler is used to translate the program written in Assembly language into
machine code. The source program is a input of assembler that contains assembly
language instructions. The output generated by assembler is the object code or machine
code understandable by the computer.
Source code
(assembly language)- Assembler-object code(machine language)

3. Interpreter –
The translation of single statement of source program into machine code is done by
language processor and executes it immediately before moving on to the next line is
called an interpreter. If there is an error in the statement, the interpreter terminates its
translating process at that statement and displays an error message. The interpreter moves
on to the next line for execution only after removal of the error. An Interpreter directly
executes instructions written in a programming or scripting language without previously
converting them to an object code or machine code.
Example: Perl, Python and Matlab.

Difference between Compiler and Interpreter –


COMPILER INTERPRETER

A compiler is a program which coverts the interpreter takes a source program and

entire source code of a programming language runs it line by line, translating each line

into executable machine code for a CPU. as it comes to it.

Interpreter takes less amount of time to

Compiler takes large amount of time to analyze analyze the source code but the overall

the entire source code but the overall execution execution time of the program is

time of the program is comparatively faster. slower.

Compiler generates the error message only after

scanning the whole program, so debugging is Its Debugging is easier as it continues

comparatively hard as the error can be present translating the program until the error

anywhere in the program. is met


COMPILER INTERPRETER

Generates intermediate object code.

No intermediate object code is generated.

Examples: C, C++, Java Examples: Python, Perl

Programming Errors

Error is an illegal operation performed by the user which results in abnormal working of the
program.
Programming errors often remain undetected until the program is compiled or executed. Some
of the errors inhibit the program from getting compiled or executed. Thus errors should be
removed before compiling and executing.
The most common errors can be broadly classified as follows
Syntax errors: Errors that occur when you violate the rules of writing C/C++ syntax are
known as syntax errors. This compiler error indicates something that must be fixed before the
code can be compiled. All these errors are detected by compiler and thus are known as compile-
time errors.
Most frequent syntax errors are:
 Missing Parenthesis (})
 Printing the value of variable without declaring it
 Missing semicolon like this

 // C program to illustrate
 // syntax error
 #include<stdio.h>
 void main()
 {
 int x = 10;
 int y = 15;
 printf("%d", (x, y)) // semicolon missed
 }
Run-time Errors : Errors which occur during program execution(run-time) after successful
compilation are called run-time errors. One of the most common run-time error is division by
zero also known as Division error. These types of error are hard to find as the compiler doesn’t
point to the line at which the error occurs.
// C program to illustrate
// run-time error
#include<stdio.h>
void main()
{
int n = 9, div = 0;

// wrong logic
// number is divided by 0,
// so this program abnormally terminates
div = n/0;

printf("resut = %d", div);


}
Error:
warning: division by zero [-Wdiv-by-zero]
div = n/0;

Linker Errors: These error occurs when after compilation we link the different object files
with main’s object using Ctrl+F9 key(RUN). These are errors generated when the executable of
the program cannot be generated. This may be due to wrong function prototyping, incorrect
header files. One of the most common linker error is writing Main() instead of main().

// C program to illustrate
// linker error
#include<stdio.h>

void Main() // Here Main() should be main()


{
int a = 10;
printf("%d", a);
}
Error:
(.text+0x20): undefined reference to `main'

Logical Errors : On compilation and execution of a program, desired output is not obtained
when certain input values are given. These types of errors which provide incorrect output but
appears to be error free are called logical errors. These are one of the most common errors done
by beginners of programming.
These errors solely depend on the logical thinking of the programmer and are easy to detect if
we follow the line of execution and determine why the program takes that path of execution.
Semantic errors : This error occurs when the statements written in the program are not
meaningful to the compiler.
// C program to illustrate
// semantic error
void main()
{
int a, b, c;
a + b = c; //semantic error
}
Error
error: lvalue required as left operand of assignment
a + b = c; //semantic error

First c program
myfirstprogram.c

Let's create our first C file.

Open Codeblocks and go to File > New > Empty File.

Write the following C code and save the file as myfirstprogram.c (File > Save File as):

#include <stdio.h>

int main() {
printf("Hello World!");
return 0;
}
Syntax

You have already seen the following code a couple of times in the first chapters. Let's break it
down to understand it better:

Example
#include <stdio.h>

int main() {
printf("Hello World!");
return 0;
}
Explanation

Line 1: #include <stdio.h> is a header file library that lets us work with input and output
functions, such as printf() (used in line 4). Header files add functionality to C programs.

Don't worry if you don't understand how #include <stdio.h> works. Just think of it as
something that (almost) always appears in your program.

Line 2: A blank line. C ignores white space. But we use it to make the code more readable.

Line 3: Another thing that always appear in a C program, is main(). This is called a function.
Any code inside its curly brackets {} will be executed.

Line 4: printf() is a function used to output/print text to the screen. In our example it will
output "Hello World".

Note that: Every C statement ends with a semicolon ;

Note: The body of int main() could also been written as:
int main(){printf("Hello World!");return 0;}

Remember: The compiler ignores white spaces. However, multiple lines makes the code more
readable.

Line 5: return 0 ends the main() function.

Line 6: Do not forget to add the closing curly bracket } to actually end the main function.

You might also like