Skip to main content

C++ Primer Sec. 2.1 2.2 Primitive Build-In Types and Variables

Primitive Build-in Types

void type
arithmetic type:
bool, char, wchar_t, char16_t, char32_t, short, int, long, long long, float, double, long double
Literals

'a' // character literal
"Hello World!" // string literal
L'a' // wide character literal, type is wchar_t
u8"hi!" // utf-8 string literal (utf-8 encodes a Unicode character in 8 bits)
42ULL // unsigned integer literal, type is unsigned long long
1E-3F // single-precision floating-point literal, type is float
3.14159L // extended-precision floating-point literal, type is long double

Variables

Initialization is not assignment. Initialization happens when a variable is given a value when it is created. Assignment obliterates an object’s current value and replaces that value with a new one.

Four different ways to define an int variable named units_sold and initialize it to 0.

int units_sold = 0;
int units_sold = {0};
int units_sold{0};
int units_sold(0);

When used with variables of built-in type, this form of initialization has one important property: The compiler will not let us list initialize variables of built-in type if the initializer might lead to the loss of information.

long double ld = 3.1415926536;
int a{ld}, b = {ld}; // error: narrowing conversion required
int c(ld), d = ld; // ok: but value will be truncated

The value of an object of built-in type that is not explicitly initialized depends on where it is defined. Variables defined outside any function body are initialized to zero. With one exception, which we cover in § 6.1.1 (p. 205), variables of built-in type defined inside a function are uninitialized. The value of an uninitialized variable of built-in type is undefined (§ 2.1.2, p. 36). It is an error to copy or otherwise try to access the value of a variable whose value is undefined.
Each class controls how we initialize objects of that class type. In particular, it is up to the class whether we can define objects of that type without an initializer. If we can, the class determines what value the resulting object will have.

Uninitialized objects of built-in type defined inside a function body have undefined value. Objects of class type that we do not explicitly initialize have a value that is defined by the class.

Variable Declarations and Definitions

C++ supports what is commonly known as separate compilation. Separate compilation lets us split our programs into several files, each of which can be compiled independently.

To support separate compilation, C++ distinguishes between declarations and definitions. A declaration makes a name known to the program. A file that wants to use a name defined elsewhere includes a declaration for that name. A definition creates the associated entity.
A variable declaration specifies the type and name of a variable.
a definition also allocates storage and may provide the variable with an initial value.
To obtain a declaration that is not also a definition, we add the extern keyword

extern int i; // declares but does not define i
int j; // declares and defines j

Any declaration that includes an explicit initializer is a definition.

extern double pi = 3.1416; // definition

Variables must be defined exactly once but can be declared many times.

Static Typing

C++ is a statically typed language, which means that types are checked at compile time. The process by which types are checked is referred to as type checking.

Identifiers

Identifiers are case-sensitive.

1_or_2; //wrong, two consecutive underscore
_Aaa; //wrong, underscore + uppercase letter
_a;; // correct if inside a function, wrong if outside a function

Reserved names can not be used as variable names.

Scope of a Name

A scope is a part of the program in which a name has a particular meaning. Most scopes in C++ are delimited by curly braces.

global scope, block scope
Advice: Define Variables Where you first use them
Nested scope: inner scope, outer scope
int reused = 42; //reused has global scope int main()
{
int reused = 0;
std::cout<<::reused < }

The global scope has no name.

Comments

Popular posts from this blog

C++ Primer Ch1. Introduction

Different compilers use different suffix conventions; the most common include .cc , .cxx , .cpp , .cp , and .C .  To compile: $ CC prog1.cc prog1.exe is generated on Windows, a.out is generated on UNIX To run: $prog1 or $.\prog1 The command used to run the C++ compiler varies across compilers and operating systems. $ g++ -o prog1 prog1.cc Depending on the release of the GNU compiler you are using, you may need to specify -std=c++0x to turn on C++ 11 support. Typically, we put all the #include directives for a program at the beginning of the source file. std::cout << "Enter two numbers:" << std::endl;  Our output statement uses the << operator twice. Because the operator returns its left-hand operand, the result of the first operator becomes the left-hand operand of the second.    Equivalent to: (std::cout << "Enter two numbers:") << std::end;   ...

C++ Primer Sec. 2.3 Compound Types

Compound Types References Reference is not an object. It is an alias. It is just another name for an already existing object. int ival = 1024; int &refVal = ival; //refVal referes to (is another name for) ival int &refVal2; // error: a reference must be initialized When we define a reference, instead of copying the initializer's value, we bind the reference to its initializer. Once initialized, a reference remains bound to its initial object. There is no way to rebind a reference to refer to a different object. Because there is no way to rebind a reference, references must be initialized. When we assign to a reference, we are assigning to the object to which the reference is bound. int i = refVal; //OKay i= 1024 Because references are not objects, we may not define a reference to a reference. We can define multiple references in a single definition. Each identifier that is a reference must be preceded by the & symbol: int ...