clrscr() is usually given after the declaration of all variables. If it is given before the declaration(of variables), the compiler points out an error. Why is this due to ? Will there be any problem in allocating the required amount of memory if at all clrscr() is given before the declaration?

Kindly reply to this question !!

Thanks !!

Read "C Program Structure" section from your text book. I am sure, you will get the answer.

Comments
Fail. Why so fail?
totally meaningless, unhelpful, and lacking in any substance or value. why are you here?

i couldn't find the answer. can u please help me if you know the answer.

Read "C Program Structure" section from your text book. I am sure, you will get the answer.

thanks for your response. but can u please tell me why executable statements cant be given before variable declaration ?

you can't put executable statements before variable declarations in C language.

If the answer is not in your text book then you need a different book! But I'm sure its in the book, you just need to study the sections about C language program structure.

// includes go here
#include <stdio.h>
...
int main(int argc, char* argv[])
{
    // all variable declarations go here
    //
    // executable statements follow
    //
    {
         // new block, so more variables here
         // followed by executable statements
    } // end of block
} // end of main() function

thanks for your response. but can u please tell me why executable statements cant be given before variable declaration ?

Yes -- because the iso standards say so. I have read that this requirement is going to be relaxed in the next revision of the standards, but no compilers have implemented that yet (for obvious reasons)

oh!! then does this problem arise because of the problem in allocating the required amount of memory by the compiler ?

Yes -- because the iso standards say so. I have read that this requirement is going to be relaxed in the next revision of the standards, but no compilers have implemented that yet (for obvious reasons)

oh!! then does this problem arise because of the problem in allocating the required amount of memory by the compiler ?

Its not a problem. The C language was designed like that some 35+ years ago (officially 1972).

hmmm... ok... thanks !!

Its not a problem. The C language was designed like that some 35+ years ago (officially 1972).

Here is the structure of c program:

C Program has three sections:


--------------------------Section 1--------------------
General declaration & File inclusion section
Note: In this section you may put header files and
declaration : variables, struct, user defined function etc.

------------------------Section 2------------------------
Entry Point main() function definition
Note: Further, every c function definition is falls into two sub-sections.
int main()
{
----------- Local declaration -------------
variables, struct, user defined function etc....

------------Statements-------------------
..
return 0;
}

--------------------Section 3 ----------------------
User defined function definition.


Lets look at this code:

/* general declaration & file inclusion */
#include <stdio.h>

void test(int n);  

/* Entry point - main() function */
int main()
 {
    /* declaration  & initilization*/
     int k=10;

    /* statements */
    test(k);

   return 0;
  }

void test(int n)
 {
     /* declaration */
      int p;
    
     /* statements */
     p=n*n;
     printf("\nSquare : %d",p);
}

>I have read that this requirement is going to be relaxed in the next
>revision of the standards, but no compilers have implemented that >yet (for obvious reasons)
Mixing declarations and executable statements is a feature that's been available since C99, and quite a few compilers support it.

This means that there is no conceptual reson for clrscr() to come after the declaration part. Its just an ISO norm.
Am I correct ???

All functions must first be declared before they can be called, that lets the compiler know the function's return value and parameter types. I was programming in 1980s before C and C++ standards, and I can't count the number of times programming errors were made because of lack of function prototypes. That was a huge proglem back then and consumed hours of debugging time. So don't think ISO committee required function declarations just for the hell of it. It became a huge time saver for all pogrammers. In those days Lattice C was the leading compiler for MS-DOS. The introduction of C standard made the company who wrote that compiler implemnent function prototypes, among other thangs. They had many problems doing that, the compiler was ridded with bugs, and they lost all their clients.

Edited 2 Years Ago by Ancient Dragon

This article has been dead for over six months. Start a new discussion instead.