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

Functions and Arrays

The document discusses various concepts in C programming, including passing arrays to functions, using pointers for direct variable manipulation, and handling command line arguments. It also covers recursive functions, string manipulation functions, and the different storage classes that determine variable scope and lifetime. Key examples illustrate each concept, demonstrating their practical applications.

Uploaded by

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

Functions and Arrays

The document discusses various concepts in C programming, including passing arrays to functions, using pointers for direct variable manipulation, and handling command line arguments. It also covers recursive functions, string manipulation functions, and the different storage classes that determine variable scope and lifetime. Key examples illustrate each concept, demonstrating their practical applications.

Uploaded by

Sahil
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

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

2. Functions and Pointers

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

3. Command Line Arguments

Command line arguments allow users to pass arguments to a program at runtime. In


C, they are accessed through the parameters int argc (argument count) and char
*argv[] (argument values).
Example:

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

To run the program with arguments:

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 provides a set of standard library functions for string manipulation in <string.h>.


Here are some commonly used ones:

 strcpy(dest, src): Copies src string to dest.


 strcat(dest, src): Concatenates src to the end of dest.
 strlen(str): Returns the length of str.
 strcmp(str1, str2): Compares two strings lexicographically.
Example:

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:

6.1 Automatic Storage Class

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
}

6.2 Register Storage Class

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
}

6.3 Static Storage Class

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

6.4 External Storage Class

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.

You might also like