Variable declaration with C89 and C99

In C89 all the declaration of local variables must be placed at the beginning of the statements or block

In short, all variable declarations must come first before you write a statement in the block or a function

To clarify, let’s see an example

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a=100; //Declaration of a local variable a
    printf("%d\n",a);
     int b=200; //Declaration of a local variable b
    printf("%d\n",b); //Error With C89
    return 0;
}

With C99 we can mix the declaration of a variable with the statements

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a=100; //Declaration of a local variable a
    printf("%d\n",a);
     int b=200; //Declaration of a local variable b
    printf("%d\n",b); //not an Error With C99
    return 0;
}

Mohammed Anees

Hey there, welcome to aneescraftsmanship I am Mohammed Anees an independent developer/blogger. I like to share and discuss the craft with others plus the things which I have learned because I believe that through discussion and sharing a new world opens up

Leave a Reply

Your email address will not be published.