[go: up one dir, main page]

0% found this document useful (0 votes)
11 views30 pages

STRUCTURED PROGRAMMING WITH C - CRASH COURSE CSC 201

The document is a crash course on structured programming with C, authored by Vicol Olawade, aimed at helping students learn to code at their own pace. It covers fundamental concepts of structured programming, the C language, its characteristics, applications, and the structure of C programs, along with detailed explanations of data types, variables, input/output functions, and more. The course is designed to provide a solid foundation for students in the Osun State University studying CSC 201.

Uploaded by

dabini6459
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)
11 views30 pages

STRUCTURED PROGRAMMING WITH C - CRASH COURSE CSC 201

The document is a crash course on structured programming with C, authored by Vicol Olawade, aimed at helping students learn to code at their own pace. It covers fundamental concepts of structured programming, the C language, its characteristics, applications, and the structure of C programs, along with detailed explanations of data types, variables, input/output functions, and more. The course is designed to provide a solid foundation for students in the Osun State University studying CSC 201.

Uploaded by

dabini6459
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/ 30

OWNER’S NAME

https://vicololawade.com

STRUCTURED PROGRAMMING WITH C - CRASH COURSE

CSC 201
- VICOL OLAWADE

STRUCTURED
PROGRAMMING
FOR MORE INQUIRIES

Please visit this website:


https://tutorials.vicololawade.com

P a g e 1 | 30
tutorials.vicololawade.com
STRUCTURED PROGRAMMING WITH C

AUTHOR’S INTRODUCTION

Author’s Aim
I am Vicol Olawade. A Computer Science student of the 2019/20 set. The
main purpose for this foundation is to help students conveniently learn to code.
And this can only be achieved if students have a very solid foundation in the field
of programming, as most of the students this manual would be useful for are
those offering STRUCTURED PROGRAMMING or its Introduction in the
university .In this regard, its very inconvenient learning to code if you’re being
taught at the same time with others because this won’t give individuals so much
time to learn and practise what have been taught. This foundation provides you
with materials and videos that can help you conveniently learn and understand
this course easily at your own pace.
Learning programming in generally sequential, meaning you take a step at
a time and it also involves practising things you have learnt over a period of time.
I understand that not all students can learn something once they are taught once,
and I also understand that not all students learn when others learn, meaning
some learn better at night than they do in the day. Therefore, this foundation
strongly lies on providing each students quality tutorial that they can learn from at
their own pace.
The YouTube video tutorial is employed because I personally don’t want to
gather you all and coordinate a tutorial for a large amount of students that would
in-turn not be beneficial to you all. Instead, I will frequently drop video tutorials on
a YouTube channel so that you can all download and learn from any day, any
time and any where. With these tools, I believe we can all learn and understand
this course .
Currently, this course is a 3Unit course in the Osun State University
Campus, which means having a good grade in this course does a good job in
boosting your GP for that semester, and that is my AIM.
I remain Vicol Olawade, you can get to contact me by visiting my website
and you can also follow up the tutorials there too…
I WISH YOU SUCCESS IN ADVANCE!

P a g e 2 | 30
tutorials.vicololawade.com
STRUCTURED PROGRAMMING WITH C

TABLE OF CONTENTS
Table of Contents

CSC 201.............................................................................................................................1
AUTHOR'S INTRODUCTION....................................................................................................................................2

AUTHOR'S AIM.................................................................................................................2
1.0 CSc 201 Introduction..........................................................................................................................................4

1.1 INTRODUCTION TO STRUCTURED PROGRAMMING.................................... 4


1.2 WHY STUDY STRUCTURED PROGRAMMING?............................................... 4
1.3 INTRODUCTION TO C-LANGUAGE AS A CASE STUDY.................................5
1.4 CHRACTERISTICS & APPLICATIONS OF C-LANGUAGE............................5-6
1.3 STRUCTURE OF A C-LANGUAGE PROGRAM.................................................. 5
2.0 C-Language Program......................................................................................................................................... 7

2.1 C-LANGUAGE FUNDAMENTALS.......................................................................7-8


2.2 C-LANGUAGE DATA TYPES.............................................................................. 8-9
2.3 C-LANGUAGE VARIABLES, DECLARATION & INITIALIZATION.............. 9-10
2.4 C-LANGUAGE INPUT/OUTPUT..................................................................... 10-11
2.5 C-LANGUAGE OPERATORS..........................................................................12-16
2.6 C-LANGUAGE ARRAY..................................................................................... 18-18
2.7 C-LANGUAGE FUNCTIONS............................................................................18-20
2.8 C-LANGUAGE DECISION MAKING, CONDITIONS & LOOPS.................21-24
2.9 C-LANGUAGE ERROR....................................................................................24-26
3.0 Glossary & Lexicon.......................................................................................................................................... 27

3.1 C-LANGUAGE GLOSSARY & LEXICON...................................................... 27-30

P a g e 3 | 30
tutorials.vicololawade.com
STRUCTURED PROGRAMMING WITH C

1.0 CSC 201 INTRODUCTION


1.1 INTRODUCTION TO STRUCTURED PROGRAMMING

A Structured Program is a program written with only the structured


programming paradigm/construction. These are:

i. Sequence: This is the default control structure of all structured


programming languages, where by codes are executed one line after
another.

ii. Selection: This has to do with decision making where the program is
given a condition and and makes several decisions where the condition
us true or false.

iii. Repetition/Looping: This also deals with decision making where the
program executes a block of codes for as long as the given condition is
true.

Structured Programming is a programming language that divides codes


into modules, subprograms or function. Examples of Structured
Programming Languages includes: PASCAL, C, C#, C++, PHP, JAVA,
VisualBasic.

1.2 WHY STUDY STRUCTURED PROGRAMMING?

 Compared to other programming paradigm, Structured Programming is


a programming paradigm aimed at reducing programs into subprograms,
modules or functions.

 It is easy to learn.

 It is user friendly and easy to comprehend.

 It requires less time to write.

 It is easy to maintain a Structured Programming Language, like C.

P a g e 4 | 30
tutorials.vicololawade.com
STRUCTURED PROGRAMMING WITH C

1.3 INTRODUCTION TO C-LANGUAGE AS A CASE STUDY

C is a general-purpose high level language that was originally developed


by Dennis Ritchie for the Unix Operating System. The UNIX Operating
System is written in the C Language. C is probably the most widely known
programming language.

1.4 CHARACTERISTICS & APPLICATIONS OF C-LANGUAGE

CHARACTERISTICS:

 C is a general-purpose programming language that is extremely


popular, simple and flexible.

 C is machine-independent, structured programming language


which is used extensively in various applications.

 C was the basics language to write everything from operating


systems (Windows and many others) to complex programs like
the Oracle database, Git, Python interpreter and more.

 The main features of C language include low-level access to


memory, a simple set of keywords, and clean style.

 Many later languages have borrowed syntax/features directly or


indirectly from C language e.g PHP.

 Most syntax of Java, PHP, JavaScript, and many other


languages are mainly based on C language..

 C can be compiled on variety of computers.

 C has libraries with rich functions.

 It also contains rich set of built-in resources such as functions,


operators and libraries.

 C is machine independent, meaning it can run on different


machine.

 These features make C language suitable for system


programming‘s like an operating system or compiler development.
P a g e 5 | 30
tutorials.vicololawade.com
STRUCTURED PROGRAMMING WITH C

1.4 CHARACTERISTICS & APPLICATIONS OF C-LANGUAGE

APPLICATIONS:

 C was initially used for system development work, in particular the


programs that make-up the operating system.

 C was adopted as a system development language because it


produces code that runs nearly as fast as code written in assembly
language.

 C Language is used in development of Operating Systems,


Language Compilers, Assemblers, Text Editors, Print Spoolers,
Network Drivers, Modern Programs, Data Bases, Language
Interpreters, Utilities.Games.

 C language is widely used in embedded systems.

 It is used for developing system applications.

 Development of New/Modern Programming Languages.

 It is widely used for developing desktop applications.

 Most of the applications by Adobe are developed using C


programming language.

 It is used for developing browsers and their extensions. Google's


Chrome is built using C programming language.

 It is used in developing an operating system, such as Apple's OS X,


Microsoft's Windows, and Symbian are developed using C language.
It is used for developing desktop as well as mobile phone's
operating system.

 It is used for compiler production.

 It is widely used in Internet of Things [IoT] applications.

 It is used to develop databases. MySQL is the most popular database


software which is built using 'C'.

P a g e 6 | 30
tutorials.vicololawade.com
STRUCTURED PROGRAMMING WITH C

1.5 STRUCTURE OF A C-LANGUAGE PROGRAM

The C Program is divided into 6 sections/structures. Accordingly, they are:

DOCUMENTATION: The programmer gives information about the project


at the very beginning of the program, these are usually written in comments.

