0% found this document useful (0 votes)
7 views127 pages

Pointer 1

Uploaded by

rs3998824
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views127 pages

Pointer 1

Uploaded by

rs3998824
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 127

POINTER

Pointer is a variable which can hold the address


of another variable.
exp: int i=3,j;

i-Variable name. j-Pointer of i


Operators Used In Pointer:

1-& (address of)


2-* (Value at that point)
How to Declare a Pointer
To declare a pointer variable in C, we use the asterisk * symbol
before the variable name.
There are two ways to declare pointer variables in C:
(1) int *p;
(2) int* p;
Both of these declarations are equivalent and they declare a pointer
variable named "p" that can hold the memory address of an integer.
if you declare multiple variables in a single statement, you
need to include the asterisk before each variable name to indicate
that they are all pointers.
example:
int *p, *q, *r;
This declares three pointer variables named "p", "q", and "r" that
can hold the memory address of an integer.
WAP for implementation of pointer concept.
#include<stdio.h>
#include<conio.h>
void main()
{
int i= 3;
printf("%d", i);
printf("%d", &i);
printf("%d", *(&i));
getch();
}
Write a concept C. program for implementation of pointer
#include<stdio.h>
#include <conio.h>
void main()
{
int i=3;
int *J;
j=&i;
printf("%u",&i);
printf ("%u", j);
printf("%u", &j);
printf("%d", i);
printf("%d",*(&j));
printf("%d",*j);
getch();
}
Output:
&i= 3010
J= 3010
&J = 4010.
i= 3
*(&J)= 3010
*J= 3
Write a c program you concept of pointer.
#include <stdio.h>
#include <conio.h>
void main()
{
int i =3,*j,**k;
j=&I;
k=&j;
printf ("%u", &i);
printf("%u", k);
printf("%u", j);
printf("%d", &j);
printf("%d",k);
printf ("%d", &k);
printf("%u", j);
printf("%u" ,*j);
printf("%d", i);
printf("%d", *(&j));
printf("%d", *j);
printf("%d", **k);
getch();
}
Output:- &i = 3010.
*(j)= 3010
k= 3010.
*j= 3
j= 3010
**k= 3
&j= 4010
k= 4010
&k= 5010
j= 3010
*j= 3
i= 3
Write a C program for addition of two numbers using pointer
#include <stdio.h>
#include<conio.h>
void main ()
{
int a, b, c;
printf("Enter the value of a and b");
scanf("%d%d", &a,&b);
c=sum (&a,&b);
printf("%d", c);
getch();
}
sum (int *x, int *y)
{int z;
z= *x + *y;
return(z);
}
Write a C. program you average of 3 numbers using pointer

#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.

(i) Sending value to the argument(call by Value )


(ii) Sending address of argument (call by
reference)
Call by value:- In call by value we pass value of
Variable in function call. It is less efficient than
call by reference.

Syntax:- swap( a, b);


Call by Reference:- In call by reference we pass
address of Variable in function call. It is more
efficient than call by value.

Syntax:- swap( &a, &b);


Program:-write a c program for call by value of
call by reference. OR
Write a C program you swapping of two
numbers using call by value call by reference.
Call by Value:

#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.

In C language two arithmetic operations are


used

