You are here:

C/turbo c complier error

Advertisement


Question
I made a program of c language in turbo c and saved it with the extension .c. I made the variable declarations inside main function. When i compiled the program it gave an error that 'Declarations are not allowed here' and it is same in all the programs i make. please solve this problem of mine.

Answer
Hai Dear Karthi !

You can find similar error in all ANSI standard C Compiler , ANSI standard enforces that , in a C Program all the declarations should be done before the first executable statement !

eg Take a look at this programme

void main()
{
 clrscr(); /* Purely a executable statement , say a function call */

 int i  = 0; /* Declaration statement */

...
..

}
once if you compile this program sure you will get the familiar error  "Declarations are not allowed here" that's because the declaration statement int i = 0 ; fixed that after the executable statement clrscr(); and hence this code results with an error during compilation.


Adjustment required in this program to eliminate "'Declarations are not allowed here" error just put all your declaration statement before the first executable statement .

void main()
{
 int i = 0;
 int k;
 float p;  /* declaration part */

 clrscr(); /* Executable statement */
 printf("hello .. "); /* Executable statement */
.
.     /* Rest of the program */
.
.
.
}


Note : This restriction is relaxed in C++ ,

Thanks and Regards !
Prince M. Premnath  

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Prince M. Premnath

Expertise


I'm sure that I can solve any doubts in Turbo C ,Graphics Programing ,Mouse, Hardware Programming ,File System ,Interrupts, BIOS handling , TSR Programming , General Concepts in C Language, handling inline Assembly statements

Experience

Research over 6+ Years

Organizations
CG-VAK Softwares and Exports Limited

Education/Credentials
Masters in Computer Applications

©2012 About.com, a part of The New York Times Company. All rights reserved.