c语言语法规范
1.#include后面要先加空格
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <unistd.h>
2.指针后面的*号要跟在变量名后面
aeEventLoop *el;const char *hostip;const char *hostsocket;
3.结构体的括号
typedef struct benchmarkThread {int index;pthread_t thread;aeEventLoop *el;} benchmarkThread;
4.函数声明加上变量名
char *redisGitSHA1(void);char *redisGitDirty(void);static void writeHandler(aeEventLoop *el, int fd, void *privdata, int mask);static void createMissingClients(client c);static benchmarkThread *createBenchmarkThread(int index);static void freeBenchmarkThread(benchmarkThread *thread);
5.if ,while,for后面先加空格,也就是说()前面要加空格,但是函数调用不用。
if (strtoll(redisGitSHA1(),NULL,16)) {version = sdscatprintf(version, " (git:%s", redisGitSHA1());if (strtoll(redisGitDirty(),NULL,10))version = sdscatprintf(version, "-dirty");version = sdscat(version, ")");}if (hostsocket == NULL)fprintf(stderr, "%s:%d\n", ip, port);while (count--) {state = (state*1103515245+12345);data[i++] = '0'+((state>>16)&63);}for (i = 1; i < argc; i++)} else if (!strcmp(argv[i],"-h")) {if (lastarg) goto invalid;config.hostip = strdup(argv[++i]);} else if (!strcmp(argv[i],"-p")) {if (lastarg) goto invalid;config.hostport = atoi(argv[++i]);} else if (!strcmp(argv[i],"-s")) {if (lastarg) goto invalid;config.hostsocket = strdup(argv[++i]);} else if (!strcmp(argv[i],"-a") ) {if (lastarg) goto invalid;config.auth = strdup(argv[++i]);} else if (!strcmp(argv[i],"--user")) {
6.= 或+=这种写法加两边必须加空格
ust = ((long)tv.tv_sec)*1000000;ust += tv.tv_usec;mst = ((long long)tv.tv_sec)*1000;mst += tv.tv_usec/1000;
7.需要用到花括号的地方第一个花括号之前要有空格
static uint64_t dictSdsHash(const void *key) {return dictGenHashFunction((unsigned char*)key, sdslen((char*)key));}
8.|| ,&&, :,?,==这类加空格
if (ctx == NULL || ctx->err) {fprintf(stderr,"Could not connect to Redis at ");char *err = (ctx != NULL ? ctx->errstr : "");if (hostsocket == NULL)fprintf(stderr,"%s:%d: %s\n",ip,port,err);elsefprintf(stderr,"%s: %s\n",hostsocket,err);goto cleanup;}
9.for循环;后面要有空格,逗号后面也必须要有空格.
for (; i < 2; i++) {int res = redisGetReply(c, &r);if (reply) freeReplyObject(reply);reply = res == REDIS_OK ? ((redisReply *) r) : NULL;if (res != REDIS_OK || !r) goto fail;if (reply->type == REDIS_REPLY_ERROR) {fprintf(stderr, "ERROR: %s\n", reply->str);goto fail;}if (reply->type != REDIS_REPLY_ARRAY || reply->elements < 2) goto fail;sub_reply = reply->element[1];char *value = sub_reply->str;if (!value) value = "";switch (i) {case 0: cfg->save = sdsnew(value); break;case 1: cfg->appendonly = sdsnew(value); break;}fprintf(stderr, "ERROR: failed to fetch CONFIG from ");
10.if,else 能够写在一行就写在一行
if (hostsocket == NULL) fprintf(stderr, "%s:%d\n", ip, port);else fprintf(stderr, "%s\n", hostsocket);
11.if,else写法
if (config.keepalive) {resetClient(c);} else {if (config.num_threads) pthread_mutex_lock(&(config.liveclients_mutex));config.liveclients--;createMissingClients(c);config.liveclients++;if (config.num_threads)pthread_mutex_unlock(&(config.liveclients_mutex));freeClient(c);}if (config.user == NULL)len = redisFormatCommand(&buf, "AUTH %s", config.auth);elselen = redisFormatCommand(&buf, "AUTH %s %s",config.user, config.auth);if (from) {c->obuf = sdscatlen(c->obuf,from->obuf+from->prefixlen,sdslen(from->obuf)-from->prefixlen);} else {for (j = 0; j < config.pipeline; j++)c->obuf = sdscatlen(c->obuf,cmd,len);
12.强制类型转换,指针类型后面加空格
(char *) config.hostip,
13.case的:后面必须空格
switch(i++){case 0: name = token; break;case 1: addr = token; break;case 2: flags = token; break;case 3: master_id = token; break;
代码来源于github上的redis源码。有些地方可能总结的不是很好,需要的花,自己可以去github上参考写的好的代码是怎么写的。