MoreAboutC

Notes for C 语言编程透视

Format your C code

1
2
$ indent -kr file.c # format C source code according to K&R format
$ man indent # more style

Before

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>
#include <dirent.h>
int main(){
FILE * fp = fopen("/root/.cache/test/mountpoint/file1","r");
char line [1000];
while( fgets(line, 1000, fp) ){ puts(line); }

DIR * dp = opendir("/root/.cache/test/mountpoint/mydir");
struct dirent * entry ;
while( (entry = readdir(dp)) != NULL ){ puts(entry->d_name); }

}

After

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>
#include <dirent.h>
int main()
{
FILE *fp = fopen("/root/.cache/test/mountpoint/file1", "r");
char line[1000];
while (fgets(line, 1000, fp)) {
puts(line);
}

DIR *dp = opendir("/root/.cache/test/mountpoint/mydir");
struct dirent *entry;
while ((entry = readdir(dp)) != NULL) {
puts(entry->d_name);
}

}