Introduction to the C99 Programming Language : Part II

In this article, we are going to discover some more interesting additions to C89 which gave us C99 standard:

  1. Variable Argument Lists: C99 brings out a small changes to the preprocessor. Macros can accept a variable number of arguments. These Macros are indicated by ellipsis (…) in their declaration. These Variable Arguments are represented by the built-in preprocessor identifier __VA_RAGS .
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <stdio.h>
    #include <string.h>
      
    #define compare(compfunc, ...) compfunc(__VA_ARGS__)
      
    int main()
    {
        // Here compare(strlen, "one")
        // transforms to strlen("one")
        printf("Length is %lu\n",
               compare(strlen, "one"));
      
        // compares variable arguements
        printf("String Comparison Result: %d\n",
               compare(strcmp, "one", "one"));
      
        return 0;
    }
    chevron_right
    
    
    Output:
    Length is 3
    String Comparison Result: 0
    
  2. _Pragma Operator: C99 specifies pragma implementation by using the operator _Pragma.

    Syntax:

    _Pragma("directive")
    
    directive is the pragma being called.
    

    This _Pragma operator helps pragmas to participate in macro replacement.

    Some Built in Pragmas are:



    Pragma Meaning
    STDC FP_CONTRACT ON/OFF/DEFAULT If STDC FP_CONTRACT is on then the floating point numbers are treated as indivisible units handled by hardware based methods.
    STDC FENV_ACCESS ON/OFF/DEFAULT STDC FENV_ACCESS ON pragma tells the compiler that the floating point environment might be accessed.
    STDC CX_LIMITED_RANGE ON/OFF/DEFAULT STDC CX_LIMITED_RANGE ON tells the compiler that certain formulas invoking complex values are safe.

    These three macros are used rarely. This feature addresses a major problem with ‘#pragma’: being a directive, it cannot be produced as the result of macro expansion.

    Some Inbuilt C89 macros are revised and new Macros like __STDC_HOSTED__, __STDC__VERSION, __STDC_IEC_559__(supports floating point arithmetic), __STDC_IEC_599_COMPLEX__(supports complex arithmetic), STDC_ISO_10646__(gives specification date in) are added.

  3. Variable Declaration Inside Loop: In C89 standard, loop variables has to be declared before the code initialization. But in C99 we can declare loop variable at the loop initialization part. The variable declared within a for loop will be local to the loop.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <stdio.h>
      
    int main(void)
    {
        // declare variable i at initialization
        int n = 5;
        for (int i = 0; i < n; i++)
            printf("%d ", i);
    }
    chevron_right
    
    
    Output:
    0 1 2 3 4
    

    Here the scope of i is only inside the loop.

  4. Compound Literals: Compound Literals are array, structure or union expressions representing objects of the given type.

    Example:

    int *a = (int[]){11, 12, 13}

    The above expression creates a pointer to an integer called a which points to the first of a three-element array of integer values.

    A compound literal is usually created by specifying a parenthesized type name followed by initialization list enclosed between the curly braces as shown above.

    A compound literal created at file scope exists throughout the program. A compound literal if created inside a block will be destroyed when the block is left i.e Compound literal is local inside the block but it is unknown outside the block.



    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <stdio.h>
      
    int main()
    {
        int* a = (int[]){ 11, 12, 13 };
        for (int i = 0; i < 3; i++)
            printf("%d ", a[i]);
    }
    chevron_right
    
    
    Output:
    11 12 13
    
  5. Flexible Array Structure Members: C99 allows to specify an unsized array as a last member of the structure. These other elements should be declared before the unsized array elements. These sizes are allocated dynamically later by using malloc().

    The below sample Program Demonstrates the usage of flexible array structures.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <stdio.h>
    #include <stdlib.h>
      
    struct GFG {
        int a;
        int arr[];
    };
      
    struct GFG* p;
      
    int main()
    {
        // Here arr will be instantiated
        // as a[10] as we use
        // sizeof(GFG)+10*sizeof(int)
        p = (struct GFG*)malloc(
            sizeof(struct GFG)
            + 10 * sizeof(int));
      
        // Results 0 as every element
        // will be set to zero
        printf("%d\n", p->arr[0]);
    }
    chevron_right
    
    
    Output:
    0
    
  6. Designated Initialization in Arrays: C99 standard helps to initialize some elements of the array during declaration. This feature is mainly helpful to the programmers who work on sparse arrays.

    The below program shows the Designated Initialization on Arrays.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <stdio.h>
      
    int main()
    {
        // initializes index 0 and 1 in a[5]
        int a[5] = {[0] = 100, [1] = 200 };
      
        for (int i = 0; i < 5; i++)
            printf("%d ", a[i]);
    }
    chevron_right
    
    
    Output:
    100 200 0 0 0
    

    We can also use this Designated Initialization in Structures as follows:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <stdio.h>
      
    struct GFG {
        int a;
        int b;
        int c;
      
        // initializes a and c
        // of structure object ob
    } ob = {.a = 1, .c = 20 };
      
    int main()
    {
        printf("The values of a and c"
               " in struct GFG are %d %d",
               ob.a, ob.c);
    }
    chevron_right
    
    
    Output:
    The values of a and c in struct GFG are 1 20
    

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.





**Passionate about things** **A Quick Learner** **Always strive for Perfection** **Campus Mantri and Technical Content Writer**

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
C
Practice Tags :