Functions and Arrays
Functions and Arrays
In C, arrays can be passed to functions, allowing for manipulation of large data sets in
an organized way. Since arrays are passed by reference, modifications within the
function affect the original array.
Example:
c
Copy code
#include <stdio.h>
void displayArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int numbers[] = {1, 2, 3, 4, 5};
int size = sizeof(numbers) / sizeof(numbers[0]);
displayArray(numbers, size); // Function call
return 0;
}
Pointers allow functions to access and modify variables directly, providing efficiency
and flexibility.
Example:
c
Copy code
#include <stdio.h>
void increment(int *n) {
(*n)++; // Increment the value at the address
}
int main() {
int num = 5;
increment(&num); // Pass the address of num
printf("Incremented value: %d\n", num); // Output will be 6
return 0;
}
c
Copy code
#include <stdio.h>
int main(int argc, char *argv[]) {
printf("Number of arguments: %d\n", argc);
for (int i = 0; i < argc; i++) {
printf("Argument %d: %s\n", i, argv[i]);
}
return 0;
}
bash
Copy code
./program_name arg1 arg2
4. Recursive Functions
Recursion occurs when a function calls itself. Commonly used for problems like
factorials, Fibonacci series, etc., recursion must have a base case to prevent infinite
loops.
Example:
c
Copy code
#include <stdio.h>
int factorial(int n) {
if (n <= 1) return 1; // Base case
return n * factorial(n - 1); // Recursive call
}
int main() {
int num = 5;
printf("Factorial of %d is %d\n", num, factorial(num));
return 0;
}
5. String Functions
c
Copy code
#include <stdio.h>#include <string.h>
int main() {
char str1[20] = "Hello";
char str2[] = "World";
strcat(str1, str2);
printf("Concatenated string: %s\n", str1); // Output: HelloWorld
printf("Length of string: %lu\n", strlen(str1));
return 0;
}
6. Storage Classes in C
Storage classes in C determine the scope, visibility, and lifetime of variables. The four
main storage classes are:
By default, local variables inside a function have an automatic storage class. They are
created when the function is called and destroyed when it ends.
c
Copy code
void example() {
int autoVar = 10; // Automatic storage, scope limited to this
function
}
Register variables are stored in CPU registers rather than RAM for faster access. They
are used for variables frequently accessed within a function.
c
Copy code
void example() {
register int counter = 0; // Hints the compiler to store counter in
a register
}
Static variables retain their value between function calls. They are initialized only
once and are persistent throughout the program’s execution.
c
Copy code
#include <stdio.h>
void example() {
static int count = 0; // Static variable retains value between calls
count++;
printf("Count is %d\n", count);
}
int main() {
example(); // Output: Count is 1
example(); // Output: Count is 2
return 0;
}
External (or global) variables are defined outside of any function and are accessible
from any function within the file (and potentially other files, if declared with extern).
Example:
c
Copy code
#include <stdio.h>
int globalVar = 10; // Global variable, accessible throughout the
program
void example() {
printf("Global variable: %d\n", globalVar);
}
int main() {
example();
return 0;
}
Summary
Functions and Arrays: Arrays passed to functions allow easy handling of large data sets.
Functions and Pointers: Functions using pointers enable direct manipulation of values.
Command Line Arguments: argc and argv are used for runtime arguments.
Recursive Functions: Functions that call themselves, useful for problems like factorial,
Fibonacci.
String Functions: Functions like strcpy, strcat, strlen, strcmp are essential for
string manipulation.
Storage Classes: Automatic (local), Register (fast access), Static (retains value across calls),
and External (global scope) storage classes control the behavior and visibility of variables.