开发者

C Idioms and little known facts [closed]

开发者 https://www.devze.com 2022-12-14 21:24 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 12 years ago.

Okay, I've seen many posts here about odd idioms and common practices in C that might not be initially intuitive. Perhaps a f开发者_C百科ew examples are in order

Elements in an array:

#define ELEMENTS(x) (sizeof (x) / sizeof (*(x)))

Odd array indexing:

a[5] = 5[a]

Single line if/else/while/for safe #defines

#define FOO(X) do { f(X); g(X); } while (0)
#define FOO(X) if (1) { f(X); g(X); } else

My question to the expert C programmers out there is: What idioms, practices, code snippits, or little known facts show up a lot in C code but might not be very intuitive but offer a good insight into C programming?


The "arrow operator" for counting down from n-1 to 0:

for ( int i = n; i --> 0; ) ...

It's not hugely common, but it's an interesting illustration that in some ways the initialize/test/update parts of a for loop are convention. That's the usual pattern, but you can still put any arbitrary expressions in there.

It's also a nice little reminder about how lexical analysis works.


Since someone will mention it anyway, it might as well be me: Duff's Device.

It's a nice illustration of the way labels work in C, and understanding it gave me an "aha experience" the first time. This is his original code:

send(to, from, count)
register short *to, *from;
register count;
{
    register n=(count+7)/8;
    switch(count%8){
    case 0: do{ *to = *from++;
    case 7:     *to = *from++;
    case 6:     *to = *from++;
    case 5:     *to = *from++;
    case 4:     *to = *from++;
    case 3:     *to = *from++;
    case 2:     *to = *from++;
    case 1:     *to = *from++;
        }while(--n>0);
    }
}

Today, one wouldn't use register, and avoid old-style function definitions.


The comma operator, while perfectly well documented (K&R etc.) appears in quite a lot of algorithmic code and is often a surprise to many programmers who have not encountered it before. It is often used to simplify some loop constructs:

#define kITERATIONS_MAX 10
int i=0,j=0,array[kITERATIONS_MAX],outArray[kITERATIONS_MAX],temp=0;

for (i=0,j=kITERATIONS_MAX-1; i < kITERATIONS_MAX; i++,j--)
{
temp = array[i]*array[j];
outArray[i]=temp;
}

The above code will multiply array elements 0 thru 9 with 9 thru 0 while avoiding nested loops.

When using the comma operator, both the first and second expressions are evaluated. The result of the first expression is ignored and the result of the second expression is returned.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号