Search code examples
cturbo-c

Defining variables after calling functions gives me a compilation error


This works

main()
{
   int c;
   struct books Book1;
   c = getchar( );
   return 0;
}

This doesn't

main()
{
   int c;
   c = getchar( );
   struct books Book1;
   return 0;
}

Expression syntax in function main (and points to the space after the word 'struct')

This doesn't either because the definition of B is below c = getchar();, error points to the space between "int" and "b"

main()
{
   int c;
   struct books Book1;
   c = getchar( );
   int b;
   return 0;
}

Is the problem that i have to define every variable before calling functions, or is it something else?

Is this how C works, or is it a turbo C thing?

Edit: found duplicates after realizing that i meant to say "definition" not "declaration"


Solution

  • In C89, variables must be declared in the beginning of a block. The restriction was removed since C99.

    It's not a surprise that Turbo C, an outdated compiler, doesn't support this C99 feature.