C/turbo c complier error
Expert: Prince M. Premnath - 8/11/2008
QuestionI 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.
AnswerHai 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