Home > C/C++, GNU/Linux, Programming > GNU C: Extensions to the C Language Family

GNU C: Extensions to the C Language Family

Hi. Today I’ll talk about the extensions to the C language family introduced by the GNU C.
The GNU C provides several language features not found in ANSI standard C. These extensions are available both in C and C++. The `-pedantic’ option directs GNU CC to print a warning message if any of these features is used.
The list of these features is very long: often we use them implicitly. I will show to you only those I consider most useful and “strange”:

- Nested Functions

A nested function is a function defined inside another function:

void foo() {

	int bar(int a) { return ++a; }
	int b = 0;

	b = bar(3);

	printf("b = %d\n", b);



In this example I have declared a nested function “bar” inside the “foo” function: the name of the nested function, obviously, is local to the block where it’s defined. The value of “b” will be 4.
The nested function can access all the variables defined first than the nested function.

void foo() {

        int z = 4;
	int bar(int a) { return a+z; }
	int b = 0;

	b = bar(3);

	printf("b = %d\n", b);



In this example, the value of “b” will be 7.

- Double-Word Integers

GNU C defines a new size (64 bit) of signed and unsigned integer: long long int and unsigned long long int. We can use them in arithmetic just like a simple int.

- Arrays of Variable Length

We can define variable-length automatic arrays: the ISO C90 forbids this type of array.

void foo(int z) {

	int a[z];


In this example I define a size of “z” for the array “a”.

- Labeled Elements in Initializers

In C standard you have to initialize an array in a fixed order: you have to start from the first element of the array.
In the GNU C you can initilize an array in any order. I.e.:

void foo() {

	int a[] = {[3]=2, [4]=3};


In this example we define an array. It has 4 locations, but we don’t start to initialize from the first one: we start from the third location. The “0-1-2″ locations are initialized to 0, according to standard C.
This feature could be useful in a simple parser:

void foo() {

        int c;
	FILE *stream;

	char pars[] = {['a']=2, ['b']=3};

	for ( c=fgetc(stream); !pars[c] && c != EOF; c=fgetc(stream) ) {


	return pars[c];

- Case Ranges

In GNU C we can declare a set of consecutive values in “switch” statement like this:

case low ... high:

In case of letters:

case 'A' ... 'C':

Or numbers:

case 1 ... 10:

The post is finished, but you can investigate this features or look up for others in the gnu site: Here.

Bye bye.

About these ads
  1. Traktopel
    November 9, 2010 at 19:24 | #1

    long long int isn’t a gnu’s c extension, it’s described on c99 standard :) best Regards

  2. June 29, 2013 at 23:59 | #2

    Reblogged this on Elmar Klausmeier's Weblog and commented:
    Good to know.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

%d bloggers like this: