INTRODUCTION TO C PROGRAMING
LANGUAGE
The C programming language is a high-level, general purpose
procedural language used in the development of computer
software and applications, system programming, games, etc. It is
widely used in a variety of applications, including operating
systems, device drivers, and application software. C is known
for its efficiency, portability, and flexibility and often used to
learn other programming languages such as C++ and Java.
Brief History of C
C language was developed by Denis Ritchie at Bell Laboratories
and was first developed for the programming of the UNIX
operating system. Most of the code for general-purpose
operating systems is written in C or C++.
Today, C is widely used in embedded devices, and it powers
most of the Internet servers, which are built using Linux. The
Linux kernel is built using C, and this also means that C powers
the core of all Android devices. We can say that C code runs a
good portion of the entire world.
Let us introduce the first C program now, which we'll call it
"My First, Program!"
#include <stdio.h>
int main(void) {
printf("My First, Program!");
}
All valid C programs must contain the main() function. The
code execution begins from the start of the main() function. The
main() function is the entry point of any C program.
The above program first imports the “stdio” library (the name
stands for standard input-output library).
This library gives us access to input/output functions. stdio is
the library that provides the printf() function. A function is a
routine that takes one or more arguments, and returns a single
value.
Definition of some basic terms in C
• 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. Examples: int, float, break, else, long, for,
while, #include, etc.
• Identifiers: refers to name given to entities such as
variables, functions, etc. Identifiers must be unique. They
are created to give a unique name to an entity to identify it
during the execution of the program.
• Variables and Constants: Variable is a container (storage
area) to hold data. To indicate the storage area, each
variable should be given a unique name (identifier).
Variable names are just the symbolic representation of a
memory location. For example:
int age = 20;
Here, age is a variable of int (integer) type. Here, the
variable is assigned an integer value 20.
Constants - are variables whose values cannot be changed.
You can use the const keyword. This will create a constant.
For example:
const double PI = 3.14;
• Data types - are declarations for variables. This determines
the type and size of data associated with variables. For
example: int myVar;
Here, myVar is a variable of int (integer) type. The
size of int is 4 bytes.
int (integers) are whole numbers that can have both
zero, positive and negative values but no decimal
values. For example, 0, -5, 10, 2345, etc.
We can use int for declaring an integer variable.
Example, int id;
Here, id is a variable of type integer.
float and double - are used to hold real numbers
(numbers with decimal place). For example:
float salary;
double price;
char - is used for declaring character type variables.
For example: char test = 'h';
• Input/Output (I/O)
Output - In C programming, printf() is one of the main
output function. The function sends formatted output to the
screen. For example:
#include <stdio.h>
int main() {
// Displays the string inside quotations
printf("C Programming");
return 0;
}
To use printf() in our program, we need to include stdio.h
header file using the #include <stdio.h> statement.
Example 2: Integer Output
#include <stdio.h>
int main()
{
int myAge = 25;
printf("My Age is = %d", myAge);
return 0;
}
We use %d format specifier to print int types. Here, the
%d inside the quotations will be replaced by the value of
myAge.
Example 3: float and double Output
#include <stdio.h>
int main()
{
float number1 = 13.5;
double number2 = 12.4;
printf("number1 = %f\n", number1);
printf("number2 = %lf", number2);
return 0;
}
To print float, we use %f format specifier. Similarly, we
use %lf to print double values.
Example 4: Print Characters
#include <stdio.h>
int main()
{
char chr = 'a';
printf("character = %c", chr);
return 0;
}
Input - 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.
Example: Integer Input/Output
#include <stdio.h>
int main()
{
int myAge;
printf("Enter your Age: ");
scanf("%d", &myAge);
printf("My Age is = %d",myAge);
return 0;
}
Here, we have used %d format specifier inside the scanf()
function to take int input from the user. When the user
enters an integer, it is stored in the myAge variable.
Example 6: Float and Double Input/Output
#include <stdio.h>
int main()
{
float num1;
double num2;
printf("Enter a number: ");
scanf("%f", &num1);
printf("Enter another number: ");
scanf("%lf", &num2);
printf("num1 = %f\n", num1);
printf("num2 = %lf", num2);
return 0;
}
Input/Output Multiple Values
Here's how you can take multiple inputs from the user and
display them.
#include <stdio.h>
int main()
{
int a;
float b;
printf("Enter integer and then a float: ");
// Taking multiple inputs
scanf("%d","%f", &a, &b);
printf("You entered %d and %f", a, b);
return 0;
}
Input/Output Format Specifiers
As you can see from the above examples, we use
%d for int
%f for float
%lf for double
%c for char
• C Programming Operators - An operator is a symbol that
operates on a value or a variable. For example: + is an
operator to perform addition. C has a wide range of
operators to perform various operations.
Arithmetic Operators - An arithmetic operator
performs mathematical operations such as addition,
subtraction, multiplication, division etc. on numerical
values (constants and variables).
Operator Meaning of Operator
+ addition or unary plus
- subtraction or unary minus
* multiplication
/ division
% modulo division (remainder after
division)
The modulo operator % computes the remainder.
When a=9 is divided by b=4, the remainder is 1. The
% operator can only be used with integers.
Increment and Decrement Operators - the
increment ++ and decrement -- operators change the
value of an operand (constant or variable) by 1.
Increment ++ increases the value by 1 whereas
decrement -- decreases the value by 1. These two
operators are unary operators, meaning they only
operate on a single operand.
Example of how to use an increment or decrement
operators
#include <stdio.h>
int main()
{
int a = 10, b = 100;
float c = 10.5, d = 100.5;
printf("++a = %d \n", ++a);
printf("--b = %d \n", --b);
printf("++c = %f \n", ++c);
printf("--d = %f \n", --d);
return 0;
}
Here, the operators ++a and --b are used as prefixes. These
two operators can also be used as postfixes like a++ and b--
Assignment Operators - is used for assigning a value
to a variable. The most common assignment operator
is =
Operator Example Same as
= a=b a=b
+= a += b a = a+b
-= a -= b a = a-b
*= a *= b a = a*b
/= a /= b a = a/b
%= a %= b a = a%b
Relational Operators - checks the relationship
between two operands. If the relation is true, it returns
1; if the relation is false, it returns value 0.
Relational operators are used in decision making and
loops.
Operator Meaning of Operator Example
== Equal to 5 == 3 is evaluated to 0
> Greater than 5 > 3 is evaluated to 1
< Less than 5 < 3 is evaluated to 0
!= Not equal to 5 != 3 is evaluated to 1
>= Greater than or equal to 5 >= 3 is evaluated to 1
<= Less than or equal to 5 <= 3 is evaluated to 0
Logical Operators - An expression containing logical
operator returns either 0 or 1 depending upon whether
expression results true or false. Logical operators are
commonly used in decision making in C
programming.
Operator Meaning Example
&& Logical AND. True only if If c = 5 and d = 2 then,
all operands are true expression ((c==5) && (d>5))
equals to 0.
|| Logical OR. True only if If c = 5 and d = 2 then,
either one operand is true expression ((c==5) || (d>5))
equals to 1.
! Logical NOT. True only if If c = 5 then, expression !(c==5)
the operand is 0 equals to 0.
Explanation of logical operator program
o (a == b) && (c > ) evaluates to 1 because both operands (a == b) and
(c > b) is 1 (true).
o (a == b) && (c < b) evaluates to 0 because operand (c < b) is 0 (false).
o (a == b) || (c < b) evaluates to 1 because (a = b) is 1 (true).
o (a != b) || (c < b) evaluates to 0 because both operand (a != b) and (c <
b) are 0 (false).
o !(a != b) evaluates to 1 because operand (a != b) is 0 (false). Hence,
!(a != b) is 1 (true).
o !(a == b) evaluates to 0 because (a == b) is 1 (true). Hence, !(a == b)
is 0 (false).
• Bitwise Operators - During computation, mathematical
operations like: addition, subtraction, multiplication,
division, etc. are converted to bit-level which makes
processing faster and saves power.
Bitwise operators are used in C programming to perform
bit-level operations.
Operators Meaning of operators
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
~ Bitwise complement
<< Shift left
>> Shift right
• Other Operators
Comma Operator - Comma operators are used to link
related expressions together. For example:
int a, c = 5, d;
The sizeof operator - The sizeof is a unary operator
that returns the size of data (constants, variables,
array, structure, etc).
Other operators are ternary operator ?:, reference
operator &, dereference operator * and member
selection operator ->
Summary
A problem is a situation that is to be resolved using well-
defined mathematical principles
Solution is a means of finding solution to a problem
Problem solving is an act of finding a solution to perplexing,
distressing, vexing or unsettled question
Computer cannot be used to solve problems relating to
physical activities or emotional problems
Computer is very good at solving repetitive tasks in fast and
consistent manner
The four Polya’s problem solving strategies are:
Understanding the problem, Device a plan, Carrying out the
plan and Looking back
An algorithm is a sequential set of instructions that are
followed in other to solve a problem using a finite amount of
data and time.
In problem solving one problem can have many algorithm
while one algorithm can be implemented using many
programs
The choice of algorithm to be chosen for a problem depends
on the following factors: Reliability, Accuracy, Ease of
modification and Execution time
An algorithm consist of three components: input, process and
output
An algorithm consist of five properties: Finiteness,
Definiteness, Effectiveness, Generality, and Input/output
An algorithm can be expressed in three ways: Natural
Language, Pseudo Code, and Flow Chart.
The two methodologies used in the development of algorithm
are top-down design and object oriented design (OOD).
Top-down design is algorithm development approach that
involves decomposition of a large problem into manageable
parts, that can be solved and unified to form the solution of
the overall problem
A module is a self-contained set of steps needed for the
solution of problems
In top down-design approach, the problems are divided in a
structured chart that is composed of modules.
Abstract step is a step in the algorithm that contains some
unspecified details
Concrete step is a step in the algorithm for which there is
no need for further specification of details.
Software Development Method is made up of the following
implementation strategies: requirement specification,
analysis, design, implementation, testing and maintenance.
Stepwise refinement is an algorithm development process
that involves providing in each step all the details needed
to solve a problem.
Desk checking is the process of simulating the computer
execution process by going through the algorithm step-
by-step in other to detect and correct errors.
Abstraction is the process of modeling a problem by
extracting the relevant information from the problem.
Exercises
Algorithm questions
1. Write an algorithm to calculate simple interest
2. Write an algorithm to calculate area of triangle
3. Write an algorithm to find the largest of three numbers x, y, z
4. Write an algorithm to test if a given integer value is prime or not
Programming questions
1. Write a C program to calculate simple interest
2. Write a C program to calculate area of triangle
3. Write a C program to find the largest of three numbers x, y, z
4. Write a C program to test if a given integer value is prime or not
Other questions
1. Write an algorithm that will read the height of a cylinder and the radius of its
base and prints its volume and surface area. The volume is computed as πr2h
and the surface area of a cylinder is 2πrh + 2πr2.
2. Draw the flowchart of the algorithm above.
3. Write a C program that implements the algorithm above. You should prompt
the user to enter the height and radius and then compute the volume and
surface area of the cylinder.
4. ABC company plans to give a 6% year-end bonus to each of its employees
earning N6,000 or more per month and a fixed N250/bonus to the remaining
employees. Draw a flowchart for calculating the bonus for an employee.
5. Write an algorithm to calculate the simple interest using the formula:
= x x /100, Where is principle Amount, is the
number of years and is the rate of interest.
Step 1: Start
Step 2: Read the three input quantities’ P, N and R.
Step 3: Calculate simple interest = P* N* R/100
Step 4: Print simple interest.
Step 5: Stop.
6. Design an algorithm to add these test scores: 26, 49, 98, 87, 62, 75 and
obtain the Average score
Step 1: Start
Step 2: Sum = 0
Step 3: Input 26, 49, 98, 87, 62, 75
Step 4: Sum = 26 + 49 + 98 + 87 + 62 + 75
Step 5: Average = Sum/6
Step 6: Output Average
Step 7: Stop
7. Find the area of a Circle of radius r.
Step 1: Start
Step 2: Read\Input the Radius
Step 3: Area = PI * Sqr(r)
Step 4: Print Area
Step 5: Stop
8. Write an algorithm to read two numbers and find their sum.
Step 1: Start
Step 2: Read\Input the first num1
Step 3: Read\Input the second num2
Step 4: sum = num1 + num2
Step 5: Print Sum
Step 6: Stop
9. Design an algorithm and the corresponding flowchart for finding the sum of
n numbers.
Step 1: Start
Step 2: Sum = 0
Step 3: Input n
Step 4: For(I = 1, I <= n, I+1)
Step 5: Input a number
Step 6: Sum = Sum + number
Step 7: ENDFOR
Step 8: Output Sum
Step 9: Stop
10. Write an algorithm to determine a student’s final grade and indicate whether
passed or failed. The final grade is calculated as the average of four marks.
References
Government of Andhra Pradesh. (n.d.). Board of Intermediate Education.
Retrieved from bieap.gov.in/Pdf/CSPaperII.pdf
Institute of Distance & Open Learning (IDOL). (n.d.). University of Mubai.
Retrieved from archive.mu.ac.in/myweb_test/syllFybscit/C++.pdf
Koffman, J. R. (2016). Problem Solving and Program Design in C. England:
Pearson Education Limited.
McQuain. (2011, 12). CS 2104: Introduction to Problem Solving. Retrieved from
Computer Science@Virginia Tech:
courses.cs.vt.edu/cs2104/Fall12/notes/T16_Algorithms.pdf
Nell Dale, John Lewis. (2002). C o m p u t e r Sc i e n c e Illuminated. Singapore:
Jones and Bartlett.