0% found this document useful (0 votes)
37 views

Session 2

Uploaded by

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

Session 2

Uploaded by

ISAAC SICHALWE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

C PROGRAMMING BIT250 Data Structures

and Algorithms

LANGUAGE INTRO I
C PROGRAMMING LANGUAGE INTRO I
OBJECTIVES
Understand the structure of a C program

Understand data types, Structs, variables and


operators in language C

Understand the use of statements and expressions

Understand conditional program flow in language C

Understand repetitive program flow in language

C PROGRAMMING LANGUAGE INTRO I


INTRODUCTION

 In 1988, the American National Standards Institute (ANSI)


had formalized the C language.

 C was invented to write UNIX operating system.

 C is a successor of 'Basic Combined Programming Language'


(BCPL) called B language.

 Linux OS, PHP, and MySQL are written in C.

 C has been written in assembly language.

C PROGRAMMING LANGUAGE INTRO I


WHAT IS C LANGUAGE OF
PROGRAMMING ?

 Inspired by previous languages A and B, Ritchie created


the C programming language
 C language was created with certain objectives

 The objectives are now the qualities that C language of


programming offers us. These include:
 Flexibility: C language does not provide limitations on the
kind of applications that can be created. Range off
applications can be built using C; Operating Systems, Word
processors, Spreadsheets and Compilers for other
programming languages.

C PROGRAMMING LANGUAGE INTRO I


WHAT IS C LANGUAGE OF
PROGRAMMING ?

 Portability: C language is hardware independent – means


that a program written to run on one particular machine
can compile and run on another machine with little or no
modifications

 Modularity: Being a structured programming language, C


source code should be written as routines called
functions
 a function is an individual module that can be reused in different
applications

 Open Dev ++ and create a C console application

C PROGRAMMING LANGUAGE INTRO I


USES OF C PROGRAMMING LANGUAGE
In the beginning, C was used for developing system
applications, e.g. :
Database Systems
Language Interpreters
Compilers and Assemblers
Operating Systems
Network Drivers
Word Processors

C PROGRAMMING LANGUAGE INTRO I


WHY C IS POPULAR

•One of the early programming languages.


•Still, the best programming language to learn quickly.
•C language is reliable, simple and easy to use.
•C language is a structured language.
•Modern programming concepts are based on C.
•It can be compiled on a variety of computer platforms.

C PROGRAMMING LANGUAGE INTRO I


FEATURES OF C PROGRAMMING
LANGUAGE
•C is a robust language with a rich set of built-in
functions and operators.
•Programs written in C are efficient and fast.
•C is highly portable, programs once written in C can be
run on other machines with minor or no modification.
•C has a collection of C library functions; we can also
create our function and add it to the C library.

C PROGRAMMING LANGUAGE INTRO I


ADVANTAGES OF C
•C is the building block for many other programming languages.
•Programs written in C are highly portable.
•Several standard functions are there (like in-built) that can be
used to develop programs.
•C programs are collections of C library functions, and it's also
easy to add own functions to the C library.
•The modular structure makes code debugging, maintenance and
testing easier.

C PROGRAMMING LANGUAGE INTRO I


DISADVANTAGES OF C

•C does not provide Object Oriented


Programming (OOP) concepts.
•There are no concepts of Namespace in C.
•C does not provide Constructor and
Destructor.

C PROGRAMMING LANGUAGE INTRO I


C KEYWORDS
The C Keywords must be in your
information because you can not use
them as a variable name.
You can't use a keyword as an identifier
in your C programs, its reserved words
in C library and used to perform an
internal operation.
The meaning and working of these
keywords are already known to the
compiler.
C PROGRAMMING LANGUAGE INTRO I
C KEYWORDS
A list of 32 reserved keywords in c language is given below:

C PROGRAMMING LANGUAGE INTRO I


STRUCTURE OF A C PROGRAM

By structure, it is meant that any


program can be written in this
structure only. Writing a C program in
any other structure will hence lead to
a Compilation Error.
The structure of a C program is as
follows:

C PROGRAMMING LANGUAGE INTRO I


STRUCTURE OF A C PROGRAM

C PROGRAMMING LANGUAGE INTRO I


STRUCTURE OF A C PROGRAM

#include directives section: also called header files


section, is a part of the program source code which contains
files that contain functions that have been developed and
ready for use.
The general form is #include <file_name>

Examples:
a. Stdio. h – a file that contains functions that enable
inputting and outputting of data through the console of the
running application.
b. Stdlib.h – a file that contains functions for typical C
language functions used for memory allocation and
management.

C PROGRAMMING LANGUAGE INTRO I


STRUCTURE OF A C PROGRAM

c. math.h – a file containing mathematical functions


such as square root, trigonometric functions such as
cos, sin, tan …etc.
These are examples of files of library functions, they
come with the programming language.