LINK SECTION: This is a section for declaring, linking and calling all
header files & libraries (such as #include <stdio.h>) used in a program.

DEFINITION SECTION: This is where the programmer defines constants


that are to be used in a program. Example: #DEFINE PI 3.142.

GLOBAL DECLARATION SECTION: This is a section where all global


variables to be used in a program are declared. User defined functions are
also declared here.

MAIN FUNCTION: This part of the program that a compiler executes.


Execution begins with the open curly brackets and ends at the closing curly
brackets. All functions and executions are called in the main function, main()
for execution.

SUB PROGRAM SECTION: All user defined functions are defined in this
section.

2.0 C-LANGUAGE PROGRAMS


2.1 C-LANGUAGE FUNDAMENTALS

C is an HIGH LEVEL LANGUAGE (a machine independent language that


are English-like and are understood by a computer with the aid of a
translator that translate these English-Like program to a readable and
executable program for the Computer/Machine).

C language file extension is ‘c’ in lowercase i.e If we are to create a test


program in C and we want to name it TEST, we would be saving the file as
‘TEST.c’ so that we can write C Programs in this file.

P a g e 7 | 30
tutorials.vicololawade.com
STRUCTURED PROGRAMMING WITH C

2.1 C-LANGUAGE FUNDAMENTALS

C uses letters A to Z in both lowercase and uppercase, 0 to 9, certain


special characters, and white spaces. The special characters are:

+-*/=%&#!?^“’|<>()[]{} :;.,~@!

2.2 C-LANGUAGE DATA TYPES

DATA STORAGE FORMAT


DESCRIPTION
TYPE SIZE SPECIFIER

→ The most basic data type in C. It stores


a single character and requires a single
byte of memory in almost all compilers.

→ The keyword, char is used for declaring


the character type variables. For
example: char test = ‘h’;. The size of the
character variable is 1 byte.
char 1 byte %c
→ The char datatype for a single character
is usually in single quotation mark while
the character datatype for more than one
collective character is in double
quotation mark, and the name of the
variable must end with [] to specify it is a
set of multiple characters. For example:
char name[] = “Vicol”;.

→ As the name suggests, an int datatype


is used to store an integer value.

int 4 bytes %d → Integers are whole numbers. They can


be positive, negative, or zero. Numbers
like -321, 497, 19345, and -976812 are
all perfectly valid integers, but 4.5 is not

P a g e 8 | 30
tutorials.vicololawade.com
STRUCTURED PROGRAMMING WITH C

because 4.5 is not a whole number.

→ int datatype cannot accommodate


floating point numbers (fractional
numbers).

→ It is used to store decimal numbers


(numbers with remainder or floating
point value).
float 4 bytes %f
→ A float data type can hold any value
between 3.4E-38 to 3.4E+38.

→ Float has 7 decimal digits of precision.

→ Double has 2x more precision then float.


double 8 bytes %lf → A double data type can hold any value
between 1.7E-308 to 1.7E+308.

long → A long double data type can hold any


16 bytes %Lf
double value between 1.7E-4932 to 1.7E+4932.

2.3 C-LANGUAGE VARIABLES, DECLARATION & INITIALIZATION

Variables: In computer programming, a variable is an abstract storage


location which contains some known or unknown quantity of information
referred to as a value; a variable is also container for a particular set of
data. Variables are dynamic because their values can be changed at
any instance of a program.

Variable in C: int a = 1; //a is a variable, storing the integer value: 1

Declaration: Declaration is the process of defining the name and data type
of a variable before its usage; In Programming, a declaration determines
the name and data type of a variable or a function.

Integer declaration in C: int a;float b;char c[]; //a is being declared as an


integer in this program, which means a can only store integer values.

P a g e 9 | 30
tutorials.vicololawade.com
STRUCTURED PROGRAMMING WITH C

2.3 C-LANGUAGE VARIABLES, DECLARATION & INITIALIZATION

Initialization: This is the assignment of an initial value to a data object or


variable; Initialization can also be seen as the process of using defined
values for a variable or data item that is used in a computer program.

Initialization in C: a=5;b=1.3;c=”Vicol” //a, b, c as variables are being


initialized to 5(integer), 1.3(floating point number), Vicol(string)
respectively based on the different data types they were declared to
have.

2.4 C-LANGUAGE INPUT/OUTPUT

In C programming, printf() is one of the main output function. The function


sends formatted output to the screen. For example, printf(“Hi there, I am
Vicol Olawade”).

IMPORTANT THINGS TO NOTE ABOUT THE printf() FUNCTION

 The printf() is a library function to send formatted output to


the screen. The function prints everything inside its quotations.

 To use printf() in our program, we need to include stdio.h


header file using the #include <stdio.h> statement.

 Format specifiers for different data types can be contained in


the quotation of the printf() function followed by a comma
after the quotation then the variable that holds the value to be
outputted. E.g
i.) int a = 13;printf(“My birthday is May %d”, a); //My birthday is
May 13
Here, the %d in the quotation is a format specifier that alerted
our compiler that we wanted to output an integer variable, a.
ii.) float b = 1.50;printf(“The minimum acceptable GPA is %f ”, b);
//The minimum acceptable GPA is 1.50
Here, the %f in the quotation is a format specifier that alerted
our compiler that we wanted to output a float variable, b.

WHILE
P a g e 10 | 30
tutorials.vicololawade.com
STRUCTURED PROGRAMMING WITH C

2.4 C-LANGUAGE INPUT/OUTPUT

In C programming, scanf() is one of the commonly used function to take


input from the user. The scanf() function reads formatted input from the
standard input such as keyboards.

IMPORTANT THINGS TO NOTE ABOUT THE scanf() FUNCTION

 The scanf() is a library function to receive input from a user’s


keyboard. The function accepts all alphabets, numbers and
characters in a keyboard. The inputs from users using scanf()
is further used in a program to perform several operations.

 To use scanf() in our program, we need to include stdio.h


header file using the #include <stdio.h> statement.

 Format specifiers for different data types is contained in the


