Read "C Program Structure" section from your text book. I am sure, you will get the answer.
__avd
Posting Genius (adatapost)
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
you can't put executable statements before variable declarations in C language.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
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
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
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)
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
oh!! then does this problem arise because of the problem in allocating the required amount of memory by the compiler ?
Its not aproblem. The C language was designed like that some 35+ years ago (officially 1972).
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
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);
}
__avd
Posting Genius (adatapost)
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
>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.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401