A user can equally create a file to hold their specific


functions. These files will also have to be indicated in
the header files section of the program.

C PROGRAMMING LANGUAGE INTRO I


STRUCTURE OF A C PROGRAM

Variable definition section: this is where variables that will be


used in the program are to be defined or declared.
Declaration of a variable results in the reservation of memory
space for the storage of the variable value.
In C, all variables must be declared before they are used.

Function prototype section: all functions that are not defined


in a header file must be stated in this section using their
prototype;
a prototype of a function is the name of the function and its arguments.
Example:
int product ( int a, int b);

C PROGRAMMING LANGUAGE INTRO I


STRUCTURE OF A C PROGRAM

main () function: this is the major component of every


C program.
It is the entry point to the program

The body of the program is made up of statement (lines of code


)

The starting point of the program is marked by a “{“ while the


end of the program is marked by a “}”

Programs run from the first statement of the body till the last
NB: Variables can be declared after the main function
(discussed later)

C PROGRAMMING LANGUAGE INTRO I


STRUCTURE OF A C PROGRAM

Program statements: program statements are the lines of


code in a program.
C statements enable us to do various operations in a program
such as;
Display information on the screen

Read key board input

Perform mathematical operations

Call functions

Read disk files

C PROGRAMMING LANGUAGE INTRO I


STRUCTURE OF A C PROGRAM

Function definition section: this is a section of the


program where functions are defined. This means we
add specific statements of the function that help achieve
the goal of the function.
Return Statement: The return statement returns the
value from main(). The returned value may be used by
operating system to know termination status of your
program. The value 0 typically means successful
termination.

C PROGRAMMING LANGUAGE INTRO I


EXAMPLE PROGRAM

C PROGRAMMING LANGUAGE INTRO I


FUNCTIONS PRINTF(), SCANF()
AND GETCHAR()

Printf() - a function that belongs to the stdio.h header


file enables us to write to the console/screen

Scanf() – enables us to read values that have been


entered through the keyboard. It equally belongs to the
stdio.h header file.

Getchar() – equally enables to read values entered by


the user

C PROGRAMMING LANGUAGE INTRO I


EXAMPLE 2

C PROGRAMMING LANGUAGE INTRO I


FORMAT SPECIFIERS
Format specifiers can be defined as the operators
which are used in association with printf() function for
printing the data that is referred by any object or any
variable.
When a value is stored in a particular variable, then
you cannot print the value stored in the variable
straightforwardly without using the format specifiers.
You can retrieve the data that are stored in the
variables and can print them onto the console screen
by implementing these format specifiers in a printf()
function.

C PROGRAMMING LANGUAGE INTRO I


LIST OF FORMAT SPECIFIERS IN C

C PROGRAMMING LANGUAGE INTRO I


EXAMPLE FORMAT SPECIFIER
The %d format specifier is implemented for representing integer
values. This is used with printf() function for printing the integer
value stored in the variable.

C PROGRAMMING LANGUAGE INTRO I


DATA TYPES IN C

C language supports two kinds of data types for variables:


1. Scalar variables : refers to variables that contain one single
value. These are equally called basic types of the
programming language. They include:
1. Integers (int ) – whole numbers only
2. Floating point variables (float and double) – real numbers with
fraction part
3. Characters (char) – any character known to computing

2. Structured variables : refers to variables that constitute


several values which can be of the same type or not. We could
equally call such kind of types as composite types. These
include:
1. Arrays
2. Structs
3. enums

C PROGRAMMING LANGUAGE INTRO I


DECISION MAKING
•C conditional statements allow you to make a decision, based
upon the result of a condition. These statements are called
Decision Making Statements or Conditional Statements.
•C languages have such decision-making capabilities within its
program by the use of following the decision making
statements:
If statement
if statement
if-else statement
Nested if-else statement
else if-statement
goto statement
switch statement
Conditional Operator

C PROGRAMMING LANGUAGE INTRO I


CONDITIONAL PROGRAM FLOW

If … statement:
if ( condition) {
// BLOCK1 of statements
}

// BLOCK2 of statements
NB:
the word ‘else’ exists in C but the word ‘then’ does not exist
in the language like in pseudo code.

The curly brackets indicate the begin ‘{’ and end ‘}‘ of a
block of statements

C PROGRAMMING LANGUAGE INTRO I


CONDITIONAL PROGRAM FLOW

If … else statement:
if ( condition) {
// BLOCK1 of statements
}
else {
// BLOCK2 of statements
}

// BLOCK3 of statements

C PROGRAMMING LANGUAGE INTRO I


CONDITIONAL PROGRAM FLOW

Nested If … else statement:


if ( condition1) {
// BLOCK1 of statements
}
else {
if ( condition2 ) {
// BLOCK2 of statements
}
else {
// BLOCK3 of statements
}
// BLOCK4 of statements
}

