0% found this document useful (0 votes)
4 views

Module - 4

The document provides an overview of structures in C programming, including their syntax, types, and key features such as flexibility and memory management. It also discusses storage classes, memory allocation, and the relationship between pointers and arrays, along with examples of dynamic memory allocation functions like malloc, calloc, realloc, and free. Additionally, it lists various programming tasks that can be performed using C.

Uploaded by

sunitha
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)
4 views

Module - 4

The document provides an overview of structures in C programming, including their syntax, types, and key features such as flexibility and memory management. It also discusses storage classes, memory allocation, and the relationship between pointers and arrays, along with examples of dynamic memory allocation functions like malloc, calloc, realloc, and free. Additionally, it lists various programming tasks that can be performed using C.

Uploaded by

sunitha
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/ 54

Function to sum upto n

Module - 4
Structure
• A structure in C is a user-defined data type
• It combine data items of different kinds
• A structure is a collection of variables (can be of different data types)
under a single name.
• Key features of structure
– Flexibility: Structures allows to store different types of data together.
– Organization: organizes complex data in a more readable and maintainable way.
– Memory Management: Structures can be used to manage memory more
efficiently
– Syntax
struct structureName
{
dataType member1;
dataType member2;
... };
Syntax

struct student struct student


{ {
char name[50]; char name[50];
int rno; int rno;
double per; double per;
}; }s,s1,s2[30];
struct student s,s1,s2[30];
Syntax and type of Structure
• Structure type discussed
1. Structure
2. Nested structure
3. Array of structure
4. Referential structure
5. Pointer to a structure
Structure
Syntax Example
Struct struct_name
{ Struct stud
datatype mem1, {
Datatype mem2, int rno;
. char name[50]
. } s1,*s2,s3[25];
.
Datatype memn Struct stud s4;
}; Struct s5={ 10,”Sunitha”};
Nested structure
Syntax Example
Struct parent_struct Struct medicine
{ {
datatype mem1; int rno;
char name[50];
struct cstruct_name
strcut spel
{
{
datatype m1;
char sname[50];
}c;
}spl;
}; } m;
Array of structures
Syntax Example
struct struct_name Struct stud
{ {
datatype mem1; int rno;
datatype mem2; char name[50];
... }s[100];
dataype memn; Struct stud s1[20];
} s[100];
Referential structure
Syntax Example
Struct struct_name Struct list
{ {
datatype mem1; int data;
... struct list *next;
datatype mn; }L;
struct struct_name
*mem;
}s;
Structure pointer
Syntax Example
Struct struct_name Struct stud
{ {
datatype mem1; int rno;
... char name[30];
datatype mem2; }*s;
}*struct_vr; Struct stud *s1,*s2[10];
Example
Example
Example
Nested structure
Access members through pointer
Structure array
Referential structures
structures that have one or more pointers
which point to the same type of structure, as
their member.
typedef
• typedef keyword is used to create an alias for
a data type
• It is used to redefine the name of already
existing data types
• Syntax of typedef
– typedef existing_type new_type;
– existing_type: The type that needs an alias (e.g.,
int, float, struct, etc.).
– new_type: The new name for the existing type.
Demo program Question No. 7
Storage classes and visibility
• In C programming, storage classes define the scope
(visibility), lifetime, and memory location of variables
• They help to manage how and where variables are
stored
• how variablescan be accessed.
• There are 4 types of storage classes in c
i. Automatic Storage (auto)
ii. Register Storage (register)
iii. Static Storage (static)
iv. External Storage (extern)
Automatic (auto)
• The auto storage class is the default for all
local variables declared inside a function or
block
• These variables are automatically created
when the block is entered and destroyed
when the block is exited
• auto: Default for local variables, limited to
the block scope.
Example
Register
• The register storage class is used to store
variables in CPU registers instead of RAM
• Register variables access and operation is
faster
• Register variables cannot have their addresses
taken.
• Register: Stored in CPU registers for faster
access, limited to block scope
static
• The static storage class is used to declare
variables that retain their value between
function calls
• Static variables are initialized only once and
exist until the program terminates
• They can be local to a function or global to a
file.
• Static: Retains value between function calls,
can be local or global.
Extern
• The extern storage class is used to declare a
global variable or function that is defined in
another file or block
• It allows different files to share the same
global variable or function.

