STUDY GUIDE
Unit: Computer Programming Principles
Topic Area: Arrays, Structures and Functions in C Programming
Level: Basic C Programming
GENERAL LEARNING OBJECTIVES
By the end of these notes and practical activities, the learner should be able to:
1. Explain the meaning and purpose of arrays, structures and functions in C programming.
2. Declare, initialize and use one-dimensional arrays to store related values.
3. Declare and use structures to group related data items under one user-defined data type.
4. Create and call simple user-defined functions in a C program.
5. Apply arrays, structures and functions to solve simple programming problems.
6. Compile, run, test and debug simple C programs using an appropriate C programming environment.
PART A: BASIC LECTURE NOTES
1.0 ARRAYS IN C PROGRAMMING
1.1 Meaning of an Array
An array is a collection of values of the same data type stored under one common name. Instead of
declaring many separate variables to store related values, a programmer can use an array. For example, if
a program needs to store marks for five learners, it is better to use one array named marks instead of
declaring mark1, mark2, mark3, mark4 and mark5.
An array is useful where the program needs to store several values that belong to the same category.
Examples include learner marks, temperatures, ages, prices, quantities, scores and salary values.
1.2 Why Arrays Are Important
They reduce the number of variables needed in a program.
They make it easier to process many related values using loops.
They help organize data in a structured way.
They make programs shorter, cleaner and easier to maintain.
They are useful in calculations such as totals, averages, maximum and minimum values.
1.3 Declaring an Array
The general syntax for declaring a one-dimensional array in C is:
data_type array_name[size];
Example:
int marks[5];
This statement declares an array named marks that can store five integer values.
1.4 Array Indexing
Array positions in C start from index 0. Therefore, an array with five elements has indexes 0, 1, 2, 3 and 4.
The first element is stored at index 0, while the last element is stored at index 4.
Array Element Index
First element 0
Second element 1
Third element 2
Fourth element 3
Fifth element 4
1.5 Initializing an Array
Array initialization means assigning starting values to an array at the time of declaration.
int marks[5] = {65, 70, 55, 80, 60};
In this example, the values are stored in the array as follows:
Index Value
0 65
1 70
2 55
3 80
4 60
1.6 Accessing Array Elements
An array element is accessed using the array name and its index. For example:
printf("%d", marks[0]);
This displays the first value stored in the marks array.
1.7 Entering Values into an Array
A loop can be used to enter several values into an array. The for-loop is commonly used because the number
of elements is usually known.
#include <stdio.h>
int main() {
int marks[5];
int i;
for (i = 0; i < 5; i++) {
printf("Enter mark %d: ", i + 1);
scanf("%d", &marks[i]);
}
printf("The marks entered are:\n");
for (i = 0; i < 5; i++) {
printf("%d\n", marks[i]);
}
return 0;
}
1.8 Calculating Total and Average Using an Array
Arrays are commonly used together with loops to calculate totals and averages. The following program
accepts five marks, calculates the total and displays the average.
#include <stdio.h>
int main() {
int marks[5];
int i, total = 0;
float average;
for (i = 0; i < 5; i++) {
printf("Enter mark %d: ", i + 1);
scanf("%d", &marks[i]);
total = total + marks[i];
}
average = total / 5.0;
printf("Total marks = %d\n", total);
printf("Average mark = %.2f\n", average);
return 0;
}
1.9 Common Mistakes When Using Arrays
Using an index outside the allowed range, such as marks[5] for an array declared as marks[5].
Forgetting that array indexing starts from 0.
Using the wrong data type for the values being stored.
Failing to initialize the total variable before adding array values.
Using a loop condition that runs too many or too few times.
1.10 Review Questions on Arrays
7. Define an array in C programming.
8. Why are arrays useful in programming?
9. What is the first index of an array in C?
10. Write a statement that declares an integer array named age with 10 elements.
11. State two errors a learner should avoid when using arrays.
2.0 STRUCTURES IN C PROGRAMMING
2.1 Meaning of a Structure
A structure is a user-defined data type in C that groups related variables of different data types under
one name. Unlike an array, which stores values of the same data type, a structure can store different
types of data together.
For example, a learner record may contain a name, admission number, age and marks. These values have
different data types, but they all describe one learner. A structure makes it possible to group them
together.
2.2 Why Structures Are Important
They allow related data to be grouped together.
They make it easier to represent real-world records such as learners, books, employees and products.
They improve program organization and readability.
They allow programmers to create their own data types.
They are useful when a program handles records with different fields.
2.3 Declaring a Structure
The general syntax for declaring a structure is:
struct structure_name {
data_type member1;
data_type member2;
data_type member3;
};
Example of a learner structure:
struct Learner {
char name[50];
int age;
float marks;
};
2.4 Creating a Structure Variable
After declaring a structure, a structure variable can be created. The variable is used to store actual data.
struct Learner learner1;
This creates a variable named learner1 based on the Learner structure.
2.5 Accessing Structure Members
The dot operator is used to access members of a structure variable.
[Link] = 20;
[Link] = 76.5;
In this example, values are assigned to the age and marks members of learner1.
2.6 Reading and Displaying Structure Data
#include <stdio.h>
struct Learner {
char name[50];
int age;
float marks;
};
int main() {
struct Learner learner1;
printf("Enter learner name: ");
scanf("%s", [Link]);
printf("Enter age: ");
scanf("%d", &[Link]);
printf("Enter marks: ");
scanf("%f", &[Link]);
printf("\nLearner Details\n");
printf("Name: %s\n", [Link]);
printf("Age: %d\n", [Link]);
printf("Marks: %.2f\n", [Link]);
return 0;
}
2.7 Structure With Several Records
A structure can be combined with an array to store several records. For example, an array of structures
can store details for several learners.
#include <stdio.h>
struct Learner {
char name[50];
int age;
float marks;
};
int main() {
struct Learner learners[3];
int i;
for (i = 0; i < 3; i++) {
printf("Enter details for learner %d\n", i + 1);
printf("Name: ");
scanf("%s", learners[i].name);
printf("Age: ");
scanf("%d", &learners[i].age);
printf("Marks: ");
scanf("%f", &learners[i].marks);
}
printf("\nLearner Records\n");
for (i = 0; i < 3; i++) {
printf("%s %d %.2f\n", learners[i].name, learners[i].age, learners[i].marks);
}
return 0;
}
2.8 Common Mistakes When Using Structures
Forgetting the semicolon after the closing brace of a structure declaration.
Using a structure member before creating a structure variable.
Using the wrong format specifier when reading or displaying structure members.
Confusing arrays and structures. Arrays store same-type values, while structures can group different
types.
Forgetting to use the dot operator when accessing structure members.
2.9 Review Questions on Structures
12. Define a structure in C programming.
13. Give two differences between an array and a structure.
14. Write a structure declaration for a Book with title, pages and price.
15. What operator is used to access members of a structure variable?
16. State two examples of real-world records that can be represented using structures.
3.0 FUNCTIONS IN C PROGRAMMING
3.1 Meaning of a Function
A function is a named block of code that performs a specific task. A function helps divide a program into
smaller and manageable parts. In C programming, every program must have the main function because
program execution begins from main.
Functions support modular programming. This means that a large program can be divided into smaller
sections, where each section performs a clear task.
3.2 Why Functions Are Important
They reduce repetition of code.
They make a program easier to read and understand.
They make testing and debugging easier.
They support code reuse.
They help programmers divide large problems into smaller tasks.
3.3 Types of Functions
C programming uses two main categories of functions:
Type of Function Meaning Example
Library function A function already provided by C libraries. printf(), scanf(), sqrt()
User-defined function A function created by the programmer to calculateArea(),
perform a specific task. displayMenu()
3.4 Parts of a Function
A user-defined function normally has the following parts:
Return type: the type of value the function returns, such as int, float or void.
Function name: the name used to call the function.
Parameter list: values passed into the function for processing.
Function body: the statements that perform the task.
Return statement: sends a result back to the calling part of the program, where necessary.
return_type function_name(parameter_list) {
statements;
return value;
}
3.5 Function Without Parameters and Without Return Value
This type of function performs a task but does not receive values and does not return a result.
#include <stdio.h>
void displayMessage() {
printf("Welcome to C Programming\n");
}
int main() {
displayMessage();
return 0;
}
3.6 Function With Parameters and Return Value
This type of function receives values, processes them and returns a result.
#include <stdio.h>
int addNumbers(int a, int b) {
int sum;
sum = a + b;
return sum;
}
int main() {
int num1, num2, answer;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
answer = addNumbers(num1, num2);
printf("Sum = %d\n", answer);
return 0;
}
3.7 Function Prototype
A function prototype tells the compiler about a function before the function is fully defined. It states the
function name, return type and parameters.
int addNumbers(int a, int b);
A prototype is commonly placed before the main function when the function definition appears after main.
3.8 Example: Function to Calculate Area of a Rectangle
#include <stdio.h>
float calculateArea(float length, float width) {
float area;
area = length * width;
return area;
}
int main() {
float length, width, area;
printf("Enter length: ");
scanf("%f", &length);
printf("Enter width: ");
scanf("%f", &width);
area = calculateArea(length, width);
printf("Area = %.2f\n", area);
return 0;
}
3.9 Common Mistakes When Using Functions
Calling a function using the wrong name.
Using parameters that do not match the function definition.
Forgetting to return a value in a function that has a non-void return type.
Forgetting to place brackets after the function name when calling it.
Confusing function declaration, function call and function definition.
3.10 Review Questions on Functions
17. Define a function in C programming.
18. State three advantages of using functions.
19. Differentiate between a library function and a user-defined function.
20. What is the purpose of a function prototype?
21. Write a function header for a function named multiply that receives two integers and returns an
integer.