Simple Programming Guidelines for C Language

The programming guidelines for C language provide recommendations based on the clarity, simplicity, readability, and maintainability of the code.

1. Indentation and Spaces

Recommendation: Use 4 spaces for indentation. Never use the Tab key, and it is recommended that your editor automatically convert Tabs to 4 spaces. This makes it convenient; for example, you can search for “notepad++ tab to 4 spaces” to see how to set it up.

Everyone uses different editors to write code, and the settings for the Tab key may vary. However, the space key is the same for everyone. Therefore, avoid using the Tab key in your code.

// Good: 4 spaces indentation
int main() {
    if (condition) {
        // code block
    } else {
        // code block
    }
}
// Bad: inconsistent indentation (sometimes spaces, sometimes Tabs, or 2 spaces)
int main() {
  if (condition) {→   // → This is the Tab key, code block
    } else {
        // code block
    }
}

Operators and Spaces:

Put spaces around binary operators.

int sum = a + b;     // Good
if (x > y && y != z) // Good
int sum=a+b;         // Bad

Do not add spaces between unary operators and operands.

i++;     // Good
i ++;    // Bad
*ptr;    // Good
* ptr;   // Bad
&value;  // Good
& value; // Bad

Commas and Semicolons:

// Good
for (i = 0; i < 10; i++) func(a, b, c);
// Bad  for (i=0;i<10;i++)func(a,b,c);

2. Braces {} Style

K&R Style, where the left brace does not start on a new line, is also commonly used in the Linux kernel, which reduces code length and makes it more compact. I personally prefer this style.

Allman Style (also known as BSD style), where the left brace starts on a new line.

// K&R Style (left brace does not start on a new line)
int main() {
    if (condition) {
        // code
    } else {
        // code
    }
}
// Allman Style
int main() {
    if (condition) {
        // code
    } else {
        // code
    }
}

3. Line Length and Line Breaks

  • Each line of code should ideally not exceed 80 or 120 characters, making it easier to read on different devices and editors.

  • Long expressions should break at low-priority operators, aligning the new line with the previous line’s expression level.

// Good: Break before the operator, with appropriate indentation
if (very_long_variable_name_1 > very_long_variable_name_2    && another_long_condition     && final_condition) {
    long_function_name(very_long_argument_1,
                        very_long_argument_2,
                       very_long_argument_3);
}
// Or indent like this
result = very_long_variable_name_1 + very_long_variable_name_2         + very_long_variable_name_3;

4. Naming Conventions

Consistency is key! The entire project should use a unified naming style.

Variables and Functions

Snake_case (all lowercase, separated by underscores)

For example:student_count, <span><span>calculate_average()</span></span>

// Good
int error_number;
int number_of_completed_connection;

Some common abbreviations:

  • argument can be abbreviated as arg
  • buffer can be abbreviated as buff
  • clock can be abbreviated as clk
  • command can be abbreviated as cmd
  • compare can be abbreviated as cmp
  • configuration can be abbreviated as cfg
  • device can be abbreviated as dev
  • error can be abbreviated as err
  • hexadecimal can be abbreviated as hex
  • increment can be abbreviated as inc
  • initialize can be abbreviated as init
  • maximum can be abbreviated as max
  • message can be abbreviated as msg
  • minimum can be abbreviated as min
  • parameter can be abbreviated as para
  • previous can be abbreviated as prev
  • register can be abbreviated as reg
  • semaphore can be abbreviated as sem
  • statistic can be abbreviated as stat
  • synchronize can be abbreviated as sync
  • temp can be abbreviated as tmp

Macros and Constants

All uppercase snake_case

For example:<span>MAX_SIZE</span>, <span>PI_VALUE</span>

<span><span> </span><strong><span><span>Type Definitions</span></span><span><span> (struct, enum, typedef)</span></span></strong></span>

<span><span> <span>Snake_case + suffix </span></span><strong><span><span><span> For example: student_info_</span><span>s</span><span>, color_info_</span><span>e</span><span>, student_info_</span><span>t</span></span></span></strong></span>

<strong><span> <span>Pointer Variables</span></span></strong>

<strong><span> </span><span><span><span>Add * next to the type</span></span></span><code><span><span>*</span></span>, and the variable name should indicate its meaning

<strong><span><span><span> For example:</span></span><span><span>char *filename;</span></span></span></strong>

5. Function Writing Standards

<strong><span><span> </span></span><span><span>Function names should describe the action the function performs, generally using a verb or a verb + noun structure, </span></span><span><span>and the total length of the function should not exceed 80 lines</span><span>.</span></span></strong>

// Good
int calculate_student_score(int student_id,
                             const char *course_name,
                            int exam_weight) {
    // Function body
    int score = 0;
    // ... calculation logic ...
    return score;
}

<strong><span><span>6. Control Statement Standards</span></span></strong>

<strong><strong><span> if-else Statements:</span></strong></strong>

// Good
if (condition) {
    // code
} else if (another_condition) {
    // code  
} else {
    // code
}
// Even if there is only one line of code, it is recommended to use braces
if (condition) {
    do_something();
}
// Bad: Missing braces can lead to errors
if (condition)
    do_something();
    // This line always executes, not within the if's scope
    do_another_thing();  

<strong><strong><strong><span> switch Statements:</span></strong></strong></strong>

switch (variable) {
case VALUE_1:
    // code
    break;
case VALUE_2:
    // code
    break;
default:
    // code
    break;
}

Leave a Comment