Skip to content
Related Articles

Related Articles

Improve Article

Introduction to the C99 Programming Language : Part II

  • Last Updated : 27 May, 2021

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 .
     

C




#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 arguments
    printf("String Comparison Result: %d\n",
           compare(strcmp, "one", "one"));
 
    return 0;
}
  1.  
Output: 
Length is 3
String Comparison Result: 0

 

  1.  
  2. _Pragma Operator: C99 specifies pragma implementation by using the operator _Pragma. 
    Syntax: 
     
_Pragma("directive")

directive is the pragma being called.
  1. This _Pragma operator helps pragmas to participate in macro replacement.
    Some Built in Pragmas are: 

     

PragmaMeaning
STDC FP_CONTRACT ON/OFF/DEFAULTIf 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/DEFAULTSTDC FENV_ACCESS ON pragma tells the compiler that the floating point environment might be accessed.
STDC CX_LIMITED_RANGE ON/OFF/DEFAULTSTDC CX_LIMITED_RANGE ON tells the compiler that certain formulas invoking complex values are safe.
  1. 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.
     
  2. 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. 
     

C




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

 

  1. Here the scope of i is only inside the loop.
     
  2. Compound Literals: Compound Literals are array, structure or union expressions representing objects of the given type.
    Example: 
     
int *a = (int[]){11, 12, 13}
  1. 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. 
     

C




#include <stdio.h>
 
int main()
{
    int* a = (int[]){ 11, 12, 13 };
    for (int i = 0; i < 3; i++)
        printf("%d ", a[i]);
}
  1.  
Output: 
11 12 13

 

  1.  
  2. 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.
     

C




#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]);
}
  1.  
Output: 



0

 

  1.  
  2. 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. 
     

C




#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]);
}
  1.  
Output: 
100 200 0 0 0

 

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

C




#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);
}
  1.  
Output: 
The values of a and c in struct GFG are 1 20

 

  1.  

 

Want to learn from the best curated videos and practice problems, check out the C Foundation Course for Basic to Advanced C.



My Personal Notes arrow_drop_up
Recommended Articles
Page :