c语言程序中注释的格式化,格式化C语言命令indent
indent是linux下一個能力極強的代碼整理軟件,使用他,可以輕松的寫出代碼風格十分精良的代碼。
但是indent的參數太多,使用起來不是很容易,怎么辦呢?
查看
/usr/src/linux-headers-/scripts/Lindent
文件 ,可以看到一行代碼:
indent -npro -kr -i8 -ts8 -sob -l80 -ss -ncs-cp1
這一行就是linux內核使用indent整理代碼的格式,使用這條命令就可以實現風格十分良好的C或C++代碼
其中-l80是每一行最多80個字母,超出會拆行,如果不喜歡可以使用更長的行字數
使用的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 tobreak 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來縮進
【轉貼】用indent 進行代碼格式化
2009-03-27 12:01
indent常用的參數設置
以前的用法:indent-kr -cli4 -i4 -nut
改進的用法:indent-kr -cli4 -nut -bl4 -bli0
注釋:
-kr, UseKernighan & Ritchie coding style.
-cli4,specifies the number of spaces that case labels should be indentedto the right of the containing switch statement.
-nut,不使用空格。
-bl4,大括號內的縮進,這里為4個空格。
-bli0,“if”語句后大括號的縮進,一般設為“0”。
indent工具參數
indent是一個常用的Linux的C代碼格式化工具,但是參數太多,很難記憶,目前發現一組參數,可以格式化為我常用的代碼格式。
-bad -bap-bbb -bbo -nbc -bl -bli0 -bls -c33 -cd33 -ncdb -ncdw -nce -cli0-cp33 -cs -d0 -nbfda-nfc1-nfca -hnl -ip5 -l75 -lp -pcs -nprs -saf -sai -saw -nsc -nsob -nss-i4 -ts4 -ut
保存在~/.indent.pro,就是默認的indent參數了。
3.5. 用 indent進行代碼格式化
一個范例,勝過前言萬語。用 indent 格式化代碼,再從格式化前后的格式變化, 來學習格式化規范。
可以用不同的參數調用 indent,
3.5.1. introduce indent
What is Indent
The `indent' program can be used to make code easier to read. Itcan also convert from one style. of writing C to another.
Download
Current Version: GNU indent 2.2.7。 Download:
3.5.2. Indent參數規范
我們參照GNU,Kernighan & Ritchie,Berkeley風格,制定了自己風格:
indent命令參數:
-bad -bap -bbb -bbo -nbc -bl -bli0 -bls -c33 -cd33 -ncdb -ncdw -nce-cli0 -cp33 -cs -d0 -nbfda -di2 -nfc1 -nfca -hnl -ip5 -l75 -lp -pcs-nprs -psl -saf -sai -saw -nsc -nsob -nss -i4 -ts4 -ut
indent配置文件
如上參數可寫入用戶目錄下的文件:".indent.pro",作為運行indent的確 省參數。
indent配置說明
表 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 theinput.
--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來縮進
總結
以上是生活随笔為你收集整理的c语言程序中注释的格式化,格式化C语言命令indent的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 郑州治疗无精症最好的医院推荐
- 下一篇: 卖精子多少钱啊?