You are on page 1of 74

IMPLEMENTATION

BOLA AKANDE
INTRODUCTION TO
PROGRAMMING
COSC101
BABCOCK UNIVERSITY
IMPLEMENTATION PHASE

 Implementation involves two major steps


*coding
*code execution
 Implementation is the process of converting the
algorithm into a program, (i.e. the form that the
computer can execute) and execution of such
program.
 This stage now leads to programming language.
PROGRAMMING LANGUAGE
 A programming language is an artificial language, made up of symbols, special
words, and a set of rules, used to construct or express a program. It is the act
of transforming the algorithm into a language the computer can understand.

 A program is a meaningful sequence of instructions for a computer.


PROGRAMMING LANGUAGE

 The uniqueness of one programming language to


another is it syntax and semantics.
 Syntax: this are the grammatical rules of a particular
programming language. As a natural language has
its grammatical rule for construction of a correct
sentence, so also programming languages has
grammatical rules for the construction of a correct
statement.
PROGRAMMING LANGUAGE

 Semantics: this is the intended interpretation


of programming statement. It implies that
meanings are attached to each programming
statement e.g. assigning a floating point i.e.
decimal to a memory location that has been
declared as integer will cause a semantic
error.
PROGRAMMING ERRORS

 Violations to the syntax and semantics of a


programming language leads to error.
 Errors in programming are also known as
bugs.
 Removal of errors is what we know as
debugging.
Programming Errors

There are 3 basic types of


program errors
– Syntax error
– Semantic error
– Logic error
PROGRAMMING ERRORS……

 These errors can occur either during


compilation time or during running time, thus
depending on when the error occur we can
categorized the errors into compile-time error
and run-time error.
Programming Errors . . .
 Compile - time error
– Error that violate the rules (syntax) of a programming
language. This category of error are reported during
compilation. Errors reported at that time are syntax error
and semantic error.
 Examples of syntax errors are
– Typographical mistakes
– Omitted semicolon
– References to undeclared variables
– Wrong type of values assigned to a variable
– Wrong number or type of arguments passed to a function
Programming Errors . . .

 Run-time error
– An error that does not prevent your program from
compiling, but causes it to fail when you try to run
it. This refers to errors reported during execution,
this can be a semantic error or it can be a logic
error.
 Example includes:
– trying to divide by zero
– trying to open a file that does not exist
Programming Errors . . .

 Logic error
– An error in design or implementation that does not
prevent your program from compiling but causes it
to not do what you intended.
– Difficult to detect and track.
– Examples are:
 an incorrect formula
 incorrect sequence of statements
INTRODUCTION TO C

 Written by Dennis Ritchie in Bell Laboratories in 1972. It was


published as a book in 1978 by Kernighan and Ritchie.
 It took most of its principles from earlier languages. These are CPL
(Combined Programming Language), developed in 1960, BCPL
(Basic Combined Programming language), 1967, and then B.
 BCPL is a simpler version of CPL which had many of its features from
a language called Algol 60.
 C gained popularity because UNIX system which was freely
distributed in academic world was written in C and these UNIX
system always come with free C compilers.
 In 1989 we have C89 standard was established as ANSI (American
National Standards Institute (ANSI)) C
 In 1990 ISO (international standard organisation) adopted ANSI C
and called it C90
INTRODUCTION TO C

 In 99 we have the C99 standard but it was


 mostly backward-compatible
 not completely implemented in many compilers
 New C standard was written in 2007. The standard is
C1X
 This course will use ANSI/ISO C (C89/90).
 It can be referred to as an high level assembly
language because it combines all the elegance and
power of assembly language with readability and
maintainability of assembly language.
Overview of C language
 General purpose language
 C is a fast, small, general-purpose, platform independent
programming language.
 C is static (compiled), typed, structured and imperative
 C is an high level procedural language
 Powerful and compact
 Use to developed software:
– Operating system
– Wordprocessor
– Spreadsheet
– Game etc
USES OF C
 C is majorly used in writing Systems Programming for
examples:
 It is the first language other than assembly language used for
writing and implementing system programming. It was first
used to implement Unix kernel.
 C has been used to write operating systems like Linux, for
writing dbase, compilers, interpreters etc.
 Other practical application are:
 Microcontrollers in automobiles and airplanes
 embedded processors in phones, portable electronics, etc.
 DSP processors in digital audio and TV systems
Limitation of C

 Limitations of C:
cannot handle exceptions
range-checking
garbage collection
object-oriented programming
polymorphism
 Thus C needs to be extended.
C EXTENSION

 C is a console application that has been


extended to generate other programming
languages like, Concurrent C, C++, objective
C, and C#
 A console application is an application that
can be run on the system command prompt
i.e. cmd, by using certain commands.
 It has strong influence on Java
Features of C

 Simplicity: it has relatively small number of


components.
 Efficiency: its commands are oriented
towards low level instructions and it can often
produce very fast and small code.
 Portability: this simply means it is available
on virtually any platform i.e. it can be
implemented or executed on any type of
operating system.
Starting out……………

 The very first thing you need to do, before


starting out in C, is to make sure that you
have a compiler.
 What is a compiler? A compiler turns the
program that you write into an executable
that your computer can actually understand
and run.
 Then you also need a text editor.
C Compiler and its Terminologies

 GNU C compilers commonly known as gcc, or g++ for C++, is considered as


the best compiler. There are two types of GNU C compiler:
 CYGWIN (used on DOS/WINDOWS) and DGJPP (used on MS-DOS/MS-
WINDOWS and they are free compiler for C and C++) .
 Compiler Generally, compiler refers to both a compiler and a "linker
 Compile Generally means to convert a source code file into an executable, but
strictly speaking, compilation is an intermediate step.
 Link The act of taking compiled code and turning it into an executable
 Linker The program that generates the executable by linking
 Build A build refers to the process of creating the end executable (what is often
colloquially referred to as compilation). Tools exist to help reduce the
complexity of the build process.
 IDE Integrated Development Environment, a combination of a text editor and a
compiler, such that you can compile and run your programs directly within the
IDE. IDEs usually have facilities to help you quickly jump to compiler errors.
IDE Integrated Development
Environment

 An IDE consists of both the compiler and the


text editor
 An IDE provides a complete environment for
writing, managing, developing, and testing
your programs.
 Popular IDEs: Eclipse (CDT), Microsoft
Visual C++ (Express Edition), KDevelop,
Xcode, Code Editor, Dev-C++ etc
 Integrated editor with compiler.
 Very convenient for larger programs
Other text editors……………

 Other commonly used text editors are:


 Notepad
 EditPlus
 editPlus is more sophisticated than notepad
because it has part of the features of an IDE
but it does not has an integrated compiler.
Advantages of IDE

 It has extensive capabilities that gives:


 An automatic facilities for laying out the program text
appropriately i.e. autoindexing.
 Color highlighting for important language elements
 More readable code.
 The color gives clear indication when you make
errors in keying in words.
 Highlighting of syntax.
Creating C Program

 There are four(4) stages:


– Editing ( using program editor)
– Compiling ( using compiler)
– Linking (Using a Linker)
– Executing – (runs the program)
Creating C program contd (flowchart)
EDITING

 It is the process of creating and modifying


your C source code.
 A source code is the name given to the set of
programming instructions your write.
 Editing of C source code is done using a text
editor or simply an editor.
CREATING A SOURCE CODE

 To create a source code you first write by


keying in the right syntax for writing a C
language.
 You should save your created file or source
code inside the “bin” folder of your C
compiler.
 The source code or your file should be saved
with a name and an extension i.e. .c
extension.
WRITING/EDITING C PROGRAM I

 Simple Structure of a .c file


 /* Begin with comments about file contents */
 Preprocessor directives: Insert #include statements or