quotation of the scanf() function followed by a comma then
an ampersand(&) and then the name of the variable to store
the input to be received by the scanf() function. Note that a
variable data type must have been declared before initializing
it to store the user’s input from the scanf() function E.g
i.) int num1;scanf(“%d”, &num1); //Store the value a user would
enter into variable num1. NOTE: num1 has already been declared
before its usage.
Here, the %d in the quotation is a format specifier that alerted
our compiler that we wanted to receive an input of the integer
data type.
ii.) float num2 = 1.50;scanf(“%f ”, num2); //Store the value a user
would enter into variable num2. NOTE: num2 has already been
declared before its usage.
Here, the %f in the quotation is a format specifier that alerted
our compiler that we wanted to receive an input of the float data
type.

P a g e 11 | 30
tutorials.vicololawade.com
STRUCTURED PROGRAMMING WITH C

2.5 C-LANGUAGE OPERATORS

An operator is a symbol that tells the compiler to perform specific


mathematical or logical functions. C language is rich in built-in operators
and provides the following types of operators:

 Arithmetic Operators: The Arithmetic operators are some of the C


Programming operators, which are used to perform arithmetic
operations. These includes operations like Addition, Subtraction,
Multiplication, Division and Modulus.

Example
Operator Description
(A=10;B=5)

+ Adds two operands. A + B = 15

- Subtracts second operand from the first. A−B=5

* Multiplies both operands. A * B = 50

/ Divides numerator by denominator. A/B=2

% Modulus Operator: Divides numerator by A%B=0


denominator and returns the value of the
remainder.

++ Increment operator: increases the integer A++ = 11


value by one.

-- Decrement operator: decreases the integer A-- = 9


value by one.

P a g e 12 | 30
tutorials.vicololawade.com
STRUCTURED PROGRAMMING WITH C

2.5 C-LANGUAGE OPERATORS

 Relational Operators: Relational Operators are the operators used


to create a relationship and compare the values of two operands.
Relational operators are used to compare two values in C language.
It checks the relationship between two values. If relation is true, it
returns 1 showing that the given condition is true. However, if the
relation is false, it returns 0 showing that the given condition is not
true.

Example
Operator Description
(A=10;B=5)

Checks if the values of two operands are


(A == B) is not
== equal or not. If yes, then the condition
true.
becomes true.

Checks if the values of two operands are


!= equal or not. If the values are not equal, (A != B) is true.
then the condition becomes true.

Checks if the value of left operand is


> greater than the value of right operand. If (B > A) is not true.
yes, then the condition becomes true.

Checks if the value of left operand is less


< than the value of right operand. If yes, then (B < A) is true.
the condition becomes true.

Checks if the value of left operand is


greater than or equal to the value of right
operand. If yes, then the condition
becomes true. The condition will remain (B >= A) is not
>=
true as long as one of the two true.
conditions(greater than, equal to) is true.
And the condition will only be false if both
conditions are not true.

P a g e 13 | 30
tutorials.vicololawade.com
STRUCTURED PROGRAMMING WITH C

Checks if the value of left operand is less


than or equal to the value of right operand.
If yes, then the condition becomes true.
<= The condition will remain true as long as (B <= A) is true.
one of the two conditions(less than, equal
to) is true. And the condition will only be
false if both conditions are not true.

2.5 C-LANGUAGE OPERATORS

 Logical Operators: Logical operators perform logical operations on


a given expression by joining two or more expressions or conditions.
These includes operations like Logical AND, Logical OR, Logical
NOT operators.

Operator Description Example

&& Called Logical AND operator. If both the operands (A && B)


are non-zero, then the condition becomes true.
is false.

|| Called Logical OR Operator. If any of the two (A || B)


operands is non-zero, then the condition becomes
true. is true.

! Called Logical NOT Operator. It is used to reverse !(A && B)


the logical state of its operand. If a condition is true,
then Logical NOT operator will make it false. is true.

 Bitwise Operators: Bitwise operator works on bits and perform bit-


by-bit operation. These operators include:

 Binary AND (&) operator

 Binary OR (|) operator

 Binary XOR (^) operator

P a g e 14 | 30
tutorials.vicololawade.com
STRUCTURED PROGRAMMING WITH C

2.5 C-LANGUAGE OPERATORS

 Binary One’s complement (~) operator

 Binary Left Shift (<<) operator

 Binary Right Shift (>>) operator

 Assignment Operators: An assignment operator is the operator


used to assign a new value to a variable, property, event or indexer
element in C programming language.
Assignment operators can also be used for logical operations such
as bitwise logical operations or operations on integral operands and
Boolean operands.
An assignment operators are used to assigning value to a variable.

Operator Description Example

Simple assignment operator. Assigns C = A + B will assign


= values from right side operands to left the value of A + B to
side operand. C

Add AND assignment operator. It adds


C += A is equivalent
+= the right operand to the left operand and
to C = C + A
assign the result to the left operand.

Subtract AND assignment operator. It


