C Directory
This directory contains C programs. Below is a brief description of each file.
🔧 C Coding Standards
Documentation Requirements
All C code must follow Doxygen-style documentation standards:
1. File Header Documentation
Every C file should include a comprehensive file header:
/**
* @file filename.c
* @brief Brief one-line description of the file
*
* Detailed description of what this file contains,
* its purpose, and main functionality.
*
* @author Your Name
* @version 1.0
* @date 2025-10-15
* @copyright MIT License
*
* @details
* Additional implementation details, algorithms used,
* or important notes about the code.
*/
2. Function Documentation
All functions must be documented with Doxygen comments:
/**
* @brief Brief description of what the function does
*
* Detailed description including algorithm explanation,
* complexity analysis, and important notes.
*
* @param param1 Description of first parameter
* @param param2 Description of second parameter
* @return Description of return value
* @retval 0 Success
* @retval -1 Error occurred
*
* @warning Important warnings about usage
* @note Additional notes or implementation details
*
* @code
* // Example usage:
* int result = my_function(10, 20);
* if (result == 0) {
* printf("Success!\n");
* }
* @endcode
*
* @see related_function()
*/
int my_function(int param1, int param2) {
// implementation
return 0;
}
3. Variable Documentation
Document global variables and important local variables:
/** @brief Maximum buffer size in bytes */
#define MAX_BUFFER_SIZE 1024
/** @brief Global counter for tracking operations */
static int operation_count = 0;
/**
* @struct Point
* @brief Represents a 2D point
*/
struct Point {
int x; /**< X coordinate */
int y; /**< Y coordinate */
};
4. Code Style Guidelines
// Naming conventions
#define MAX_SIZE 100 // UPPER_SNAKE_CASE for macros
typedef struct Point Point; // PascalCase for types
int calculate_sum(int a); // snake_case for functions
int total_count; // snake_case for variables
// Indentation: 4 spaces (no tabs)
int main(void) {
if (condition) {
// Opening brace on same line
do_something();
} else {
do_something_else();
}
return 0;
}
// Function prototypes at top of file
int add(int a, int b);
void print_result(int value);
// Always check for NULL
int* ptr = malloc(sizeof(int));
if (ptr == NULL) {
fprintf(stderr, "Memory allocation failed\n");
return -1;
}
5. Best Practices
- Memory Management: Always free allocated memory
char* buffer = malloc(100); if (buffer != NULL) { // use buffer free(buffer); buffer = NULL; // Prevent use-after-free } - Error Handling: Check return values
FILE* file = fopen("data.txt", "r"); if (file == NULL) { perror("Failed to open file"); return -1; } fclose(file); - Const Correctness: Use const for read-only parameters
void print_string(const char* str) { printf("%s\n", str); } - Header Guards: Always use header guards
#ifndef MYHEADER_H #define MYHEADER_H // declarations #endif /* MYHEADER_H */
6. Complete Example
/**
* @file array_utils.c
* @brief Utility functions for array manipulation
* @author Code-Contribution Community
* @version 1.0
*/
#include <stdio.h>
#include <stdlib.h>
/**
* @brief Finds the maximum value in an integer array
*
* @param arr Pointer to the array (must not be NULL)
* @param size Number of elements in the array
* @return Maximum value found in the array
*
* @warning Returns INT_MIN if array is NULL or size is 0
*
* @code
* int numbers[] = {3, 7, 2, 9, 1};
* int max = find_max(numbers, 5);
* printf("Max: %d\n", max); // Output: Max: 9
* @endcode
*/
int find_max(const int* arr, size_t size) {
if (arr == NULL || size == 0) {
return INT_MIN;
}
int max = arr[0];
for (size_t i = 1; i < size; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
Files
bubblesort.c: Implementation of the Bubble Sort algorithm.checkPrime.c: Program to check for prime numbers.hello-world.c: A simple “Hello, World!” program in C.insertionSort.c: Implementation of the Insertion Sort algorithm.
How to Compile and Run
To compile and run any of the C programs, use the following commands:
gcc filename.c -o outputfile
./outputfile
Replace filename.c with the name of the C file you want to compile, and outputfile with the desired name of the executable.
Contribution Guidelines
Feel free to add more C programs to this directory. Make sure to update this README.md file with a brief description of the new programs you add.