The New C
C99 is the C standard ratified by the ANSI and ISO standardizaion groups. It presents a significant amount of changes to the C language. These changes are the result of sibling competition between C and C++. The initial version of C is named after Kernighan and Ritchie. Classic C is K&R C with structure assignment, enumerations, and void. C89, or ANSI C, had some influences from C with Classes. For example, C89 adopted function prototypes in a similar form to what C with Classes provided. The most significant changes in C99, compared to C89, support a variety of new features.
Following is a brief list of C99 features:
Boolean data types <stdbool.h>
Increased identifier size limits
C++ style/line comments
Inline functions
Restricted pointers
Variable Declarations
Variable length arrays
New long long type
Specific standards
The new C standard for gcc is applied with the following command.
Let us consider the following program named bool.c:
Linking C99 programs with external libraries
The C standard library consists of a collection of headers and library routines used by C programs. The external library is usually stored with an extension .a and known as a static library. For instance, the C math library is typically stored in the file /usr/lib/libm.a on Linux. The prototype declarations for the functions in the math library are specified in the header file /usr/include/math.h.
Consider the above program hypotenuse.c. The following commands are then executed:
gcc provides an -l option to override long paths when linking against libraries. The following command illustrates this option:
Mixed declarations
ISO C99 allows declarations and code to simultaneously exist in compound statements. The following programming example illustrates this feature:
The identifier is visible from where it is declared to the end of the enclosing block.
Variable Length Arrays(VLA)
Variable Length Arrays are not dynamic arrays. Rather, they are created with different sizes each time a declaration is encountered. Only local arrays which are within block scope can be variable arrays.
Previously array sizes were of fixed size. C99 removes this constraint. It also frees you from performing allocate( ) and delete( ) operations on memory explicitly. The output of VLA is illustrated below.
New Long Long Type
Long long is 64 bit wide integer type. This is the biggest integer type in the C language standard. The long long type was specified to give 32-bit machines a way to handle 64-bit data when interactingwith 64 bit machines. Consider the following C program longdt.c:
Restricted Pointers
C99 lets you prefix pointer declarations with the restrict keyword. Thus the pointer itself will be used to access the object it points to. This features takes care of the shortfall of aliasing. It also aids in code optimization. For example, consider the signature of strcat() function in the string.h file:
The source string is appended to the end of the destination string. Here, the destination and source strings can be referenced only through the pointers dest and src. The compiler can then optimize the code generated for the function.
Inline Functions
Inline functions save the overhead of function calls. Consider the following program inlinecode.c to demonstrate the use of inline in C99.
Conclusion
C99 is a step ahead in the evolution of ANSI C. It incorporates elegant features like single line comments, boolean data types and larger size data types. C99 also supports code optimization through restricted pointer usage and supports inline functions. Now programmers can exploit these new features and further optimize their code for programming efficiency.