Sunday, July 15, 2007

GCC Extensions

Gcc provides some cool extensions to the C language. The following, I think, are very useful.

Designated Initializers for arrays

Just like you can initialize structure variables by specifying name of structure members, you can initialize array elements by specifying indices(You can even specify ranges).
For ex:-
int id[256] = { ['A' ... 'Z'] = 1, ['a' ... 'z'] = 1, ['0' ... '9'] = 1, ['_'] = 1 };

__func__ variable

The __func__ variable contains the name of the current function. This comes in handy when you want to print debugging messages of the form,

func-name : msg

The following macro accomplishes this task

#define DEBUGP(format, ...) fprintf(stderr,"%s : " format,__func__, ## __VA_ARGS__)

Note that you can't pass a char * variable directly to DEBUGP to print it.Instead use

DEBUGP("%s", c); /* print the message in c */

No comments: