c
examples
examples.c🔧c
/*
* ============================================================================
* IDEs AND COMPILERS - EXAMPLES
* ============================================================================
* This file contains various examples to test your compiler setup.
*
* Try different compilation commands:
*
* Basic: gcc examples.c -o examples
* Warnings: gcc -Wall -Wextra examples.c -o examples
* Debug: gcc -g examples.c -o examples
* Optimize: gcc -O2 examples.c -o examples
* Standard: gcc -std=c11 examples.c -o examples -lm
*
* Run: ./examples
* ============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
/*
* Example 1: Test Basic Compilation
* ---------------------------------
* If this works, your compiler is set up correctly.
*/
void example_basic_compilation() {
printf("\n=== Example 1: Basic Compilation Test ===\n");
printf("Hello, World!\n");
printf("Your compiler is working correctly!\n");
// Print compiler info
#ifdef __GNUC__
printf("\nCompiler: GCC %d.%d.%d\n",
__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__);
#elif defined(__clang__)
printf("\nCompiler: Clang %d.%d.%d\n",
__clang_major__, __clang_minor__, __clang_patchlevel__);
#elif defined(_MSC_VER)
printf("\nCompiler: MSVC %d\n", _MSC_VER);
#else
printf("\nCompiler: Unknown\n");
#endif
}
/*
* Example 2: Test Warning Flags
* -----------------------------
* Compile with -Wall -Wextra to see warnings.
*/
void example_warning_flags() {
printf("\n=== Example 2: Warning Flags Test ===\n");
// These might generate warnings with -Wall -Wextra
int x = 10;
int y; // Uninitialized variable warning
printf("x = %d\n", x);
// Intentionally unused variable
int unused = 42;
(void)unused; // Cast to void to suppress warning
// Use y to avoid warning
y = 20;
printf("y = %d\n", y);
printf("\nCompile with: gcc -Wall -Wextra examples.c\n");
printf("to see potential warnings.\n");
}
/*
* Example 3: Test Debugging Support
* ---------------------------------
* Compile with -g for debugging.
*/
void example_debugging() {
printf("\n=== Example 3: Debugging Test ===\n");
int numbers[] = {10, 20, 30, 40, 50};
int sum = 0;
// Put a breakpoint here in your debugger
for (int i = 0; i < 5; i++) {
sum += numbers[i]; // Step through this loop
}
printf("Sum of array: %d\n", sum);
printf("\nTo debug:\n");
printf("1. Compile: gcc -g examples.c -o examples\n");
printf("2. Run GDB: gdb ./examples\n");
printf("3. Set breakpoint: break example_debugging\n");
printf("4. Run: run\n");
printf("5. Step: next\n");
printf("6. Print: print sum\n");
}
/*
* Example 4: Test Math Library
* ----------------------------
* Requires linking with -lm
*/
void example_math_library() {
printf("\n=== Example 4: Math Library Test ===\n");
double x = 2.0;
printf("sqrt(%.1f) = %.4f\n", x, sqrt(x));
printf("pow(%.1f, 3) = %.1f\n", x, pow(x, 3));
printf("sin(PI/2) = %.4f\n", sin(M_PI / 2));
printf("cos(0) = %.4f\n", cos(0));
printf("log(10) = %.4f\n", log(10));
printf("log10(100) = %.4f\n", log10(100));
printf("\nCompile with: gcc examples.c -o examples -lm\n");
printf("(-lm links the math library)\n");
}
/*
* Example 5: Test Optimization
* ----------------------------
* Compare speed with different -O flags.
*/
void example_optimization() {
printf("\n=== Example 5: Optimization Test ===\n");
clock_t start, end;
double cpu_time;
// A computationally intensive task
volatile long sum = 0;
int iterations = 50000000;
start = clock();
for (int i = 0; i < iterations; i++) {
sum += i;
}
end = clock();
cpu_time = ((double)(end - start)) / CLOCKS_PER_SEC;
printf("Summed %d numbers\n", iterations);
printf("Result: %ld\n", sum);
printf("Time: %.3f seconds\n", cpu_time);
printf("\nCompare with different optimization levels:\n");
printf(" gcc -O0 examples.c -o examples -lm (no optimization)\n");
printf(" gcc -O2 examples.c -o examples -lm (standard)\n");
printf(" gcc -O3 examples.c -o examples -lm (maximum)\n");
}
/*
* Example 6: Test C Standard Versions
* ------------------------------------
* Different features available in C99, C11, C17
*/
void example_c_standards() {
printf("\n=== Example 6: C Standard Test ===\n");
// Show current standard
#if defined(__STDC_VERSION__)
printf("C Standard: ");
#if __STDC_VERSION__ >= 201710L
printf("C17 or later\n");
#elif __STDC_VERSION__ >= 201112L
printf("C11\n");
#elif __STDC_VERSION__ >= 199901L
printf("C99\n");
#else
printf("Pre-C99\n");
#endif
printf("__STDC_VERSION__ = %ld\n", __STDC_VERSION__);
#else
printf("C89/C90 (no __STDC_VERSION__ defined)\n");
#endif
// C99 feature: Variable-length arrays (VLA)
printf("\nC99 feature: Variable-length arrays\n");
int n = 5;
int vla[n]; // VLA - size determined at runtime
for (int i = 0; i < n; i++) {
vla[i] = i * 10;
}
printf("VLA contents: ");
for (int i = 0; i < n; i++) {
printf("%d ", vla[i]);
}
printf("\n");
// C99 feature: Designated initializers
printf("\nC99 feature: Designated initializers\n");
int arr[5] = {[0] = 1, [2] = 3, [4] = 5};
printf("arr = {%d, %d, %d, %d, %d}\n",
arr[0], arr[1], arr[2], arr[3], arr[4]);
printf("\nCompile with: gcc -std=c99 examples.c\n");
printf(" or: gcc -std=c11 examples.c\n");
printf(" or: gcc -std=c17 examples.c\n");
}
/*
* Example 7: Test Multiple File Compilation
* -----------------------------------------
* Demonstrates how multi-file projects work.
*/
// Normally these would be in separate files
int external_add(int a, int b); // Prototype
int external_multiply(int a, int b);
// Implementations (would be in separate .c files)
int external_add(int a, int b) {
return a + b;
}
int external_multiply(int a, int b) {
return a * b;
}
void example_multiple_files() {
printf("\n=== Example 7: Multi-File Compilation ===\n");
int result1 = external_add(10, 20);
int result2 = external_multiply(10, 20);
printf("external_add(10, 20) = %d\n", result1);
printf("external_multiply(10, 20) = %d\n", result2);
printf("\nFor real multi-file projects:\n");
printf(" 1. Create main.c with main()\n");
printf(" 2. Create utils.c with helper functions\n");
printf(" 3. Create utils.h with prototypes\n");
printf(" 4. Compile: gcc main.c utils.c -o program\n");
printf("\n Or use object files:\n");
printf(" gcc -c main.c -o main.o\n");
printf(" gcc -c utils.c -o utils.o\n");
printf(" gcc main.o utils.o -o program\n");
}
/*
* Example 8: Test Makefile Usage
* ------------------------------
* Shows how to use Makefiles.
*/
void example_makefile() {
printf("\n=== Example 8: Makefile Example ===\n");
printf("Create a file named 'Makefile' with:\n\n");
printf("# Simple Makefile for C projects\n");
printf("CC = gcc\n");
printf("CFLAGS = -Wall -Wextra -g -std=c11\n");
printf("TARGET = program\n");
printf("SOURCES = main.c utils.c\n");
printf("OBJECTS = $(SOURCES:.c=.o)\n");
printf("\n");
printf("$(TARGET): $(OBJECTS)\n");
printf("\t$(CC) $(OBJECTS) -o $(TARGET)\n");
printf("\n");
printf("%%.o: %%.c\n");
printf("\t$(CC) $(CFLAGS) -c $< -o $@\n");
printf("\n");
printf("clean:\n");
printf("\trm -f $(OBJECTS) $(TARGET)\n");
printf("\n");
printf("Then run: make\n");
printf("Clean: make clean\n");
}
/*
* Example 9: Test IDE Features
* ----------------------------
* Code for testing IDE features.
*/
void example_ide_features() {
printf("\n=== Example 9: IDE Features Test ===\n");
// Test code completion
struct Person {
char name[50];
int age;
float height;
};
struct Person p1;
strcpy(p1.name, "Alice"); // Your IDE should suggest members after p1.
p1.age = 25;
p1.height = 5.6;
printf("Person: %s, %d years, %.1f ft\n", p1.name, p1.age, p1.height);
// Test go to definition
printf("\nIDE Tips:\n");
printf("- Hover over 'printf' to see documentation\n");
printf("- Right-click 'Person' and 'Go to Definition'\n");
printf("- Press Ctrl+Space for code completion\n");
printf("- Set breakpoints by clicking line numbers\n");
}
/*
* Example 10: Complete Setup Test
* -------------------------------
* Verifies everything is working.
*/
void example_complete_test() {
printf("\n=== Example 10: Complete Setup Test ===\n");
int tests_passed = 0;
int total_tests = 5;
// Test 1: Basic output
printf("[Test 1] Basic output: ");
printf("PASSED\n");
tests_passed++;
// Test 2: Math operations
printf("[Test 2] Math operations: ");
if (sqrt(16.0) == 4.0) {
printf("PASSED\n");
tests_passed++;
} else {
printf("FAILED\n");
}
// Test 3: String operations
printf("[Test 3] String operations: ");
if (strlen("Hello") == 5) {
printf("PASSED\n");
tests_passed++;
} else {
printf("FAILED\n");
}
// Test 4: Memory allocation
printf("[Test 4] Memory allocation: ");
int *ptr = malloc(sizeof(int));
if (ptr != NULL) {
*ptr = 42;
if (*ptr == 42) {
printf("PASSED\n");
tests_passed++;
} else {
printf("FAILED\n");
}
free(ptr);
} else {
printf("FAILED\n");
}
// Test 5: Time functions
printf("[Test 5] Time functions: ");
time_t now = time(NULL);
if (now > 0) {
printf("PASSED\n");
tests_passed++;
} else {
printf("FAILED\n");
}
// Summary
printf("\n==================\n");
printf("Tests passed: %d/%d\n", tests_passed, total_tests);
if (tests_passed == total_tests) {
printf("Your setup is complete! ✓\n");
} else {
printf("Some tests failed. Check your setup.\n");
}
}
/*
* Main Function
*/
int main() {
printf("╔════════════════════════════════════════════════╗\n");
printf("║ IDE AND COMPILER SETUP TEST ║\n");
printf("║ Testing your development environment ║\n");
printf("╚════════════════════════════════════════════════╝\n");
example_basic_compilation();
example_warning_flags();
example_debugging();
example_math_library();
example_optimization();
example_c_standards();
example_multiple_files();
example_makefile();
example_ide_features();
example_complete_test();
printf("\n=== All Examples Completed! ===\n");
printf("Your development environment is ready.\n");
return 0;
}