当前位置:网站首页>C language code specification

C language code specification

2022-04-23 06:47:00 tilblackout

(1)switch case
swich and case Labels should be aligned in the same column

switch (character) {
    
case 'a':
	...
	break;
default:
	break;
}

(2) The length of a line is limited to 80 Column
More than a line break , And indent appropriately , Usually an indentation is enough .
(3) Brackets
Curly braces are used in the code {} The place of , You should put the starting brace “{” Put it at the end of the line , Put the closing braces “}” Put it at the beginning of the line . Such as ifswitchfordowhile.

  • Be careful : The starting brace of the function should be placed at the beginning of the next line

When there is only one single statement, do not enlarge the parentheses :

if (condition)
	action();
// Multi branch case : As long as a branch is not a separate statement, it should be bracketed 
if (condition) {
    
	do_this();
	do_that();
} else {
    
	otherwise();
}

(4) Space

  • Do not add spaces after unary operators , Such as *( The pointer )、!~sizeof
  • Binary operators ( Both sides are followed by operands ) You need to add a space on both sides , Such as *( Multiplication )、=!===%
  • In pointer type ,* It should be close to the variable name or function name , Not close to the type name .

(5) notes
Comments should tell others what the code does , Not how to do it . Comments should be placed at the head of the function , And... Should be selected /* ……… */, instead of //.
For multiline comments , It should be like this :

/*
* This is the preferred style for multi-line
* Please use it consistently.
*/

File information notes ( Put it at the beginning of the file )

/*************************************************
Copyright  zuozhongkai Co., Ltd. 1998-2018. All rights reserved.
File name: //  file name 
Author: // author 
Version: // Version number 
Description: //  Used to specify the main functions of this program file , With other modules 
			 //  Or function interface , Output value 、 Value range 、 The control between meaning and parameters 
			 //  system 、 The order 、 Independence or dependence, etc 
Others: //  Description of other contents 
Log: //  Modification log , Including the modification of the content , date , Modifier, etc 
*************************************************/

Comments on functions

/*
 * @Description:  Function description , Describe the basic functions of this function 
 * @param 1 –  Parameters  1.
 * @param 2 –  Parameters  2
 * @return –  Return value 
*/

(6) Naming rules

  • Words in lowercase , Then underline each word “_” come together (Linux Naming style )
  • Use complete words or abbreviations that everyone knows
  • Variables with mutually exclusive meaning or functions with opposite actions should be named with mutually exclusive phrases

(7) function

  • A function completes a function
  • Duplicate code should be refined into functions
  • Different functions are separated by empty lines , If you need to export ,EXPORT* Macro should stick close to his end
    Under braces
int system_is_up(void)
{
    
	return system_state == SYSTEM_RUNNING;
}
EXPORT_SYMBOL(system_is_up);
  • If there are multiple loops in a function , Or exit from multiple locations , It is recommended to use goto
  • Function nesting should not exceed 4 layer
  • Check the validity of the parameters of the function
  • Only the functions used in the current file should be added static

(8) macro

  • The name of the macro that defines the constant and the label in the enumeration should be capitalized , But function macros can be lowercase
  • A macro that uses an expression to define a constant must place the expression within a pair of parentheses , Such as
    #define CONSTEXP (CONSTANT | 3)
  • For macros with multiple statements , The proposal is written as do while(0) Format
  • If the macro is a constant , It is recommended to use const Instead of

版权声明
本文为[tilblackout]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230549283010.html