Coureses Plan


▢    Introduction
▢    Variables
▢    Data Types
▢    Input and Output
▢    Operations
▢    Conditional Statements

▢    Loops
▢    Functions
▢    Recursion
▢    Array
▢    String
▢    Pointer

⋙ Introduction

C is a procedural programming language initially developed by Dennis Ritchie in the year 1972 at Bell Laboratories of AT&T Labs. It was mainly developed as a system programming language to write the UNIX operating system.



▣ The main features of the C language include :


These features make the C language suitable for system programming like an operating system or compiler development.



▣ Why Should We Learn C ?


Many later languages have borrowed syntax/features directly or indirectly from the C language like the syntax of Java, PHP, JavaScript, and many other languages that are mainly based on the C language. C++ is nearly a superset of C language (Only a few programs may compile in C, but not in C++).
So, if a person learns C programming first, it will help them to learn any modern programming language as well. Also, learning C helps to understand a lot of the underlying architecture of the operating system like pointers, working with memory locations, etc.

⋙ Variables

In C, variable is a name given to the memory location to easily store data and access it when required. It allows us to use the memory without having to memorize the exact memory address. A variable name can be used anywhere as a substitute in place of the value it stores.



▣ Create Variables :


To create a variable in C, we have to specify its name and the type of data it is going to store. This is called variable declaration.We can also create multiple variables of same in a single statement by separating them using comma.C provides a set of different data type that are able to store almost all kind of data. For example, integers are stored as int type, decimal numbers are stored by float and double data types, characters are stored as char data type.



▣ Rules for Naming Variables :


We can assign any name to the variable as long as it follows the following rules:



▣ Initialization & Updating :


No useful data is stored inside a variable that is just declared. We have to initialize it to store some meaningful value to the variable. It is done using assignment "operator = ".

We can also update the value of a variable with new value whenever needed by using the assignment "operator = ".



▣ Constants :


We know that we can change the value stored by any variable anytime, but C also provides some variables whose value cannot be changed. These variables are called constants and are created simply by prefixing "const" keyword in variable declaration.

⋙ Data Types

Each variable in C has an associated data type. It specifies the type of data that the variable can store like integer, character, floating, double, etc.


C is a statically type language where each variable’s type must be specified at the declaration and once specified, it cannot be changed. This is because each data type requires different amounts of memory and allows type specific operations.

Types Description Data Types
Primitive Data Types Primitive data types are the most basic data types that are used for representing simple values. int, char, float, double, void
Derived Types The data types that are derived from the primitive or built-in datatypes are referred to as Derived Data Types. array, pointers, function
User Defined Data Types The user-defined data types are defined by the user himself. structure, union, enum



In this article, we will discuss the basic (primary) data types in C.



▣ Integer Data Type


The integer datatype in C is used to store the integer numbers (any number including positive, negative and zero without decimal part). Octal values, hexadecimal values, and decimal values can also be stored in int data type in C.


The integer data type can also be used as:

  1. unsigned int: It can store the data values from zero to positive numbers, but it can’t store negative values
  2. short int: It is lesser in size than the int by 2 bytes so can only store values from -32,768 to 32,767.
  3. long int: Larger version of the int datatype so can store values greater than int.
  4. unsigned short int: Similar in relationship with short int as unsigned int with int.



▣ Character Data Type


Character data type allows its variable to store only a single character. The size of the character is 1 byte. It is the most basic data type in C. It stores a single character and requires a single byte of memory in almost all compilers.



▣ Float Data Type


In C programming, float data type is used to store single precision floating-point values. These values are decimal and exponential numbers.



▣ Double Data Type


The double data type in C is used to store decimal numbers (numbers with floating point values) with double precision. It can easily accommodate about 16 to 17 digits after or before a decimal point.



▣ Void Data Type


The void data type in C is used to indicate the absence of a value. Variables of void data type are not allowed. It can only be used for pointers and function return type and parameters.
Void function means it doesn’t return any value.

