Can statements be written outside function body?

J

Jian H. Li

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
 
G

Guillaume Brocker

Jian said:
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.
 
J

John Harrison

Jian H. Li said:
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
 
D

David Harmon

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++;
 
J

Jian H. Li

John Harrison said:
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;
}
 
J

John Harrison

Jian H. Li said:
"John Harrison" <[email protected]> wrote in message



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
 
O

osmium

Jian said:
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.
 
J

Jian H. Li

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
 
J

Jian H. Li

John Harrison said:
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
 
M

Mike Wahler

John Harrison said:
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
 
V

Victor Bazarov

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.
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top