#define
 Define main() function
 { //left brace that indicates the beginning of a
block of statement
 Main Function body //contains executable or action
statements that a function is expected to
perform
 } //right brace that indicates the end of a block of
statement
 Library (stdio) #include <stdio.h>
 File #include “registration.c”
 #define PIE 3.142
Commenting styles

 /
*………………………………………………………
…………………………………………………………
…………………………………………………………
……………………*/ multiple line commenting
 //…………………………………………………
 //…………………………………………………
 // … single line commenting
Simple .c program

//first program
int main(void) //return-type function-name(parameters
{ //Function body
int age;
printf(“Akande Oyebola\n”);
printf(“I am good”);
return 0;
}
format specifier

 \n
 \t
 printf(“Course code\tCourse tittle\t\tCourse
Unit\n”);
 Printf(“C0SC101\tIntro Prog\t\t3\n”);
Assignment

 Course form
1.Open the bin folder

2.Give your program a title 3. Choose


Source fil
Program explanation
 #include <stdio.h>
is a directive to the C preprocessor
This line tells the compiler to include the information found in
the file stdio.h, which is a standard part of all C compiler
packages; this file provides support for keyboard input and for
displaying output.

 int main(void)
Every C program must have at least a main function
int – mean main returns integer (whole number)
Void – means main does not receives information( argument)
Explanation

 /* program execution begins here */


Any statement enclosed between /* */ indicate a comment.
It is use to describe the code.

• {
This opening brace marks the start of the statements that
make up the function. And must be ended with a closing
brace (}).
This pair of braces and the portion of the program between the
braces is called a block
Explanation

 printf(" The first program in C ");


 instructs the computer to perform an action, namely to
print on the screen the string of characters marked by the
quotation marks
This displays the phrase The first C
program
 printf() is part of the standard C library. It's
termed a function, and using a function in the
program is termed calling a function.
Explanation

 return 0;
 Indicate the program ended successfully
 The keyword return is one of several means
we’ll use to exit a function
 Every statement must end with a semicolon (also
known as the statement terminator).
Classwork

 Write a C program to show the list of


activities you do on Mondays.
 Write a C program showing your timetable
from Monday to Sunday.
WRITING/EDITING C PROGRAM
II
 Advance Structure of a .c file
 /* Begins with comments about file contents */
 Insert #include statements and preprocessor
definitions
 Function prototypes and variable declarations
 Define main() function
 {
 Function body
 }
 Define other function i.e. function definition
 {
 Function body
 }
COMMENT
 Comment is used to explain line of codes.
 Its actually a way of documenting your source
code.
 It is completely ignored by the compiler
 And it can appear anywhere in the program.
 There are two commenting styles:
 Single line comment
 Multiple line comment
Single line comment
 Single line comment is used for commenting
every single line of comment.
 Its inserted at the beginning of every line.
 Examples:
 For comment on a line
 //………………….
//This is my first program
 For comment on multiple lines
 To use single line comment when the comment
is more than a line, ‘//’ will be inserted at the
beginning of each line.
// This Application is to perform application,
//and registration
Multiple line comment

 /*……*/ this is multiple line commenting style,


 For examples
 For comment on a line
/* This is a registration application*/
 For comment on multiple line
/*This Application is to perform application, and
registration */
Where to use comment
 At the beginning of your code, explaining what the
program is all about.
 You can comment a line or a segment of your code
to explaining what the line or segment is doing.
 You can also use comment on sections of the code
so to see what the output is really like if the
section is not written.
WARNING
 To use multiple line for comment do not forget to
close the comment with the */ or else the codes
entered after will also be taken as comment by
the compiler.
 For a single line comment, do not forget to add
the comment sign “//” at the beginning of every
line or else the comments will be seen as code by
the compiler. This will results into error during
compilation.

GOOD PROGRAMMING STYLE

 Always start you coding with comment, the comment


