 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Jian H. Li Guest
|
Posted: Wed Feb 11, 2004 11:46 am Post subject: Can statements be written outside function body? |
|
|
Hello,
As a beginner to C++, I need Your kind help to clarify the basic
concept. The sample C++ code as following:
int i; // A
i++; // B, error
int main()
{
i++; // C
return 0;
}
Though both are legal C++ statement, //A & //B are different in this
code. //A is correct but //B is wrong. The same as statement //A, //B
is legal also, but //B can't occur outside function body. After moving
//B to position //C, the error alert disapeared. PLEASE EXPLAIN WHY.
Thank you.
Your Sincerely
Joe Li
|
|
| Back to top |
|
 |
Guillaume Brocker Guest
|
Posted: Wed Feb 11, 2004 11:53 am Post subject: Re: Can statements be written outside function body? |
|
|
Jian H. Li wrote:
| Quote: | Hello,
As a beginner to C++, I need Your kind help to clarify the basic
concept. The sample C++ code as following:
int i; // A
i++; // B, error
int main()
{
i++; // C
return 0;
}
Though both are legal C++ statement, //A & //B are different in this
code. //A is correct but //B is wrong. The same as statement //A, //B
is legal also, but //B can't occur outside function body. After moving
//B to position //C, the error alert disapeared. PLEASE EXPLAIN WHY.
|
You can declare a variable in the global scope (outside any function or
class body), but must put statements that are not declarations into a
function body. That's how C and C++ have been designed.
|
|
| Back to top |
|
 |
John Harrison Guest
|
Posted: Wed Feb 11, 2004 12:12 pm Post subject: Re: Can statements be written outside function body? |
|
|
"Jian H. Li" <joe_li (AT) xinhuanet (DOT) com> wrote
| Quote: | Hello,
As a beginner to C++, I need Your kind help to clarify the basic
concept. The sample C++ code as following:
int i; // A
i++; // B, error
int main()
{
i++; // C
return 0;
}
Though both are legal C++ statement, //A & //B are different in this
code. //A is correct but //B is wrong. The same as statement //A, //B
is legal also, but //B can't occur outside function body. After moving
//B to position //C, the error alert disapeared. PLEASE EXPLAIN WHY.
Thank you.
Your Sincerely
Joe Li
|
Because you can't put statements outside a function body.
If you could then when do you think the statements should be executed?
john
|
|
| Back to top |
|
 |
David Harmon Guest
|
Posted: Wed Feb 11, 2004 1:34 pm Post subject: Re: Can statements be written outside function body? |
|
|
On Wed, 11 Feb 2004 12:12:24 -0000 in comp.lang.c++, "John Harrison"
<john_andronicus (AT) hotmail (DOT) com> was alleged to have written:
| Quote: | If you could then when do you think the statements should be executed?
|
Well, obviously it should be executed at the same time it would be if
you had written:
int i;
int j = i++;
|
|
| Back to top |
|
 |
Jian H. Li Guest
|
Posted: Thu Feb 12, 2004 2:20 am Post subject: Re: Can statements be written outside function body? |
|
|
"John Harrison" <john_andronicus (AT) hotmail (DOT) com> wrote
| Quote: |
Because you can't put statements outside a function body.
If you could then when do you think the statements should be executed?
john
|
Not precise.
As shown below, code line //A is a legal statement outside a function body.
int i; // A
i++; // B, error
int main()
{
i++; // C
return 0;
}
|
|
| Back to top |
|
 |
John Harrison Guest
|
Posted: Thu Feb 12, 2004 9:38 am Post subject: Re: Can statements be written outside function body? |
|
|
"Jian H. Li" <joe_li (AT) xinhuanet (DOT) com> wrote
| Quote: | "John Harrison" <john_andronicus (AT) hotmail (DOT) com> wrote
Because you can't put statements outside a function body.
If you could then when do you think the statements should be executed?
john
Not precise.
As shown below, code line //A is a legal statement outside a function
body.
int i; // A
i++; // B, error
int main()
{
i++; // C
return 0;
}
|
A is not a statement, it is a declaration. Declarations are legal outside of
function bodies, statements are not.
john
|
|
| Back to top |
|
 |
osmium Guest
|
Posted: Thu Feb 12, 2004 2:31 pm Post subject: Re: Can statements be written outside function body? |
|
|
Jian H. Li writes:
| Quote: | Not precise.
As shown below, code line file://A is a legal statement outside a function
body.
int i; // A
i++; // B, error
int main()
{
i++; // C
return 0;
}
|
Statements *and* declarations both end in semicolons. Look at the BNF to
make this believable.
|
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Thu Feb 12, 2004 4:45 pm Post subject: Re: Can statements be written outside function body? |
|
|
"osmium" <r124c4u102 (AT) comcast (DOT) net> wrote
| Quote: |
Statements *and* declarations both end in semicolons. Look at the BNF to
make this believable.
Some statements end in semicolons. |
|
|
| Back to top |
|
 |