subtracts the right operand from the left C -= A is equivalent to
-=
operand and assigns the result to the left C=C-A
operand.

Multiply AND assignment operator. It


multiplies the right operand with the left C *= A is equivalent
*=
operand and assigns the result to the left to C = C * A
operand.

/= Divide AND assignment operator. It C /= A is equivalent to

P a g e 15 | 30
tutorials.vicololawade.com
STRUCTURED PROGRAMMING WITH C

divides the left operand with the right C=C/A


operand and assigns the result to the left
operand.

Modulus AND assignment operator. It


C %= A is equivalent
%= takes modulus using two operands and
to C = C % A
assigns the result to the left operand.

Left shift AND assignment operator. C <<= 2 is same as C


<<=
= C << 2

Right shift AND assignment operator. C >>= 2 is same as C


>>=
= C >> 2

Bitwise AND assignment operator. C &= 2 is same as C


&=
=C&2

Bitwise exclusive OR and assignment C ^= 2 is same as C =


^=
operator. C^2

Bitwise inclusive OR and assignment C |= 2 is same as C =


|=
operator. C|2

P a g e 16 | 30
tutorials.vicololawade.com
STRUCTURED PROGRAMMING WITH C

2.6 C-LANGUAGE ARRAY

An array is a collection of similar data elements stored at contiguous


memory locations that can be individually referenced by using an index to a
unique identifier.

All arrays have 0 as the index of their first element which is also called the
base index and the last index of an array will be total size of the array
minus 1.

Array is a kind of data structure that can store a fixed-size sequential


collection of elements of the same type. An array is used to store a
collection of data, but it is often more useful to think of an array as a
collection of variables of the same type.

All arrays consist of contiguous memory locations. The lowest address


corresponds to the first element and the highest address to the last element.

To declare an array in C:

→ float fractionalNumbers[5] //this declares an array that will contain only 5


floating numbers or variables because the data type is float and the
array size is defined to be 5.

→ int wholeNumbers[] //this declares an array that can contain infinite


amount of integers because the data type is int the array size is infinite
because it was not defined.

To Initialize an array in C:

→ float fractionalNumbers[5] = {13.0, 2.1, 3.4, 7.0, 4.5}


The number of values between braces { } cannot be larger than the number of
elements that we declare for the array between square brackets [ ].

→ float fractionalNumbers[] = {13.0, 2.1, 3.4, 7.0, 4.5}


If you omit the size of the array, an array just big enough to hold the initialization is
created. Therefore, you will create exactly the same array as you did in the previous
example.

P a g e 17 | 30
tutorials.vicololawade.com
STRUCTURED PROGRAMMING WITH C

2.6 C-LANGUAGE ARRAY

To Access an array element in C:

An element is accessed by indexing the array name. This is done by


placing the index of the element within square brackets after the name
of the array. For example − fractionalNumbers[2]; // 3.4
In arrays, the first element of an array is usually indexed as 0, then the
second element is indexed 1, the third element is indexed 2.

balance is an array of float data type. The size of balance is 5; To


access the first item in balance, we use balance[0] because the first
item is initialized as 0; To access the last item in balance, we use
balance[4] because it is the 5th item and it’s index is therefore 4.

2.7 C-LANGUAGE FUNCTIONS

A function or method is a group of statements that together perform a task.

A function can also be seen as a block of statements or codes that


performs a specific task. Every C program has at least one function, which
is main(), and all the most trivial programs can define additional functions.

You can divide up your code into several functions. Dividing your code into
different functions is up to you, but logically the division is such that each
function performs a specific task. Dividing your codes into functions helps
to reduce the length of your code and allows you to reuse and perform a
task over and over again without writing extensive amount of codes.

A function declaration tells the compiler about a function's name, return


type, and parameters(optional). A function definition provides the actual
body of the function.

P a g e 18 | 30
tutorials.vicololawade.com
STRUCTURED PROGRAMMING WITH C

2.7 C-LANGUAGE FUNCTIONS

Defining a function in C:
The general form of a function definition in C programming language is as follows

return_type function_name( parameter list ) {


body of the function
}

A function definition in C programming consists of a function header and a


function body. Here are all the parts of a function:

→ Return Type − A function may return a value. The return_type is the


data type(char, int, float, double) of the value the function returns. Some
functions perform the desired operations without returning a value. In
this case, the return_type is the keyword void.

→ Function Name − This is the actual name of the function. The function
name and the parameter list together constitute the function signature.

→ Parameters − A parameter is like a placeholder. When a function is


invoked, you pass a value to the parameter. This value is referred to as
actual parameter or argument. The parameter list refers to the type,
order, and number of the parameters of a function. Parameters are
optional; that is, a function may contain no parameters.

→ Function Body − The function body contains a collection of statements


that define what the function does.

Example of a function in C:
Given below is the source code for a function called max(). This function takes
two parameters num1 and num2 and returns the maximum value between the two:

P a g e 19 | 30
tutorials.vicololawade.com
STRUCTURED PROGRAMMING WITH C

2.7 C-LANGUAGE FUNCTIONS