(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.

Increment Arithmetic Pointer: - If there is a variable a then the


increment arithmetic pointer
(&a)++
++(&a)
&a= &(a)+1

Decrement Arithmetic Pointer:- If we want to decrement any


address of variable then it is called decrement arithmetic pointer
(&a)--,
--(&a)
&a= (&a)-1
We can also increment and decrement the
address like this -
Address X = X + 10
X=X-100

Que-
a=3
&a= 3010
++ (&a)= 3011

&(++a)=&(3) = 3010(same location)


Write a c program you implementation of painter arithmetic.

#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.

Program No.-Write a Operator C program you implementation of


sizeof.
#include <stdio.h>
#include <conio.h>
void main()
{ int x, y;
y=sizeof(x);
printf("%d",y);
getch();
}
Output: 2
Program No.-Write a c program for sizeof() calculation float,
Char, double.

#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."

for ex: - A particular employee records consist the following fields.


• Employee name
• Salary
• Department
• Employee Code/id
• Education
• Designation
Types of File: There are two types of file.
(1) Text file
(2) Binary file

Text File:- It is a stream of character that computer


can process sequentially. The extension of text file
is .txt

Binary File:- In this file the data is stored in the


form of Binary (0&1).
Operation On File: We can perform following
Operation On a file.
》 Opening a file
》 Reading from a file
》 Writing to a file
》 Appending to a file
》 Updating a file
》 Deleting a file.
》 Renaming a file
》 Closing a the file
file Handling function
• fopen()
• fclose()
• putc()
• getc()
• fputs()
• fgets()
Function for Opening a file:- To perform any
Operation on File. File should be Open first .To
open a file we use fopen().

Syntax for fopen():-

file pointer = fopen( "File name“ , "mode“ );


For ex: ptr= fopen("abc.c", "r");
Modes of file:-
Mode. Meaning
r read
w write
a append
fclose():- To close a file we use fclose() .After every
operation on file we must close the file using
fclose.
fclose()-We pass the file pointer to the fclose().
Syntax for fclose():

fclose(file pointer);

for ex: fclose (ptr);


Error In Reading the file:-
There are various reasons for error in file.
• file may be corrupted.
• file may be deleted
Write a c program to open a file read its content & close the file.

#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];

string_name is any name given to the string


variable and size is used to define the length of the
string, i.e the number of characters strings will
store.
Input and output function in used string-
putchar() and getchar
puts() and gets()
printf() and scanf()
Input and output function-
puts() & gets()
The C programming language includes various
input and output functions that can read and write
these strings.
The most commonly used functions are gets() and
puts().
gets() is an input function used to read string
characters, while puts() is an output function used
to write string characters in unformatted forms.
Header file used for string handling function-

#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-

The “malloc” or “memory allocation” method in


C is used to dynamically allocate a single large
block of memory with the specified size. It
returns a pointer of type void which can be cast
into a pointer of any form . we pass one
argument in malloc().
It doesn’t Initialize memory at execution time
so that it has initialized each block with the
default garbage value initially.
Syntax of malloc() in C

ptr = (cast-type*) malloc(byte-size)


For Example:

ptr = (int*) malloc(100 * sizeof(int));

Since the size of int is 4 bytes, this statement will


allocate 400 bytes of memory. And, the pointer ptr
holds the address of the first byte in the allocated
memory.
C calloc() method
“calloc” or “contiguous allocation” method in C is
used to dynamically allocate the specified number
of blocks of memory of the specified type. it is
very much similar to malloc() but has two different
points and these are:
It initializes each block with a default value ‘0’.
It has two parameters or arguments as compare to
malloc().
Syntax of calloc() in C
ptr = (cast-type*)calloc(n, element-size);
here, n is the no. of elements and element-size is
the size of each element.

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);

where ptr is reallocated with new size 'newSize'.


// Program for dynamic memory allocation
#include <stdio.h>
#include<conio.h>
#include <stdlib.h>
int main()
{ int n, *ptr;
printf("Enter number of elements: ");
scanf("%d", &n);
ptr = (int*) malloc(n * sizeof(int));
if(ptr == NULL)
{
printf("Error! memory not allocated.");
}
else
{
strcpy(ptr,“india”);
printf(“%s”,ptr);
}
free(ptr);
getch();
// Program for dynamic memory allocation
#include <stdio.h>
#include<conio.h>
#include <stdlib.h>
int main()
{ int n, *ptr;
printf("Enter number of elements: ");
scanf("%d", &n);
ptr = (char*) calloc(n , sizeof(int));
strcpy(ptr,“india”);
printf(“%s”,ptr);
realloc(ptr,”1000);
strcpy(ptr,”uttar pradesh”);
printf(“%s”,ptr);
free(ptr);
getch();
Advantages of Dynamic Memory
allocation
• This allocation method has no memory
wastage.
• The memory allocation is done at run
time.
• Memory size can be changed based on
the requirements of the dynamic
memory allocation.
• If memory is not required, it can be
Disadvantages of Dynamic Memory
allocation
• It requires more execution time due
to execution during runtime.
• The compiler does not help with
allocation and deallocation.
Programmer needs to both allocate
and deallocate the memory.
Command-line arguments
The most important function of C is
the main() function. It is mostly
defined with a return type of int and
without parameters.
int main()
{ ...
}
We can also give command-line
Command-line arguments are the
values given after the name of the
program in the command-line shell of
Operating Systems.

Command-line arguments are


handled by the main() function of a
C program.
To pass command-line arguments, we
typically define main() with two
arguments:
(1) the first argument is the number
of command-line arguments
(2) the second is a list of command-
line arguments.
Syntax
int main(int argc, char *argv[] )
{
/* ... */
}
argc (ARGument Count) is an integer
variable that stores the number of
command-line arguments passed by
the user including the name of the
program. So if we pass a value to a
program, the value of argc would be
2 (one for argument and one for
program name)
The value of argc should be non-
negative.
argv (ARGument Vector) is an array
of character pointers listing all the
arguments.
If argc is greater than zero, the array
elements from argv[0] to argv[argc-1]
will contain pointers to strings.
argv[0] is the name of the program ,
After that till argv[argc-1] every
element is command -line arguments.
#include <stdio.h>
#include<conio.h>
int main(int argc, char* argv[])
{
printf("Program name is: %s", argv[0]);
if (argc == 1)
printf("\nNo Extra Command Line Argument Passed "
"Other Than Program Name");
if (argc >= 2) {
printf("\nNumber Of Arguments Passed: %d", argc);
printf("\n----Following Are The Command Line "
"Arguments Passed----");
for (int i = 0; i < argc; i++)
printf("\nargv[%d]: %s", i, argv[i]);
}
getch();
}
• Program Name Is: ./a.out
• Number Of Arguments Passed: 4
• ----Following Are The Command
Line Arguments Passed----
• argv[0]: ./a.out
• argv[1]: First
• argv[2]: Second
• argv[3]: Third
• Properties of Command Line Arguments in C
• They are passed to the main() function.
• They are parameters/arguments supplied
to the program when it is invoked.
• They are used to control programs from
outside instead of hard coding those values
inside the code.
• argv[argc] is a NULL pointer.
• argv[0] holds the name of the program.
• argv[1] points to the first command line
argument and argv[argc-1] points to the last
argument.
Accessing Structure/union Members-
There are two ways to access structure/union variable
(1) using dot(.) operator
(2) Using arrow (->)operator(through
Pointers)
(2)Using arrow (->)operator(through Pointers)
In C, we can also use pointers to access
structure members . To access structure
members using pointers we use the arrow(->)
operator. The arrow operator is used when we
have a pointer to a structure variable.
#include <stdio.h>
#include<conio.h>
#include <string.h>
void main() {
struct Person {
char name[50];
int age;
float height;
};
struct Person person1;
struct Person *personPtr; // Declare a pointer to a structure
personPtr = &person1; // Assign the address of person1 to the pointer
strcpy(personPtr->name, “coding
world"); // Accessing structure members through pointer
personPtr->age = 25;
personPtr->height = 10.0;
printf("Name: %s\n", personPtr->name); // Printing structure member values
printf("Age: %d\n", personPtr->age);
printf("Height: %.2f\n", personPtr->height);
getch();
}
A self-referential structure- a self-
referential structure is a structure
that contains a pointer to a variable
of the same type. This allows the
structure to refer to itself, creating a
linked data structure. Here, Node is a
self-referential structure because it
contains a pointer to a Node object
(next).
C Program to print Fibonacci series
using recursion.

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;

result = a / b; // Division by zero

printf("Result: %d", result);


return 0;
}
A run-time error occurs in this case because we attempt to divide an integer by
zero. During program execution, a run-time error is detected, causing the
program to terminate abruptly. The division by zero is not allowed and leads to
undefined behaviour.
3. Logical Error-Logical error in C, also known as semantic
errors, occur when the program runs without any syntax or
runtime errors but produces incorrect or unexpected
results. These errors are caused by flawed logic or incorrect
algorithmic implementation in the code.
#include <stdio.h>
int main()
{
int radius = 5;
double pi = 3.14;
double area;
area = pi * radius;
printf("Area: %f", area);
return 0; }
4. Semantic Error
Errors that occur because the compiler is unable
to understand the written code are called
Semantic Errors. A semantic error will be
generated if the code makes no sense to the
compiler, even though it is syntactically correct.
It is like using the wrong word in the wrong
place in the English language. For example,
adding a string to an integer will generate a
semantic error.
c=a + b; right
a+b=c; wrong
5. Linker Error
Linker errors occur during the linking phase of the
program's compilation process. The linker resolves
external references, combines multiple object files,
and generates the final executable file. Linker errors
occur when there are issues with linking the object
files together to create the executable.
#include <stdio.h>

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

// y implicitly converted to int. ASCII


// value of 'a' is 97
x = x + y;

// x is implicitly converted to float


float z = x + 1.0;
printf(“%d%d”,x,z);
}
2. Explicit Type Conversion
This process is also called type casting and it is
user-defined. Here the user can typecast the
result to make it of a particular data type. The
syntax in C Programming:
(type) expression Type indicated the data type
to which the final result is converted.
#include<stdio.h>

int main()
{
double x = 1.2;

int sum = (int)x + 1;

printf("sum = %d", sum);

You might also like