0% found this document useful (0 votes)
3 views4 pages

C Programming Full Concepts

The document covers key C programming concepts including variable lifetime, storage classes, recursion vs iteration, and array operations. It provides examples and outputs for static variables, recursion for GCD and Fibonacci, as well as array insertion, deletion, and searching. Additionally, it includes a demonstration of transposing a 3x3 matrix.

Uploaded by

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

C Programming Full Concepts

The document covers key C programming concepts including variable lifetime, storage classes, recursion vs iteration, and array operations. It provides examples and outputs for static variables, recursion for GCD and Fibonacci, as well as array insertion, deletion, and searching. Additionally, it includes a demonstration of transposing a 3x3 matrix.

Uploaded by

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

C Programming Concepts with Examples and Output

1. Lifetime of Variables

The lifetime of a variable is the time during which the variable exists in memory.

Types:
1. Automatic (local): Exists within a function block.
2. Static: Persists throughout the program execution.
3. Dynamic: Allocated manually via malloc(), exists until freed.
4. Global: Exists until program termination.

Example (Static Variable):


--------------------------
void counter() {
static int count = 0;
count++;
printf("%d ", count);
}

Output:
1 2 3 ... (retains value across calls)

2. Storage Classes in C with Examples

Storage classes define scope, visibility and lifetime.

1. auto
----------
int main() {
auto int a = 5;
printf("%d", a);
}
Output: 5

2. register
----------
int main() {
register int speed = 100;
printf("%d", speed);
}
Output: 100

3. static
C Programming Concepts with Examples and Output

----------
void demo() {
static int i = 0;
i++;
printf("%d ", i);
}
Output (called 3 times): 1 2 3

4. extern
----------
// file1.c
int x = 10;

// file2.c
extern int x;
printf("%d", x);

3. Recursion vs Iteration

| Feature | Recursion | Iteration


|
|---------------|------------------------------------|----------------------------------
--|
| Definition | Function calls itself | Loop repeats set of instructions
|
| Memory | High (stack used) | Low
|
| Speed | Usually slower | Faster
|
| Use Cases | Factorial, Fibonacci, Tree | Looping, Counting, Searching
|

4. GCD using Recursion

int gcd(int a, int b) {


if (b == 0)
return a;
return gcd(b, a % b);
}

int main() {
int a = 36, b = 60;
C Programming Concepts with Examples and Output

printf("GCD is %d", gcd(a, b));


}
Output: GCD is 12

5. Fibonacci using Recursion

int fibonacci(int n) {
if (n == 0) return 0;
else if (n == 1) return 1;
else return fibonacci(n - 1) + fibonacci(n - 2);
}

int main() {
int n = 5;
printf("Fibonacci(%d) = %d", n, fibonacci(n));
}
Output: Fibonacci(5) = 5

6. Array Operations with Examples

a) Insertion
--------------
int arr[5] = {1, 2, 3, 5};
int pos = 3, val = 4;
for (int i = 4; i > pos; i--) arr[i] = arr[i-1];
arr[pos] = val;

Output: 1 2 3 4 5

b) Deletion
--------------
int arr[] = {1, 2, 3, 4, 5}, pos = 2;
for (int i = pos; i < 4; i++) arr[i] = arr[i+1];

Output: 1 2 4 5

c) Search
--------------
int arr[] = {10, 20, 30}, key = 20;
for (int i = 0; i < 3; i++) {
if (arr[i] == key) printf("Found at %d", i);
}
C Programming Concepts with Examples and Output

Output: Found at 1

7. Transpose of a 3x3 Matrix

int matrix[3][3] = {{1,2,3},{4,5,6},{7,8,9}};


int transpose[3][3];

for (int i = 0; i < 3; i++) {


for (int j = 0; j < 3; j++) {
transpose[j][i] = matrix[i][j];
}
}

Output:
Original:
1 2 3
4 5 6
7 8 9

Transpose:
1 4 7
2 5 8
3 6 9

You might also like