Lecture overview -- Keyboard shortcut: 'u'  Previous page: Declaring several names together -- Keyboard shortcut: 'p'  Next page: Constants -- Keyboard shortcut: 'n'  Lecture notes - all slides and notes together  slide -- Keyboard shortcut: 't'  Help page about these notes  Alphabetic index  Course home  Lecture 2 - Page 8 : 42
Notes about C++
Basic facilities
Declarations as statements - declarations in conditions

Introduce and initialize a variable at the place where it is first used

It is possible to delare variables in a condition of an if statement

  • The C++ Programming Language: Page 133, 135
 

  • Declarations inside blocks

    • C89: All declarations appear before the first statement

    • C99 and C++: declarations and statements can be mixed

    • Avoids having all declarations first in block, and assignments later in the block

  • Declarations in the condition of an if statement

    • The scope of such a name is the rest of the if, including the else part

    • Serves as a scope restriction, compared with a declaration just before the if statement

y:/Kurt/Files/Advanced-programming-cpp/cpp/kn/declaration-statements/less-localized.ccDeclarations before statements - C89 Style.

Some words about compilation of these programs: I use g++ to compile C++ programs. In most examples in this material, C++ files have file extension cc. Alternatively, cpp is also a popular file extension for C++ files. If gcc is activated on a cc or cpp file, it will activate the C++ compiler. To prevent this, use the compiler option -x c. Thus, to compile this file as a C89 c program, write gcc -x c -std=c89 -pedantic less-localized.cc

y:/Kurt/Files/Advanced-programming-cpp/cpp/kn/declaration-statements/localized.ccA declaration is a statement in C++.


y:/Kurt/Files/Advanced-programming-cpp/cpp/kn/declaration-statements/decl-in-if.ccA declaration in the condition of an if.