here can be used for giving title to the code or explain
what the code want to accomplish or its all about.
Lab Work

 Write a comment on multiple lines using multiple


line comment style, but do not close the
comment. Continue coding after writing the
comment and then compile your work.
 Write a comment on multiple line using single line
comment style, but do not add comment sign “//”
before the last line of the comment. Then,
continue coding after writing the comment and
then compile your work.

Preprocessors Definitions

 Preprocessor is a separate step of compilation. One of its


uses is to allow C to make use of facilities which are not
originally in C language. Any line in the source code
stating with # sign is a preprocessor definition. There are
different categories of C:
 File Inclusion
 Macro-substitution
File Inclusion

 File inclusion makes it easy to;


 access library functions in an header file
 handle collection of #define and decalaration statements.
 The most popular preprocessor for file inclusion is #include
 #include is a preprocessor directory that tells the compiler to put
code from the headers such as stdio.h into our program before
actually creating the executable.

 Typically you will use the preprocessor to define constants, perform


conditional code inclusion, include header files or to create
shortcuts
 The C preprocessor permits you to define simple macros that are
evaluated and expanded prior to compilation.
File Inclusion
 To access library functions in an header file the
most popular preprocessor to use is #include.
 #include is a preprocessor directory that tells
the compiler to put code from the headers such
as stdio.h into our program before actually
creating the executable.
 Syntax:
 #include <headerfile>
 For example:
 #include <stdio.h>
 Note that extension .h is added to make the
standard input and output library an header
file.
File Inclusion

 Header files contain definitions of variables and


functions inside standard C libraries.
 Such declarations of variables and functions are
necessary for the functioning of a program.
Examples of such functions are printf, scanf,
getchar etc. which are part of standard input and
output library, i.e. stdio.
 stdio.h provides support for keyboard input and
for displaying output.
Libraries/header files in C
standard library
 < stdio.h> -> defining input and output routines
 < ctype.h> -> defining character manipulation routines
 < string.h> -> defining string manipulation routines
 < math.h> -> defining mathematical routines
 < stdlib.h> -> defining number conversion, storage allocation
and similar tasks
 < stdarg.h> -> defining libraries to handle routines with
variable numbers of arguments
 < time.h> -> defining time-manipulation routines
 < assert.h> -> defining diagnostic routines
 < setjmp.h> -> defining non-local function calls
 < signal.h> -> defining signal handlers
 < limits.h> -> defining constants of the int type
 < float.h> -> defining constants of the float type
File Inclusion

 #include preprocessor directory also handles files written


by users.
 Syntax:
 #include “filename”
 For example:
 #include “fileA.c”
Common C headers in include
preprocessor.
Standard Headers you should know about:
stdio.h – file and console (also a file) IO: perror, printf,
open, close, read, write, scanf, etc.
stdlib.h - common utility functions: malloc, calloc,
strtol, atoi, etc
string.h - string and byte manipulation: strlen, strcpy,
strcat, memcpy, memset, etc.
ctype.h – character types: isalnum, isprint, isupport,
tolower, etc.
errno.h – defines errno used for reporting system
errors
math.h – math functions: ceil, exp, floor, sqrt, etc.
signal.h – signal handling facility: raise, signal, etc
stdint.h – standard integer: intN_t, uintN_t, etc
time.h – time related facility: asctime, clock, time_t,
etc.
Macro-substitution
 Macro-substitution implies that any
subsequent occurrence of the token
name will be replaced by the
replacement text.
 The most popular preprocessor used
is #define
 The C preprocessor permits you to
define simple macros that are
evaluated and expanded prior to
compilation.
Macro-substitution

 Syntax:
 #define token-name replacement text
 #define can be used for symbolic constant e.g.
 #define PIE 3.142
 This means subsequence occurrence of PIE will be
replaced by 3.142
Function Prototype and variable
declarations
 Function prototypes and variable declarations is the third step
