notes-c
notes-c
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. They are also called subroutines or
procedures in other languages.
Types of Functions:-
• The printf() is a standard library function to send formatted output to the screen (display
output on the screen). This function is defined in the stdio.h header file.
Hence, to use the printf()function, we need to include the stdio.h header file using #include
<stdio.h>.
• The sqrt() function calculates the square root of a number. The function is defined in
the math.h header file.
Functions that the programmer creates are known as User-Defined functions. User-defined functions
can be improved and modified according to the need of the programmer.
Advantage of functions in C
o By using functions, we can avoid rewriting same logic/code again and again in a program.
o We can call C functions any number of times in a program and from any place in a program.
o A large program can be divided into smaller modules. Hence, a large project can be divided
among many programmers.
1. Function Definition
2. Function Calls
3. Function Declaration
Function Definition:-
The function definition consists of actual statements which are executed when the function is called.
A function definition in C programming consists of a function header and a function body. Here are all
the parts of a function −
• Return Type − A function may return a value. The return_type is the data type of the value
the function returns. Some functions perform the desired operations without returning a
value. In this case, the return_type is the keyword void.
• Argument List − An argument (also called parameter) is like a placeholder. When a function
is invoked, you pass a value as a parameter. This value is referred to as the actual parameter
or argument. The parameter list refers to the type, order, and number of the parameters of a
function. Parameters are optional; that is, a function may contain no parameters.
• Function Body − The function body contains a collection of statements that defines what the
function does.
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 can be called from anywhere in the program. The
parameter list must not differ in function calling and function declaration. We must pass the same
number of functions as it is declared in the function declaration.
Syntax: fun_name(arg1,arg2,……argn);
Or
Variable_name= fun_name(arg1,arg2,……argn);
Example:
sum(a,b);
Or
c= sum(a,b);
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
The parameter name is not mandatory while declaring functions. We can also declare the function
without using the name of the data variables.
Example
Recursion:-
Recursion is the process by which a function calls itself. C language allows writing of such functions
which call itself to solve complicated problems by breaking them down into simple and easy
problems. These functions are known as recursive functions.
int main()
main ();
While using recursion, programmers need to be careful to define an exit condition from the function,
otherwise it will go into an infinite loop.
Recursion cannot be applied to all the problem, but it is more useful for the tasks that can be defined
in terms of similar subtasks.
#include <stdio.h>
int main()
int n,f;
scanf("%d",&n);
f = fact(n);
printf("factorial = %d",f);
int fact(int n)
if ( n == 1)
return 1;
else
return n*fact(n-1);
Pointers:-
A pointer can be used to store the address of other variables. The use of pointers allows low-level
memory access, dynamic memory allocation, and many other functionality in C. We can access and
manipulate the data stored in that memory location using pointers.
As the pointers in C store the memory addresses, their size is independent of the type of data they
are pointing to. This size of pointers in C only depends on the system architecture.
1.Address of operator (&):- The address of operator is used to print address of variables. It is also
called as reference operator.
2.Value at address operator (*):- value at address operator gives the value stored at particular
address. It is also called as dereference operator or indirection operator. This operator is also used to
declare pointer variables.
Pointer Declaration:-
The syntax of pointers is similar to the variable declaration in C, but we use the ( * ) dereferencing
operator in the pointer declaration.
Syntax:-
datatype * pointer_variable;
where
Example:
Pointer Initialization:-
Pointer initialization is the process where we assign some initial value to the pointer variable. We
generally use the ( &: ampersand ) address of operator to get the memory address of a variable and
then store it in the pointer variable.
pointer_variable=&variable;
Example:
int a = 10,*p;
p = &a;
or
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.
The value of the variable which is pointed by a pointer can be accessed and manipulated by using the
pointer variable. You need to use the asterisk (*) sign with the pointer variable to access and
manipulate the variable's value.
Pointer Example
#include <stdio.h>
void main()
ptr = &var;
Output
Value at var = 10
Value at *ptr = 10
Parameter Copies the actual value of an Passes the address of the actual
passing argument into the formal parameter parameter into the formal parameter
Memory Requires additional memory for Requires minimal memory for storing
allocation storing copied values addresses
Access to original Cannot directly modify the original Can directly modify the original data
data data through references
int x = 10,y=20;
int x = 10,y=20;
Example swap(&x,&y); // x and y are
swap(x,y); // x and y remains same
exchanged
Structure:-
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.
C Structure Declaration:-
We have to declare structure in C before using it in our program. In structure declaration, we specify
its member variables along with their datatype. We can use the struct keyword to declare the
structure in C using the following syntax:
Syntax
struct structure_name
{
data_type member1;
data_type member2;
....
....
data_type membern;
};
3.Inside curly brackets we mention all variables with different data types
4. variables inside structure are also called members, elements or fields of structure
Example
struct student
int rno;
float per;
};
C Structure Definition
To use structure in our program, we have to define its instance. We can do that by creating variables
of the structure type. We can define structure variables using two methods:
struct structure_name
{
data_type member1;
data_type member2;
....
....
struct student
int rno;
float per;
}s1;
In above example s1 is the variable of structure student .we can declare more than one variables by
separating each variable using comma operator.
struct structure_name
data_type member1;
data_type member2;
....
....
};
void main()
…….
…….
.
1.By (member or dot operator)
2.By -> (structure pointer operator)
Syntax:-
Variable1.member1;
Variable2.member2;
Structure members cannot be initialized with the declaration. For example, the following C program
fails in the compilation.
struct Point
};
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.
str.member1 = value1;
str.member2 = value2;
str.member3 = value3;
In this type of initialization, the values are assigned in sequential order as they are declared in the
structure template.
Nested structure:-
A nested structure in C is a structure within structure. One structure can be declared inside another
structure in the same way structure members are declared inside a structure.
Syntax:
struct name_1
struct name_2
}var1:
} var2;
In above syntax
2.var1 is the variable of inside structure and var2 is the variable of outer structure
Var2.member1;
Var2.member2;
..
..
Var2.membern;
Var2. var1.member2;
..
..
Var2. var1.membern;