{"id":18448892,"url":"https://github.com/coding-pelican/c-style-guide","last_synced_at":"2025-07-06T18:35:46.884Z","repository":{"id":194463628,"uuid":"690886215","full_name":"coding-pelican/c-style-guide","owner":"coding-pelican","description":"Guidelines for modern C coding, including style conventions and layout organization.","archived":false,"fork":false,"pushed_at":"2024-06-17T07:14:27.000Z","size":77,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-16T01:51:44.389Z","etag":null,"topics":["c","c-coding","c-style","code-organization","conventions","guide","guidelines","modern-c"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/coding-pelican.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-09-13T04:50:08.000Z","updated_at":"2025-02-23T17:42:19.000Z","dependencies_parsed_at":"2023-09-13T12:56:19.214Z","dependency_job_id":"bba5057d-84b5-47e4-89df-737ef64dd1fd","html_url":"https://github.com/coding-pelican/c-style-guide","commit_stats":null,"previous_names":["coding-pelican/c-cpp-convention","coding-pelican/c-style-guide"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/coding-pelican/c-style-guide","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coding-pelican%2Fc-style-guide","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coding-pelican%2Fc-style-guide/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coding-pelican%2Fc-style-guide/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coding-pelican%2Fc-style-guide/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/coding-pelican","download_url":"https://codeload.github.com/coding-pelican/c-style-guide/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coding-pelican%2Fc-style-guide/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263952431,"owners_count":23534907,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c","c-coding","c-style","code-organization","conventions","guide","guidelines","modern-c"],"created_at":"2024-11-06T07:17:35.146Z","updated_at":"2025-07-06T18:35:46.861Z","avatar_url":"https://github.com/coding-pelican.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# C Style Guide\n\nGuidelines for modern C coding, including style conventions and layout organization.\n\nStill a work in progress.\n\n## Layout \u0026 Order of Groupings\n\n`Public`  ::= `extern`\n\n`Private` ::= `static`\n\n### Header files\n\nThe following groupings should appear in all C header files in the following order:\n\n```txt\n1.  Heading comment\n  2.  Doxygen file prologue\n  3.  Release statement and copyright\n\n4-s.  Header file include guard (opening)\n5-s.  C language link (opening)\n\n  6.  Header file inclusions (when NEEDED instead of forward declarations)\n  7.  Pre-processor definitions\n  8.  Forward declarations (when NEEDED in this header file)\n\n  9.  Public types (definitions: const, enum, struct, ...)\n  10. Public data (declarations: global, extern, static with extern)\n  11. Inline functions (definitions)\n  12. Public function prototypes (declarations)\n\n5-e.  C language link (closing)\n4-e.  Header file include guard (closing)\n```\n\nexample.h:\n\n```c\n/**\n * @file example.h\n * @author {YourName} ({your.email_address@where.com})\n *\n// 2. Doxygen file prologue\n * @brief Example header file.\n * @details This header file shows an example of the layout and grouping order.\n *\n// 3. Release statement and copyright\n * @version {major}.{minor}.{path}{-{pre.release}(optional)}\n * @date YYYY-MM-DD\n *\n * @copyright Copyright {YYYY}. {Your{Name|Company}} All rights reserved.\n            | Released under the {YourLicense a}. See LICENSE file for details.\n */\n\n// 4-s. Header file include guard (opening)\n#ifndef EXAMPLE_H\n#define EXAMPLE_H\n\n// 5-s. C language link (opening)\n#if defined(__cplusplus)\nextern \"C\" {\n#endif // defined(__cplusplus)\n\n// 6. Header file inclusions (when NEEDED instead of forward declarations)\n#include \u003cstdbool.h\u003e\n#include \u003cstddef.h\u003e\n#include \u003cstdint.h\u003e\n\n#include \"foo.h\"\n\n// 7. Pre-processor definitions\n#define EXAMPLE_MAX_SIZE 100\n\n// 8. Forward declarations (when NEEDED in this header file)\ntypedef struct Foo Foo;\n\n// 9. Public types (definitions)\nenum eExampleType {\n    eExampleType_None = -1,\n    eExampleType_Foo  =  0,\n    eExampleType_Bar,\n    eExampleType_Baz,\n    kExampleType_Count\n};\ntypedef int32_t ExampleType;\n\ntypedef struct Point Point;\nstruct Point {\n    int x;\n    int y;\n};\n\ntypedef struct Bar Bar;\nstruct Bar {\n    ExampleType type;\n    Foo foo;\n    Point p;\n};\n\nstatic char const* const kBarTypeName = \"Bar\";\nstatic int         const kBarInitX = 0;\nstatic int         const kBarInitY = 0;\nstatic Point       const kBarInitPoint = {kBarInitX, kBarInitY};\n\n// 10. Public data (declarations)\nextern int gBarCreatedCount;\nextern int gBarDestroyedCount;\n\n// 11. Inline functions (definitions)\nstatic inline Point Point_AddPoint(Point p, Point q) {\n    return (Point){\n        .x = p.x + q.x,\n        .y = p.y + q.y\n    };\n}\n\nstatic inline Point Point_Scale(Point p, int s) {\n    return (Point){\n        .x = p.x * s,\n        .y = p.y * s\n    };\n}\n\n// 12. Public function prototypes (declarations)\nbool ProcessData(uint8_t const* refData, size_t size);\nBar* Bar_Create();\nBar* Bar_Initialize(Bar* outBar, Foo foo, Point p);\nvoid Bar_Destroy(Bar** outBarPtr);\n\n// 5-e. C language link (closing)\n#if defined(__cplusplus)\n}\n#endif // defined(__cplusplus)\n\n// 4-e. Header file include guard (closing)\n#endif // !EXAMPLE_H\n```\n\n### Source files\n\nThe following groupings should appear in all C source files in the following order:\n\n```txt\n1.  Heading comment\n  2.  Doxygen file prologue\n  3.  Release statement and copyright\n\n4.  Header file inclusions (only those that are NECESSARY)\n5.  Pre-processor definitions\n\n6.  Private types (definitions)\n7.  Private data (definitions)\n8.  Private function prototypes (declarations)\n\n9.  Public data (definitions)\n10. Public functions (definitions)\n11. Private functions (definitions)\n```\n\nexample.c\n\n```c\n// 1. Heading comment\n/**\n * @file example.c\n * @author {YourName} ({your.email_address@where.com})\n *\n// 2. Doxygen file prologue\n * @brief Example source file.\n * @details This source file provides implementations for functions declared in example.h.\n *\n// 3. Release statement and copyright\n * @version {major}.{minor}.{path}{-{pre.release}(optional)}\n * @date YYYY-MM-DD\n *\n * @copyright Copyright {YYYY} {Your{Name|Company}}. All rights reserved.\n            | Released under the {YourLicense a}. See LICENSE file for details.\n */\n\n// 4. Header file inclusions (only those that are NECESSARY)\n#include \"example.h\"\n#include \u003cstdbool.h\u003e // Include standard boolean type\n#include \u003cstdio.h\u003e   // Include standard I/O library for printf\n#include \u003cstdlib.h\u003e // Include standard library for malloc\n\n// 5. Pre-processor definitions\n#define EXAMPLE_ADD(_x, _y) ((_x) + (_y))\n#define EXAMPLE_SUB(_x, _y) ((_x) + (_y))\n#define EXAMPLE_MAX(_a, _b) (((_a) \u003c (_b)) ? (_b) : (_a))\n#define EXAMPLE_MIN(_a, _b) (((_a) \u003e (_b)) ? (_b) : (_a))\n\n#define EXAMPLE_DEBUG\n#ifdef EXAMPLE_DEBUG\n#  define ASSERT(_condition)                               \\\n    do {                                                   \\\n        if (!(_condition)) {                               \\\n            (void)fprintf(                                 \\\n                stderr, \"%s:%d: Assertion '%s' failed.\\n\", \\\n                __FILE__, __LINE__, #_condition            \\\n            );                                             \\\n            __builtin_trap();                              \\\n        }                                                  \\\n    } while(0)\n#else\n#  define ASSERT(condition) ((void)0)\n#endif // EXAMPLE_DEBUG\n\n#define EXAMPLE_SWAP(_T, _a, _b) \\\n    do {                         \\\n        _T __t = (_a);           \\\n        (_a)   = (_b);           \\\n        (_b)   = __t;            \\\n    } while (0)\n\n#define EXAMPLE_DO_ASSIGN(_a, _b) do { (_a) = (_b); } while (0)\n\n#define EXAMPLE_TYPE_IS_FOO(_obj) ((_obj).type == eExampleType_Foo)\n\n// 6. Private types (definitions)\ntypedef struct Baz Baz;\nstruct Baz {\n    ExampleType type;\n    Bar         bar;\n    float       value;\n};\n\n// 7. Private data (definitions)\nstatic int sCurrentBarCount = 0;\n\n// 8. Private function prototypes (declarations)\nstatic void TestPoint();\nstatic char const* ExampleType_ToStringType(ExampleType type);\n\n// 9. Public Data (definitions)\nint gBarCreatedCount   = 0; // Initialize global variable\nint gBarDestroyedCount = 0; // Initialize global variable\n\n// 10. Public functions (definitions)\nbool ProcessData(uint8_t const* refData, size_t size) {\n    (void)(refData);\n    (void)(size);\n    // Implementation of ProcessData function\n    return true;\n}\n\nBar* Bar_Create() {\n    gBarCreatedCount++;\n    sCurrentBarCount++;\n    return (Bar*)malloc(sizeof(Bar));\n}\n\nBar* Bar_Initialize(Bar* outBar, Foo foo, Point p) {\n    ASSERT(outBar);\n    ASSERT(EXAMPLE_TYPE_IS_FOO(foo));\n\n    outBar-\u003etype = eExampleType_Bar;\n    outBar-\u003efoo  = foo;\n    outBar-\u003ep    = p;\n\n    return outBar;\n}\n\nvoid Bar_Destroy(Bar** outBarPtr) {\n    ASSERT(outBarPtr);\n    ASSERT(*outBarPtr);\n    free(*outBarPtr);\n    *outBarPtr = NULL;\n\n    gBarDestroyedCount++;\n    sCurrentBarCount--;\n}\n\nint main() {\n    TestPoint();\n\n    Bar* bar = Bar_Initialize(Bar_Create(), (Foo){.type = eExampleType_Foo, .value = 42}, kBarInitPoint);\n    ASSERT(bar);\n    ASSERT(bar-\u003etype == eExampleType_Bar);\n    ASSERT(gBarCreatedCount == 1);\n    ASSERT(gBarDestroyedCount == 0);\n    ASSERT(sCurrentBarCount == 1);\n\n    char const* const typeName = ExampleType_ToStringType(bar-\u003etype);\n    Bar_Destroy(\u0026bar);\n    if (!bar) {\n        printf(\"%s object was successfully destroyed.\\n\", typeName);\n    }\n    ASSERT(gBarCreatedCount == 1);\n    ASSERT(gBarDestroyedCount == 1);\n    ASSERT(sCurrentBarCount == 0);\n\n    return 0;\n}\n\n// 11. Private functions (definitions)\nvoid TestPoint() {\n  // ...\n}\n\nchar const* ExampleType_ToStringType(ExampleType type) {\n    switch (type) {\n    case eExampleType_Foo:\n        return kFooTypeName;\n    case eExampleType_Bar:\n        return kBarTypeName;\n    default:\n        return \"Unknown\";\n    }\n}\n```\n\n## References\n\n[Syque's C Style/CHAPTER 7 : File Layout](http://syque.com/cstyle/ch7.3.htm)\n\n[NuttX documentation's C Coding Standard/File Organization](https://nuttx.apache.org/docs/latest/contributing/coding_style.html#file-organization)\n\n[Axom documentation's Coding Guidelines/4 Header File Organization](https://axom.readthedocs.io/en/develop/docs/sphinx/coding_guide/sec04_header_org.html)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcoding-pelican%2Fc-style-guide","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcoding-pelican%2Fc-style-guide","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcoding-pelican%2Fc-style-guide/lists"}