Chapter 6 Flashcards
Compound Statement
Also called block, is a group of zero or more statements that is treated by the compiler as a single statement
Block structure
Begins and ends with curly braces {}
Statement to be executed in between braces
No semicolon is needed
Nesting blocks
Enclosing block is called outer block, the enclosed block is called inner or nested block
Use of blocks
In conjunction with if statements, if we want multiple statements to execute when the condition evaluates to true
Nesting level (depth)
The maximum number of blocks you can have in a function, inuding outer block
Defining a namespace
Via namespace keyword
User defined namespaces
Namespaces you create for your own declarations
Scope resolution operator (::)
Used to look for an identifier in a particular namespace, identifier specified by the right operand should be looked for in the scope of the left operand
Scope resolution with no prefex
The identifier is looked for in the global namespace
Multiple namespace blocks
You can declare namespace blocks in multiple locations, all declarations within the namespace are considered part of the namespace
Nested namespaces
We can acces as ex. foo::goo::add
Namespace aliases
Allow us to temporarily shorten a long sequience of namespaces into something shorter
Use of namespaces
- in applications, to seperate application-specific code from code that can be reused
- when you want to distribute the code
- to allow the user to see the contents of your library
Identifier’s Scope
Determines where an identifier can be accessed within the source code, compile time property
Block scope
Variables are in in scope from their point of definition to the end of the block the variables are defined within
Variables names
Must be unique within a given scope
Variables storage duration
Determines what rules govern when and how a variable will be created and destroyed
Automatic storage duration
In local variables, which means they are created at the point of definition and destroyed at the end of the block they are defined in
Local variables in nested blocks
Limited to the inner block, not accessible in the outer block, variables defined in outer block can be accessed in the inner blocks
Linkage
Property of identifiers, it determines whether other declarations of that name refers to the same object or not, local variables don’t have linkage
Defining variables
Should be defined in the most limited scope
Global variables
Variables declared outside of a function
Declaring global variables
At the top of a file, below the includes, above any code
Naming global variables
Using a g or g_ prefix