/* function returning the max between two numbers */


int max(int num1, int num2) {
/* local variable declaration */
int result;

if (num1 > num2){


result = num1;
else{
result = num2;
}

return result;
}

Function Declarations in C:
A function declaration tells the compiler about a function name and how to call
the function. The actual body of the function can be defined separately. A function
declaration has the following parts:

return_type function_name( parameter list );


For the above defined function max(), the function declaration is as follows:

int max(int num1, int num2);


Parameter names are not important in function declaration only their type is required, so
the following is also a valid declaration:

int max(int, int);


Function declaration is required when you define a function in one source file and you
call that function in another file. In such case, you should declare the function at the top
of the file calling the function.

Calling a Function in C:
While creating a C function, you give a definition of what the function has to do.
To use a function, you will have to call that function to perform the defined task.
To call a function, you simply need to pass the required parameters along with
the function name(if parameters exists), and if the function returns a value, then you can
store the returned value. For example: max(14,5) //should return 14 since num1(14) is
greater than num2(5).
P a g e 20 | 30
tutorials.vicololawade.com
STRUCTURED PROGRAMMING WITH C

2.8 C-LANGUAGE DECISION MAKING, CONDITIONS & LOOPS

Decision making structures require that the programmer specifies one or


more conditions to be evaluated or tested by the program, along with a
statement or statements to be executed if the condition is determined to be
true, and optionally, other statements to be executed if the condition is
determined to be false.

Decision making statements contain conditions that are evaluated by the


program. If the condition is true, then a set of statements are executed and
if the condition is false then another set of statements is executed...
Otherwise, statements in the body of the else conditions are executed.

DECISION-MAKING STATEMENTS IN C LANGUAGE

If statement: The if statement is the most simple decision-making


statement. It is used to decide whether a certain statement or block of
statements will be executed or not i.e if a certain condition is true then a
block of statement is executed otherwise nothing is done. For example:

if(condition) {
// Statements to execute if
// condition is true
}

if-else statement: The if statement alone tells us that if a condition is true it


will execute a block of statements and if the condition is false it won’t. But
what if we want to do something else if the condition is false. Here comes
the C else statement. We can use the else statement with if statement to
execute a block of code when the condition is false. For example:

if(condition) {
// Executes this block if
// condition is true
} else {
// Executes this block if
// condition is false
}

P a g e 21 | 30
tutorials.vicololawade.com
STRUCTURED PROGRAMMING WITH C

2.8 C-LANGUAGE DECISION MAKING, CONDITIONS & LOOPS

if-else-if ladder statement: Here, a user can decide among multiple


options. The C if statements are executed from the top down. As soon as
one of the conditions controlling the if is true, the statement associated with
that if is executed, and the rest of the C else-if ladder is bypassed. If none
of the conditions are true, then the final else statement will be executed.
For example:

if(condition) {
// Executes this block if
// condition is true
} else if (condition2) {
// Executes this block if
// condition2 is true
} else if (condition3) {
// Executes this block if
// condition3 is true
} else {
// Executes this block if
//all conditions are false
}

switch statement: Switch case statement evaluates a given expression


and based on the evaluated value(matching a certain condition), it
executes the statements associated with it. The switch statement is a multi-
way branch statement. It provides an easy way to dispatch execution to
different parts of code based on the value of the expression. For example:

switch (n) {
case 1: // code to be executed if n = 1;
break;
case 2: // code to be executed if n = 2;
break;
default: // code to be executed if n doesn't match any cases
}

P a g e 22 | 30
tutorials.vicololawade.com
STRUCTURED PROGRAMMING WITH C

2.8 C-LANGUAGE DECISION MAKING, CONDITIONS & LOOPS

LOOPS IN C LANGUAGE

A loop is a block of code that will repeat over and over again. Loops in
programming come into use when we need to repeatedly execute a block
of statements. For example: Suppose we want to print “I am Vicol
Olawade” 40 times. Rather than typing printf(“I am Vicol Olawade”); on 40
separate lines in our program, we can create a loop that tells the computer
to print “I am Vicol Olawade” multiple times and stop once it prints the 40th
line. These stated above are basically what loops are for. We have 3 types
of loops in C Language, they are:

For loop: A for loop is a repetition control structure which allows us to write
a loop that is executed a specific number of times. The loop enables us to
perform n number of steps together in one line. For example:

for (int i=1; i<41; i++) {


printf(“I am Vicol Olawade \n”); // body of the loop
}
In the for loop above, we declared variable i as an integer and then assigned the value
of 1 to i (as the first parameter inside the brackets). Afterwards, we gave the condition
for which this code will run. Here, our program would continue to print I am Vicol
Olawade because the condition is true i.e 1 is surely less than 41. The third parameter
is the expression update. Here, we told our program to increment the value of i by 1
each time the code runs. (i++).
Logically, for the
1st iteration: i=1;1<41(CONDITION IS TRUE THEREFORE RE-RUN THE CODE);i++
2nd iteration: i=2;2<41(CONDITION IS TRUE THEREFORE RE-RUN THE CODE);i++
3st iteration: i=3;3<41(CONDITION IS TRUE THEREFORE RE-RUN THE CODE);i++
4st iteration: i=4;4<41(CONDITION IS TRUE THEREFORE RE-RUN THE CODE);i++
40st iteration: i=40;40<41(CONDITION IS TRUE THEREFORE RE-RUN THE
CODE);i++
41 iteration: i=41;41<41(CONDITION IS NOT TRUE THEREFORE TERMINATES
st

THE CODE AND JUMPS OUT OF THE LOOP);i++

P a g e 23 | 30
tutorials.vicololawade.com
STRUCTURED PROGRAMMING WITH C

2.8 C-LANGUAGE DECISION MAKING, CONDITIONS & LOOPS

While loop: While loops are used in situations where we do not know the
exact number of iterations of loop beforehand. The loop execution is
terminated on the basis of test condition. For example:

int i = 1;
while (i < 6) {
printf( "Hello World\n");
// update expression
i++;
}
In the while loop above, Our program would keep printing Hello World repeatedly for 5
times and would terminate at the 6th time because the condition will no longer be valid at
the 6th time since i(6) is not greater than 6.

do while loop: a do while loop is a control flow statement that executes a


block of code at least once, and then either repeatedly executes the block,
or stops executing it, depending on a given condition at the end of the block.
For example:

int i = 2; // Initialization expression


do {

// loop body
printf( "Hello World\n");
// update expression
i++;
} while (i < 1); // test expression
In the above program, the test condition (i<1) evaluates to false because 2 is not less
than 1 but this program will still print out a line of Hello World.

DIFFERENCES BETWEEN WHILE AND DO WHILE LOOPS

While loop checks the condition first and then executes the statement(s),
whereas do while loop will execute the statement(s) at least once, then the
condition is checked.
While loop statement(s) is executed zero times if the condition is false
whereas do while statement is executed at least once.

P a g e 24 | 30
tutorials.vicololawade.com
STRUCTURED PROGRAMMING WITH C

2.9 C-LANGUAGE ERROR

Errors are the problems or the faults that occur in the program, which
makes the behavior of the program abnormal, and experienced developers
can also make these faults. Programming errors are also known as the
bugs or faults, and the process of removing these bugs is known as
debugging.

TYPES OF ERRORS THAT EXISTS IN C LANGUAGE

1) SYNTAX ERROR: Syntax errors are also known as compilation errors