⋙ Input and Output

In C programming, input and output operations refer to reading data from external sources and writing data to external destinations outside the program. C provides a standard set of functions to handle input from the user and output to the screen or to files. These functions are part of the standard input/output library "stdio.h".
In C, there are many functions used for input and output in different situations but the most commonly used functions for Input/Output are scanf() and printf() respectively.



▣ Standard Output Function – printf ( )


The printf() function is used to print formatted output to the standard output stdout (which is generally the console screen). It is one of the most commonly used functions in C.

Syntax : printf(“formatted_string”, variables/values);



▣ Standard Input Function – scanf ( )


scanf () is used to read user input from the console. It takes the format string and the addresses of the variables where the input will be stored.

Syntax : scanf(“formatted_string”, address_of_variables/values);

Remember that this function takes the address of the arguments where the read value is to be stored.

⋙ Operators

In C language, operators are symbols that represent some kind of operations to be performed. They are the basic components of the C programming. In this article, we will learn about all the operators in C with examples.



▣ What is an Operator in C ?


A C operator can be defined as the symbol that helps us to perform some specific mathematical, relational, bitwise, conditional, or logical computations on values and variables. The values and variables used with operators are called operands. So, we can say that the operators are the symbols that perform operations on operands.



▣ Types of Operators in C


C language provides a wide range of built in operators that can be classified into 6 types based on their functionality.
The main 4 operators are:



1. Arithmetic Operators :


The arithmetic operators are used to perform arithmetic/mathematical operations on operands.
There are 9 arithmetic operators in C language:

S. No. Symbol Operator Description Syntax
1. + Plus Adds two numeric values. a + b
2. - Minus Subtracts right operand from left operand. a - b
3. * Multiply Multiply two numeric values. a * b
4. / Divide Divide two numeric values. a / b
5. % Modulus Returns the remainder after diving the left operand with the right operand. a % b
6. + Unary Plus Used to specify the positive values. +a
7. - Unary Minus Flips the sign of the value. -a
8. ++ Increment Increases the value of the operand by 1. a++
9. -- Decrement Decreases the value of the operand by 1. a--



2. Relational Operators :


The relational operators in C are used for the comparison of the two operands. All these operators are binary operators that return true or false values as the result of comparison.
These are a total of 6 relational operators in C:

S. No. Symbol Operator Description Syntax
1. < Less than Returns true if the left operand is less than the right operand. Else false a < b
2. > Greater than Returns true if the left operand is greater than the right operand. Else false a > b
3. <= Less than or equal to Returns true if the left operand is less than or equal to the right operand. Else false a <= b
4. >= Greater than or equal to Returns true if the left operand is greater than or equal to right operand. Else false a >= b
5. == Equal to Returns true if both the operands are equal. a == b
6. != Not equal to Returns true if both the operands are NOT equal. a != b



3. Logical Operator :


Logical Operator are used to combine two or more conditions/constraints or to complement the evaluation of the original condition in consideration. The result of the operation of a logical operator is a Boolean value either true or false.
There are 3 logical operators in C:

S. No. Symbol Operator Description Syntax
1. && Logical AND Returns true if both the operands are true. a && b
2. || Logical OR Returns true if both or any of the operand is true. a || b
3. ! Logical NOT Returns true if the operand is false. !a



4. Assignment Operators:


Assignment Operators are used to assign value to a variable. The left side operand of the assignment operator is a variable and the right side operand of the assignment operator is a value. The value on the right side must be of the same data type as the variable on the left side otherwise the compiler will raise an error.
The assignment operators can be combined with some other operators in C to provide multiple operations using single operator. These operators are called compound operators.
The main 7 assignment operators are:

