Introduction to the C99 Programming Language : Part I - GeeksforGeeks (2024)

Last Updated : 10 Apr, 2023

Comments

Improve

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.000000Imaginary part: 4.000000Result: 5Result: 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;

}



avsadityavardhan

Introduction to the C99 Programming Language : Part I - GeeksforGeeks (2)

Improve

Next Article

Introduction to the C99 Programming Language : Part II

Please Login to comment...

Introduction to the C99 Programming Language : Part I - GeeksforGeeks (2024)

References

Top Articles
These Gift Baskets Make Valentine's Day Gifting Easy, Yet Still Thoughtful
You'll Want to Make These Bite-Size Taco Cups for Your Super Bowl Party Guests
Kathleen Hixson Leaked
Pnct Terminal Camera
Ofw Pinoy Channel Su
Ashlyn Peaks Bio
Tugboat Information
World of White Sturgeon Caviar: Origins, Taste & Culinary Uses
Pollen Count Central Islip
Trini Sandwich Crossword Clue
Local Dog Boarding Kennels Near Me
Tracking Your Shipments with Maher Terminal
Playgirl Magazine Cover Template Free
Eka Vore Portal
National Weather Service Denver Co Forecast
Overton Funeral Home Waterloo Iowa
1-833-955-4522
Costco Great Oaks Gas Price
Mccain Agportal
Jet Ski Rental Conneaut Lake Pa
Menards Eau Claire Weekly Ad
Rufus Benton "Bent" Moulds Jr. Obituary 2024 - Webb & Stephens Funeral Homes
Craigslist Org Appleton Wi
Directions To Cvs Pharmacy
Shadbase Get Out Of Jail
Weve Got You Surrounded Meme
Colonial Executive Park - CRE Consultants
European Wax Center Toms River Reviews
Local Collector Buying Old Motorcycles Z1 KZ900 KZ 900 KZ1000 Kawasaki - wanted - by dealer - sale - craigslist
Xpanas Indo
Little Einsteins Transcript
60 Second Burger Run Unblocked
Www.craigslist.com Syracuse Ny
Garrison Blacksmith's Bench
Http://N14.Ultipro.com
School Tool / School Tool Parent Portal
Personalised Handmade 50th, 60th, 70th, 80th Birthday Card, Sister, Mum, Friend | eBay
How Does The Common App Work? A Guide To The Common App
Ethan Cutkosky co*ck
30 Years Of Adonis Eng Sub
Funkin' on the Heights
Lorton Transfer Station
Wisconsin Volleyball titt*es
Leland Westerlund
Dolce Luna Italian Restaurant & Pizzeria
Campaign Blacksmith Bench
Divisadero Florist
Frank 26 Forum
Metra Union Pacific West Schedule
Pulpo Yonke Houston Tx
Craigslist.raleigh
Varsity Competition Results 2022
Latest Posts
Article information

Author: Nicola Considine CPA

Last Updated:

Views: 5991

Rating: 4.9 / 5 (69 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Nicola Considine CPA

Birthday: 1993-02-26

Address: 3809 Clinton Inlet, East Aleisha, UT 46318-2392

Phone: +2681424145499

Job: Government Technician

Hobby: Calligraphy, Lego building, Worldbuilding, Shooting, Bird watching, Shopping, Cooking

Introduction: My name is Nicola Considine CPA, I am a determined, witty, powerful, brainy, open, smiling, proud person who loves writing and wants to share my knowledge and understanding with you.