as they occur at compilation time. These errors are mainly occurred due
to the mistakes while typing or not following the syntax of the C
programming language. These mistakes are generally made by
beginners only because they are new to the language. These errors can
be easily debugged or corrected.
For example:

i. A semicolon(;) is used for terminating a statement in a code to


show the end of that line of code. A missing semicolon will cause
a syntax error.

ii. If we want to declare the variable of type integer, we write int a;


// this is the correct form BUT Int a; // this is an incorrect form.
This will generate a syntax Error due to the capital letter I starting
the keyword int.

2) RUN-TIME ERROR: Sometimes errors exist during the execution-time


even after the successful compilation known as run-time errors. When
the program is running, and it is not able to perform the operation is the
main cause of the run-time error. The division by zero is the common
example of the run-time error because the result is infinite. These
errors are very difficult to find, as the compiler does not point to these
errors.

3) LINKER ERROR: Linker errors are mainly generated when the


executable file of the program is not created. The most common linker
error that occurs is that we use Main() instead of main().

P a g e 25 | 30
tutorials.vicololawade.com
STRUCTURED PROGRAMMING WITH C

2.9 C-LANGUAGE ERROR

4) LOGICAL ERROR: The logical error is an error that leads to an


undesired output. These errors produce the incorrect output. These
types of mistakes are mainly done by beginners. The occurrence of
these errors mainly depends upon the logical thinking of the developer.
If the programmers sound logically good, then there will be fewer
chances of these errors.
For example:
int sum=0;int k = 1 // variable initialization
for(int i=1;i<=10;i++); // logical error, as we put the
semicolon after the for() loop
{
sum=sum+k;
k++;
}
printf("The value of sum is %d", sum);
Here, the programmer would expect the program to output: The value of sum. BUT
DUE TO THE PRESENCE OF A SEMICOLON AFTER THE for(), The output would
be a different thing entirely . This is an example of a logical error.

5) SEMANTIC ERROR: Semantic errors are the errors that occurred when
the statements are not understandable by the compiler. The following
can be the cases for the semantic error:

a) Use of a un-initialized variable.

b) Type compatibility i.e int b = “Vicol” //This will display an error


because Vicol is not an integer data type.

c) Errors in expressions i.e writing a+b = c; INSTEAD OF c = a+b;

d) Array index out of bound. i.e int a[10];a[10] = 13; //This will produce
an error because the size of the array is 10 and therefore, its
indexing should end at 9 instead of 10.