// BLOCK5 of statements

C PROGRAMMING LANGUAGE INTRO I


CONDITIONAL PROGRAM FLOW

Switch statement:
switch ( n) {
case 1 :
// BLOCK1 of statements
break;
case 2 :
// BLOCK2 of statements
break;
case 3 :
// BLOCK3 of statements
break;
case 4 :
// BLOCK4 of statements
break;
default :
// BLOCK5 of statements
}
// BLOCK6 of statements

C PROGRAMMING LANGUAGE INTRO I


CONDITIONAL PROGRAM FLOW

Switch statement (another version):


switch ( n) {
case 1 :
// BLOCK1 of statements
break;
case 2 :
case 3 :
case 4 :
// BLOCK3 of statements
break;
default :
// BLOCK4 of statements
}
// BLOCK5 of statements

C PROGRAMMING LANGUAGE INTRO I


REPETITIVE PROGRAM FLOW

do …while statement:
do {
// BLOCK1 of statements
}
while ( condition);
// BLOCK2 of statements

NB:
 repeats Block1 until the condition is false

C PROGRAMMING LANGUAGE INTRO I


REPETITIVE PROGRAM FLOW

while statement:
while ( condition) {
// BLOCK1 of statements
}
// BLOCK2 of statements

C PROGRAMMING LANGUAGE INTRO I


REPETITIVE PROGRAM FLOW

For statement:
for( <initial value> ; <condition> ; <increment>) {
// BLOCK1 of statements
}
// BLOCK2 of statements

NB:
• Not always increasing, as regards to the increment part.
• Before Block1 is executed the, the condition must be verified.

C PROGRAMMING LANGUAGE INTRO I


REPETITIVE PROGRAM FLOW

Break statement:
 Already covered in the switch statement.
 Could be applied to the loops.
 Enables us to escape a loop before it continues to the next
iteration. The takes us to the next block after the loop.

Example:
while ( condition) {
// BLOCK1 of statements
if (a > b) {
break;
}
}
// BLOCK2 of statements

C PROGRAMMING LANGUAGE INTRO I


REPETITIVE PROGRAM FLOW

Continue statement:
 Enables us to go to the next iteration/ round of the
loop prematurely

Example:
while ( condition) {
// BLOCK1 of statements
if (a > b) {
continue;
}
// BLOCK2 of statements
}
// BLOCK3 of statements

C PROGRAMMING LANGUAGE INTRO I


REPETITIVE PROGRAM FLOW

Goto statement:
 Enables us to go to a specified part of the program

Example:
while ( condition) {
// BLOCK1 of statements
if (a == b) {
goto MyDestination;
}
// BLOCK2 of statements
}
// BLOCK3 of statements
MyDestination :
// BLOCK4 of statements

C PROGRAMMING LANGUAGE INTRO I


VARIABLES IN C
Variables are memory locations(storage area) in
the C programming language.
The primary purpose of variables is to store data in
memory for later use.
Unlike constants which do not change during the
program execution, variables value may change
during execution.
If you declare a variable in C, that means you are
asking to the operating system for reserve a piece
of memory with that variable name.

C PROGRAMMING LANGUAGE INTRO I


VARIABLES IN C :DEFINITION

C PROGRAMMING LANGUAGE INTRO I


RULES FOR CHOOSING VARIABLES

•A variable name can consist of Capital letters A-Z,


lowercase letters a-z, digits 0-9, and the underscore
character.
•The first character must be a letter or underscore.
•Blank spaces cannot be used in variable names.
•Special characters like #, $ are not allowed.
•C keywords cannot be used as variable names.
•Variable names are case sensitive.
•Values of the variables can be numeric or alphabetic.
•Variable type can be char, int, float, double or void.

C PROGRAMMING LANGUAGE INTRO I


EXAMPLE

C PROGRAMMING LANGUAGE INTRO I


OPERATORS IN C

Relational operators:
< : less than
<= : less than or equal to
> : greater than
>= : greater than or equal to
== : equal to
!= : different to
C PROGRAMMING LANGUAGE INTRO I
OPERATORS IN C

Logical operators:
(a > b) && ( c < d) : operator AND

(a > b) || ( c < d) : operator OR

! (a > b) : operator NOT

C PROGRAMMING LANGUAGE INTRO I


OPERATORS IN C

Cast operator:
Used when the programmer wants to force the
conversion of a given expression to a type of
his/her choice.

C PROGRAMMING LANGUAGE INTRO I


STRUCTURED VARIABLES :
STRUCT

A struct is a structure that enables us to design a data


type under one name which consists of several
values which can be of different types.

Declaration of a struct.
struct myDate {
int day ;
int month;
int year ;
}

C PROGRAMMING LANGUAGE INTRO I


END OF SESSION

Next session: C programming language 2

C PROGRAMMING LANGUAGE INTRO I

You might also like