1

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"

Jose V
  • 1,655
  • 1
  • 17
  • 31
  • 5
    Because Turbo C is an obsolete compiler from a time when declaring variables after you've called code was illegal. – Havenard Apr 18 '17 at 02:49
  • 2
    Turbo C is not a C compiler, because C is first standardize in 1989, long after Turbo C was released. As a result it contains a lot of non-standard things http://stackoverflow.com/q/3920351/995714 http://stackoverflow.com/q/1961828/995714 – phuclv Apr 18 '17 at 02:50
  • alright thank you, i found a couple of duplicates after looking up "defining variable before function call" instead of "declaring variable after function call", [Where you can and cannot declare new variables in C?](https://stackoverflow.com/questions/19058143/cant-define-variables-after-a-function-call) and [Can't define variables after a function call](https://stackoverflow.com/questions/19058143/cant-define-variables-after-a-function-call) , should i delete the question? – Jose V Apr 18 '17 at 02:54
  • 1
    You *should* use an up-to-date compiler, unless you don't want the benefits of technology improvements from the last thirty years such as [valgrind](http://valgrind.org/) and [dramatically](https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html)... [improved](http://llvm.org/docs/Passes.html)... [optimisations](https://clang.llvm.org/docs/UsersManual.html#profile-guided-optimization)... – autistic Apr 18 '17 at 03:36
  • Of course the use of such old technology is for no practical purpose, in my case it's just an assignment – Jose V Apr 18 '17 at 03:37

1 Answers1

4

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.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294