Function
A function is a self-contained block of statement that performs a coherent task of some kind.
Every C program can be thought of a collection of these functions.
Functions are generally used as a abbreviation for a series of instruction that are t be executed more than once.
Functions are easy to write and understand.
C function can be classified into two category
Library functions are not be required to be written by user. printf() and scanf() belong to the category of library
function
User defined function has to be developed by the user at the time
C function involves the following components :
1. Prototype
2. Calling
3. Definition
CALL BY VALUE
In this method the value of in the calling function is copied in the corresponding formal arguments of the called function
The change made to the formal arguments in the called function have no effect on the values of actual argument in the calling
function
CALL BY ADDRESS
In call by address , the function is allowed access to the actual memory location of the argument in the calling function
are copied into formal argument of the called function.
RECURSION
A RECURISON is a process of calling a function itself.
A function is said to be recursive if a statement within the body of a function calls the same function.
Following example represents recursion :-
main()
{
}
COMMAND LINE ARGUMENT
We can pass arguments to main() in the way we pass to other function
For this specify the argument at command prompt at the execution time of the program
Variables which are used to collect passed argument in main() are called argc and argv
Variable argc contains the count
POINTER
INDEX
Pointer to pointer
Pointer to pointer to pointer
Pointer to variable
Pointer to constant
Constant pointer to variable
Constant pointer to constant
Wild pointer
Void pointer / Generic pointer
NULL pointer
Pointer to Array
Array of pointer
Pointer to Function
Pointer to string
Array of pointer to string
Pointer arithmetic
Size of pointer
Scale of pointer
Pointer to structure
Pointer to FILE
WAP in C to print value at address :-
//Prg1
#include<stdio.h>
int main( )
{
int a = 786;
int *p=&a; // int describe scale of pointer
printf(“%d”,*p);
return 0;
}
//Prg2
#include<stdio.h>
int main( )
{
int a = 786;
char *p=&a ; // char describe scale of pointer
printf(“%d”,*p);
return 0;
}
Here,
printf(“%d”, *p) ; // In ‘*p’ * is also known as de-reference operator ,indirection operator, value at address
& -> address of / reference operator
* -> dereference
//Prg :- POINTER TO POINTER TO POINTER
//Prg :-
//Prg :- PROGRAM FOR POINTER TO VARIABLE
//Prg :- PROGRAM FOR POINTER TO VARIABLE
//Prg :- COMMON ERROR OCCURRED DURING CONST POINTER TO CONST VARIABLE
//Prg :- RIGHT WAY FOR CONST POINTER TO CONST VARIABLE
//Prg :- COMMON ERROR IN POINTER TO CONST :
//Prg :- POINTER TO CONST :-
//Prg :- ERROR IN Constant pointer to variable :-
//Prg :- Constant pointer to variable :-
//Prg :- WHEN VOID POINTER GOES TO PICK THE VALUE OF “a” THEN VOID POINTER GET CONFUSE TO GET THE ACTUAL
NEEDED SPACE :-
NOTE: We Cannot Create Void Type Variable( void a ;) But We Can Create Void Type Special Character ( void *p ; )
//Prg :- Right way to Program Void pointer or Generic pointer :-
//Prg :- “ WILD POINTER ” :-
It is an Un-initialised pointer therefore it can cause abnormal termination and this un-initialised pointer is known as Wild
Pointer
OUTPUT :
//Prg :- NULL POINTER:-
// PRG:- POINTER to String:-
// PRG:- ARRAY OF POINTER:-
// PRG:- POINTER to Element Of Array :-
here, p= &ar[0] ; // pointer to element of array of integer
#PRG : Array Using Pointer notation :-
# PRG : Pointer to Function :-
#PRG : POINTER TO ARRAY OF FUNCTION :-
DYNAMIC MEMORY ALLOCATION [ DMA ]
Memory can be understand as a piece of area which a program need to run itself.
A computer program always need some amount of memory to run which is allocated to it by Operating System
To run
MEMORY ALLOCATION
There are two types of allocations :-
1. Static Memory Allocation: [STACK SEGMENT]
Ordered
int a ;
int arr[100];
2. Dynamic Memory Allocation:[ HEAP SEGMENT]
Unordered
int *ptr = (int *)malloc(sizeof( int )) ;
NOTE :
If we want to allocate a memory before the execution of the program then it is considered
as Static Memory Allocation
Here d and g1 , g2 and g3 stored in Data Segment whereas DMA variables stored in Heap
Segment(Anonymous)
Here we
#PRG : DYNAMIC ARRAY :-
p=(int *)calloc(10,sizeof(int )); // 10-> total number of bytes ,size of each block
NOTE :
Initially , malloc stores garbage .
Initially , calloc stores 0(zero) .
When we call free( ) function then the memory will be free for reallocation, OS reallocate for
request
2000
2004 2008 2012
HEAP
STACK
STRUCTURE IN C PROGRAMMING
INDEX
What is structure?
What is user defined data type? ?
Global and Local structure?
Structure using structure?
What is nested structure? ?
What is anonymous structure? ?
Self-referential structure ?
Array of structure?
Pointer of structure?
Structure as a Argument
Structure Memory Allocation
Introduction:-
# Initialisation of Sturcture ( LOCAL SCOPE) :-
# Scanning of Sturcture :-
# Assigment of Sturcture:-
# Sturcture of Sturcture:-
# Nested Sturcture:-
# ANONYMOUS Sturcture :-
#STRUCTURE OF ARRAY
Members in C are ‘.’ and ‘ -> ‘ :-
. = left side is structure variable
=left side is pointer variable
#ARRAY OF STRUCTURE :-
#POINTER TO ELEMENT OF ARRAY OF STRUCTURE :-
#UNIION
FILE HANDLING
FILE is typedef of a structure that is created in c initially in the stdio.h .
Fopen( ) is takes two argument first argument is file name with path and second argument is file mode
Basically there are different types of file mode :-
W(write only),r(read only),a(append only),w+(write and read),r+(read and write),a+(append and read)
1. In w mode there might be two cases
i. If file does not exist then w mode will create a new file and start writing from beginning
ii. If file already exist then w mode will clear all the content of the file from beginning
2. In r mode
i. If file does not exist then it will not create the new file
ii. If file already exist the it will send the File pointer at the beginning of the to read
3. In a mode
i. If file does not then it create a new file start appending from the beginning .
ii. If file already exit then in a mode it will send the file pointer at the end of the file.
Questions:-
Q.Difference b/w dynamic array in heap static array in heap
Q.Difference b/w union and structure
Q.difference b/w fprintf anf printf
Q. difference b/w fscanf and