in creating a C file.
 Function prototypes are also known as function declarations.
 Declaration refers to where the nature of a variable is stated
but no storage is allocated.
 At this stage user-defined function are declared. Also any
variable declared here are called external variable.
 External variable is a variable which is not declared within any
function and can be accessed by any function.
Function

 Function is simply a collection of commands that


does something.
 Function breaks large computing task into smaller
ones.
 It makes a program reusable i.e. enable people to
build on what others have done instead of building
another one from the scratch.
 It also hides the detail of operation from parts of
the program that do not need to know about
them.
MODIFYING SOURCE CODE

 Modification involves changing a part of your


source code to attain program correctness.
 Thus you modify your source code in order to
correct syntax error, semantic error, logic
error or in order for it to meet required
specification.
 Remember that syntax error, semantic error,
and logic error can be reported either at
compile-time or run-time.
Creating C Program

 There are four(4) stages:


 Editing ( using program editor)
 Compiling ( using a compiler)
 Linking (using a Linker)
 Executing – (runs the program)
EDITING

 It is the process of creating and modifying your C


source code.
 A source code is the name given to the set of
programming instructions your write.
 Editing of C source code is done using a text editor or
simply an editor.
CREATING A SOURCE CODE

 To create a source code you first write by keying in


the right syntax for writing a C language.
 You should save your created file or source code
inside the “bin” folder of your C compiler.
 The source code or your file should be saved with a
name and an extension i.e. .c extension.
WRITING/EDITING C PROGRAM I

 Simple Structure of a .c file


 /* Begin with comments about file contents */
 Insert #include statements and preprocessor definitions
 Define main() function
 { //left brace that indicates the beginning of a
block of statement
 Main Function body //contains executable or action
statements that a function is
expected to perform
} //right brace that indicates the end of a block of
statement
Structure of a Simple .c file
Declare the main function 1.Begin with comment
Define the library

# indicates
That this
Line should be interpreted
By the preprocessor

This line is the preprocessor


directives
Int indicates the return type
Void indicates no argument

Use left and


Right
braces.
They
contain System pause
The body of Is used by Dev
The main C++ for viewing
fun- Console application
ction
 Steps
 Structure of a simple C
1.Open the bin folder

2.Give your program a title 3. Choose


Source fil
Using Special Constants

 Escape Sequences
 C uses special character constants to represent unprintable characters
 These characters usually perform control functions
 For example, '\n' is the new line sequence.
 The statement below tells the program to start a new line
of text on screen.
 printf('\n');
 The difference between a character constant and a special character
constant is the backslash
 If you are using a special character constant on its own, you must
enclose it in single quotes
Using Special Constants contd
Some common examples of escape sequences are presented below:
Escape Sequence Description

\a sound (a beep on your computer not


used much these days)
\b Backspace

\f Form-feed or page eject

\n Newline

\r Carriage return (for printers) or move to


the beginning of the current line for
output to the screen
\t Horizontal tab
Tokens

 A token is a string of one or more character which is


significant or meaningful to the compiler.
 There are different types of tokens, they are
 Keywords

 Identifier
 Constants
 Operators
 Punctuation/Separators
 White space
Assignment
 Using simple structure of C, write out your answers to the
classwork.
QUIZ
 Problem solving problem
 Structure of c program
 Draw Flowchart to read score and determine pass or fail
 Input score and determine grade from A to F
QUIZ
 Comfort model school want to celebrate each pupils on
their birthdays. They want a record which will indicate the
current age of each pupil each year. Do the following in
tabular form:
 Identify the input data
 Identify the output data
 Identify the processing requirements
 Give each data a variable name
 Indicate the data type of each variable
CLASSWORK
 A class teacher needs to compute the average score
of 20 pupils in eight (8) subjects. Thus, before
developing the application you need to understand
and analyse the problem clearly stating the input
data, output data, and processing requirements.

You might also like