Friday, October 02, 2009

GCC C Extension

This is the GNU C Compiler extension for the C programming language.

Arithmetic on void- and Function-Pointers

In GNU C, addition and subtraction operations are supported on pointers to void and on pointers to functions. This is done by treating the size of a void or of a function as 1.

A consequence of this is that sizeof is also allowed on void and on function types, and returns 1.

The option -Wpointer-arith requests a warning if these extensions are used.

Example:

void *ptr;
/* validate ptr pointer */
func(ptr + 2);



Non-Constant Initializers

As in standard C++, the elements of an aggregate initializer for an automatic variable are not required to be constant expressions in GNU C. Here is an example of an initializer with run-time varying elements:

foo (float f, float g)
{
    float beat_freqs[2] = { f-g, f+g };
    ...
}



Nested Functions

A nested function is a function defined inside another function. (Nested functions are not supported for GNU C++.) The nested function's name is local to the block where it is defined. For example, here we define a nested function named square, and call it twice:

foo (double a, double b)
{
    double square(double z) { return z * z; }

    return square(a) + square(b);
}


The nested function can access all the variables of the containing function that are visible at the point of its definition. This is called lexical scoping. For example, here we show a nested function which uses an inherited variable named offset:

bar (int *array, int offset, int size)
{
    int access(int *array, int index)
      { return array[index + offset]; }
    int i;
    ...
    for (i = 0; i < size; i++)
        ... access(array, i) ...
}



64 Bit Integer

GCC support 64 bit integer value with long long type. Example:

unsigned long long value64 = 23ULL;
unsigned long value32 = 23UL;

printf("sizeof(value64) is %i bit\n", sizeof(value64) << 3);
printf("sizeof(value32) is %i bit\n", sizeof(value32) << 3);



http://gcc.gnu.org