FUNCTIONS
———
Function:
It is a self contained block of statements and is used several
times in a program but defined only once.
library function:-
The functions which are inbuilt with the compiler is called as
library function.
ex:- printf(),scanf(),etc…..
User defined functions:-
User can defined functions to do a task relavent to their program.
such functions are called user defined functions.
Any function has 3 things.
1) Function declaration
2) Function definition
3) Function calling.
In case of library functions the function declaration is in headers,
definition is in C libraries and calling is in your source program.
In case of user defined functions all the three things are in your
source program.
Function declaration:-
syn:- returntype func_name([arg_list]);
ex:- void sum(int,int);
Function definition:-
syn:- returntype func_name([arg_list])
{
body;
}
Function calling:-
syn:- func_name([arg_list]);
ex:- sum(a,b);
NOTE:- The arguments which are given at the time of function declaration and
function definion are called arguments (or) formal arguments.
The arguments which are given at the time of function calling
are known as parameters (or) actual arguments.
Void :- Empty data type.
RULES FOR CREATING AND ACCESSING USER DEFINED FUNCTIONS:
——————————————————-
1.A function can be called by any no of times.
2.A function may or may not return a value.
3.A function may or may not receive arguments
4.If a function does not return any value,the function will be specified as
void.
5.If a function returns a value only one value can be returned.
6.If a function returns a value the returning value must be returned with a
statement “return”.
7.If a function returns a value the execution of return statement should be
last.
8.A function returns an integer value by default;
9.If a function returns a value , the returning value should be match with
the function return data type.
10.A function is defined after (or) before main function.
11.Before calling a function , the function declaration (or) definition
is must and should.
12.If a function declaration is specified before the function call,The function
definition can be specified any where in the program.
13.If a function definition is specified before the function call,then the
function declaration is not necessary.
14.A function is executed when the function is call by its name.
15.The function defintion should not be terminated with semicolon.
RETURN:-
Exits immediately from the corresponding executing function to the
calling. return Optionlly returning a value.
syntax:- return [<expression>];
Function prototypes (or) Function categories:-
———————————————-
A Function depending on whether arguments are present (or) not and whether
a value is returning or not .They belong to one of the following categories.
1)Function with no arguments and no return value
2)Function with arguments and no return value.
3)Function with arguments and return value.
4)Function with no orguements and return value.
Recursive function:-
——————-
Calling a function with in same function is known as recursion (or)
Recursive function.If we want to work with recursive function we must follow
the two conditions.
1. Termination condition.
2. Calling by itself.
POINTERS
——–
C provides the important feature of data manipulations with the address of
the variables,hence the execution time is very much reduced such concept is
possible with the special data type called Pointers.
Pointer:-
A pointer is a variable which stores the address of anther variable.
Declaration:-
Pointer declaration is similar to normal variable declaration but
preceded by a *
syn:- data type *identifier;
ex:- int *p;
Initialization:-
datatype *identifier=address;
ex:- int n;
int *p=&n;
NOTE:- Any type of pointer it allocates 2 bytes momory.Because it stores
address of memory location.
Void *:-
It is a generic pointer.It refers the address of any type of variable
and also it will assigned to any type of pointer.
ex:- int n=100;
int *p=&n;
FUNCTIONS AND POINTERS
———————-
Call by value:-
————–
The process of passing a value to the function call is known as
call by value.
Call by Reference:-
——————-
The process of calling a function using pointers to pass the address
of the variables is known as call by reference.
POINTERS AND ARRAYS
———————
When an array is declared,the compiler allocates a BASE address and
sufficient amount of storage and contain all the elements of array
in continuous memory allocation.
The base address is the location of the first element (index 0 of
the array).
The compiler also defines the array name as a constant pointer pointed
to the first element.
suppose we declare an array ‘a’ as follows.
int a[5]={1,2,3,4,5};
suppose the base address of a is 1000 and assuming that each integer
requires 2 bytes.Then the 5 elements will be stored as follows.
Elements——> a[0] a[1] a[2] a[3] a[4]
————————
values——-> 1 2 3 4 5
————————
address——> 1000 1002 1004 1006 1008
|
base address
Here ‘a’ is a pointer that points to the first element. so the value
of a is 1000
there fore a=&a[0]=1000
If we declare ‘p’ is an integer pointer,then we can make the pointer
p to point to the array ‘a’ by the following assignment.
int *p;
p=&a[0]=a;
Now we can access every value of a using p++(or)p+1 to move one
element to another.
The relationship between p and a is shown below.
p+0 = &a[0]=1000
p+1 = &a[1]=1002
p+2 = &a[2]=1004
p+3 = &a[3]=1006
p+4 = &a[4]=1008
When handling arrays instead of using array indexing,we can use pointer
to access array elements.
Note that *(p+k) gives the value of a[k].
Pointer accessing method is much faster than array indexing.
DYNAMIC MEMORY ALLOCATION
————————-
C language requires the no of elements in an array to be specified
at compile time.But we may not be able to do so always our initial
judgement of size,if it is wrong,it make cause failure of the program
(or) wastage of the memory space.In this situation we use Dynamic
Memory allocation.
Definition:-
The process of allocating memory at run time is known as
Dynamic memory allocation.
Malloc():- <alloc.h>
It is a function which is used to allocating memory at
run time.
syn:- void *malloc(size_t size);
size_t:- unsigned integer. this is used for memory object sizes.
Pointer variable=(type casting)malloc(memory size);
Ex:- int *p;
p=(int *)malloc(sizeof(int)); //for 1 location
p=(int *)malloc(n*sizeof(int)); //for n locations
Calloc():- <alloc.h>
This is also used for allocating memory at run time.
syn:- void *calloc(size_t nitems, size_t size);
NOTE:- Calloc allocates a block (n times*size)bytes and clears into 0.
Realloc():- It reallocates Main memory.
syn:- void *realloc(void *block, size_t size);
free():- It deallocates the allocated memory.
syn:- void free(void *block);
FILES
—–
C language permits the usage of limited input and output functions to read
and write data.
These functions are used for only smaller volumes of data and it becomes
difficult to handle longer data volumes.Also the entire data is lost when the
program is over.
To overcome these difficulties a flexible method is devoloped by employing
the concept of FILES to store,to read (0r) write data and to written them even
when program is over.
Definition:-
A FILE is one which enable the user to read , write and store a
group of a related data . (or)
A FILE is a collection of related data stored in a perticular area
On the disk.
C supports a no of functions to have the ability to perform the basic file
operations which indicates.
1) Naming a file
2) Opening a file
3) Reading data from a file
4) Writing data into file
5) Closing a file
FILE:- It is a predefined structure and is used to declare a file pointer.
Using this pointer we can perform all file operations.
Declaration of file pointer:-
FILE *identifier;
ex:- FILE *fp;
TEXT FILES:-
For reading and writing data in continuous blocks.
BINARY FILES:-
For reading and writing data in orbitrary structured files.
fopen():-
It opens a file stream.
syn:- FILE *fopen(const char *filename,const char *mode);
file name:- File that the functions open.
MODE:-
String ³ Description
***********************************************************************************
r ³ Open for reading only
w ³ Create for writing
³ If a file by that name already exists, it will be overwritten.
a ³ Append; open for writing at end of file, or create for
³ writing if the file does not exist.
r+ ³ Open an existing file for update (reading and writing)
w+ ³ Create a new file for update (reading and writing).
³ If a file by that name already exists, it will be overwritten.
a+ ³ Open for append; open for update at the end of the file, or
³ create if the file does not exist.
þ To specify that a given file is being opened or created in text mode,
append “t” to the string (rt, w+t, etc.).
þ To specify binary mode, append “b” to the string (wb, a+b, etc.).
fclose():- It closes a file stream
syn:- int fclose(FILE *stream);
fcloseall():- It closes all opened file streams.
syn:- int fcloseall(void);
TEXT FILES
———-
Macros:-
——–
1)getc():- It is a macro that gets one character from a stream.
syn:- int getc(FILE *stream);
2)putc():- It is a macro that out puts a character to a file stream.
syn:- int putc(int c,FILE *stream);
Functions:-
———-
1)fgetc():- It is a function.It gets a character from a file stream.
syn:- int fgetc(FILE *stream);
2)fputc():- It is a function.It outputs a character to a file stream.
syn:- int fputc(int c,FILE *stream);
EOF:- A constant indicating that end of file has been reached on a file.
To get this character from keyboard press ctrl+z.
fgets():-
It gets string from file stream.
syn:- char *fgets(char *s,int n,FILE *stream);
remarks:-
———
fgets reads characters from stream into the string s. It stops when it reads
either n – 1 characters or a newline character, whichever comes first.
fputs():-
It outputs a string to file stream.
syn:- int fputs(const char *s,FILE *stream);
Remarks:-
———-
fputs copies the null-terminated string s to the given output stream. It
does not append a newline character, and the terminating null character is
not copied.
feof():-
It is a Macro that tests if end of file has been reached on a stream.
syn:- int feof(FILE *stream);
returned value:-
—————
-> Returns non-zero if an end-of-file
indicator was detected on the last input
operation on the named stream.
-> Returns 0 if end-of-file has not been
reached.
Rewind():-
It Repositions file pointer to stream’s beginning.
syn:- void rewind(FILE *stream);
fscanf():-
It scans and formats input from a file stream.
syn:- int fscanf (FILE *stream,const char *format [, address, ...]);
fprintf():-
It sends formatted output to the file stream.
syn:- int fprintf (FILE *stream, const char *format [, argument, ...]);
NOTE:-In fprintf (or) fscanf, between formats atleast one space is neccessary.
ftell():- It Returns the current file pointer.
syntax:- long ftell(FILE *stream);
fseek():- It repositions the file pointer of a stream.
syntax:- int fseek(FILE *stream,long offset,int whence);
Argument³ What It Is/Does
**************************************************************************
stream ³ Stream whose file pointer fseek sets
offset ³ Difference in bytes between whence (a file pointer position)
³ and new position. For text mode streams, offset should be 0 or
³ a value returned by ftell.
whence ³ One of three they are
Constant ³Value³ File location
*******************************************************************************
SEEK_SET ³ 0 ³ Seeks from beginning of file
SEEK_CUR ³ 1 ³ Seeks from current position
SEEK_END ³ 2 ³ Seeks from end of file
BINARY FILES
————
putw():-
——-
It outputs an integer on a stream.
syn:- int putw(int w,FILE *stream);
getw():-
It gets an integer from stream.
syn:- int getw(FILE *stream);
fwrite():-
It appends specified no of equal sized data items to an output file.
syn:-size_t fwrite(const void *ptr, size_t size, size_t n, FILE*stream);
fread():-
It reads a specified no of equal sized data items from an input stream
into a block.
syn:- size_t fread(void *ptr, size_t size, size_t n, FILE *stream);
Argument³ What It Is/Does
***********************************************************************************
ptr ³ Points to a block into which data is read/write
size ³ Length of each item read/write, in bytes
n ³ Number of items read/write
stream ³ Points to input/output stream
remove():-
It is a Macro that removes a file
Declaration: int remove(const char *filename);
rename():-
It Renames a file
Declaration: int rename(const char *oldname, const char *newname);
STORAGE CLASSES
—————
By the declaration statement the memory is allocated temporarly for
all the variables.The size of momory varies with respect to the type of the
variable.The availability of variables for access depends on it’s declaration
time.the storage class specifiers are used to specify the life, scope of
variables with in the block , functions and the entire program.There are
four types of storage class specifiers.
They are
1. Automatic variables
2. Static variables
3. external variables (or) global variables
4. Register variables.
1. Automatic variables :-
These variables are declared inside the function block.
storage : Main memory
Default value : garbage value
Scope : Local to the block in which it is defined
Life : Till the control remaines with in the block in which it
it is defined
Keyword : auto
NOTE:-
If there is no storage class specifier before the declaration of any
variable inside a function block by default it takes auto storage class.
#include<stdio.h>
#include<conio.h>
void main()
{
auto int a,b;
clrscr();
printf(“\na=%d”,a);
printf(“\nb=%d”,b);
getch();
}
o/p : a = garbage value
b = garbage value
2. Static variables:-
The memory of static variable remaines unchanged until the end of the
program.
storage : Main memory
default value : zero
scope : Local to the block in which it is defined
life : The value of static variable persists between the different
function calls.i.e.., It can not re-initialize between
the different function calls.
keyword : static
#include<stdio.h>
#include<conio.h>
void main()
{
static int a,b;
clrscr();
printf(“\na=%d”,a);
printf(“\nb=%d”,b);
getch();
}
o/p : a = 0
b = 0
#include<stdio.h>
#include<conio.h>
void main()
{
void disp();
int i;
clrscr();
for(i=1;i<=10;i++)
{
disp();
}
getch();
}
void disp()
{
static int n=1;
printf(“%5d”,n);
n++;
}
o/p :- 1 2 3 4 5 6 7 8 9 10
NOTE :-
If we can not use static storage class specifier in the above program
o/p: 1 1 1 1 1 1 1 1 1 1
3. External variables (or) Global variables:-
The variables that are both alive and active through out the entire
program are known as external variables.
storage : Main memory
default value : zero
scope : global
life :as long as the execution of the program does not comes to end
keyword : extern
#include<stdio.h>
#include<conio.h>
int a,b;
void main()
{
clrscr();
printf(“\na=%d”,a);
printf(“\nb=%d”,b);
getch();
}
o/p : a = 0
b = 0
#include<stdio.h>
#include<conio.h>
void main()
{
extern int n=100;
clrscr();
printf(“\n n=%d”,n);
getch();
}
o/p : n = 100
4. Register variables: -
We can use the register variables for frequently used variables to
Improve faster execution of the program.
storage : CPU register
default value : garbage value
scope : Local to the block in which it is defined
life : Till the control remaines with in the block in which it is defined.
keyword: register
Note : These variables are also declared inside the function block.
#include<stdio.h>
#include<conio.h>
void main()
{
register int a,b;
clrscr();
printf(“\na =%d”,a);
printf(“\nb =%d”,b);
getch();
}
o/p : a = garbage value
b = garbage value
Note :
The register variables supports only integral data type ( int (or) char)