#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdarg.h>
#include <signal.h>
#define MAX_SIZE 1024
#define SQUARE(x) ((x) * (x))
#define DEBUG_PRINT(fmt, ...) printf("[DEBUG] " fmt "\n", ##__VA_ARGS__)
#ifdef DEBUG_BUILD
#define LOG_ENABLED 1
#define LOG(msg) printf("[LOG] %s\n", msg)
#else
#define LOG_ENABLED 0
#define LOG(msg)
#endif
#if defined(FEATURE_ADVANCED) || defined(FEATURE_EXTENDED)
#define ADVANCED_FEATURES_AVAILABLE
#ifdef FEATURE_ADVANCED
#define ADVANCED_MODE 1
#elif defined(FEATURE_EXTENDED)
#define ADVANCED_MODE 2
#else
#define ADVANCED_MODE 0
#endif
#endif
#define CONCAT(a, b) a##b
#define STRINGIFY(x) #x
#define IS_DEFINED(x) defined(x)
struct Node;
typedef struct Node Node;
typedef int (*compare_fn)(const void *, const void *);
typedef uint32_t user_id_t;
typedef enum {
STATUS_OK = 0,
STATUS_ERROR = -1,
STATUS_INVALID = -2
} status_t;
struct Point {
float x;
float y;
const char *label;
};
typedef struct {
int width;
int height;
struct Point origin;
} Rectangle;
struct Node {
int data;
struct Node *next;
struct Node *prev;
};
union Value {
int i;
float f;
char str[32];
void *ptr;
};
struct __attribute__((packed)) PackedData {
char flag;
int value;
char data[3];
} __attribute__((aligned(8)));
struct Config {
int max_connections;
float timeout_seconds;
const char *host;
int ports[8];
};
#ifdef __cplusplus
extern "C" {
#endif
int add(int a, int b);
void print_point(const struct Point *p);
Rectangle *create_rectangle(int w, int h, struct Point origin);
int compare_ints(const void *a, const void *b);
static inline int is_valid(int value);
static int global_counter = 0;
extern const char *program_name;
volatile sig_atomic_t signal_flag = 0;
int add(int a, int b) {
return a + b;
}
void print_point(const struct Point *p) {
if (p == NULL) {
printf("Point is NULL\n");
return;
}
printf("Point: (%.2f, %.2f) - %s\n", p->x, p->y, p->label ? p->label : "unnamed");
}
Rectangle *create_rectangle(int w, int h, struct Point origin) {
Rectangle *rect = malloc(sizeof(Rectangle));
if (!rect) {
return NULL;
}
rect->width = w;
rect->height = h;
rect->origin = origin;
return rect;
}
int compare_ints(const void *a, const void *b) {
int ia = *(const int*)a;
int ib = *(const int*)b;
return (ia > ib) - (ia < ib);
}
static inline int is_valid(int value) {
return value >= 0 && value < MAX_SIZE;
}
void complex_function(void) {
int numbers[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
struct Point points[] = {
{0.0f, 0.0f, "origin"},
{1.0f, 1.0f, "unit"},
{-1.0f, -1.0f, "negative"}
};
for (int i = 0; i < 10; i++) {
if (numbers[i] % 3 == 0) {
continue; }
if (numbers[i] % 2 == 0) {
DEBUG_PRINT("Even number: %d", numbers[i]);
} else {
DEBUG_PRINT("Odd number: %d", numbers[i]);
}
}
struct Point temp_point = (struct Point){.x = 42.0, .y = 24.0, .label = "compound"};
print_point(&temp_point);
Rectangle temp_rect = (Rectangle){
.width = 100,
.height = 50,
.origin = (struct Point){10.0, 20.0, "rect_origin"}
};
int sparse_array[10] = {
[0] = 1,
[3] = 4,
[7] = 8,
[9] = 10
};
struct Config default_config = {
.max_connections = 100,
.timeout_seconds = 30.5f,
.host = "localhost",
.ports = {80, 443, 8080, [7] = 9000}
};
status_t status = STATUS_OK;
switch (status) {
case STATUS_OK:
printf("Operation successful\n");
break;
case STATUS_ERROR:
printf("Error occurred\n");
break;
default:
printf("Unknown status\n");
break;
}
int count = 0;
while (count < 3) {
print_point(&points[count]);
count++;
}
int j = 0;
do {
printf("Value: %d\n", SQUARE(j));
j++;
} while (j < 3);
if (global_counter > 100) {
goto cleanup;
}
compare_fn cmp = compare_ints;
qsort(numbers, 10, sizeof(int), cmp);
union Value val;
val.i = 42;
printf("Integer value: %d\n", val.i);
val.f = 3.14f;
printf("Float value: %.2f\n", val.f);
cleanup:
printf("Cleanup completed\n");
}
void debug_log(const char *format, ...) {
va_list args;
va_start(args, format);
vprintf(format, args);
va_end(args);
printf("\n");
}
uint32_t reverse_bits(uint32_t num) {
uint32_t result = 0;
for (int i = 0; i < 32; i++) {
if (num & (1U << i)) {
result |= (1U << (31 - i));
}
}
return result;
}
int main(int argc, char *argv[]) {
printf("C Comprehensive Example\n");
printf("Arguments: %d\n", argc);
if (argc > 1) {
printf("First argument: %s\n", argv[1]);
}
int sum = add(5, 3);
printf("Sum: %d\n", sum);
struct Point p = {10.5f, 20.3f, "test point"};
print_point(&p);
Rectangle *rect = create_rectangle(100, 200, p);
if (rect) {
printf("Rectangle: %dx%d at (%.1f, %.1f)\n",
rect->width, rect->height,
rect->origin.x, rect->origin.y);
free(rect); }
complex_function();
uint32_t original = 0x12345678;
uint32_t reversed = reverse_bits(original);
printf("Original: 0x%08X, Reversed: 0x%08X\n", original, reversed);
debug_log("Debug message with values: %d, %s, %.2f", 42, "hello", 3.14);
int concat_result = CONCAT(12, 34); const char *stringified = STRINGIFY(MAX_SIZE);
#ifdef DEBUG_BUILD
LOG("Debug build detected");
#endif
#if IS_DEFINED(ADVANCED_FEATURES_AVAILABLE)
printf("Advanced features are available\n");
#endif
return EXIT_SUCCESS;
}
static void helper_function(void) {
static int call_count = 0;
call_count++;
printf("Helper called %d times\n", call_count);
}
#ifdef __cplusplus
}
#endif