C Programming Examples:
1. Array and String in C
--------------------------------
Array: A collection of elements of the same data type stored in contiguous memory.
String: A character array that ends with a null character (\0).
Example:
#include <stdio.h>
#include <string.h>
int main() {
// Array Example
int arr[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
// String Example
char str[20] = "Hello, World!";
printf("\nString: %s", str);
return 0;
2. Pointer in C
--------------------------------
Pointers are variables that store the address of another variable.
Example:
#include <stdio.h>
int main() {
int a = 10;
int *p = &a;
printf("Value of a: %d\n", a);
printf("Address of a: %p\n", p);
printf("Value using pointer: %d\n", *p);
return 0;
3. Array with Pointer
--------------------------------
Arrays and pointers are closely related; the name of the array represents the address of its first
element.
Example:
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50};
int *p = arr;
for (int i = 0; i < 5; i++) {
printf("Element %d: %d\n", i, *(p + i));
return 0;
4. Functions in C
--------------------------------
Functions are reusable blocks of code.
Built-in Libraries: Use libraries like #include <math.h>, #include <string.h>, etc.
Example:
#include <stdio.h>
#include <math.h>
void greet() {
printf("Hello from a function!\n");
int main() {
greet();
printf("Square root of 16: %.2f", sqrt(16));
return 0;
}
5. Parameter Passing in C
--------------------------------
Call by Value: A copy of the variable is passed.
Call by Reference: The address of the variable is passed.
Example:
#include <stdio.h>
// Call by Value
void swapValue(int x, int y) {
int temp = x;
x = y;
y = temp;
printf("Inside swapValue: x = %d, y = %d\n", x, y);
// Call by Reference
void swapReference(int *x, int *y) {
int temp = *x;
*x = *y;
*y = temp;
printf("Inside swapReference: x = %d, y = %d\n", *x, *y);
int main() {
int a = 5, b = 10;
printf("Before swapValue: a = %d, b = %d\n", a, b);
swapValue(a, b);
printf("After swapValue: a = %d, b = %d\n", a, b);
printf("Before swapReference: a = %d, b = %d\n", a, b);
swapReference(&a, &b);
printf("After swapReference: a = %d, b = %d\n", a, b);
return 0;
6. Recursion in C
--------------------------------
Recursion is a process where a function calls itself.
(a) Factorial:
Example:
#include <stdio.h>
int factorial(int n) {
if (n == 0 || n == 1)
return 1;
return n * factorial(n - 1);
int main() {
int num = 5;
printf("Factorial of %d is %d\n", num, factorial(num));
return 0;
(b) Fibonacci Series:
Example:
#include <stdio.h>
int fibonacci(int n) {
if (n == 0)
return 0;
if (n == 1)
return 1;
return fibonacci(n - 1) + fibonacci(n - 2);
int main() {
int terms = 10;
printf("Fibonacci Series: ");
for (int i = 0; i < terms; i++) {
printf("%d ", fibonacci(i));
return 0;