Introduction to the C99 Programming Language : Part I

C99 is another name of ISO/IEC 9899:1999 standards specification for C that was adopted in 1999. This article mainly concentrates on the new features added in C99 by comparing with the C89 standard. In the development stage of the C99 standard, every element of the C language was re-examined, usage patterns were analyzed, and future demands were anticipated. C’s relationship with C++ provided a backdrop for the entire development process. The resulting C99 standard is a testimonial to the strengths of the original.

  1. Keywords added in C99:
    • inline: C99 adds the keyword inline which is applicable to functions. If we write inline datatype function_name (param) to any function it means that we are specifying the compiler to optimize calls to that function i.e. the function’s code will be expanded inline rather than called.

      Example:

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      // C program to demonstrate inline keyword
        
      #include <stdio.h>
        
      inline int maximum(int a, int b)
      {
          return a > b ? a : b;
      }
        
      int main()
      {
          int x = 5, y = 10;
          printf("Maximum of %d and %d is %d",
                 x, y, maximum(x, y));
          return 0;
      }

      chevron_right

      
      

      Note: Some Compilers show undefined reference to maximum(). This inline feature is used rarely as a keyword.

      The above program is equivalent to the following

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      #include <stdio.h>
        
      int main()
      {
          int x = 5, y = 10;
          printf("Maximum of %d and %d is %d",
                 x, y, (x > y ? x : y));
          return 0;
      }

      chevron_right

      
      

      Output:



      Maximum of 5 and 10 is 10
      
      Output:

      Maximum of 5 and 10 is 10
      

      Inline functions help us to create efficient code by maintaining a structured, function-based approach.

    • restrict: restrict is a type qualifier applicable only for pointers. A pointer qualified by restrict is initially the only means by which the object it points to can be accessed. Access to the object by another pointer can occur only if the second pointer is based on the first.

      We use restrict qualifier to restrict access to the object. These are primarily used as function parameters or in malloc(). The restrict qualifier never changes the semantics of the program.

    • _Bool Datatype: _Bool datatype was added in C99 standard which stores 0 and 1 values. _Bool is an integer type.

      Note: bool keyword in C++ and _Bool in C are different.

      _Bool is used rarely instead C99 defines a new header file stdbool.h which defines three macros bool, true and false.

    • _Complex: C99 adds support for performing operations on complex numbers through _Complex and _Imaginary keywords. These keywords provide better support for numerical programming.

      Types defined in _Complex:

      • float _Complex
      • double _Complex
      • long double _Complex
    • _Imaginary: As said above, C99 adds support for performing operations on complex numbers through _Complex and _Imaginary keywords. These keywords provide better support for numerical programming.

      Types defined in _Complex and _Imaginary:

      • float _Imaginary
      • double _Imaginary
      • long double _Imaginary

      In the same way discussed in _Bool datatype, these _Complex and _Imaginary aren’t used frequently. Instead, we use the header-file which consists of in-built macros complex and imaginary macros.

  2. Addition of Type Qualifiers: Another important aspect added in C99 is the introduction of long long and unsigned long long type modifiers. A long long int has a range of –(2^63 – 1) to +(2^63 –1). An Unsigned long long int has a minimal range starting from 0 to +(2^64 –1). This long long type allows 64-bit integers to support as a built-in type.
  3. Changes in Arrays: C99 added two important features to arrays:
    • Variable Length Arrays: In C89 standard, the array size has to be specified using integer constants at the array declaration and array size is fixed at compile time. In C99 standard, we can declare an array whose dimensions are specified by any integer expressions whose values known at run-time. This is called Variable Length Arrays(VLA).
    • Inclusion of Type Qualifiers: In C99, we can use the keyword static inside the square brackets during array declaration. This is only applied when array is declared in function parameters i.e

      Example:

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      int fun1(char arr[static 80])
      {
          // code
      }

      chevron_right

      
      

      In the above example arr is always a pointer to an 80-element array i.e arr is guaranteed to point the starting element of the array of chars that contains atleast 80 elements. We can also use the keywords restrict, volatile and const inside the square brackets if that array is declared inside a function parameters. Keyword restrict specifies that the pointer is the sole initial means of access to the object. const states that the pointer always points to the same object. volatile is allowed, but it has no meaning.

      Example:

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      #include <stdio.h>
      void fun(int a[static 10])
      {
          for (int i = 0; i < 10; i++) {
              a[i] += 1;
              printf("%d ", a[i]);
          }
      }
        
      int main()
      {
          int a[] = { 1, 2, 3, 4,
                      4, 4, 4, 4,
                      5, 5, 6, 7,
                      8, 9, 10 };
          fun(a);
      }

      chevron_right

      
      

      Output:

      2 3 4 5 5 5 5 5 6 6
      
  4. Single Line Comments: Single Line comments aren’t accepted in C89 standard. C99 standard introduces Single Line Comments which are used only when brief remarks are needed. These comments begin with // and runs to the end of the line.

    Eg:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // First Comment
    int a; // another comment

    chevron_right

    
    

  5. Declaration of Identifiers:
    According to the C89 standard, all Identifiers should be declared at the start of the code block. If we need any other identifier at the middle, we can’t declare for that instance or time. We need to declare that at the start. C99 has changed this rule as we can declare identifiers whenever we need in a code.

    In simple, we can see this as:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <stdio.h>
    int main()
    {
        int i;
        i = 1;
        int j; // this declaration is invalid in C89 standard, but valid in C99 and C++
        j = 3;
    }

    chevron_right

    
    

    Output:

    
    

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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 :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.