Programming in C Unit III
Programming in C Unit III
Introduction to C Programming
Unit-III Notes
3.1 Arrays:-
• 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 in C is one of the most used data structures in C programming.
• It is a simple and fast way of storing multiple values under a single name.
The C arrays are static in nature, i.e., they are allocated memory at the compile time.
B. 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.
Syntax of 3D Array in C:
array_name [size1] [size2] [size3];
3.2 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.
• Functions are also called subroutines or procedures in other languages.
1. 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.
Example
int sum(int a, int b); // Function declaration with parameter names
int sum(int , int); // Function declaration without parameter names
Function Declaration
2. 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 Definition in C
3. 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.
In the below example, the first sum function is called and 10,30 are passed to the sum function.
After the function call sum of a and b is returned and control is also returned back to the main
function of the program.
Working of function in C
Note: Function call is neccessary to bring the program control to the function definition. If not
called, the function statements will not be executed.
Types of Functions in C
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.
For Example:
pow(), sqrt(), strcmp(), strcpy() etc.
Advantages of C library functions
• C Library functions are easy to use and optimized for better performance.
• C library functions save a lot of time i.e, function development time.
• C library functions are convenient as they always work.
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
• Changeable functions can be modified as per need.
• The Code of these functions is reusable in other programs.
• These functions are easy to understand, debug and maintain.
In this method, the value of each variable in the In this method, the address of actual variables in the
calling function is copied into corresponding calling function is copied into the dummy variables of
dummy variables of the called function. the called function.
In call-by-values, we cannot alter the values of In call by reference, we can alter the values of
actual variables through function calls. variables through function calls.
Values of variables are passed by the Simple Pointer variables are necessary to define to store the
technique. address values of variables.
This method is preferred when we have to pass This method is preferred when we have to pass a large
some small values that should not change. amount of data to the function.
Call by value is considered safer as original Call by reference is risky as it allows direct
data is preserved modification in original data
3.3 Pointers:-
• A pointer is defined as a derived data type that can store the address of other C variables or a
memory location.
• We can access and manipulate the data stored in that memory location using pointers.
• Pointers are one of the core components of the C programming language.
• A pointer can be used to store the memory address of other variables, functions, or even other
pointers.
• The use of pointers allows low-level memory access, dynamic memory allocation, and many other
functionality in C.
1. Pointer Declaration
• In pointer declaration, we only declare the pointer but do not initialize it. To declare a pointer, we use
the ( * ) dereference operator before its name.
Example
int *ptr;
The pointer declared here will point to some random memory address as it is not initialized. Such
pointers are called wild pointers.
2. Pointer Initialization
• Pointer initialization is the process where we assign some initial value to the pointer variable.
• We generally use the ( &: ampersand ) addressof operator to get the memory address of a variable
and then store it in the pointer variable.
Example
int var = 10;
int * ptr;
ptr = &var;
• We can also declare and initialize the pointer in a single step. This method is called pointer
definition as the pointer is declared and initialized at the same time.
Example
int *ptr = &var;
Note: It is recommended that the pointers should always be initialized to some value before starting
using it. Otherwise, it may lead to number of errors.
3. Pointer Dereferencing
• Dereferencing a pointer is the process of accessing the value stored in the memory address specified
in the pointer. We use the same ( * ) dereferencing operator that we used in the pointer declaration.
Dereferencing a Pointer in C
3.4 Structures:-
• The structure in C is a user-defined data type that can be used to group items of possibly different
types into a single type.
• The struct keyword is used to define the structure in the C programming language.
• The items in the structure are called its member and they can be of any valid data type.
• Additionally, the values of a structure are stored in contiguous memory locations.
The reason for the above error is simple. When a datatype is declared, no memory is allocated for it.
Memory is allocated only when variables are created.
Default Initialization
• By default, structure members are not automatically initialized to 0 or NULL.
• Uninitialized structure members will contain garbage values. However, when a structure variable is
declared with an initializer, all members not explicitly initialized are zero-initialized.
struct Point
{
int x;
int y;
};
struct parent {
int member1;
struct member_str member2;
...
}
One thing to note here is that the declaration of the structure should always be present before its definition as
a structure member. For example, the declaration below is invalid as the struct mem is not defined when it
is declared inside the parent structure.
struct parent {
struct mem a;
};
struct mem {
int var;
};
Accessing Nested Members:
We can access nested Members by using the same ( . ) dot operator two times as shown:
str_parent.str_child.member;
Assignment-III
Q1.Write Short Notes On:-
(a) C Array Initialization
(b) Types of Arrays
(c) Syntax of Functions in C
(d) C Function Prototype
(e) Types of Functions
(f) Call by Value & Call by Reference
(g) Recursive Functions in C
(h) Nested Structures