Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Introduction to the C99 Programming Language : Part I

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Introduction:

C99 is a standardized version of the C programming language that was published in 1999 by the International Organization for Standardization (ISO). It introduced a number of new features and improvements over the previous C89 standard, including support for variable-length arrays, flexible array members, complex numbers, and new keywords such as inline and restrict.

In this first part of the introduction to C99 programming language, we will cover some of the key features and improvements that were introduced in C99.

1.Variable-Length Arrays (VLAs)

C99 introduced support for variable-length arrays (VLAs), which allows arrays to be declared with a length that is determined at runtime rather than at compile time. This can be useful in situations where the size of an array is not known until the program is executed. The syntax for declaring a VLA is similar to that of a regular array, but with empty brackets indicating that the size is not known until runtime:

C




void foo(int n) {
  int array[n];
  // ...
}

2.Flexible Array Members (FAMs)

Flexible array members (FAMs) are another new feature in C99 that allows arrays to be declared as the last member of a struct with an unspecified size. This can be useful in situations where a struct needs to have a variable-sized array as one of its members. The syntax for declaring a FAM is similar to that of a regular array, but with empty brackets indicating that the size is not specified:

Sure, here are some advantages, disadvantages, and recommended books for learning C99:

Advantages:

  1. Variable-length arrays allow for more flexible memory allocation at runtime.
  2. Flexible array members allow for variable-sized arrays to be part of a struct.
  3. Complex numbers provide built-in support for complex arithmetic.
  4. Inline functions and the restrict keyword can improve performance.
  5. Designated initializers and compound literals provide a more concise and expressive way to initialize data structures.

Disadvantages:

  1. Not all compilers fully support C99, so some features may not be available or may require compiler-specific extensions.
  2. The use of variable-length arrays and flexible array members can potentially lead to memory allocation issues if not used carefully.

Recommended books:

  1. “The C Programming Language” by Brian W. Kernighan and Dennis M. Ritchie
  2. “C Programming Absolute Beginner’s Guide” by Greg Perry and Dean Miller
  3. “C Programming: A Modern Approach” by K. N. King

Here is an example code with output demonstrating some of the features of C99:

C




#include <stdio.h>
#include <complex.h>
 
int main() {
  int n = 5;
  int array[n];
  for (int i = 0; i < n; i++) {
    array[i] = i;
    printf("%d ", array[i]);
  }
  printf("\n");
   
  struct foo {
    int x;
    int y[];
  };
  struct foo* f = malloc(sizeof(struct foo) + n * sizeof(int));
  f->x = 42;
  for (int i = 0; i < n; i++) {
    f->y[i] = i;
    printf("%d ", f->y[i]);
  }
  printf("\n");
   
  double complex z = 3.0 + 4.0*I;
  printf("Real part: %f\n", creal(z));
  printf("Imaginary part: %f\n", cimag(z));
   
  inline int add(int x, int y) {
    return x + y;
  }
  printf("Result: %d\n", add(2, 3));
   
  int* restrict a = malloc(n * sizeof(int));
  int* restrict b = malloc(n * sizeof(int));
  for (int i = 0; i < n; i++) {
    a[i] = i;
    b[i] = n - i - 1;
  }
  int sum = 0;
  for (int i = 0; i < n; i++) {
    sum += a[i] * b[i];
  }
  printf("Result: %d\n", sum);
   
  return 0;
}

Output

0 1 2 3 4 
0 1 2 3 4 
Real part: 3.000000
Imaginary part: 4.000000
Result: 5
Result: 10

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: 

C




// 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;
}

  • 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 

C




#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;
}

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

C




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

  • 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: 

C




#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);
}

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

C




// First Comment
int a; // another comment

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

C




#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;
}


My Personal Notes arrow_drop_up
Last Updated : 10 Apr, 2023
Like Article
Save Article
Similar Reads