S. No. Symbol Operator Description Syntax
1. = Simple Assignment Assign the value of the right operand to the left operand. a = b
2. += Plus and assign Add the right operand and left operand and assign this value to the left operand. a += b
3. -= Minus and assign Subtract the right operand and left operand and assign this value to the left operand. a -= b
4. *= Multiply and assign Multiply the right operand and left operand and assign this value to the left operand. a *= b
5. /= Divide and assign Divide the left operand with the right operand and assign this value to the left operand. a /= b
6. %= Modulus and assign Assign the remainder in the division of left operand with the right operand to the left operand. a %= b
7. &= AND and assign Performs bitwise AND and assigns this value to the left operand. a &= b

⋙ Conditional Statements

In C, programs can choose which part of the code to execute based on some condition. This ability is called decision making and the statements used for it are called conditional statements. These statements evaluate one or more conditions and make the decision whether to execute a block of code or not.



▣ Types of Conditional Statements in C


There are many different types of conditional statements available in C language:



1. if in C :


The if statement is the simplest decision-making statement. It is used to decide whether a certain statement or block of statements will be executed or not i.e if a certain condition is true then a block of statements is executed otherwise not.
A condition is any expression that evaluates to either a true or false (or values convertible to true or flase).

The expression inside () parenthesis is the condition and set of statements inside {} braces is its body. If the condition is true, only then the body will be executed.



2. if-else in C :


The ifstatement alone tells us that if a condition is true, it will execute a block of statements and if the condition is false, it won’t. But what if we want to do something else when the condition is false? Here comes the C elsestatement. We can use the elsestatement with the ifstatement to execute a block of code when the condition is false. The if-else statement consists of two blocks, one for false expression and one for true expression.

The block of code following the else statement is executed as the condition present in the if statement is false.



3. Nested if-else in C :


A nested if in C is an if statement that is the target of another if statement. Nested if statements mean an if statement inside another if statement. Yes, C allow us to nested if statements within if statements, i.e, we can place an if statement inside another if statement.



4. if-else-if Ladder in C :


The if else if statements are used when the user has to decide among multiple options. The C if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the C else-if ladder is bypassed. If none of the conditions is true, then the final else statement will be executed. if-else-if ladder is similar to the switch statement.



5. switch Statement in C :


The switch case statement is an alternative to the if else if ladder that can be used to execute the conditional code based on the value of the variable specified in the switch statement. The switch block consists of cases to be executed based on the value of the switch variable.



6. Conditional Operator in C :


The conditional operator is used to add conditional code in our program. It is similar to the if-else statement. It is also known as the ternary operator as it works on three operands.



7. Jump Statements in C :


These statements are used in C for the unconditional flow of control throughout the functions in a program. They support four types of jump statements:



A) break :


This loop control statement is used to terminate the loop. As soon as the break statement is encountered from within a loop, the loop iterations stop there, and control returns from the loop immediately to the first statement after the loop.



B) continue :


This loop control statement is just like the break statement. The continue statement is opposite to that of the break statement, instead of terminating the loop, it forces to execute the next iteration of the loop. As the name suggests the continue statement forces the loop to continue or execute the next iteration. When the continue statement is executed in the loop, the code inside the loop following the continue statement will be skipped and the next iteration of the loop will begin.



C) goto :


The goto statement in C also referred to as the unconditional jump statement can be used to jump from one point to another within a function.



D) return :


The return in C returns the flow of the execution to the function from where it is called. This statement does not mandatorily need any conditional statements. As soon as the statement is executed, the flow of the program stops immediately and returns the control from where it was called. The return statement may or may not return anything for a void function, but for a non-void function, a return value must be returned.

⋙ Loops

In C programming, there is often a need for repeating the same part of the code multiple times. For example, to print a text three times, we have to use printf() three times. vBut if we say to write this 20 times, it will take some time to write statement. Now imagine writing it 100 or 1000 times. Then it becomes a really hectic task to write same statements again and again. To solve such kind of problems, we have loops in programming languages.



▣ What are loops in C ?


Loops in C programming are used to repeat a block of code until the specified condition is met. It allows programmers to execute a statement or group of statements multiple times without writing the code again and again.



