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

Function

The document provides an overview of functions in C, explaining their purpose, types (library and user-defined), and advantages such as code organization and reusability. It also discusses 2D arrays, their declaration, and applications, as well as the concept of storage classes in C, detailing the four main types: automatic, external, static, and register, along with their characteristics. Each storage class is explained with examples to illustrate their scope and lifetime within a program.

Uploaded by

pgup2612
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Function

The document provides an overview of functions in C, explaining their purpose, types (library and user-defined), and advantages such as code organization and reusability. It also discusses 2D arrays, their declaration, and applications, as well as the concept of storage classes in C, detailing the four main types: automatic, external, static, and register, along with their characteristics. Each storage class is explained with examples to illustrate their scope and lifetime within a program.

Uploaded by

pgup2612
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Function
A function is a block of code designed to perform a specific task, improving
code reusability and organization. Instead of writing the same code repeatedly,
you can call a function whenever needed. Functions are also useful for
breaking down complex programs into manageable parts.
• Function are also called modules or procedures

• ADVANTEGES
• Organize the code
• Reusability
Types of Functions in C
1.Library Functions: Predefined functions provided by C's standard libraries, such as printf(), scanf(), sqrt(), etc
.
2.User-defined Functions: Functions created by the programmer to perform specific tasks.
2D array
• A 2D array in C is essentially an array of arrays. It is used to store
tabular data, like a matrix or a grid, where each element is addressed
by two indices: the row index and the column index

• data_type array_name[row_size][column_size];
• #include <stdio.h>
int main() {
// Declaring and initializing a 2D array
int matrix[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
// Printing the 2D array
printf("Matrix:\n");
for (int i = 0; i < 3; i++)
{// Row loop
for (int j = 0; j < 3; j++) { // Column loop
printf("%d ", matrix[i][j]);
}
printf("\n");
}
return 0;}
#include <stdio.h> 2D arrays in C
int main() {
int rows, cols; // Input rows and columns
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols); // Declare a 2D array int
array[rows][cols]; // Input elements into the array
printf("Enter the elements of the array:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++)
{ printf("Element [%d][%d]: ", i, j);
scanf("%d", &array[i][j]);
} }
// Calculate and print the sum of each row printf("\
nRow-wise sum:\n");
for (int i = 0; i < rows; i++)
{ int rowSum = 0;
for (int j = 0; j < cols; j++)
{
rowSum += array[i][j];
}
printf("Sum of row %d: %d\n", i + 1, rowSum);
}
return 0;
String with Character Array

#include int main()


{char str[20] = "Hello, World!"; // Declaring a string using a character array
printf("String: %s\n", str); // Printing the string
printf("First character: %c\n", str[0]); // Accessing individual characters in the array
printf("Last character: %c\n", str[11]); // Note: '\0' terminates the string
str[7] = 'M’; // Modifying the string
str[8] = 'a’;
str[9] = 'r’;
str[10] = 's’;
printf("Modified String: %s\n", str);
return 0; }
Applications of 2D Arrays:
• Matrices: Mathematical operations like addition, subtraction, and
multiplication.
• Images/Grids: Storing pixel data or grids.
• Game Boards: Representing chessboards, Sudoku grids, etc.
Storage Class
• Storage class determines the scope (visibility) and lifetime (existence)
of variables or functions. It tells the compiler how to store a variable,
where to store it, and how long it should exist during the program's
execution. There are four main storage classes in C:
• Automatic(Auto)
• External(extern)
• Static(static)
• Register(register)
Storge Class Memory Default Value Scope lifetime

auto RAM Garbage with in still the block active


block

Statie RAM 0 with in till the terminatin of


block prog.

Extern RAM O Anywhere in till the Termination


program of prog

Register Register Garbage with in still the block active


block
1.Automatic (auto):
•Automatic variables are simply local variable
•Default for variables inside a function.
•Scope: Local to the block in which the variable is defined.
•Lifetime: Only exists while the block is being executed.
•Example:
syntax auto datatype variable_ name;
void func()
{
auto int a = 10;
}
Declare a variable with the auto storage class inside a function, the variable is local to the function and gets
memory allocated only during the function's execution. After the function ends, the variable is destroyed,
and its value is lost.
• void func()
{
auto int x = 5;
printf("%d\n", x);
}
// Outputs: 5
2.External (extern):
•Used to declare a global variable or function that is accessible across multiple files.
•Scope: Global (outside all blocks).
•Lifetime: Entire runtime of the program.
•Example: extern int count;

The extern storage class is used to share global variables across multiple files. It informs the compiler that the variable
is defined elsewhere, possibly in another file.
File 1:
int count = 0; // Definition
File 2:
extern int count; // Declaration
void func()
{
printf("%d\n", count); // Accessing the variable from File 1
}
3.Static (static):
•Retains the value of a variable across function calls or limits the scope of a global variable to the file in
which it is declared.
•Scope: Local to the block (if declared inside a function) or file (if declared globally).
•Lifetime: Entire runtime of the program.
•Example:
void func()
{
static int counter = 0; counter++;
}
When a variable inside a function is declared as static, its value is retained across multiple calls to the function, even
though its scope is local to the function.
void func()
{
static int counter = 0; counter++;
printf("Counter: %d\n", counter); // Outputs: Counter: 1, Counter: 2, ...
}
4.Register (register):
•Suggests that the variable should be stored in a CPU register for faster access.
•Scope: Local to the block.
•Lifetime: Only exists while the block is being executed.
•Example:
void func()
{
register int i;
}
5. The register storage class suggests to the compiler that the variable should be stored in a CPU register for faster access. This
is particularly useful for variables that are frequently used in loops.
void func()
{
register int i;
for (i = 0; i < 10; i++)
{
printf("%d ", i); // Outputs: 0 1 2 3 4 5 6 7 8 9
}}
• Each storage class has its unique use cases based on variable scope, lifetime, and performance
considerations.

You might also like