Extern: Used for global variables/functions
shared across files.
Comparison of storage class variables
Dynamic memory allocation
The Memory is divided into three sections.
• Heap Memory: It is a part of the main memory. It is
unorganized and treated as a resource when you require
the use of it if not release. Heap memory can’t be used
directly it can be used with the help of a pointer.
• Stack Memory: It stores temporary variables created
by a function. In stack, variables are declared, stored,
and initialized during runtime. It follows the
First in last out method .
• Code Section: Whenever the program is to be
executed it will be brought into the main memory. This
program will get stored under the code section
Static Memory Allocation
• Static memory allocation refers to allocating memory for variables during
the compilation phase

• The memory allocated using static allocation remains fixed throughout the
program’s execution and is determined at compile time
Key Features:
1. Allocation and deallocation are done by the compiler.
2. It uses a data structures stack for static memory allocation.
3. Variables get allocated permanently.
4. No reusability.
5. Execution is faster than dynamic memory allocation.
6. Memory is allocated before runtime.
Ex: int a,b[100]; char n[50]; struct stud s;
Dynamic memory allocation
• Dynamic memory allocation refers to allocating and deallocating
memory for variables during the runtime of a program
• dynamic memory allocation allows to request memory from the
operating system as needed and release it when it’s no longer
required
• Provides flexibility in managing memory resources
• In c following functions are used to allocate memory
dynamically
1. malloc()
2. calloc()
3. realloc()
4. free()
malloc
• malloc function in C is used to allocate a block of
memory dynamically
• The memory allocated by malloc() is
uninitialized
• Syntax
– void* malloc(size_t size);
– size: The number of bytes to allocate.
– Returns: A pointer to the allocated memory, or NULL if the
allocation fails
– Ex
int *p;
p = (int *) malloc( 10 * sizeof(int));
The above statement allocates 40 bytes of memory and p is pointing to
the 1st address
malloc
calloc
• Calloc function allocates memory and
initializes all bits to zero
• Syntax :
– int *p;
– p=(int *) calloc(100,sizeof(int))
– A 200 bytes of memory is allocated and 1st address
is hold by the pointer p and all bits are initialized
to 0
– Returns null if sufficient memory is not available
realloc
• It is used to resize a previously
allocated memory block
• It allows to change the size of an
existing memory allocation without
needing to free the old memory
• Syntax
– ptr = realloc(ptr,size)
– ptr will reallocate the existing size with
the new size
realloc
free
• The free() function is used to release
dynamically allocated memory back to the
operating system
• After freeing a memory block, the pointer
becomes invalid, and it is no longer pointing to
a valid memory location
• Syntax
– free(ptr)
Singly linked list

Singly Linked List is a linear and unidirectional data


structure, where data is saved on the nodes, and each node is
connected via a link to its next node
Each node contains a data field and a link to the next node
Explain Formatted Input and Output Statements in C
Formatted input and output statements in C allow for structured input and output of
data. The most commonly used functions for formatted I/O are:
1. printf() – Used for formatted output.
2. scanf() – Used for formatted input.
The printf() function is used to display data on the screen in a specified format.
Syntax:
printf("format string", argument_list);
The scanf() function is used to read input from the user in a formatted way.
Syntax
scanf("format string", argument_list);
Explain how pointers and arrays are related
Relationship Between Pointers and Arrays in C
In C, pointers and arrays are closely related because an array name acts as a pointer
to its first element.
Example:
int a={12,82,33,14,51}; int *p; p=a;
1. The name of an array acts as a pointer to its first element.
2. The expression a[i] is equivalent to *(p + i). where i=0,1,2,3,4
3. The expression &a[i] = p+I where i=0,1,2,3,4
4. pointer arithmetic like p++, p--, p+2, p-2 allows to traverse on array elements
Programs
1. finding sum upto n
2. Factorial of a number
3. Checking number is odd or even
4. Finding the area ( 3.14*r*r) or circumference of a circle (2 * 3.14 *
r)
5. Adding two numbers
6. Checking given number is prime or not
7. Finding sum and average of array elements using pointers
8. Swapping of two numbers using pointers
9. Sum numbers from m to n
10. Check given string is palindrome or not
11. Check the type of quadratic equation

You might also like