▣ Types of Loops in C


There are 3 looping statements in C:

  1. for Loop
  2. while Loop
  3. do-while Loop



▣ for Loop


for loop in C programming is a repetition control structure that allows programmers to write a loop that will be executed a specific number of times. It enables programmers to perform n number of repetitions in a single line. for loop is entry-controlled loop, which means that the condition is checked before the loop’s body executes.


Syntax :
for (initialization; condition; updation) {
      // body of for loop
      }



The various parts of the for loop are:



▣ while Loop


A while loop is a block of code to perform repeated task till given condition is true. When condition become false it terminates. while loop is also an entry-controlled loop in which the condition is checked before entering the body.


Syntax :
while (condition) {
           // Body of the loop
         }


Only the condition is the part of while loop syntax, we have to initialize and update loop variable manually.



▣ do-while Loop


The do-while loop is similar to a while loop, but the difference is that do-while loop tests condition after entering the body at the end. do-while loop is exit-controlled loop, which means that the condition is checked after executing the loop body. Due to this, the loop body will execute at least once irrespective of the test condition.


Syntax :
do {
         // Body of the loop
      } while (condition);


Like while loop, only the condition is the part of do while loop syntax, we have to do the initialization and updating of loop variable manually.



▣ Nested Loops


Nesting loops means placing one loop inside another. The inner loop runs fully for each iteration of the outer loop. This technique is helpful when you need to perform multiple iterations within each cycle of a larger loop, like when working with a two-dimensional array or performing tasks that require multiple levels of iteration.



▣ Loop Control Statements


Loop control statements in C programming are used to change execution from its normal sequence.

Name Description
break The break statement is used to terminate the loop statement.
continue When encountered, the continue statement skips the remaining body and jumps to the next iteration of the loop.
goto goto statement transfers the control to the labeled statement.

⋙ Functions

A function in C is a set of statements that when called perform some specific tasks. It is the basic building block of a C program that provides modularity and code reusability. The programming statements of a function are enclosed within { } braces, having certain meanings and performing certain operations. They are also called subroutines or procedures in other languages.
In this article, we will learn about functions, function definition. declaration, arguments and parameters, return values, and many more.



▣ Syntax of Functions in C


The syntax of function can be divided into 3 aspects:

  1. Function Declaration
  2. Function Definition
  3. Function Calls



▣ Function Declarations


In a function declaration, we must provide the function name, its return type, and the number and type of its parameters. A function declaration tells the compiler that there is a function with the given name defined somewhere else in the program.


Syntax: return_type name_of_the_function (parameter_1, parameter_2);


The parameter name is not mandatory while declaring functions. We can also declare the function without using the name of the data variables.



▣ Function Definition


The function definition consists of actual statements which are executed when the function is called (i.e. when the program control comes to the function).
A C function is generally defined and declared in a single step because the function definition always starts with the function declaration so we do not need to declare it explicitly.



▣ Function Call


A function call is a statement that instructs the compiler to execute the function. We use the function name and parameters in the function call.



▣ Function Return Type


Function return type tells what type of value is returned after all function is executed. When we don’t want to return a value, we can use the void data type.



▣ Function Arguments


Function Arguments (also known as Function Parameters) are the data that is passed to a function.



▣ Conditions of Return Types and Arguments


In C programming language, functions can be called either with or without arguments and might return values. They may or might not return values to the calling functions.

  1. Function with no arguments and no return value
  2. Function with no arguments and with return value
  3. Function with argument and with no return value
  4. Function with arguments and with return value



▣ Types of Functions


There are two types of functions in C:

  1. Library Functions
  2. User Defined Functions



1. Library Function :


A library function is also referred to as a “built-in function”. A compiler package already exists that contains these functions, each of which has a specific meaning and is included in the package. Built-in functions have the advantage of being directly usable without being defined, whereas user-defined functions must be declared and defined before being used.


Advantages of C library functions :



