Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
21 views

Computer Programming IBA - 3

Uploaded by

uditha tharuka
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Computer Programming IBA - 3

Uploaded by

uditha tharuka
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

COMPUTER

PROGRAMMING

Bachelor of Information Communication


Technology ( Hons)
Uditha Landekumbura
Bsc. in Software Engineering
SCQF Level 7,8,9
EQF Level 5,6
Chapter Outline
1. Introduction

2. Variables, Data Types, I/O and Operators

3. Decision Making

4. Repetition Structures

5. C Input Functions

6. C Operators

7. File Handling Functions in C


Computer Languages
In order to communicate with the computer user also needs to have a language
that should be understood by the computer. For this purpose, different
languages are developed for performing different types of work on the
computer. Basically, languages are divided into two categories

according to their interpretation.

1. Low Level Languages (Machine Language, Assembly Language

2. High Level Languages (Algebraic Formula-Type Processing, Business Data


Processing, Object Oriented Programming Language, Visual programming
language)
Origin of C

Predecessors of C

BCPL (Basic Combined Programming Language): Developed in the 1960s, BCPL


influenced C by providing a simple syntax and structure for systems
programming.

B Language: Created by Ken Thompson in 1969, B was a direct predecessor of C


and was used for writing utilities for the UNIX operating system. However, it
had limitations that prompted further development.
Development of C

In the early 1970s, Dennis Ritchie and his colleagues at Bell Labs sought to
overcome the limitations of the B language. In 1972, Ritchie developed C as an
improvement over B, adding data types and other features to support more complex
programming tasks.

UNIX Operating System: C was initially designed to rewrite the UNIX operating
system, which had been originally developed in assembly language. This transition
made UNIX more portable and easier to maintain, as C allowed for more abstract
coding.
Reasons to Use C
01 Industry Presence

● Widely used in various domains, including systems programming, embedded


systems, and application development.
● A foundational language in many modern programming environments and
applications.

02 Middle Level Language

● Acts as a bridge between high-level and low-level programming languages.


● Provides features of both assembly language (low-level) and higher-level
languages, allowing direct manipulation of hardware while maintaining code
readability.
03 Portability

● Programs written in C can be compiled and run on different hardware platforms


with minimal changes.
● Standardized (ANSI C) to ensure consistent behavior across various systems.

04 Flexibility

● Allows developers to write programs in a variety of programming styles


(procedural, modular, etc.).
● Supports low-level memory management, giving programmers control over
system resources.
05 Malleable

● Code can be easily modified and extended, making it adaptable to changing


requirements.
● Libraries and frameworks built on C can be integrated with other languages.

06 Speed

● C is known for its high performance and efficiency due to low-level access to
memory and system resources.
● Ideal for performance-critical applications, such as operating systems, game
engines, and real-time systems.
Advantages of C

● High performance and efficiency


● Low-level memory access
● Portability across platforms
● Extensive use in systems programming
● Rich set of built-in operators
● Structured programming capabilities
● Strong community support and resources
● Flexibility in programming styles
● Malleable and easy to modify code
Disadvantages of C

● Lack of built-in error handling


● Manual memory management (risk of memory leaks)
● Complex syntax compared to modern languages
● No support for object-oriented programming (in standard C)
● Limited standard libraries
● Potential for undefined behavior
● Steeper learning curve for beginners
● Not ideal for high-level application development without additional
libraries
Algorithm

An algorithm is a description of a procedure which terminates with a result.


Algorithm is a step-by-step method of solving a problem.

Pseudocode
is an artificial and informal language that helps programmers develop algorithms.
Pseudocode is very similar to everyday English.
Example 1: Write an algorithm to determine a student's final grade and indicate
whether it is passing or failing. The final grade is calculated as the average of four
marks.

Pseudocode::

● Input a set of 4 marks


● Calculate their average by summing and dividing by 4
● if average is below 50
○ Print “FAIL”
○ else
○ Print “PASS”
Detailed Algorithm :

Step 1: Input all marks M1,M2,M3,M4

Step 2: Calculate Average (M1+M2+M3+M4)/4

Step 3: if (Average < 50) then

Should show “FAIL”

otherwise

Should show “PASS”

End process
Flowcharts

The pictorial representation of algorithm is called flowchart.

flow chart helps to understand the program easily.

As different symbols are used to specify the type of operation performed, it is


easier to understand the complex programs with the help of flowcharts.
Algorithm to find whether a number even or odd:

Step1: Begin Step1: START

Step2: Take a number Step2: Read num

Step3: if the number is divisible by 2 then Step3: if(num%2=0) then

Show that number is even print “num is even”

otherwise Show that number is odd else

Step4: End print “num is odd”

Step4: STOP

(Algorithm in natural language) (pseudo code)


Creating and Running Programs

There are four steps in this process.

1. Writing and editing the program using Text editor (source code).

2. Compile the program using any C compiler.(.bak file)

3. Linking the program with the required library modules(object file)

4. Executing the program. (.Exe file)


First C Program #include <stdio.h> includes the standard input output
library functions. The printf() function is defined in
stdio.h .
1. #include <stdio.h> #include <conio.h> includes the console input output
library functions. The getch() function is defined in
2. #include <conio.h>
conio.h file.
3. void main(){ void main() The main() function is the entry point of
every program in c language. The void keyword
4. printf("Hello C Language");
specifies that it returns no value.
5. getch();
printf() The printf() function is used to print data on the
console.
6. }
getch() The getch() function asks for a single character.
Until you press any key, it blocks the screen.
C TOKENS
The smallest individual units are known as tokens. C has six types of tokens.

1: Identifiers

2: Keywords

3: Constants

4: Strings

5: Special Symbols

6: Operators
Identifiers
Identifiers refer to the names of variables, constants, functions and arrays. These are
user-defined names is called Identifiers. These identifier are defined against a set of
rules.

Rules for an Identifier

1. An Identifier can only have alphanumeric characters( a-z , A-Z , 0-9 ) and underscore(
_).

2. The first character of an identifier can only contain alphabet( a-z , A-Z ) or
underscore ( _).
3. Identifiers are also case sensitive in C. For example name and Name are two
different identifier in C.

4. Keywords are not allowed to be used as Identifiers.

5. No special characters, such as semicolon, period, whitespaces, slash or comma are

permitted to be used in or as Identifier.

6. C‟ compiler recognizes only the first 31 characters of an identifiers.


Keywords
A keyword is a reserved word. All keywords have fixed meaning that means we
cannot change. Keywords serve as basic building blocks for program statements. All
keywords must be written in lowercase. A list of 32 keywords in c language.
Data Types

To store data the program must reserve space which is done using datatype. A
datatype is a keyword/predefined instruction used for allocating memory for data.
Basic Data Types
Variables
A variable is a name of memory location. It is used to store data. Variables are
changeable, we can change value of a variable during execution of a program.
It can be reused many times.

Rules to write variable names:

● A variable name contains maximum of 30 characters/ Variable name must be


upto 8 characters.
● A variable name includes alphabets and numbers, but it must start with an
alphabet.
● It cannot accept any special characters, blank spaces except under score( _ ).
● It should not be a reserved word.
Declaration of Variables

A variable can be used to store a value of any data type. The declaration of variables
must be done before they are used in the program. The general format for declaring
a variable.

Syntax : data_type variable-1,variable-2,------, variable-n;

Variables are separated by commas and declaration statement ends with a


semicolon.

Ex : int x,y,z;

float a,b;

char m,n;
Assigning values to variables : values can be assigned to variables using the
assignment operator (=). The general format statement is :

Syntax : variable = constant;

Ex : x=100; a= 12.25; m=‟f‟;

Syntax : data_type variable = constant;

Ex : int x=100;

float a=12.25; char m=‟f‟;


3 new things I learned
World’s largest Can be seen from Located in
1 2 3
coral reef system outer space Queensland, AU

Lorem ipsum dolor sit amet, Ut enim ad minim veniam, quis Excepteur sint occaecat
consectetur adipiscing elit, nostrud exercitation cupidatat non proident, sunt
sed do eiusmod tempor in culpa qui officia deserunt
incididunt ut labore et dolore ● Duis aute irure dolor in mollit anim id est laborum.
magna aliqua. reprehenderit in
voluptate velit
Ut enim ad minim veniam, quis
● Esse cillum dolore eu
nostrud exercitation
fugiat nulla pariatur
Thank You !!

You might also like