Am I missing something in my C ++ statement?

You cannot have code outside functions except declarations, definitions, and preprocessor directives.

Is this statement accurate, or is there something I am missing? I teach my nephew a program, and he tried to put the cycle to the beginning. He is quite young, I want to give him a firm rule that he can understand.

+3
source share
5 answers

Not really - you can also put expressions in global variable declarations:

int myGlobalVar = 3 + SomeFunction(4) - anotherGlobalVar;

, , . ( , if , ..). , main() , , . , .

+2
  • :
    , .
  • :
    . , . , , ++, . , (, ) . HTH
+1

- .

0

, , - , , ( ).

This is because C ++ is a structured programming language that surrounds its behavior inside procedures, unlike unstructured ones in which you have only one level of code and no areas.

0
source

Well, there are namespaces ... and the stuff Adam Rosenfield talked about ... and there is also a try / catch exception, which can be kind of external to functions. Unfortunately, I cannot recall the syntax and cannot find it using Google.

0
source

Source: https://habr.com/ru/post/1771968/


All Articles