2. User Defined Function :


Functions that the programmer creates are known as User-Defined functions or “tailor-made functions”. User-defined functions can be improved and modified according to the need of the programmer. Whenever we write a function that is case-specific and is not defined in any header file, we need to declare and define our own functions according to the syntax.


Advantages of User-Defined Functions :



▣ Passing Parameters to Functions


The data passed when the function is being invoked is known as the Actual parameters. In the below program, 10 and 30 are known as actual parameters. Formal Parameters are the variable and the data type as mentioned in the function declaration. In the below program, a and b are known as formal parameters.


We can pass arguments to the C function in two ways:



1. Pass by Value:


Parameter passing in this method copies values from actual parameters into formal function parameters. As a result, any changes made inside the functions do not reflect in the caller’s parameters.



2. Pass by Reference:


The caller’s actual parameters and the function’s actual parameters refer to the same locations, so any changes made inside the function are reflected in the caller’s actual parameters.

⋙ Recursion

Recursion is the process of a function calling itself repeatedly till the given condition is satisfied. A function that calls itself directly or indirectly is called a recursive function and such kind of function calls are called recursive calls.



▣ Recursive Functions


In C, a function that calls itself is called Recursive Function. The recursive functions contain a call to themselves somewhere in the function body. Moreover, such functions can contain multiple recursive calls.



▣ Recursion Case


The recursion case refers to the recursive call present in the recursive function. It decides what type of recursion will occur and how the problem will be divided into smaller subproblems.



▣ Base Condition


The base condition specifies when the recursion is going to terminate. It is the condition that determines the exit point of the recursion. It is important to define the base condition before the recursive case otherwise, the base condition may never encounter and recursion might continue till infinity



▣ Memory Management in Recursion


Like other functions, the data of a recursive function is stored in stack memory as a stack frame. This stack frame is removed once the function returns a value. In recursion,



▣ Stack Overflow


The program’s call stack has limited memory assigned to it by the operating system and is generally enough for program execution. But if we have a recursive function that goes on for infinite times, sooner or later, the memory will be exhausted, and no more data can be stored. This is called stack overflow. In other words,



▣ Types of Recursions in C


In C, recursion can be classified into different types based on what kind of recursive case is present. These types are:


1) Direct Recursion


1) Indirect Recursion



1. Direct Recursion


Direct recursion is the most common type of recursion, where a function calls itself directly within its own body. The recursive call can occur once or multiple times within the function due to which we can further classify the direct recursion


A. Head Recursion

The head recursion is a linear recursion where the position of its only recursive call is at the start of the function. It is generally the first statement in the function.


B. Tail Recursion

The tail recursion is also a liner recursion like head recursion, but the position of the recursive call is at the end of the function. Due to this, the tail recursion can be optimized to minimize the stack memory usage. This process is called Tail Call Optimization.


C. Tree Recursion

In tree recursion, there are multiple recursive calls present in the body of the function. Due to this, while tracing the program flow, it makes a tree-like structure, hence the name Tree Recursion.



2. Indirect Recursion


Indirect recursion is an interesting form of recursion where a function calls another function, which eventually calls the first function or any other function in the chain, leading to a cycle of function calls. In other words, the functions are mutually recursive. This type of recursion involves multiple functions collaborating to solve a problem.



▣ Applications of Recursion in C


Recursion is widely used to solve different kinds of problems from simple ones like printing linked lists to being extensively used in AI. Some of the common uses of recursion are:

⋙ Array

An array in C is a fixed-size collection of similar data items stored in contiguous memory locations. It can be used to store the collection of primitive data types such as int, char, float, etc., and also derived and user-defined data types such as pointers, structures, etc.



▣ Array Declaration


In C, we have to declare the array like any other variable before using it. We can declare an array by specifying its name, the type of its elements, and the size of its dimensions. When we declare an array in C, the compiler allocates the memory block of the specified size to the array name.



▣ Array Initialization


