Pointer 1
Pointer 1
#include <stdio.h>
#include <conio.h>
void main()
{int a, b, c;
float x;
clrscr();
printf("Enter the value of a, b, c);
scanf("%d%d%d", &a,&b,&c);
x= avg (&a,&b,&c);
printf ("%f",x);
getch();
}
avg (int *x, int *y, int *z)
{
float m;
m = (*x + *y +*z)/3;
return (m);
}
Write a c program you calculation of (Heron's formula)
#include <math.h>
#include <stdio.h>
#include <conio.h>
void main()
int a, b, c, A;
{clrscr();
printf("Enter the value of a,b,c”);
scanf("%d%d%d", &a,&b,&c);
A= area(&a,&b,&c);
printf("%d", A);
getch();
}
area (int *x, int *y, int *z)
{ int p,q;
P=(*x+*y+*z)/2;
q= sqrt (p* (p-*x)*(p-*y)*(p-*z));
return(q);
}
WAP for implementation of this formula d= a²+b²+c²
#include<stdio.h>
#include<conio.h>
#include <math.h>
void main()
{ int a, b, c;
float d;
clrscr();
printf("Enter the value of a, b and c");
scanf("%d%d%d", &a,&b,&c);
d=sq (&a, &b,&c);
printf("%f",d);
getch();
}
sq(int *x, int *y, int *z)
{ float m;
m= sqrt(pow(*x, 2) + pow(*y, 2) + pow(*z,2));
return(m);
}
Function call: Argument can be generally passed
through function using two ways.
#include<stdio.h>
#include<conio.h>
void main()
{ int a=2, b= 3;
swap(a, b);
printf("a= %d b=%d", a, b);
getch();
}
swap(int x, int y)
{
int z;
z = x;
x = y;
y=z;
printf("%d %d", x, y);
}
Call by Reference:
#include<stdio.h>
#include<conio.h>
void main()
{ int a=2, b= 3;
swap(&a, &b);
printf("a= %d b=%d", a, b);
getch();
}
swap(int *x, int *y)
{
int z;
z = *x;
*x = *y;
*y=z;
printf("%d %d", *x, *y);
}
Pointer Arithmetic. It refers to the use of
painter with various arithmetic operation.
(i) Addition
(ii) Subtraction
Pointer arithmetic means we can apply arithmetic operation on
address and the operator are ++ and --- .
It means we can increment or decrement the address of variable.
Que-
a=3
&a= 3010
++ (&a)= 3011
#include <stdio.h>
#include<conio.h>
void main()
{
int x, y;
y=&x;
printf("%d“ ,y);
++y;
printf("%d",y);
getch();
}
Output: 3010 ( if address of x is 3010)
3011
write a C. program for implementation of pointer arithmetic
#include <stdio.h>
#include <conio.h>
void main()
{
int x=3,*y;
y=&x;
Printf("%d", y)
&(++ x)
printf("%d", y);
getch();
}
y=3010( if address of x is 3010)
Y=3010(same location)
sizeof() operater :- sizeof ()is used to calculate the size
of variable.
#include<stdio.h>
#include<conio.h>
void main()
{ int m,n, l;
float x;
char y;
double z;
m= sizeof (x);
n= sizeof (y);
l=sizeof (z);
printf ("%d %d %d",m,n,l);
getch();
}
Qutput: 4,1,8
Void pointer- A void pointer is a pointer that has no
associated data type with it. A void pointer can
hold an address of any type and can be typecasted
to any type.
void *ptrName;
Advantage of void pointer-
malloc() and calloc() return void * type and this
allows these functions to be used to allocate
memory of any data type (just because of void *)
NULL pointer-A Null pointer is a variable that has
a value of zero or has an address that points to
nothing. we use the keyword NULL to make a
variable a null pointer, which is a predefined macro.
Syntax
datatype *ptrName = NULL;
Dangling pointer -A dangling pointer in C is a
pointer that points to a memory location that has
been deallocated or is no longer valid. Dangling
pointers can cause various problems in a program,
including segmentation faults, memory leaks, and
unpredictable behavior.
Wild pointer A pointer that has not been initialized
to anything (not even NULL) is known as a wild
pointer. The pointer may be initialized to a non-
NULL garbage value that may not be a valid
address.
Syntax-
dataType *pointerName;
Conclusion-
Dangling (when pointing to deallocated memory),
Void (pointing to some data location that doesn’t
have any specific type), Null (absence of a valid
address), and Wild (uninitialized) pointers.
Advantages of Pointer:-
• Pointers save memory space.
• Execution time with pointers is faster because
data are manipulated with the address, that is,
direct access to memory location.
• Memory is accessed efficiently with the pointers.
The pointer assigns and releases the memory as
well. Hence it can be said the Memory of pointers
is dynamically allocated.
Advantages of Pointer:-
• Pointers are used with data structures. They are
useful for representing two-dimensional and
multi-dimensional arrays.
• An array, of any type, can be accessed with the
help of pointers, without considering its subscript
range.
• Pointers are used for file handling.
• Pointers are used to allocate memory dynamically.
Disadvantages of Pointer :-
*Pointers are a bit difficult to understand.
*Pointers can cause several errors, such as
segmentation errors or unrequired memory
access.
*If a pointer has an incorrect value, it may
corrupt the memory.
*Pointers may also cause memory leakage.
* In pointer we work on address so there will
be a chance of virus or hacking.
Application of Pointer :-
Pointer is used in the following cases
( i) It is used to access array elements
(ii) It is used for dynamic memory allocation.
(iii) It is used in Call by reference
(iv) It is used in data structures like trees,
graph, linked list etc.
File:- Storage of data in variable and array are temporary . File are
used for permanent storage of Large amount of data Computer
saves file on secondary storage devices such as magnetic disk,
Optical disk, tape.
"File is a collection of records"
"Collection of field is called record"
"Collection of file is called directory."
fclose(file pointer);
#include <stdio.h>
#include <conio.h>
void main()
{
FILE *fp;
fp = fopen("abc.c“,"r");
if (fp == NULL)
{
printf(" file does not exist or error in reading the file");
}
else
{
printf(" file Open successfully);
}
fclose (fp);
getch();
}
Write a C program to open the filk, write some content on
file & close the file.
#include< stdio.h>
#include<conio.h>
void main()
{ char ch;
FILE *fp;
fp = fopen("abc.c“ ,"w");
printf(" Enter the data to write on file');
while(( (ch = getchar() )!=EOF) ) // (EOF means End of file)
puts(ch, fp);
}
fclose (fp);
getch();
}
Write a c program to open the file, append some content on file &
close the file.
#include <stdio.h>
#include <conio.h>
void main()
{
char ch;
FILE *fp;
fp= fopen("abc.c", "a");
printf("Enter the data to append on file");
while( (ch=getchar() )!=EOF))
{ putc (ch, fp);
}
fclose(fp);
getch();
}
Write a C program to copy the content of one file to another file
#include <stdio.h>
#include <conio.h>
void main()
{
char ch;
FILE *fp1,*fp2;
fp1= fopen("abc.c" ,"r");
fp2= fopen("bcd.c","w");
while (( (ch=getchar (fp1) ) != EOF))
{
putc (ch, fp2);
fclose(fp1);
fclose (fp2);
getch();
}
Strings in C
A String is a sequence of characters
terminated with a null character ‘\0’.
The C String is stored 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’.
C String Declaration Syntax
Declaring a string in C is as simple as declaring a
one-dimensional array.
char string_name[size];
#include<string.h>
C String handling Functions-
The C string functions are built-in functions that
can be used for various operations and
manipulations on strings. These string functions
can be used to perform tasks such as string copy,
concatenation, comparison, length, etc.
C String handling Functions-
(1) strlen()
(2) strcmp()
(3) strcpy()
(4) strcat()
(5) strrev()
strlen() -
The strlen() function takes a string as an argument and
returns its length. The returned value is of type size
(an unsigned integer type).
#include <stdio.h>
#include<conio.h>
#include <string.h>
void main()
{
char a[20]=“computer";
unsigned int b;
printf("Length of string a b= %u ",strlen(a));
getch();
}
strcpy() -
The strcpy() function copies the string pointed by
source (including the null character) to the
destination.
The strcpy() function also returns the copied
string.
Syntax- strcpy ( destination,source)
#include <stdio.h>
#include<conio.h>
#include <string.h>
void main()
{
char s[20]=“computer";
char d[20];
strcpy (d , s);
printf(" string is= %s ",d);
getch();
}
strcat()-
Syntax- strcat (destination , source)
As you can see, the strcat() function
takes two arguments:
destination - destination string
source - source string
The strcat() function concatenates
the destination string and the
source string, and the result is stored in
the destination string.
#include <stdio.h>
#include<conio.h>
#include <string.h>
void main()
{
char s[20]=“science”
char d[20]= “computer";
strcat(d , s);
printf(" string is= %s ",d);
getch();
}
C strcmp()
The strcmp() compares two strings
character by character. If the strings are
equal, the function returns 0.
Syntax- strcmp(destination,source)
As you can see, the strcmp() function
takes two arguments:
destination - destination string
source - source string
It compare ASCCI value
It compare ASCCI value
If different string stop comparison and
print the difference of ASCCI values.
If same string print zero.
ASCII value-
A-Z-97-122
a-z-65-90
0-9-48-57
#include <stdio.h>
#include<conio.h>
#include <string.h>
void main()
{
int i;
char s[20]=“Science";
char d[20]=“Sciencee”
i=strcmp(s,d);
printf(" %d ",i);
getch();
}
strrev()-The strrev() function is used to
reverse the given string.
strrev(string);
strrev(“india”);
#include <stdio.h>
#include<conio.h>
#include <string.h>
void main()
{
char s[20]=“science”
char d[20];
d=strrev(s);
printf(" reverse of string is= %s ",d);
getch();
}
DYNAMIC MEMORY
ALLOCATION
Static memory Allocation- An array is a
collection of items stored at contiguous
memory locations. And we can not
change this size at run time.
Static memory allocation is compile time
memory allocation.
So if we need more memory/less memory at
run time we can not increase/decrease.
So if we need more less memory at run time
we can not decrease.so memory wastage.
int a,b;
int a[20];
Static memory allocation is compile time
memory allocation.
So if we need more memory/less memory at
run time we can not increase/decrease.
So if we need more less memory at run time
we can not decrease.so memory wastage.
here, the length (size) of the array above is 9. But what if
there is a requirement to change this length (size)?
For example,
Take another situation. In this, there is an array of 9 elements
with all 9 indices filled. But there is a need to enter 3 more
elements in this array. In this case, 3 indices more are
required. So the length (size) of the array needs to be changed
from 9 to 12.
Disadvantage of Static memory
Allocation-
• This allocation method leads to memory
wastage.
• Memory cannot be changed while executing a
program.
• Exact memory requirements must be known.
• If memory is not required, it cannot be freed.
Dynamic memory Allocation-
Therefore, C Dynamic Memory
Allocation can be defined as a procedure
in which the size of a data structure (like
Array) is changed during the runtime.
C provides some functions to achieve
these tasks. There are 4 library functions
provided by C defined under <stdlib.h>
header file to facilitate dynamic memory
allocation in C programming. They are:
• malloc()
• calloc()
• free()
• realloc()
Header file used in dynamic memory
allocation
#include<stdlib.h>
#include<alloc.h>
C malloc() method-
For Example:
ptr = (float*) calloc(25, sizeof(float));
This statement allocates contiguous space in
memory for 25 elements each with the size of the
float.
C free() method
“free” method in C is used to dynamically de-
allocate the memory. The memory allocated using
functions malloc() and calloc() is not de-allocated
on their own. Hence the free() method is used,
whenever the dynamic memory allocation takes
place. It helps to reduce wastage of memory by
freeing it.
Syntax of free() in C
free(ptr);
C realloc() method
“realloc” or “re-allocation” method in C is used
to dynamically change the memory allocation of
a previously allocated memory.
if the memory previously allocated with the help
of malloc or calloc is insufficient, realloc can be
used to dynamically re-allocate memory.
Re-allocation of memory maintains the already
present value and new blocks will be initialized
with the default garbage value.
Syntax of realloc() in C
ptr = realloc(ptr, newSize);
0 1 1 2 3 5 8 13…..
Recursion- When function call itself
it is called recursion.
abc()
{
abc();
}
Types of Recursion- there are two types of recursion.
(1)Direct Recursion-
abc()
{
abc();
}
(2)Indirect Recursion-
abc1()
{
abc2();
}
abc2()
{
abc1();
#include <stdio.h>
#include<conio.h>
int fact (int);
int main()
{ int n,f;
printf("Enter the number whose factorial you want to calculate?");
scanf("%d",&n);
f = fact(n);
printf("factorial = %d",f);
}
int fact(int n)
{ if (n==0)
{ return 0; }
else if ( n == 1)
{ return 1; }
else
{ return n*fact(n-1); }
}
FIBONACCI SERIES-
0 1 1 2 3 5 8 13………
#include <stdio.h>
#include<conio.h>
int fibonacci(int x)
{
if(x == 0) {
return 0; }
if(x == 1) {
return 1; }
return fibonacci(x-1) + fibonacci(x-2);
}
int main() {
int x;
for (x = 0; x < 10; x++) {
printf(“%d\t\n”, fibonacci(x));
}
getch();
}
Linked List -Linked List In C is a linear data
structure consisting of nodes. Each node in a
linked list is divided into two sections that
hold data and the address of the successive
node, stored at the random address in the
memory.
A linked list is a linear data structure, in which
the elements are not stored at contiguous
memory locations. The elements in a linked
list are linked using pointers. In simple words,
a linked list consists of nodes where each node
contains a data field and a reference(link) to
type of linked list
Singly linked lists.
Doubly linked lists.
Circular linked lists.
Circular doubly linked lists.
1. Singly Linked List
It is the simplest type of linked list in which every node
contains some data and a pointer to the next node of the
same data type.
The node contains a pointer to the next node means that
the node stores the address of the next node in the
sequence. A single linked list allows the traversal of data
only in one way. Below is the image for the same:
2. Doubly Linked List
A doubly linked list or a two-way linked list is a more
complex type of linked list that contains a pointer to
the next as well as the previous node in sequence.
3. Circular Linked List
A circular linked list is that in which
the last node contains the pointer to
the first node of the list.
Doubly Circular linked list
A Doubly Circular linked list or a circular two-way linked list
is a more complex type of linked list that contains a pointer
to the next as well as the previous node in the sequence.
The difference between the doubly linked and circular
doubly list is the same as that between a singly linked list
and a circular linked list. The circular doubly linked list does
not contain null in the previous field of the first node.
Below is the image for the same:
type of linked list
Singly linked lists.
Doubly linked lists.
Circular linked lists.
Circular doubly linked lists.
Stack-A stack is a linear data structure, a
collection of items of the same type.
In a stack, the insertion and deletion
of elements happen only at one endpoint.
The behavior of a stack is described as
“Last In, First Out” (LIFO).
A stack is a collection of components that
can be accessed using the push and pop
operations. When an element is “pushed” onto
the stack, it becomes the first item that will be
“popped” out of the stack.
Queue-
A queue in C is basically a linear data structure
to store and manipulate the data elements. It
follows the order of First In First Out (FIFO).
A queue is open at both ends. One end is
provided for the insertion of data and the other
end for the deletion of data
What is error ? and types of error.
Errors in C programming can disrupt the
intended functionality of a program, causing
issues such as failed compilation, program
crashes, or incorrect output
Syntax Error-Syntax errors occur when a
programmer makes mistakes in typing the
code's syntax correctly or makes typos. In other
words, syntax errors occur when a programmer
does not follow the set of rules defined for the
syntax of C language.
Missing semi-colon (;)
Missing parenthesis ({})
#include <stdio.h>
int main()
{
printf("Hello, world!")
return 0;
}
2. Runtime Error-
#include <stdio.h>
#include <stdio.h>
int main() {
int a = 5;
int b = 0;
int result;
void Main() {
int var = 10;
printf("%d", var);
}
Operating System =An Operating System provide
as a communication bridge (interface) between
the user and computer hardware. It’s system
software.
The purpose of an operating system is to provide
a platform on which a user can execute
programs conveniently and efficiently. An
operating system is a program that manages a
computer’s hardware.
It also provides a basis for application programs
and acts as an intermediary between the
computer user and computer hardware.
What are implicit and explicit type castings?
type conversion in C is the process of converting one
data type to another. The type conversion is only
performed to those data types where conversion is
possible. Type conversion is performed by a compiler.
In type conversion, the destination data type can’t be
smaller than the source data type
1. Implicit Type Conversion
(a)Done by the compiler on its own, without any
external trigger from the user.
(b) Generally takes place when in an expression more
than one data type is present. In such conditions type
conversion (type promotion) takes place to avoid loss of
data.
C. All the data types of the variables are
upgraded to the data type of the variable with
the largest data type.
bool -> char -> short int -> int -> unsigned int ->
long -> unsigned -> long long -> float -> double -
> long double D.
It is possible for implicit conversions to lose
information, signs can be lost (when signed is
implicitly converted to unsigned), and overflow
can occur (when long is implicitly converted to
float).
#include <stdio.h>
int main()
{
int x = 10; // integer x
float z;
char y = 'a'; // character c
int main()
{
double x = 1.2;