A statement in a program is like a sentence in English. It is the smallest complete executable unit. A statement consists of expressions and /or keywords and ends with a semicolon (;).
There are five types of statements in C programming language:
i. Declaration statements
ii. Expression statements
iii. Jump statements
iv. Selection statements
v. Iteration statements
vi. Compound statements
a. Declaration statements
In C programming language, a declaration statement is a statement formed from a primary expression of declaration that introduces one or more identifiers into the program and specifies their meaning and properties.
In C programming language, a declaration statement is used to declare a variable, a named constant, or a function. The syntax of a declaration statement looks like:
data_type variable_name; //declare a variable
or
data_type variable_name = initial_value; //declare a variable with initial value
or
data_type function_name(parameter_list); //declare a function prototype
For example,
int my_var = 0;
In this example, variable my_var is declared with initial value 0.
In C programming language, an expression statement is a statement that consists of an expression followed by a semicolon. The execution of such a statement will get an evaluated result of the expression. Most C programs' statements are expression statements.
Here are some examples of expression statements in C:
a = 5; // assignment statement formed from assignment expression
printf("Hello World!"); // function call statement from a function call expression
x++; // increment statement from arithmetic expression
In C programming language, a jump statement is a statement that causes the program to transfer control to another part of the program.
The four types of jump statements in C are:
goto statement
The goto statement in C is a jump statement that is used to jump from one part of the code to any other part of the code in C. It helps in altering the normal flow of the program according to our needs. The goto statement can be used to jump from anywhere to a specified place in a function. When the C compiler reaches the goto statement, it will jump unconditionally (both forward and backward) to the location specified in it (we called it as a label). However, it is highly discouraged as the goto statement makes the program logic very complex and makes tracing the flow of the program very difficult. The use of goto makes it hard to analyze and verify the correctness of programs. The use of goto can be simply avoided by using break and continue statements.
The goto statement is formed from goto primary expression. For example (please test it in the Online C Compiler (programiz.com)),
#include <stdio.h>
int main() {
int i = 0;
loop: //label
printf("%d\n", i);
i++;
if (i < 10) {
goto loop; //goto statement
}
return 0;
}
In this example, the goto statement transfers control back to the beginning of the statement labeled “loop”.
break statement
The break statement is used to stop continuing to execute a loop. The break statement is formed from break primary expression. For example (please test it in the Online C Compiler (programiz.com)),
#include <stdio.h>
int main() {
for (int i = 0; i < 15; i++) {
if (i == 5) {
break; //break statement
}
printf("%d\n", i);
}
return 0;
}
In this example, the break statement is used to exit the loop when the value of i is equal to 5. The result should be
0
1
2
3
4
continue statement
The continue statement is used to skip the current iteration of a loop and move on to the next iteration. It is formed from continue primary expression. For example (please test it in the Online C Compiler (programiz.com)),
#include <stdio.h>
int main() {
for (int i = 0; i < 15; i++) {
if (i == 5) {
continue; //continue statement
}
printf("%d\n", i);
}
return 0; //return statement
}
In this example, the continue statement is used to skip the loop when the value of i is equal to 5, and continue the next cycle of the loop. The result should be
0
1
2
3
4
6
7
8
9
10
11
12
13
14
return statement
The return statement returns a value from inside a function. The return statement is formed from a return primary expression as we can see from the above example.
The goto statement is harmful and should not be applied in programming practice. The other three jump statements are used in loops and functions to control the flow of execution. We will see more of their usage and examples when we discuss loops and functions.
d. Selection or conditional statements
In C programming language, selection statements are used to control the flow of execution based on the value of an expression.
The two types of selection statements in C are if- statement and switch statement:
i. if statement
An if statement is a selection statement that allows for conditional execution of code based on the value of an expression. For example (please test it in the Online C Compiler (programiz.com)),
#include <stdio.h>
int main() {
int x = 5;
//the statement below is an example of if-statement
if (x == 5) {
printf("x is equal to 5\n");
}
return 0;
}
As shown in the example, if statement consists of three parts:
1) the if keyword.
2) the conditional expression enclosed in a pair of parentheses.
3) the if statement body enclosed in a pair of curly brackets. When the if statement body consists of only a single statement, the curly brackets can be omitted such as
if(x==5) printf("x is equal to 5\n");
Often, keywords if and else combine to form multiple branches or nested if statements, which we will see more examples when we discuss control structures later.
ii. switch statement
In C programming language, switch statements are used to control the flow of execution based on the value of an expression. For example (please test it in the Online C Compiler (programiz.com)),
#include <stdio.h>
int main() {
int x = 2;
//Example of a switch statement
switch (x) {
case 1:
printf("x is equal to 1\n");
break;
case 2:
printf("x is equal to 2\n");
break;
default:
printf("x's value is not 1 or 2\n");
}
return 0;
}
As shown in the example, the switch statement consists of three parts:
1) the switch keyword.
2) the conditional expression enclosed in a pair of parentheses.
3) the switch statement body enclosed in a pair of curly brackets. The switch statement body consists of one or more case branches and end with a default branch. A case branch consists of a case header and a case body. The case header is in the form of
case value:
It consists of keyword case and a possible value of the conditional expression in the parentheses. In this example, 1 and 2 are the possible values of x. The case header ends with colon (:).
The case body consists of a few statements, which can be any types of statements. In this example, there is only a printf() statement in a case. The case body ends with a break statement, which stop continuing to execute the other branches below.
The default branch is the last branch in the switch body, which shall be executed once no case branch meets the condition.
The main difference between if statements and switch statements is that if statements are used when there are only two conditions to check, whereas switch statements are used when there are multiple conditions to check.
e. Loop or iterative statements
Iterative statements in C are used to execute a block of code repeatedly for a specified number of times or until a condition is met. They change the control flow of the program so that they can also be classified as control statements in C . There are three types of iterative statements in C: For Loop, While Loop and Do-while loop. A loop consists of three parts: conditional value initialization, test expression of condition, and increment/decrement or update the conditional value. For different types of loops, the conditional expressions might be at various places of the loop.
i. For loop
The For Loop is one of the three types of iterative statements in C. It is the most commonly used loop because it has all the three parts of the loop in the one line. The syntax of a for loop look like:
for(initialization; test expression; update expression) {
//body of loop
}
A for loop consists of three parts:
1) the keyword for.
2) the condition enclosed in a pair of parentheses.
3) the for loop body enclosed in a pair of curly brackets.
The condition consists of three parts: the initialization, the test expression, and the update expression.
The initialization part is executed only once at the beginning of the loop. It is the entry condition to enter the loop.
The test expression is evaluated at the beginning of each iteration. If it is true, then the body of the loop is executed. otherwise, the program execution moves on to the next statement following this for loop.
After each iteration, the update expression is executed before evaluating the test expression again.
The for-loop body consists of a number of other statements. When there is zero or one statement in the for loop, the curly brackets can be omitted.
Here's an example of a for loop:
#include <stdio.h>
int main() {
int i;
//for loop
for (i = 1; i <= 5; i++) {
printf("%d\n", i);
}
return 0;
}
This program initializes the variable i to 1, then tests whether i is less than or equal to 5. If it is, it executes the body of the loop (which prints the value of i), then increments i by 1. It repeats this process until i is no longer less than or equal to 5.
ii. While loop
The while loop is another type of iterative statement in C. It executes the statement(s) in the loop body as long as a given condition is true. The syntax of a while loop in C looks like:
while (condition) {
statement(s);
}
A while loop consists of three parts:
1) the keyword while.
2) the condition enclosed in a pair of parentheses.
3) the while loop body enclosed in a pair of curly brackets.
The condition is of expression that produces a Boolean result, true or false. In C, false is 0, true is any nonzero value.
The while loop body, statement(s), may be a single statement or multiple statements. Below is an example of while loop:
#include <stdio.h>
int main() {
int i = 1;
//while loop statement
while (i <= 5) {
printf("%d\n", i);
++i;
}
return 0;
}
This program initializes the variable i to 1, then tests whether i is less than or equal to 5. If it is, it executes the body of the loop (which prints the value of i), then increments i by 1. It repeats this process until i is no longer less than or equal to 5.
iii. Do-while loop
The do-while loop is another one of the three iterative statements in C. It is popularly used to traverse arrays, vectors, and some other data structures. The do-while loop is similar to the while loop but the body of do-while loop is executed at least once and then, the test expression is evaluated. The syntax of the do...while loop looks like:
do {
statement(s);
} while (condition);
Do-while loop consists of three parts:
1) the keyword do.
2) the loop body enclosed in the curly brackets.
3) the while condition with the keyword while and condition enclosed in a pair of parentheses.
Please note that the conditional expression is at the end of the loop, so the statement(s) in the loop executes once before the condition is tested.
Here's an example of a do-while loop that prompts the user to enter a number (please test it in the Online C Compiler (programiz.com)):
#include <stdio.h>
int main() {
double number, sum = 0;
do {// The loop body is executed first
printf("Enter a number: ");
scanf("%lf", &number);
sum += number;
} while(number != 0.0); // The test expression is evaluated
printf("Sum = %.2lf",sum);
return 0;
}
This program prompts the user to enter a number, then it tests the conditional expression. The loop shall work until the input number is 0. The do-while loop executes at least once because the first iteration executes without checking the condition.
A compound statement is a group of statements that are enclosed in curly braces `{}`. It is used where multiple statements are required. The compound statement is treated as a single statement by the compiler. The syntax of a compound statement looks like:
{
statement1;
statement2;
.
.
.
statementN;
}
That is, a group of statements enclosed in a pair of curly brackets. Here, statement1, statement2, ..., statementN are any valid C statements as discussed above. The compound statements can be nested in another compound statements.
The compound statements are usually used as a structure body, function body, loop body, selection statement body, and so on.
A block in C is a set of statements written within a pair of curly braces ({}). A block may contain other blocks within itself, that is, nested blocks. A variable declared inside a block has a block scope and is accessible inside the block and its inner blocks.
A compound statement is actually a block.
An array in C is a collection of similar data items stored in contiguous memory locations. The data items in an array can be of primitive data types such as int, char, float, etc., and also derived and user-defined data types such as pointers, structures, and so on. The elements of an array are accessed by their index number. The index number starts from 0 and goes up to (size of the array - 1). The size of an array must be specified at the time of declaration.
Arrays are used in C for a variety of reasons. They are used to store a collection of similar data items. They are also used to store the collection of derived and user-defined data types such as pointers, structures, etc. Arrays are also used to pass a large number of arguments to functions. They are also used to implement other data structures such as stacks, queues, and trees.
The syntax of declaring an array in C is:
type arrayName[arraySize];
Here, type is the type of data that the array will hold. arrayName is the name of the array, and arraySize is the number of elements that the array can hold. For example,
int numbers[5];
This declares an array called numbers that can hold 5 integers.
Once an array is declared, its elements are accessed by their index numbers. The index number starts from 0 and goes up to (size of the array - 1). For example,
int numbers[7] = {1, 2, 3, 4, 5, 6, 7};// An array of 7 int items is declared and initialized
printf("%d", numbers[0]);// The first item is printed
This prints the first element of the array, which is `1`.
We can easily loop through an array with a for loop. For example,
int numbers[7] = {1, 2, 3, 4, 5, 6, 7};
for (int in = 0; in < 7; in++) {
printf("%d\n", numbers[in]);
}
This program shall print all the seven elements of the array.
Arrays are popularly used in processing a group of same type of data. For example, we want to sum up a group of integers, we can use array to easily implement it.
#include <stdio.h>
int main() {
int numbers[7] = {1, 2, 3, 4, 5, 6, 7};
int sum = 0;
for(int in = 0; in < 7; in++) {
sum += numbers[in];
}
printf("The sum of all the elements of the array is %d", sum);
}
This program creates an array of integers called numbers with five elements and initializes them with values from 1 to 5. It then uses a for loop to iterate over the array and add up all the values. Finally, it prints out the sum of the array.
In C programming, we can create an array of arrays, which is known as multidimensional arrays. For example,
float x[2][3];
Here, x is a two-dimensional (2d) array. The array can hold 6 elements. You can regard this array as a table that has 2 rows and has3 columns in a row, which looks like
x[0][0] x[0][1] x[0][2]
x[1][1] x[1][2] x[1][2]
To access an element from the multidimensional array, add the index for the element you want. For example, x[1][2] will access the element at the second row and the third column of the array. You can also use a for loop or for each loop to access multiple elements in a multidimensional array.
We can use a nested for loops (one for loop inside another for loop) to access elements of a multidimensional array in C. Here is an example of how to use a for loop to initialize each member of a 2D array one by one:
int x [2] [3];
for (int ind1 = 0; ind1 < 2; ind1++) {
for (int ind2 = 0; ind2 < 3; ind2++) {
x [ind1] [ind2] = ind1 + ind2;
}
}
This method is useful when the value of each element has some sequential relation.
Strings are used in C programming language to represent text such as words and sentences. They are also used to manipulate text data. For example, you can use strings to store user input or read data from a file. Strings are also used in functions such as printf() and scanf().
In C programming language, a string is an array of characters that is terminated by a null character \0 (this character is viewable in the text). A string can be declared as an array of characters (V, 2019) . For example:
char str[] = "Hello World!";
Here, str is an array of 13 characters. The null character `\0` automatically gets added at the end of the string.
We can process strings in C using various string functions such as strlen(), strcpy(), strcat(), and strcmp(). These functions are defined in the string.h header file. You can also use loops to iterate over the characters in a string and perform operations on them. For example:
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
int len;
printf("Enter a string: ");
scanf("%s", str);
len = strlen(str);
printf("The string length is %d.\n", len);
return 0;
}
This program reads a string from the user using scanf() and then uses the strlen() function to find the length of the string. The length of the string is then printed to the console.
Here is another example about processing string:
#include <stdio.h>
#include <string.h>
int main() {
char str1[100] = "Hello ";
char str2[] = "World!";
strcat(str1, str2);
printf("%s", str1);
return 0;
}
This program concatenates two strings using strcat(): "Hello " and "World!". The result is stored in the first string str1. The output of this program is "Hello World!".
The advantages of math processing in C include the ability to perform complex mathematical calculations and operations quickly and accurately. This is especially useful in scientific and engineering applications where precise calculations are required. C also provides a wide range of math functions that can be used to perform various mathematical tasks such as calculating square roots, exponents, and trigonometric functions.
Besides arithmetic operators such as +, -, *, /, and % to perform basic math operations, C provides various math functions such as sqrt(), pow(), sin(), cos(), tan(),floor(), ceil(), fabs(), rand(), srand(), etc.. These functions are defined in the math.h header file.
Here is an example of how to use math processing in C (please test it in the Online C Compiler (programiz.com)):
#include <stdio.h>
#include <math.h>
int main() {
double x = 2.0;
double y = sqrt(x);
printf("The square root of %f is %f.\n", x, y);// Display a string that contains two float variables.
return 0;
}
This program calculates the square root of a number using the sqrt() function from the math.h header file. The output of this program shall be "The square root of 2.000000 is 1.414214."
Here is an example of how to use the floor() and ceil() functions in C (please test it in the Online C Compiler (programiz.com)):
#include <stdio.h>
#include <math.h>
int main() {
double num = 8.33;
int myCeiling, myFloor;
myCeiling= ceil(num);
printf("Ceiling integer of %.2f = %d\n", num, myCeiling);
myFloor= floor(num);
printf("Floor integer of %.2f = %d\n", num, myFloor);
return 0;
}
This program uses the ceil() and floor() functions to find the ceiling and floor integers of a given floating-point number.
Here is an example of how to use the rand() function in C:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
printf("%d ", rand());
printf("%d", rand());
return 0;
}
This program generates two random numbers using the rand() function.
C programming language provides several functions for input and output operations. The functions used for standard input and output are present in the stdio.h header file. To use those functions, we need to include the stdio.h header file in our program. The following two streams are used while dealing with input-output operations in C:
i. Standard Input (stdin)
ii. Standard Output (stdout)
Standard input or stdin is used for taking input while standard output or stdout is used for giving output.
The printf() function is one of the main output functions in C programming language. The function prints formatted text to the screen. The syntax of printf() function in C programming language is:
printf("format string", argument_list);
Here, "format string" is a string that contains text to the output and may contain format specifiers for variables to display. The argument_list are the variable names corresponding to the format specifiers. For example,
printf("Ceiling integer of %.2f = %d\n", num, myCeiling);
This statement applies printf() function to display a string with format specifiers %.2f for a float num and %d for an integer myCeiling.
The scanf() is a function used to receive input from the user. The scanf() function reads formatted input from the standard input, generally, the keyboard. The syntax of scanf() function in C programming language is:
scanf("format string", &argument_list);
Here, "format string" is a string that contains the format specifier(s), and &argument_list is a list of addresses of the specified variables.
Format specifiers used in both printf() and scanf() are special operators that define the type of data to be printed on standard output with printf() function. They start with a percent (%) character and may have arguments of their own. A complete list of format specifiers is in table 1 above.
Some of the commonly used format specifiers in C are listed below:
i. `%d` - integer
ii. `%f` - float
iii. `%c` - character
iv. `%s` - string
v. `%p` - pointer
For example, if you want to print an integer value using printf() function, you can use the `%d` format specifier. Similarly, if you want to read an integer value using scanf() function, you can use the `%d` format specifier.
To limit the decimal digits printed with printf(), we can specify the precision field in the format specifiers. The precision field specifies the maximum number of characters to be printed for the corresponding argument. For example,
printf("%.2f", 3.14159);
This will print the value with only two decimal places.
scanf() reads characters from the input stream until it encounters a whitespace character (space, tab, newline) or until it reaches the end of the input stream.
Here is an example of using scanf() and printf() functions in C programming language:
#include <stdio.h>
int main() {
float num;
printf("Enter a number: ");
scanf("%f", &num);
printf("The number is: %.3f", num);
return 0;
}
This program reads in a float value from the user and displays it with 3 decimal places on the screen.
Beside printf() and scanf(), C provides two other popularly used functions for input and output: putchar() and getchar().
The putchar() function writes a single character to the standard output stream. It takes a single argument of type int which represents the character to be written. While getchar() reads a single character from the standard input stream and returns it as an integer value.
Here is an example of using getchar() and putchar() input and output in C programming language:
#include <stdio.h>
int main() {
int c;
printf("Enter a value: ");
c = getchar();
printf("\nYou entered: ");
putchar(c);
return 0;
}
This program takes an input from the user and displays it on the screen.
In programming, control structures are used to specify the flow of control in a program. They are used to analyze and choose in which direction a program flows based on certain parameters or conditions. There are three fundamental types of flow of control, or logic, known as:
- Sequence logic, or sequential flow
- Selection logic, or conditional flow
- Iteration logic, or repetitive flow
In C programming language, sequential flow is the default flow of execution. The program executes one statement after another in their order as in the source code of a function.
Conditional flow and repetitive flow change the default sequential flow in a program.
C provides three main control structures to implement the conditional flow and the repetitive flow: decision-making control structure, looping control structure, and unconditional control structure.
Decision-making control structure allows you to execute one set of statements when an expression is true and another set of statements when the expression is false. This control structure implements conditional flow using conditional statements such as if, if-else, nested if-else, and switch statements. If there is some condition for a block of statements, the execution flow may change according to the evaluated result of the conditional expression. An example of an if-else statement is below:
#include<stdio.h>
int main(){
int num = 10;
if(num > 0) {
printf("%d is a positive number\n", num);
}
else{
printf("%d is a negative number\n", num);
}
return0;
}
In this example, the program checks whether the variable num is greater than zero. If it is, the program prints a string saying that number is a positive number. Otherwise, it prints another string saying that number is a negative number (programiz.com).
Looping control structure allows you to execute a set of statements repeatedly while a condition is true. This control structure implements repetitive flow using looping statements such as for loop, while loop and do-while loop. For examples of looping control structure, please refer to section 5.e. Loop or iterative statements.
Unconditional control structure allows you to jump to another part of the program. This control structure is implemented using jump statements such as goto, break, continue, and return. While break, continue, and return are very useful in helping make conditional flow and repetitive flow, goto statements bring much negative effects in programming because they make the code harder to read and understand and can also make the code more difficult to maintain and debug. For these reasons, it is generally recommended to avoid using goto statements. For examples of unconditional control structure, please refer to section 5.c. Jump statements.
In C programming language, a pointer is a variable that stores the memory address of another variable. Pointers are used in C programming language for a variety of purposes such as:
Dynamic memory allocation
Passing arguments to functions by reference
Building complex data structures such as linked lists and trees (Shilpa, 2018)
Accessing hardware devices and memory-mapped I/O
Implementing low-level operations such as bit manipulation and pointer arithmetic
The * operator is used to declare a pointer:
int *ptr;
This declares a pointer variable called ptr that can point to an integer value.
To use the pointer, you can assign it the address of another variable using the & operator. Here is an example:
int var = 5;
int *ptr = &var;
This assigns the address of var to the pointer variable ptr. You can then use the pointer to access the value stored in var using the * operator. Here is an example:
printf("%d", *ptr);
This prints the value stored in var, which is 5.
Here is an example of how to use a pointer to process an array in C:
int arr[3] = {1, 2, 3};
int *ptr = arr;
for(int index = 0; index < 3; index++) {
printf("%d ", *(ptr + index));
}
This assigns the address of the first element of the array arr to the pointer variable ptr and then prints all the elements of the array using the * operator.
Memory handling in C refers to the process of allocating and deallocating memory during program execution. This is typically done using functions such as malloc(), calloc(), realloc(), and free() in header file stdlib.h.
In C programming language, the heap and stack are two regions of memory used for memory allocation. The heap is used for dynamic memory allocation and the stack is used for static memory allocation.
The stack is a region of memory used to store local variables and function call information. When a function is called, the stack is used to store the return address and the values of the function’s parameters and local variables. For example,
#include <stdio.h>
int main() {
int x = 5;
printf("%d\n", x);
return 0;
}
This program automatically uses the stack to store the value of the variable x.
In C programming language, the stack is organized as a LIFO (Last In First Out) data structure. Data stored in an Array can mimic the LIFO operation on a stack by incrementing the index to store a data in the array or decrementing the index to fetch a data from the array. For example (please test it in the Online C Compiler (programiz.com)),
#include <stdio.h>
#include <stdlib.h>
int main() {
int stack[5];
int top = -1;
// Push operation
stack[++top] = 5;
// Pop operation
int item = stack[top--];
return 0;
}
However, we can define functions, push() and pop(), to implement operations on a stack. Function push() is used to store a data on to a stack, and function pop() is used to fetch a data from the stack. For example (please test it in the Online C Compiler (programiz.com)),
#include <stdio.h>
// Define a struct for stack elements
typedef struct stack_element {
int data; // The data stored in the element
struct stack_element *next; // A pointer to the next element in the stack
} stack_element;
// Define a struct for stack
typedef struct stack {
int size; // The number of elements stored in the stack
stack_element *top; // A pointer to the top element of the stack
} stack;
// Initialize an empty stack
stack *create_stack() {
stack *s = malloc(sizeof(stack)); // Allocate memory for the stack
s->size = 0; // Set the size to zero
s->top = NULL; // Set the top to NULL
return s; // Return the stack
}
// Defining function push*() that stores an element to the top of the stack
void push(stack *s, int data) {
stack_element *e = malloc(sizeof(stack_element)); // Allocate memory for the element
e->data = data; // Set the data of the element
e->next = s->top; // Set the next of the element to the current top of the stack
s->top = e; // Set the top of the stack to the new element
s->size++; // Increment the size of the stack
}
// Defining function pop() that removes an element from the top of the stack and return its data
int pop(stack *s) {
if (s->size == 0) { // If the stack is empty, return -1 as an error code
return -1;
}
int data = s->top->data; // Get the data of the top element
stack_element *e = s->top; // Get a pointer to the top element
s->top = s->top->next; // Set the top of the stack to the next element
free(e); // Free the memory of the popped element
s->size--; // Decrement the size of the stack
return data; // Return the data of the popped element
}
int main(){
printf("To create a stack\n");
stack *myStack = create_stack();
push(myStack, 1);
push(myStack, 2);
int top1 = pop(myStack);
printf("The top value is %d\n", top1);
int top2 = pop(myStack);
printf("The second top value is %d\n", top2);
return 0;
}
The output shall be:
To create a stack
The top value is 2
The second top value is 1
This program shows how to build the structure of a stack, and how to define functions push() and pop(). In the main() function, a stack called myStack is declared, and then pushed 1 and 2 onto myStack by using function push(). After that, it uses function pop() to pop the two data from the stack. Since 2 is on top of 1, the result show the sequence 2 and 1. From this example, we can see the FILO sequence of data in a stack.
In C programming language, the heap is a region of memory used for dynamic memory allocation, which allocates memory during program execution. Heap is useful when it is necessary to repeatedly remove the object with the highest (or lowest) priority, or when insertions need to be interspersed with removals of the root node, which are the cases of dynamically allocate and deallocate memory for multiple programs to run in an operating system.
In C, malloc() is used for dynamic memory allocation. It is useful when you don’t know the amount of memory needed during compile time. calloc() is also used for dynamic memory allocation. It is similar to malloc() but it initializes the allocated memory with zeros. realloc() is used to resize the previously allocated memory. free() is used to deallocate memory that was previously allocated by malloc(), calloc(), or realloc().
Here is an example of using calloc() in C (please test it in the Online C Compiler (programiz.com)):
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr;
int n = 5;
ptr = (int*)calloc(n, sizeof(int));
if (ptr == NULL) {
printf("Memory not allocated.\n");
exit(0);
} else {
printf("Memory successfully allocated using calloc.\n");
for (int seq= 0; seq< n; ++seq) {
ptr[seq] = seq+ 1;
}
printf("The stored elements of the array are: ");
for (int seq= 0; seq< n; ++seq) {
printf("%d, ", ptr[seq]);
}
}
free(ptr);
return 0;
}
Here is an example of multiple memory allocations in C (please test it in the Online C Compiler (programiz.com)):
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr1, *ptr2;
int num1 = 13, num2 = 17;
ptr1 = (int*)calloc(num1, sizeof(int));
ptr2 = (int*)calloc(num2, sizeof(int));
printf("Memory successfully allocated using calloc.\n");
free(ptr1);
free(ptr2);
return 0;
}
a. General introduction of files
In C, files are needed to store data on secondary drives. Memory stores data temporarily and loses data when a program terminates. Files stores data permanently even if the program terminates because files are stored on secondary drives such as hard drive, CD/DVD, SSD, etc.
From the perspective of what file content is stored, there are text files and binary files. A text file contains data in the form of ASCII characters. Each line in a text file ends with a new line character (‘\n’). It can be read or written by any text editor. A binary file contains data in binary form (i.e. 0’s and 1’s) instead of ASCII characters. They contain data that is stored in a similar manner to how it is stored in the main memory. The binary files can be created only from within a program and their contents can only be read by a program (www.geeksforgeeks.org, 2023) .
From the perspective of how a file content is read/write, there are sequential access and random access. With sequential access, the file has to be rewound to the beginning and data is read from or written to a location count from the beginning in a manner like using a tape. With random access, the data can be read from any were and written to anywhere in the file.
The general steps in processing a file are:
i. Declare a file pointer variable.
ii. Open a file using fopen() function.
iii. Process the file using the suitable function.
iv. Close the file using fclose() function.
C provides multiple file handling functions like file creation, opening and reading files, writing to the file, searching inside of the file, and closing a file. These operations can be performed using different functions such as fopen(), fwrite(), fread(), fseek(), fprintf(), fclose(), etc. They are all defined in <stdio.h> header file. Before I give examples of how to process files in C, I'd like to list the prototypes of these functions for reference.
Note: A function prototype is a statement that tells the compiler about the function’s name, its return type, numbers and data types of its parameters. By using this information, the compiler cross-checks function parameters and their data type with function definition and function call.
You can skip to the next section about handling sequential files if you are not interested in the syntax right now. You can refer them back when you need.
The prototype of fopen()function in C is:
FILE *fopen(const char *filename, const char *mode);
This function opens a file and returns a pointer to the file. Here is what each parameter means:
filename: This is the name of the file to be opened.
mode: This is the file access mode: reading, writing, appending, reading and writing, or reading and appending. The mode specifies the type of access that will be permitted to the file. Here are the modes that can be used:
i. "r": Open for reading. The file must exist.
ii. "w": Open for writing. If the file exists, its contents shall be over-written. Otherwise, it shall be created.
iii. "a": Open for appending. If the file exists, the data shall be written at the end of the file. Otherwise, it shall be created.
iv. "r+": Open for reading and writing. The file must exist.
v. "w+": Open for reading and writing. If the file exists, its contents shall be over-written. Otherwise, it shall be created.
vi. "a+": Open for reading and appending. If the file exists, the data shall be written at the end of the file. Otherwise, it shall be created.
The fopen() function shall return a pointer to the file stream. A null pointer is returned if the file cannot be opened.
The prototype of fwrite() function in C is:
size_t fwrite(const void *ptr, size_t size, size_t count, FILE *stream); (fwrite C Library Function (techcrashcourse.com) )
This function writes data to a file. Here is what each parameter means:
ptr: This is the pointer that points to the array of elements to be written.
size: This is the size in bytes of each element to be written (Asadi, 1970).
count: This is the number of elements, each one with a size of size bytes (Asadi, 1970).
stream: This is the pointer to a FILE object that specifies an output stream (Asadi, 1970).
The total number of elements successfully returned are returned as a size_t object, which is an integral data type.
The prototype of fread() function in C is as follows:
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream); (www.tutorialspoint.com)
This function read data from a file. Here is what each parameter means:
ptr: This is the pointer that points to a block of memory with a minimum size of size*nmemb bytes.
size: This is the size in bytes of each element to be read(www.tutorialspoint.com) .
nmemb: This is the number of elements, each one with a size of size bytes(www.tutorialspoint.com) .
stream: This is the pointer to a FILE object that specifies an input stream(www.tutorialspoint.com) .
The prototype of fseek() function in C is as follows:
int fseek(FILE *stream, long int offset, int whence); (www.tutorialspoint.com)
The fseek() function in C is used to move the file pointer position to a specific location within the file. Here is what each parameter means:
stream: This is the pointer that points to a FILE object that specifies an input stream.
offset: This is the number of bytes to offset from whence (brainly.in).
whence: This is the position from where the offset is added. It can be one of the following:
The function returns 0 if successful.
The prototype of fprintf() function in C is as follows:
int fprintf(FILE *stream, const char *format, ...);
The function returns the number of characters written to the output stream. Here is what each parameter means:
stream: This is the pointer that points to a FILE object that specifies an output stream.
format: This is the string that contains the text to be written to the output stream.
...: This is the variable argument list.
The prototype of fclose() function in C is as follows:
int fclose(FILE *stream);
This function closes the currently opened file. Here is what the parameter means:
The function returns 0 if successful.
b. Handling sequential files
Here is an example code snippet that reads and prints the contents of a sequential file:
#include <stdio.h>
int main() {
FILE *fp;
char ch;
fp = fopen("file.txt", "r"); //open file.txt for reading
if (fp == NULL) {//if the file pointer points to null, print error and return 1
printf("Error opening file\n");
return 1;
}
while ((ch = fgetc(fp)) != EOF) {//read file using fgetc() until end of file
printf("%c", ch);//print the char
}
fclose(fp); //close the file
return 0;
}
This code opens the file "file.txt" in read mode using fopen()function. It then reads the contents of the file using fgetc() function and prints them to the console using printf() function. Finally, it closes the file using fclose() function.()
Note: in order to correctly run this program, you should run it on your locally installed C compiler (or IDE such as Microsoft Visual Studio Code) and create a file named file.txt. The online C compiler shall not point to a local file without a path to point to.
Note: Standard C library function fopen() is used to open a file. It returns a pointer to a FILE structure so that the file variable fp should be a pointer.
Note: In C, EOF is a constant integer value that means "End of File".
c. Handling random access files
Here is an example code snippet that reads and prints the contents of a random-access file:
#include <stdio.h>
int main() {
FILE *fp;
char ch;
fp = fopen("myFile.txt", "r+"); //open a file for reading and writing
if (fp == NULL) {//if the file pointer is null, print error and return 1
printf("Error opening file\n");
return 1;
}
fseek(fp, 5, SEEK_SET); //moves the file pointer to the 5th byte from the beginning of the file.
ch = fgetc(fp); //reads a single character from the file pointed to by fp
printf("%c\n", ch); //print the character
fseek(fp, -2, SEEK_CUR); //moves the file pointer 2 bytes back from the current position.
ch = fgetc(fp); //reads a single character from the file
printf("%c\n", ch); //print the character
fclose(fp); //close the file
return 0;
}
This code opens the file "myFile.txt" in read/write mode using fopen()function. It then uses fseek() function to move the file pointer to a specific location within the file. It then reads the contents of the file using fgetc() function and prints them to the console using printf() function. Finally, it closes the file using fclose() function.
Note: in order to correctly run this program, you should run it on your locally installed C compiler (or IDE such as Microsoft Visual Studio Code) and create a file named myFile.txt. The online C compiler shall not point to a local file without a path to point to.
Note: please refer back to section "General introduction of files" above for constants SEEK_SET, SEEK_CUR, and SEEK_END. They are the third argument to the function fseek() to specify the reference position in the file.
Functions in C are self-contained blocks of code that perform a specific task. They are application of "divide and conquer" technique in programming that break down a large program into smaller and more manageable pieces. They make the code more modular and easier to read and maintain. Functions can also be reused in different parts of the program or in different programs altogether.
Here is the syntax of how a function is defined in C:
type function_name(parameter list) {
// function body
}
Here is what each part means:
type : This is the data type of the value returned by the function. It can be any data type discussed in the data type section above. If the function does not return anything, the return type shall be void.
function_name: This is the name of the function.
parameter list: This is the list of parameters that the function takes.
function body: This is the code that performs the task of the function.
Here is an example of a function that returns an integer in C:
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int main() {
int sum = add(3, 5); //call the user defined function add(), assign its value to sum.
printf("The sum is %d\n", sum);
return 0;
}
This code defines a function called add() that takes two integer arguments and returns their sum. The function is then called from the main()function and the result is printed to the console.
Here is an example of a void function in C:
#include <stdio.h>
void add(int i, int j) //this function does not return a value, so it is void
{
printf("%d + %d = %d", i, j , (i+j));
}
int main()
{
int i,j;
i = 1;
j=2;
add( i, j); //call the function add(). Since it is void, it cannot be assigned to a variable
return 0;
}
This function takes two integer arguments and returns nothing. It simply prints the sum of the two integers passed to it.
In C programming language, a function prototype is a declaration of a function that specifies the function's name and type signature (arity and types of arguments) but omits the function body. The syntax of function prototype in C programming is as follows:
type function_name (type argument1, type argument2,...);
Here, type is the data type of the value returned by the function. function_name is the name of the function. type is the data type of the arguments passed to the function. The arguments are optional. If there are no arguments, then an empty pair of parentheses is used. If there are multiple arguments, commas are applied to separate them. The semicolon at the end of the prototype is required.
It is used to tell the compiler about the existence of a function before it is actually defined. This is useful when a program calls a function before its definition. The prototype serves as an agreement between the caller and the callee about what the function will do and what arguments it will take, helps in detecting errors at compile-time rather than at runtime. Using function prototype allows declaration of functions being placed in header files, so that they can be compiled separately from programs and be called by programs as a library.
Here is an example of using function prototype in C programming language (please test it in the Online C Compiler (programiz.com)):
#include <stdio.h>
// Function prototype
int add(int x, int y);
int main() {
int a = 10;
int b = 20;
int sum = add(a, b);
printf("The sum of values %d and %d is %d\n", a, b, sum);
return 0;
}
// Function definition
int add(int x, int y) {
return x + y;
}
In this example, we have declared a function prototype for the add() function before the main() function. The prototype tells the compiler that the add() function takes two integer arguments and returns an integer value. The add() function definition is actually placed after the main() function.
c. Pass by value vs. pass by reference
In C programming language, there are two ways to pass arguments to a function: pass by value and pass by reference.
Pass by value means that the function receives a copy of the argument’s value. Any changes made to the parameter inside the function have no effect on the original argument.
Pass by reference means that the function receives a reference to the original argument. Any changes made to the parameter inside the function shall affect the original argument.
In C, pass by value is used by default. To pass an argument by reference, you should use a pointer.
Note: an argument is a value that is passed to a function when the function is called, while a parameter is a variable in the declaration of a function. The parameter is used to receive the argument that are passed during a function call.
For example, the above program is an example of pass by value: arguments a and b are passed to parameters x and y in function add(int x, int y). Values of a and b are copied to function add() so that any change on x and y in the function definition shall not have effects on a or b.
Here is an example of pass by reference in C programming language(please test it in the Online C Compiler (programiz.com)):
#include <stdio.h>
void swap(int *x, int *y) {
int temp = *x;
*x = *y;
*y = temp;
}
int main() {
int a = 10;
int b = 20;
printf("Before swapping: the values a = %d and b = %d\n", a, b);
swap(&a, &b);
printf("After swapping: the values a = %d and b = %d\n", a, b);
return 0;
}
In this example, we have defined a function swap() that takes two integer pointers as arguments. The function swaps the values of the two integers using pointers. Inside the main() function, we declare two integer variables a and b. We then call the swap() function with the addresses of these variables using the & operator. This allows us to pass the variables by reference rather than by value. In pass by reference, any changes on x or y shall affect the values of a and b.
Recursion is a technique in C programming language where a function calls itself repeatedly until a certain condition is met. A function that calls itself is called as a recursive function and the technique is called as recursion.
Recursion is used when a problem can be broken down into smaller subproblems that are similar in nature to the original problem. The function calls itself with a smaller version of the problem until it reaches the base case, which is the smallest version of the problem that can be solved without recursion. So that the base case is to stop the recursion.
Here is an example of recursion in C programming language (please test it in the Online C Compiler (programiz.com)):
#include <stdio.h>
int factorial(int n) {
if (n == 0) {
return 1;
} else {
return n * factorial(n - 1);
}
}
int main() {
int m = 5;
int fac = factorial(n);
printf("The result of factorial of %d is %d\n", m, fac);
return 0;
}
In this example, we have defined a function factorial() that takes an integer argument (n). The function calculates the factorial of n using recursion. In the main() function, we declare an integer variable n and assign it the value 5. We then call the factorial() function with n as an argument. The function calculates the factorial of 5 using recursion and returns the result.
Recursion has its advantages and disadvantages in C programming language.
Advantages:
Recursion can make the code more elegant and easier to understand.
Recursion can be used to solve problems that are difficult or impossible to solve using iteration.
Recursion can reduce the number of variables needed in a program.
Disadvantages:
Recursion can be slower than iteration due to the overhead of maintaining the stack.
Recursion can use more memory than iteration due to the stack.
Recursion can cause stack overflow errors if not implemented carefully.
(Referred in July ~ September 2023)
#Define in C | How does C# directive work in C with Examples? (educba.com)
262588213843476 (no date) C++ Pointer tutorial, Gist. Available at: https://gist.github.com/ericandrewlewis/720c374c29bbafadedc9
Accessing multi-dimensional arrays in C using pointer notation. https://stackoverflow.com/questions/43907616/accessing-multi-dimensional-arrays-in-c-using-pointer-notation.
ANSI c - Are all functions in C global? - Stack Overflow. https://stackoverflow.com/questions/60932140/are-all-functions-in-c-global.
Arrays - C# Programming Guide | Microsoft Learn. https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/.
Basic Input and Output in C - GeeksforGeeks. https://www.geeksforgeeks.org/basic-input-and-output-in-c/.
Basics of File Handling in C - GeeksforGeeks. https://www.geeksforgeeks.org/basics-file-handling-c/.
Blocks | Microsoft Learn. https://learn.microsoft.com/en-us/cpp/c-language/blocks?view=msvc-170.
C - File I/O - GeeksforGeeks. https://www.geeksforgeeks.org/c-file-io/.
C - Input and Output | Tutorialspoint - Online Tutorials Library. https://www.tutorialspoint.com/cprogramming/c_input_output.htm.
C - Recursion | Tutorialspoint - Online Tutorials Library. https://www.tutorialspoint.com/cprogramming/c_recursion.htm.
c - using “push” and “pop” in a stack - Stack Overflow. https://stackoverflow.com/questions/18755948/using-push-and-pop-in-a-stack.
c - When and why to use malloc - Stack Overflow. https://stackoverflow.com/questions/8800482/when-and-why-to-use-malloc.
c - Why does 'fopen' return a NULL pointer? - Stack Overflow. https://stackoverflow.com/questions/5987626/why-does-fopen-return-a-null-pointer.
C <stdio.h> SEEK_SET, SEEK_CUR, SEEK_END - AlphaCodingSkills - Java. https://www.alphacodingskills.com/c/notes/c-stdio-fseek-arguments.php.
C Arrays - GeeksforGeeks. https://www.geeksforgeeks.org/c-arrays/.
C Arrays (With Examples) - Programiz. https://www.programiz.com/c-programming/c-arrays.
C Coding Standard (no date) C coding standard. Available at: https://users.ece.cmu.edu/~eno/coding/CCodingStandard.html (Accessed: 29 August 2023).
C Conditional Statement: IF, IF Else and Nested IF Else with Example. https://www.guru99.com/c-if-else-statement.html.
C Dynamic Memory Allocation Using malloc (), calloc (), free .... https://www.programiz.com/c-programming/c-dynamic-memory-allocation.
C fabs() - C Standard Library - Programiz. https://www.programiz.com/c-programming/library-function/math.h/fabs.
C Files I/O: Create, Open, Read, Write and Close a File - Guru99. https://www.guru99.com/c-file-input-output.html.
C Files I/O: Opening, Reading, Writing and Closing a file - Programiz. https://www.programiz.com/c-programming/c-file-input-output.
C fopen() function with Examples - GeeksforGeeks. https://www.geeksforgeeks.org/c-fopen-function-with-examples/.
C fread() Function - GeeksforGeeks. https://www.geeksforgeeks.org/fread-function-in-c/.
C Function Recursions - W3Schools. https://www.w3schools.com/c/c_functions_recursion.php.
C global function (not C++) - Stack Overflow. https://stackoverflow.com/questions/28634427/c-global-function-not-c.
C Input/Output functions - printf(), scanf(), etc. | Studytonight. https://www.studytonight.com/c/c-input-output-function.php.
C Input/Output: printf() and scanf() - Programiz. https://www.programiz.com/c-programming/c-input-output.
C keywords defined in ANSI, C99 and C11 (no date) C Programming Simple Steps. Available at: https://www.c-programming-simple-steps.com/c-keywords.html (Accessed: 29 August 2023).
C library function - fopen() | Tutorialspoint.
C Local and Global Variables - onlinetutorialspoint. https://www.onlinetutorialspoint.com/c-program/c-local-and-global-variables.html.
C Math - W3Schools. https://www.w3schools.com/c/c_math.php.
C Multidimensional Arrays (2d and 3d Array) - Programiz. https://www.programiz.com/c-programming/c-multi-dimensional-arrays.
C Pass By Reference - Learn C Programming from Scratch. https://www.learnc.net/c-tutorial/c-pass-by-reference/.
c printf and scanf - W3schools. https://www.w3schools.blog/c-printf-and-scanf.
C printf() and scanf() functions - Tutorials Class. https://tutorialsclass.com/c-printf-scanf/.
C printf() function - w3resource. https://www.w3resource.com/c-programming/c-printf-statement.php.
C Program for Input/Output of Integer, Character and Floating point numbers. https://btechgeeks.com/c-program-input-output-integer/.
C Programming - File Input/Output - University of Utah. https://users.cs.utah.edu/~germain/PPS/Topics/C_Language/file_IO.html.
C Programming Tutorial on Random Access File Handling - ThoughtCo. https://www.thoughtco.com/random-access-file-handling-958450.
C Programming: Control Structure - SlideShare. https://www.slideshare.net/sokngimlasy/c-programming-control-structure.
C prototype functions - Stack Overflow. https://stackoverflow.com/questions/4914589/c-prototype-functions.
C Recursion (Recursive function) - Programiz. https://www.programiz.com/c-programming/c-recursion.
C while and do...while Loop - Programiz. https://www.programiz.com/c-programming/c-do-while-loops.
C While Loop - W3Schools. https://www.w3schools.com/c/c_while_loop.php.
C while loop - W3Schools. https://www.w3schools.in/c-programming/loops/while.
C# String (With Examples) - Programiz. https://www.programiz.com/csharp-programming/string.
C, Initialize 2D array with a for loop - Stack Overflow. https://stackoverflow.com/questions/22992911/c-initialize-2d-array-with-a-for-loop.
C++ <cstdio> SEEK_SET, SEEK_CUR, SEEK_END - AlphaCodingSkills - Java. https://alphacodingskills.com/cpp/notes/cpp-cstdio-fseek-arguments.php.
C++ fabs() - C++ Standard Library - Programiz. https://www.programiz.com/cpp-programming/library-function/cmath/fabs.
C++ float and double - Programiz. https://www.programiz.com/cpp-programming/float-double.
C++ malloc() - GeeksforGeeks. https://www.geeksforgeeks.org/cpp-malloc/.
calloc - cppreference.com. https://en.cppreference.com/w/c/memory/calloc.
calloc | Microsoft Learn. https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/calloc?view=msvc-170.
calloc function in C language with an example program. https://sillycodes.com/calloc-function-in-c-language-with-an-example-program/.
calloc() Function in C - Scaler Topics. https://www.scaler.com/topics/calloc-function-in-c/.
calloc() Function in C Library with Program EXAMPLE - Guru99. https://www.guru99.com/calloc-in-c-example.html.
calloc() in C / C++ - OpenGenus IQ. https://iq.opengenus.org/calloc-in-c/.
calloc() versus malloc() in C - Online Tutorials Library. https://www.tutorialspoint.com/calloc-versus-malloc-in-c.
Compiling a C Program: Behind the Scenes - GeeksforGeeks. https://www.geeksforgeeks.org/compiling-a-c-program-behind-the-scenes/.
Control structures and statements in C and C++ - CircuitsToday. https://www.circuitstoday.com/control-structures-in-c-and-cpp.
CONTROL STRUCTURES IN C - Tutorialswebsite. https://www.tutorialswebsite.com/control-structures-c/.
Control Structures in C: Getting Started with Control Structures. https://www.skillsoft.com/course/control-structures-in-c-getting-started-with-control-structures-d0ef6d33-9d6a-48e6-ba1d-a448478bbc4c.
Control Structures in Programming Languages - GeeksforGeeks. https://www.geeksforgeeks.org/control-structures-in-programming-languages/.
Cornell Virtual Workshop: Program Flow & Conditional Statements. https://cvw.cac.cornell.edu/Cintro/flow.
Data Structures - GeeksforGeeks. https://www.geeksforgeeks.org/data-structures/.
Data Structures in C - Computer Notes. https://ecomputernotes.com/what-is-c/data-structures-in-c.
Difference Between printf and scanf in C Language - PHPTPOINT. https://www.phptpoint.com/difference-between-print-and-scanf-in-c/.
do...while Loop in C - GeeksforGeeks. https://www.geeksforgeeks.org/c-do-while-loop/.
do...while loop in C | Tutorialspoint. https://www.tutorialspoint.com/cprogramming/c_do_while_loop.htm.
Dynamic Memory Allocation in C using malloc(), calloc() Functions - Guru99. https://www.guru99.com/c-dynamic-memory-allocation.html.
Dynamic Memory Allocation in C using malloc(), calloc(), free() and .... https://www.geeksforgeeks.org/dynamic-memory-allocation-in-c-using-malloc-calloc-free-and-realloc/.
End of File (EOF) in C - Stack Overflow. https://stackoverflow.com/questions/4358728/end-of-file-eof-in-c.
EOF, getc() and feof() in C - GeeksforGeeks. https://www.geeksforgeeks.org/eof-and-feof-in-c/.
Fabs and abs in C++ - OpenGenus IQ. https://iq.opengenus.org/fabs-and-abs-in-cpp/.
fabs() Function in C - GeeksforGeeks. https://www.geeksforgeeks.org/fabs-function-in-c/.
FIFA Women’s World Cup 2023: All fixtures, results, goalscorers, and .... https://olympics.com/en/news/fifa-women-world-cup-2023-results-scores-standings-points-table.
File Handling in C | GATE Notes - BYJU'S. https://byjus.com/gate/file-handling-in-c/.
File Handling in C Language - W3Schools. https://www.w3schools.in/c-programming/file-handling.
Files used in C Programming - Tutorial Ride. https://www.tutorialride.com/c-programming/files-used-in-c-programming.htm.
floating point - Number of decimal digits in C - Stack Overflow. https://stackoverflow.com/questions/12761493/number-of-decimal-digits-in-c.
Floating-point numeric types - C# reference | Microsoft Learn. https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/floating-point-numeric-types.
fopen() Function Usage In C and C++ with Examples – POFTUT. https://www.poftut.com/fopen-function-usage-in-c-and-cpp-with-examples/.
for loop - Iterating 2D array with pointer expressions in C - Stack .... https://stackoverflow.com/questions/26125746/iterating-2d-array-with-pointer-expressions-in-c.
FOR loop in C programming language – Iteration Statements - CodinGeek. https://www.codingeek.com/tutorials/c-programming/for-loop-in-c-programming-language-iteration-statements/.
for-each-loop in c++ for two dimensional array - Stack Overflow. https://stackoverflow.com/questions/51223535/for-each-loop-in-c-for-two-dimensional-array.
Format Specifiers in C - freeCodeCamp.org. https://www.freecodecamp.org/news/format-specifiers-in-c/.
fread() function in C - Scaler Topics. https://www.scaler.com/topics/fread-function-in-c/.
fread() Function in C++ - Scaler Topics. https://www.scaler.com/topics/cpp-fread/.
fseek(), SEEK_SET, SEEK_CUR, SEEK_END functions in C - Fresh2Refresh. https://www.fresh2refresh.com/c-programming/c-file-handling/fseek-seek_set-seek_cur-seek_end-functions-c/.
Function Descriptions : fopen - SAS Support. https://support.sas.com/documentation/onlinedoc/ccompiler/doc700/html/lr1/z2055143.htm.
Function prototype - Wikipedia. https://en.wikipedia.org/wiki/Function_prototype.
Function Prototype in C - GeeksforGeeks. https://www.geeksforgeeks.org/function-prototype-in-c/.
Function Prototype in C - Scaler Topics. https://www.scaler.com/topics/what-is-a-function-prototype-in-c/.
Function Prototype: Definition, Concept, Use of Void, Examples - Toppr. https://www.toppr.com/guides/computer-science/programming-in-c-/introduction-to-user-defined-function-and-its-requirements/function-prototype/.
Getchar and Putchar in C: A Comprehensive Guide - HPlus Academy. https://hplusacademy.com/getchar-and-putchar-in-c/.
getchar Function in C - GeeksforGeeks. https://www.geeksforgeeks.org/getchar-function-in-c/.
Getty Images. https://www.gettyimages.com/detail/photo/big-data-analytics-visualization-technology-with-royalty-free-image/1205428317.
Greg Perry et el, C Programming Absolute Begginer's Guide, Que Publisher PTG, eText ISBN 9780133414240, 201
Guide to Examples of Function Prototype in C - EDUCBA. https://www.educba.com/function-prototype-in-c/.
How do I use the fopen function in C with examples? • GITNUX. https://blog.gitnux.com/code/c-fopen-function-with-examples/.
How do you define a global function in C++? - Stack Overflow. https://stackoverflow.com/questions/6874346/how-do-you-define-a-global-function-in-c.
How to access elements from multi-dimensional array in C. https://www.tutorialspoint.com/How-to-access-elements-from-multi-dimensional-array-in-Chash.
How to extract the decimal part from a floating point number in C .... https://stackoverflow.com/questions/499939/how-to-extract-the-decimal-part-from-a-floating-point-number-in-c.
How to get the size (length) of a string in Python?. https://stackoverflow.com/questions/4967580/how-to-get-the-size-length-of-a-string-in-python.
How To Implement a Stack in C Programming | DigitalOcean. https://www.digitalocean.com/community/tutorials/stack-in-c.
How to Use scanf( ) in C to Read and Store User Input - freeCodeCamp.org. https://www.freecodecamp.org/news/using-scanf-in-c/.
http://tutorialtous.com/c/operators.php
https://brainly.in/question/25069846
https://en.cppreference.com/w/c/language/declarations
https://en.cppreference.com/w/c/language/expressions
https://en.cppreference.com/w/c/language/statements
https://en.wikipedia.org/wiki/Constant_%28computer_programming%29
https://en.wikipedia.org/wiki/Label_%28computer_science%29
https://learn.microsoft.com/en-us/cpp/c-language/c-primary-expressions?view=msvc-170
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/statements
https://link.springer.com/chapter/10.1007/978-3-031-35711-4_15
https://stackhowto.com/how-to-print-double-in-c/
https://users.cs.utah.edu/~germain/PPS/Topics/variables.html
https://users.ece.cmu.edu/~eno/coding/CCodingStandard.html
https://w3schools.com
https://www.ascii-code.com/
https://www.codespeedy.com/data-types-in-cpp/
https://www.c-programming-simple-steps.com/c-const.html
https://www.c-programming-simple-steps.com/c-keywords.html
https://www.educative.io/answers/what-are-expressions-in-c
https://www.educative.io/answers/what-is-block-scope-in-c
https://www.geeksforgeeks.org/arithmetic-operators-in-c/
https://www.geeksforgeeks.org/basics-file-handling-c/
https://www.geeksforgeeks.org/c-typecasting/
https://www.geeksforgeeks.org/goto-statement-in-c/
https://www.geeksforgeeks.org/local-labels-in-c/
https://www.geeksforgeeks.org/operators-in-c/
https://www.geeksforgeeks.org/sizeof-operator-c/
https://www.programiz.com/cpp-programming/goto
https://www.programiz.com/c-programming/c-enumeration
https://www.programiz.com/c-programming/online-compiler/
https://www.programiz.com/c-programming/precedence-associativity-operators
https://www.scaler.com/topics/c/goto-statement-in-c/
https://www.techcrashcourse.com/2015/08/fwrite-stdio-c-library-function.html
https://www.tutorialgateway.org/goto-statement-in-c/
https://www.tutorialspoint.com/c_standard_library/c_function_fread.htm
https://www.tutorialspoint.com/c_standard_library/c_function_scanf.htm.
https://www.tutorialspoint.com/cprogramming/c_bitwise_operators.htm
https://www.tutorialspoint.com/cprogramming/c_operators_precedence.htm
https://www.tutorialspoint.com/sizeof-operator-in-c
https://www.w3schools.com/c/c_operators.php
https://www.w3schools.com/c/index.php
https://www.wikihow.com/Convert-from-Decimal-to-Binary
Input and Output Functions in C | GATE Notes - BYJU'S. https://byjus.com/gate/input-and-output-functions-in-c/.
Learn What Are Data Structures in C and Their Uses. https://www.simplilearn.com/tutorials/c-tutorial/data-structures-in-c.
Local and Global Variables in C++ - Dot Net Tutorials. https://dotnettutorials.net/lesson/local-and-global-variables-in-cpp/.
Local, Global and Static variables in C - OverIQ.com. https://overiq.com/c-programming-101/local-global-and-static-variables-in-c/.
Loops in C: For, While, Do While looping Statements [Examples] - Guru99. https://www.guru99.com/c-loop-statement.html.
malloc in C: Dynamic Memory Allocation in C Explained - freeCodeCamp.org. https://www.freecodecamp.org/news/malloc-in-c-dynamic-memory-allocation-in-c-explained/.
Math Functions in C Programming - Tuts Make. https://www.tutsmake.com/math-functions-in-c-programming/.
Memory Allocation in C | Guide to Static and Dynamic Memory ... - EDUCBA. https://www.educba.com/memory-allocation-in-c/.
Multi Dimensional Array In C Programming - Tutorial Gateway. https://www.tutorialgateway.org/multi-dimensional-array-in-c/.
Multidimensional Arrays in C - GeeksforGeeks. https://www.geeksforgeeks.org/multidimensional-arrays-in-c/.
Multi-dimensional Arrays in C | Tutorialspoint - Online Tutorials Library. https://www.tutorialspoint.com/cprogramming/c_multi_dimensional_arrays.htm.
Objective-C Blocks | Tutorialspoint - Online Tutorials Library. https://www.tutorialspoint.com/objective_c/objective_c_blocks.htm.
Pass by Reference in C Programming - Sanfoundry. https://www.sanfoundry.com/c-program-pass-by-reference/.
Passing a C++ Member Function Pointer to an STL Algorithm. https://saturncloud.io/blog/passing-a-c-member-function-pointer-to-an-stl-algorithm/.
Passing by value, passing by reference in C - DEV Community. https://dev.to/mikkel250/passing-by-value-passing-by-reference-in-c-1acg.
Pointer Arithmetics in C with Examples - GeeksforGeeks. https://www.geeksforgeeks.org/pointer-arithmetics-in-c-with-examples/.
pointers - Passing by reference in C - Stack Overflow. https://stackoverflow.com/questions/2229498/passing-by-reference-in-c.
Pregnant women show drastic increase in rates for Hepatitis C. https://www.upi.com/Health_News/2023/07/22/usphepatitis-C-pregnant-women/7861690040401/.
printf in C - GeeksforGeeks. https://www.geeksforgeeks.org/printf-in-c/.
printf scanf in C - javatpoint. https://www.javatpoint.com/printf-scanf.
printf skipped after scanf statement [C] - Stack Overflow. https://stackoverflow.com/questions/76786902/printf-skipped-after-scanf-statement-c.
printf() sprintf() and fprintf() in C - Online Tutorials Library. https://www.tutorialspoint.com/printf-sprintf-and-fprintf-in-c.
programiz.com (no date a) C if...else statement, Programiz. Available at: https://www.programiz.com/c-programming/c-if-else-statement (Accessed: 30 August 2023).
programiz.com (no date b) C online compiler, Online C Compiler. Available at: https://www.programiz.com/c-programming/online-compiler/ (Accessed: 29 August 2023).
Push and Pop Operation in Stack in Data Structure - javatpoint. https://www.javatpoint.com/push-and-pop-operation-in-stack-in-data-structure.
putchar(), getchar() function in C | C File Handling - Fresh2Refresh. https://www.fresh2refresh.com/c-programming/c-file-handling/putchar-getchar-function-c/.
queue push() and pop() in C++ STL - GeeksforGeeks. https://www.geeksforgeeks.org/queue-push-and-queue-pop-in-cpp-stl/.
Random Access File in C - Scaler Topics. https://www.scaler.com/topics/c/random-access-file-in-c/.
Recursion in C - Scaler Topics. https://www.scaler.com/topics/c/recursion-in-c/.
Recursion in C Programming - Programtopia. https://www.programtopia.net/c-programming/docs/recursion-c-programming.
References:
Reverse a String in C - Coding Ninjas. https://www.codingninjas.com/studio/library/how-to-reverse-a-string-in-c.
RKplus (2023) What is stdio.h and why do we use?, Cplusplus. Available at: https://www.cplusplus.in/what-is-stdio-h-and-why-do-we-use/ (Accessed: 29 August 2023).
scanf in C - GeeksforGeeks. https://www.geeksforgeeks.org/scanf-in-c/.
scanf() and fscanf() in C - GeeksforGeeks. https://www.geeksforgeeks.org/scanf-and-fscanf-in-c/.
Scope rules in C - GeeksforGeeks. https://www.geeksforgeeks.org/scope-rules-in-c/.
Source: Conversation with Bing, 8/30/2023
Strings in C - GeeksforGeeks. https://www.geeksforgeeks.org/strings-in-c/.
Strings in C (With Examples) - Programiz. https://www.programiz.com/c-programming/c-strings.
Two Dimensional Array in C - C Programming Tutorial - OverIQ.com. https://overiq.com/c-programming-101/two-dimensional-array-in-c/.
Types of C files after its compilation - GeeksforGeeks. https://www.geeksforgeeks.org/types-of-c-files-after-its-compilation/.
V, R. (2019) Data types in C++, CodeSpeedy. Available at: https://www.codespeedy.com/data-types-in-cpp/ (Accessed: 30 August 2023).
Value of EOF in c programming language - Includehelp.com. https://www.includehelp.com/c/value-of-eof-in-c-programming-language.aspx.
Variable Scope in C – Local and Global Scope Explained - freeCodeCamp.org. https://www.freecodecamp.org/news/scope-of-variables-in-c-local-and-global-scope-explained/.
void (C++) | Microsoft Learn. https://learn.microsoft.com/en-us/cpp/cpp/void-cpp?view=msvc-170.
Void function (C language) - Stack Overflow. https://stackoverflow.com/questions/30274112/void-function-c-language.
Void Pointer in C | Examples on How Void Pointer Work in C? - EDUCBA. https://www.educba.com/void-pointer-in-c/.
w3schools.com (no date) C Tutorial, C tutorial. Available at: https://www.w3schools.com/c/index.php (Accessed: 29 August 2023).
What are "prototypes" in a C program? - Stack Overflow. https://stackoverflow.com/questions/23661729/what-are-prototypes-in-a-c-program.
What are Data Structures in C and How to use them? | Edureka. https://www.edureka.co/blog/c-data-structures/.
What does fopen() return to the file pointer if file is opened?. https://stackoverflow.com/questions/53459083/what-does-fopen-return-to-the-file-pointer-if-file-is-opened.
What does void mean in C, C++, and C#? - Stack Overflow. https://stackoverflow.com/questions/1043034/what-does-void-mean-in-c-c-and-c.
What is A Void Function In The C Programming language? - Learn C++. https://learncplusplus.org/what-is-a-void-function-in-the-c-programming-language/.
What is Block Scope in C? - Educative. https://www.educative.io/answers/what-is-block-scope-in-c.
What is EOF in the C programming language? - Stack Overflow. https://stackoverflow.com/questions/1782080/what-is-eof-in-the-c-programming-language.
What is Global Variable in C? - Scaler Topics. https://www.scaler.com/topics/global-variable-in-c/.
What is the use of getchar () and putchar () function in C?. https://quick-qa.com/what-is-the-use-of-getchar-and-putchar-function-in-c/.
while loop in C - GeeksforGeeks. https://www.geeksforgeeks.org/c-while-loop/.
while loop in C | Tutorialspoint - Online Tutorials Library. https://www.tutorialspoint.com/cprogramming/c_while_loop.htm.
Why is the beginning of a C file stream called `SEEK_SET`?. https://stackoverflow.com/questions/56433377/why-is-the-beginning-of-a-c-file-stream-called-seek-set.
Working and examples of C++ pass by reference - EDUCBA. https://www.educba.com/c-plus-plus-pass-by-reference/.
Working of Format Specifier in the C programming - EDUCBA. https://www.educba.com/format-specifiers-in-c/.
Writing a push and pop in c - Stack Overflow. https://stackoverflow.com/questions/20157199/writing-a-push-and-pop-in-c.