Initialization in C is the process to assign some initial value to the variable. When the array is declared or allocated memory, the elements of the array contain some garbage value. So, we need to initialize the array to some meaningful values by using initializer list is the list of values enclosed within braces { } separated by a comma.



▣ Accessing


We can access any element of the array by providing the position of the element, called the index. Indexes start from 0. We pass the index inside square brackets [] with the name of the array.



▣ Update Array Element


We can update the value of an element at the given index i in a similar way to accessing an element by using the array square brackets [] and assignment operator (=).



▣ C Array Traversal


Traversal is the process in which we visit every element of the data structure. For C array traversal, we use loops to iterate through each element of the array.



▣ Size of Array


The size of the array refers to the number of elements that can be stored in the array. We can extract the size using sizeof() operator.



▣ Passing array to Function


In C, arrays are passed to functions using pointers, as the array name decays to a pointer to the first element. There are three common methods:



▣ Multidimensional Array in C


In the above, we only discussed one-dimension array. Multi-dimensional arrays in C are arrays that grow in multiple directions or dimensions. A one-dimensional array grows linearly, like parallel to the X-axis, while in a multi-dimensional array, such as a two-dimensional array, the elements can grow along both the X and Y axes.



▣ Two-Dimensional Array in C


A Two-Dimensional array or 2D array in C is an array that has exactly two dimensions. They can be visualized in the form of rows and columns organized in a two-dimensional plane.



▣ Three-Dimensional Array in C


Another popular form of a multi-dimensional array is Three-Dimensional Array or 3D Array. A 3D array has exactly three dimensions. It can be visualized as a collection of 2D arrays stacked on top of each other to create the third dimension.



▣ Relationship between Arrays and Pointers


Arrays and Pointers are closely related to each other such that we can use pointers to perform all the possible operations of the array. The array name is a constant pointer to the first element of the array and the array decays to the pointers when passed to the function.



▣ Properties of Arrays in C


It is very important to understand the properties of the C array so that we can avoid bugs while using it. The following are the main properties of an array in C:

⋙ Array

A String in C programming is a sequence of characters terminated with a null character ‘\0’. The C String is work as an array of characters. The difference between a character array and a C string is that the string in C is terminated with a unique character ‘\0’.



▣ Declaration


Declaring a string in C is as simple as declaring a one-dimensional array of character type. Below is the syntax for declaring a string.
In the above syntax string_name is any name given to the string variable and size is used to define the length of the string, i.e. the number of characters strings will store.
Like array, we can skip the size in the above statement:



▣ Initialization


We can initialize a string either by specifying the list of characters or string literal.



▣ Accessing


We can access any character of the string by providing the position of the character, like in array. We pass the index inside square brackets [] with the name of the string.



▣ Update


Updating a character in a string is also possible. We can update any character of a string by using the index of that character.



▣ String Length


To find the length of a string, you need to iterate through it until you reach the null character (‘\0’). The strlen() function from the C standard library is commonly used to get the length of a string.



▣ String Input


In C, reading a string from the user can be done using different functions, and depending on the use case, one method might be chosen over another. Below, the common methods of reading strings in C will be discussed, including how to handle whitespace, and concepts will be clarified to better understand how string input works.



Using scanf()


The simplest way to read a string in C is by using the scanf() function.
There is a limitation with the scanf() function. scanf() will stop reading input as soon as it encounters a whitespace (space, tab, or newline).



Using scanf() with a Scanset


We can also use scanf() to read strings with spaces by utilizing a scanset. A scanset in scanf() allows specifying the characters to include or exclude from the input.



Using fgets()


If someone wants to read a complete string, including spaces, they should use the fgets() function. Unlike scanf(), fgets() reads the entire line, including spaces, until it encounters a newline.



▣ Passing Strings to Function


As strings are character arrays, we can pass strings to functions in the same way we pass an array to a function. Below is a sample program to do this:



▣ Strings and Pointers in C


Similar to arrays, In C, we can create a character pointer to a string that points to the starting address of the string which is the first character of the string. The string can be accessed with the help of pointers as shown in the below example.

⋙ Pointer

A pointer is a variable that stores the memory address of another variable. Instead of holding a direct value, it has the address where the value is stored in memory. This allows us to manipulate the data stored at a specific memory location without actually using its variable. It is the backbone of low-level memory manipulation in C.



▣ Declare a Pointer


A pointer is declared by specifying its name and type, just like simple variable declaration but with an asterisk (*) symbol added before the pointer’s name.
An integer type pointer can only point to an integer. Similarly, a pointer of float type can point to a floating-point data, and so on.



▣ Initialize the Pointer


Pointer initialization means assigning some address to the pointer variable. In C, the (&) addressof operator is used to get the memory address of any variable. This memory address is then stored in a pointer variable.



▣ Dereference a Pointer


Accessing the pointer directly will just give us the address that is stored in the pointer.
We have to first dereference the pointer to access the value present at the memory address. This is done with the help of dereferencing operator(*) (same operator used in declaration).



▣ Size of Pointers


The size of a pointer in C depends on the architecture (bit system) of the machine, not the data type it points to.



▣ Special Types of Pointers


There are 4 special types of pointers that used or referred to in different contexts:



NULL Pointer


The NULL Pointers are those pointers that do not point to any memory location. They can be created by assigning NULL value to the pointer. A pointer of any type can be assigned the NULL value.
NULL pointers are generally used to represent the absence of any address. This allows us to check whether the pointer is pointing to any valid memory location by checking if it is equal to NULL.



Void Pointer


The void pointers in C are the pointers of type void. It means that they do not have any associated data type. They are also called generic pointers as they can point to any type and can be typecasted to any type.



Wild Pointers


The wild pointers are pointers that have not been initialized with something yet. These types of C-pointers can cause problems in our programs and can eventually cause them to crash. If values are updated using wild pointers, they could cause data abort or data corruption.



Dangling Pointer


A pointer pointing to a memory location that has been deleted (or freed) is called a dangling pointer. Such a situation can lead to unexpected behavior in the program and also serve as a source of bugs in C programs.



▣ C Pointer Arithmetic


The pointer arithmetic refers to the arithmetic operations that can be performed on a pointer. It is slightly different from the ones that we generally use for mathematical calculations as only a limited set of operations can be performed on pointers. These operations include:



▣ C Pointers and Arrays


In C programming language, pointers and arrays are closely related. An array name acts like a pointer constant. The value of this pointer constant is the address of the first element. For example, if we have an array named val, then val and &val[0] can be used interchangeably.

If we assign this value to a non-constant pointer to array of the same type, then we can access the elements of the array using this pointer. Not only that, as the array elements are stored continuously, we can use pointer arithmetic operations such as increment, decrement, addition, and subtraction of integers on pointer to move between array elements.

This concept is not limited to the one-dimensional array, we can refer to a multidimensional array element as well using pointers.



▣ Constant Pointers


In constant pointers, the memory address stored inside the pointer is constant and cannot be modified once it is defined. It will always point to the same memory address.
We can also create a pointer to constant or even constant pointer to constant. Refer to this article to know more – Constant pointer, Pointers to Constant and Constant Pointers to Constant



▣ Pointer to Function


A function pointer is a type of pointer that stores the address of a function, allowing functions to be passed as arguments and invoked dynamically. It is useful in techniques such as callback functions, event-driven programs.



▣ Multilevel Pointers


In C, we can create multi-level pointers with any number of levels such as – ***ptr3, ****ptr4, ******ptr5 and so on. Most popular of them is double pointer (pointer to pointer). It stores the memory address of another pointer. Instead of pointing to a data value, they point to another pointer.



▣ Uses of Pointers in C


The C pointer is a very powerful tool that is widely used in C programming to perform various useful operations. It is used to achieve the following functionalities in C: