c
examples
examples.c🔧c
/*
* ============================================================================
* HISTORY AND FEATURES OF C - EXAMPLES
* ============================================================================
* This file demonstrates the key features of C language that make it unique.
*
* Compile: gcc examples.c -o examples
* Run: ./examples
* ============================================================================
*/
#include <stdio.h> // Standard I/O library
#include <stdlib.h> // Standard library (malloc, free)
#include <string.h> // String functions
#include <time.h> // Time functions
/*
* Example 1: Simple and Efficient Syntax
* --------------------------------------
* C has a clean, minimal syntax that's easy to understand.
*/
void example_simple_syntax() {
printf("\n=== Example 1: Simple Syntax ===\n");
// Variable declaration - simple and clear
int age = 25;
float height = 5.9;
char grade = 'A';
// Output - straightforward
printf("Age: %d\n", age);
printf("Height: %.1f\n", height);
printf("Grade: %c\n", grade);
}
/*
* Example 2: Middle-Level Language Features
* -----------------------------------------
* C allows both high-level and low-level operations.
*/
void example_middle_level() {
printf("\n=== Example 2: Middle-Level Features ===\n");
int number = 42;
// High-level: Simple variable operations
printf("High-level: number = %d\n", number);
// Low-level: Access memory address
printf("Low-level: address of number = %p\n", (void*)&number);
// Low-level: Bit manipulation
printf("Low-level: number in binary representation\n");
printf(" number = %d (decimal)\n", number);
printf(" number << 1 = %d (left shift, multiply by 2)\n", number << 1);
printf(" number >> 1 = %d (right shift, divide by 2)\n", number >> 1);
printf(" number & 1 = %d (check if odd)\n", number & 1);
}
/*
* Example 3: Pointer Feature (Unique to C)
* ----------------------------------------
* Pointers give direct access to memory.
*/
void example_pointers() {
printf("\n=== Example 3: Pointers ===\n");
int value = 100;
int *ptr = &value; // Pointer stores address of value
printf("value = %d\n", value);
printf("Address of value (&value) = %p\n", (void*)&value);
printf("Pointer ptr stores = %p\n", (void*)ptr);
printf("Value at pointer (*ptr) = %d\n", *ptr);
// Modify value through pointer
*ptr = 200;
printf("\nAfter *ptr = 200:\n");
printf("value = %d (changed through pointer!)\n", value);
}
/*
* Example 4: Structured Programming
* ---------------------------------
* Programs are divided into functions for modularity.
*/
// Helper functions for structured programming example
int add(int a, int b) {
return a + b;
}
int multiply(int a, int b) {
return a * b;
}
void example_structured() {
printf("\n=== Example 4: Structured Programming ===\n");
int x = 5, y = 3;
// Using modular functions
printf("%d + %d = %d\n", x, y, add(x, y));
printf("%d * %d = %d\n", x, y, multiply(x, y));
printf("Code is organized into reusable functions!\n");
}
/*
* Example 5: Rich Library Support
* -------------------------------
* C provides many built-in library functions.
*/
void example_library() {
printf("\n=== Example 5: Rich Library ===\n");
// stdio.h - Input/Output
printf("Using stdio.h: printf for output\n");
// string.h - String operations
char str1[20] = "Hello";
char str2[20] = " World";
strcat(str1, str2);
printf("Using string.h: strcat result = %s\n", str1);
printf("Using string.h: strlen result = %lu\n", strlen(str1));
// time.h - Time operations
time_t now = time(NULL);
printf("Using time.h: Current time = %s", ctime(&now));
// stdlib.h - Random numbers
srand(time(NULL));
printf("Using stdlib.h: Random number = %d\n", rand() % 100);
}
/*
* Example 6: Memory Management
* ----------------------------
* C allows manual control over memory allocation.
*/
void example_memory() {
printf("\n=== Example 6: Memory Management ===\n");
// Static allocation (at compile time)
int static_arr[5] = {1, 2, 3, 4, 5};
printf("Static array: ");
for (int i = 0; i < 5; i++) {
printf("%d ", static_arr[i]);
}
printf("\n");
// Dynamic allocation (at runtime)
int *dynamic_arr = (int*)malloc(5 * sizeof(int));
if (dynamic_arr != NULL) {
// Initialize dynamic array
for (int i = 0; i < 5; i++) {
dynamic_arr[i] = (i + 1) * 10;
}
printf("Dynamic array: ");
for (int i = 0; i < 5; i++) {
printf("%d ", dynamic_arr[i]);
}
printf("\n");
// Free the allocated memory
free(dynamic_arr);
printf("Memory freed successfully!\n");
}
}
/*
* Example 7: Recursion
* --------------------
* Functions can call themselves.
*/
int factorial(int n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}
int fibonacci(int n) {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
void example_recursion() {
printf("\n=== Example 7: Recursion ===\n");
printf("Factorial of 5 = %d\n", factorial(5));
printf("First 10 Fibonacci numbers: ");
for (int i = 0; i < 10; i++) {
printf("%d ", fibonacci(i));
}
printf("\n");
}
/*
* Example 8: Fast Execution Demonstration
* ---------------------------------------
* C performs operations very quickly.
*/
void example_speed() {
printf("\n=== Example 8: Fast Execution ===\n");
clock_t start, end;
double cpu_time_used;
long sum = 0;
int iterations = 10000000; // 10 million iterations
start = clock();
for (int i = 0; i < iterations; i++) {
sum += i;
}
end = clock();
cpu_time_used = ((double)(end - start)) / CLOCKS_PER_SEC;
printf("Summed %d numbers\n", iterations);
printf("Result: %ld\n", sum);
printf("Time taken: %f seconds\n", cpu_time_used);
printf("C is fast!\n");
}
/*
* Example 9: Portability
* ----------------------
* The same code runs on different platforms.
*/
void example_portability() {
printf("\n=== Example 9: Portability ===\n");
// This code compiles and runs on:
// - Windows
// - Linux
// - macOS
// - And many other platforms!
printf("This code is portable!\n");
printf("Compile it on any system with a C compiler.\n");
// Check the operating system
#ifdef _WIN32
printf("Currently running on: Windows\n");
#elif __linux__
printf("Currently running on: Linux\n");
#elif __APPLE__
printf("Currently running on: macOS\n");
#else
printf("Currently running on: Unknown OS\n");
#endif
}
/*
* Main Function - Program Entry Point
*/
int main() {
printf("╔════════════════════════════════════════════════╗\n");
printf("║ FEATURES OF C PROGRAMMING LANGUAGE ║\n");
printf("║ Demonstrating what makes C special ║\n");
printf("╚════════════════════════════════════════════════╝\n");
// Run all examples
example_simple_syntax();
example_middle_level();
example_pointers();
example_structured();
example_library();
example_memory();
example_recursion();
example_speed();
example_portability();
printf("\n=== All Examples Completed! ===\n");
printf("Review each example to understand C's features.\n");
return 0;
}