Jian H. Li Guest
|
Posted: Tue Mar 02, 2004 6:04 am Post subject: Re: Can statements be written outside a function body? |
|
|
hello,
in iso/iec 14882, C++ standard, page 674~679 (A.5~A.7):
in A.5:
statement:
...
declaration-statement
...
declaration-statement:
block-declaration
A.6:
block-declaration:
simple-declaration
...
simple-declaration:
decl-specifier-seq(opt) init-declarator-list(opt) ;
decl-specifier-seq:
decl-specifier-seq(opt) decl-specifier
decl-specifier:
type-specifier
...
type-specifier:
simple-type-specifier
...
simple-type-specifier:
char
int
short
long
float
double
wchar_t
void
...
A.7:
init-declaration-list:
init-declarator
...
init-declarator:
declarator initializer(opt)
declarator:
direct-declarator
...
derect-declarator:
declarator-id
...
declarator-id:
id-expression
...
A.4:
id-expression:
unqualified-id
...
unqualified-id:
identifier
...
so, it's obvious "int i;" is a block-declaration, and also a statement.
Regards.
Jian H. Li
|
|
| Back to top |
|
 |
Jian H. Li Guest
|
Posted: Wed Mar 03, 2004 3:10 am Post subject: Re: Can statements be written outside function body? |
|
|
"John Harrison" <john_andronicus (AT) hotmail (DOT) com> wrote
| Quote: | A is not a statement, it is a declaration. Declarations are legal outside of
function bodies, statements are not.
john
|
hello,
in iso/iec 14882, C++ standard, page 674~679 (A.5~A.7):
in A.5:
statement:
...
declaration-statement
...
declaration-statement:
block-declaration
A.6:
block-declaration:
simple-declaration
...
simple-declaration:
decl-specifier-seq(opt) init-declarator-list(opt) ;
decl-specifier-seq:
decl-specifier-seq(opt) decl-specifier
decl-specifier:
type-specifier
...
type-specifier:
simple-type-specifier
...
simple-type-specifier:
char
int
short
long
float
double
wchar_t
void
...
A.7:
init-declaration-list:
init-declarator
...
init-declarator:
declarator initializer(opt)
declarator:
direct-declarator
...
derect-declarator:
declarator-id
...
declarator-id:
id-expression
...
A.4:
id-expression:
unqualified-id
...
unqualified-id:
identifier
...
so, it's obvious "int i;" is a block-declaration, and also a statement.
Regards.
Jian H. Li
|
|
| Back to top |
|
 |
Mike Wahler Guest
|
Posted: Fri May 28, 2004 9:23 pm Post subject: Re: Can statements be written outside function body? |
|
|
"John Harrison" <john_andronicus (AT) hotmail (DOT) com> wrote
| Quote: |
"Jian H. Li" <joe_li (AT) xinhuanet (DOT) com> wrote in message
news:c930b2b5.0402110346.2299dfbd (AT) posting (DOT) google.com...
Hello,
As a beginner to C++, I need Your kind help to clarify the basic
concept. The sample C++ code as following:
int i; // A
i++; // B, error
int main()
{
i++; // C
return 0;
}
Though both are legal C++ statement, //A & //B are different in this
code. //A is correct but //B is wrong. The same as statement //A, //B
is legal also, but //B can't occur outside function body. After moving
//B to position //C, the error alert disapeared. PLEASE EXPLAIN WHY.
Thank you.
Your Sincerely
Joe Li
Because you can't put statements outside a function body.
If you could then when do you think the statements should be executed?
|
He probably believes "In the order they appear". Probably came
from BASIC.
-Mike
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Fri May 28, 2004 9:34 pm Post subject: Re: Can statements be written outside function body? |
|
|
Mike Wahler wrote:
| Quote: | "John Harrison" <john_andronicus (AT) hotmail (DOT) com> wrote in message
news:c0d62i$13k6oj$1 (AT) ID-196037 (DOT) news.uni-berlin.de...
"Jian H. Li" <joe_li (AT) xinhuanet (DOT) com> wrote in message
news:c930b2b5.0402110346.2299dfbd (AT) posting (DOT) google.com...
Hello,
As a beginner to C++, I need Your kind help to clarify the basic
concept. The sample C++ code as following:
int i; // A
i++; // B, error
int main()
{
i++; // C
return 0;
}
Though both are legal C++ statement, //A & //B are different in this
code. //A is correct but //B is wrong. The same as statement //A, //B
is legal also, but //B can't occur outside function body. After moving
//B to position //C, the error alert disapeared. PLEASE EXPLAIN WHY.
Thank you.
Your Sincerely
Joe Li
Because you can't put statements outside a function body.
|
You can, if they are declaration statements. The ++i is not
a statement by itself, it's an expression and it could be forced
to be evaluated, if you put it in a declaration/definition statement.
| Quote: |
If you could then when do you think the statements should be executed?
He probably believes "In the order they appear". Probably came
from BASIC.
|
But it is possible, generally, I believe. [this seems like a rather
old thread, though, maybe the question has already been answered...]
If you do
int i = 42;
int b = i++;
Then the (i++) part is going to be executed after the 'i's definition
and should probably do the trick...
Victor
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|