P a g e 26 | 30
tutorials.vicololawade.com
STRUCTURED PROGRAMMING WITH C

3.0 GLOSSARY & LEXICON


3.1 C-LANGUAGE GLOSSARY & LEXICON

Preprocessor Directives: Preprocessor directives are lines in your


program that start with # . The # is followed by an identifier that is the
directive name. All preprocessor commands begin with a hash symbol (#).
Example: #include, #define.

#include <stdio.h>: This is the preprocessor directive to include standard


input & output to a program. The stdio.h is a header file which has the
necessary information to include the input/output related functions in our
program.

Scope of an Identifier: The scope of an identifier is the range or region of


a program within which an identifier is recognized and can be used.

Inbuilt Functions: These are predefined functions in the library of the C.


They are used to perform the most common operations like calculations.
Some of the library functions are printf(), scanf(), sqrt(), etc. To use this
functions in the program the user have to use the associate header file
associated to the corresponding function in the program.

User defined Functions: These functions are designed by the user to their
own taste when they are writing any program. To perform such functions,
the user have to define the proper definition of the function.

main() Function: In C, program execution starts from the main() function.


Every C program must contain a main() function. The main function can
contain any number of statements. These statements are executed
sequentially in the order which they are written. The main function can in-
turn call other functions. When main calls a function, it passes the
execution control to that function. The function returns control to main when
a return statement is executed or when end of function is reached.

Statement: A statement is a command given to the computer that instructs


the computer to take a specific action, such as display to the screen, or
collect input.

P a g e 27 | 30
tutorials.vicololawade.com
STRUCTURED PROGRAMMING WITH C

3.1 C-LANGUAGE GLOSSARY & LEXICON

Expression: An expression is a combination of zero or more operators and


one or more operands that specifies the computation of a value.

Identifiers: We can say that an identifier is a collection of alphanumeric


characters that begins either with an alphabetical character or an
underscore, which are used to represent various programming elements
such as variables, functions, arrays, structures, unions, labels, etc.

Keywords: Keywords are predefined, reserved words used in


programming that have special meanings to the compiler. Keywords are
part of the syntax and they cannot be used as an identifier.

Escape Sequence: In C, all escape sequences consist of two or more


characters, the first of which is the backslash, \ (called the "Escape
character"); the remaining characters determine the interpretation of the
escape sequence. For example, \n is an escape sequence that denotes a
newline, \t is an escape sequence that denotes an horizontal tab, \? is an
escape sequence that denotes a question mark, \v is an escape sequence
that denotes a vertical tab, \b is an escape sequence that denotes a
backspace.

Pointer: A pointer is a variable that holds an address in a computer’s


memory.

Record: A record is a data structure for storing a fixed number of element.


It can store more than one data type together compared to an ARRAY.

const: An identifier can be declared a constant by using the const keyword


e.g const int a = 5 //a becomes a constant throughout the program with the
value of 5.

goto: This is used to transfer the control of a program to a specific label or


block. e.g if(i == 1) {goto test;} test: printf(“Error”);.

return: This is used to terminate the function and return a value.

static: Static is a keyword used in C language. It can be used with both


variables and functions, i.e we can declare a static variable and function.

P a g e 28 | 30
tutorials.vicololawade.com
STRUCTURED PROGRAMMING WITH C

3.1 C-LANGUAGE GLOSSARY & LEXICON

putchar: The putchar(int char) method in C is used to write a character, of


unsigned char type, to stdout.

getchar: The getchar function is part of the <stdio. h> header file in C. It is
used when single character input is required from the user.

break: Image result for what is break in c The break is a keyword in C


which is used to bring the program control out of the loop. The break
statement is used inside loops or switch statement.

continue: The continue statement terminates the current iteration and


resumes the control to the next iteration of the loop.

Compiler: A compiler is used to translate High Level Language into Low


Level Language. A compiler scans the entire program and translates it as a
whole into machine code at once.

Interpreter: An interpreter is used to translate High Level Language into


Low Level Language. An interpreter translates a program one statement at
a time.

Assembler: An assembler is used to translate Assembly Language into


Machine Language.

USEFULNESS OF FUNCTIONS

 By using functions, we can avoid rewriting same code over and over
again in a program.

 We can call C functions any number of times in a program and from any
place in a program.

 We can track a large C program easily when it is divided into multiple


functions therefore makes the modification of a program more easier.

 Re-usability is the main achievement of C functions as you can call a


function more than once In a program.

 Functions allows the break down a problem into smaller parts.

P a g e 29 | 30
tutorials.vicololawade.com
STRUCTURED PROGRAMMING WITH C

3.1 C-LANGUAGE GLOSSARY & LEXICON

RULES FOR DEFINING VARIABLES

 A variable can have alphabets, digits, and underscore.

 A variable name can ONLY start with an alphabet and underscore only.
It can’t start with a digit.

 No whitespace is allowed within the variable name.

 A variable name must not be any reserved word or keyword, e.g. int,
goto , etc.

P a g e 30 | 30
tutorials.vicololawade.com

You might also like