Common styles
There are several common styles of C code, including the GNU style, the Kernighan & Ritchie style, and the original Berkeley style. A style may be selected with a single background option, which specifies a set of values for all other options. However, explicitly specified options always override options implied by a background option.
As of version 1.2, the default style of GNU indent
is the GNU style. Thus, it is no longer necessary to specify the option ‘-gnu’ to obtain this format, although doing so will not cause an error. Option settings which correspond to the GNU style are:
|
-nbad -bap -nbc -bbo -bl -bli2 -bls -ncdb -nce -cp1 -cs -di2 -ndj -nfc1 -nfca -hnl -i2 -ip5 -lp -pcs -nprs -psl -saf -sai -saw -nsc -nsob
|
The GNU coding style is that preferred by the GNU project. It is the style that the GNU Emacs C mode encourages and which is used in the C portions of GNU Emacs. (People interested in writing programs for Project GNU should get a copy of The GNU Coding Standards, which also covers semantic and portability issues such as memory usage, the size of integers, etc.)
The Kernighan & Ritchie style is used throughout their well-known book The C Programming Language. It is enabled with the ‘-kr’ option. The Kernighan & Ritchie style corresponds to the following set of options:
|
-nbad -bap -bbo -nbc -br -brs -c33 -cd33 -ncdb -ce -ci4 -cli0 -cp33 -cs -d0 -di1 -nfc1 -nfca -hnl -i4 -ip0 -l75 -lp -npcs -nprs -npsl -saf -sai -saw -nsc -nsob -nss |
Kernighan & Ritchie style does not put comments to the right of code in the same column at all times (nor does it use only one space to the right of the code), so for this style indent
has arbitrarily chosen column 33.
The style of the original Berkeley indent
may be obtained by specifying ‘-orig’ (or by specifying ‘--original’, using the long option name). This style is equivalent to the following settings:
|
-nbad -nbap -bbo -bc -br -brs -c33 -cd33 -cdb -ce -ci4 -cli0 -cp33 -di16 -fc1 -fca -hnl -i4 -ip4 -l75 -lp -npcs -nprs -psl -saf -sai -saw -sc -nsob -nss -ts8
|
The Linux style is used in the linux kernel code and drivers. Code generally has to follow the Linux coding style to be accepted. This style is equivalent to the following settings:
|
-nbad -bap -nbc -bbo -hnl -br -brs -c33 -cd33 -ncdb -ce -ci4 -cli0 -d0 -di1 -nfc1 -i8 -ip0 -l80 -lp -npcs -nprs -npsl -sai -saf -saw -ncs -nsc -sob -nfca -cp33 -ss -ts8 -il1
|
表 1. Indent代码格式化说明
使用的indent参数 |
值 |
含义 |
--blank-lines-after-declarations |
bad |
变量声明后加空行 |
--blank-lines-after-procedures |
bap |
函数结束后加空行 |
--blank-lines-before-block-comments |
bbb |
块注释前加空行 |
--break-before-boolean-operator |
bbo |
较长的行,在逻辑运算符前分行 |
--blank-lines-after-commas |
nbc |
变量声明中,逗号分隔的变量不分行 |
--braces-after-if-line |
bl |
"if"和"{"分做两行 |
--brace-indent 0 |
bli0 |
"{"不继续缩进 |
--braces-after-struct-decl-line |
bls |
定义结构,"struct"和"{"分行 |
--comment-indentationn |
c33 |
语句后注释开始于行33 |
--declaration-comment-columnn |
cd33 |
变量声明后注释开始于行33 |
--comment-delimiters-on-blank-lines |
ncdb |
不将单行注释变为块注释 |
--cuddle-do-while |
ncdw |
"do --- while"的"while"和其前面的"}"另起一行 |
--cuddle-else |
nce |
"else"和其前面的"}"另起一行 |
--case-indentation 0 |
cli0 |
switch中的case语句所进0个空格 |
--else-endif-columnn |
cp33 |
#else, #endif后面的注释开始于行33 |
--space-after-cast |
cs |
在类型转换后面加空格 |
--line-comments-indentation n |
d0 |
单行注释(不从1列开始的),不向左缩进 |
--break-function-decl-args |
nbfda |
关闭:函数的参数一个一行 |
--declaration-indentationn |
di2 |
变量声明,变量开始于2行,即不必对齐 |
--format-first-column-comments |
nfc1 |
不格式化起于第一行的注释 |
--format-all-comments |
nfca |
不开启全部格式化注释的开关 |
--honour-newlines |
hnl |
Prefer to break long lines at the position of newlines in the input. |
--indent-leveln |
i4 |
设置缩进多少字符,如果为tab的整数倍,用tab来缩进,否则用空格填充。 |
--parameter-indentationn |
ip5 |
旧风格的函数定义中参数说明缩进5个空格 |
--line-length 75 |
l75 |
非注释行最长75 |
--continue-at-parentheses |
lp |
续行从上一行出现的括号开始 |
--space-after-procedure-calls |
pcs |
函数和"("之间插入一个空格 |
--space-after-parentheses |
nprs |
在"("后")"前不插入空格 |
--procnames-start-lines |
psl |
将函数名和返回类型放在两行定义 |
--space-after-for |
saf |
for后面有空格 |
--space-after-if |
sai |
if后面有空格 |
--space-after-while |
saw |
while后面有空格 |
--start-left-side-of-comments |
nsc |
不在生成的块注释中加* |
--swallow-optional-blank-lines |
nsob |
不去掉可添加的空行 |
--space-special-semicolon |
nss |
一行的for或while语句,在";"前不加空。 |
--tab-size |
ts4 |
一个tab为4个空格(要能整除"-in") |
--use-tabs |
ut |
使用tab来缩进 |