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 Sec. 2.6 Defining our own data structures

Defining our own data structures Class definition must have ; at the end. Preprocessor The most common technique for making it safe to include a header multiple times relies on the preprocessor. #include Header guards #ifndef SALES_DATA_H #define SALES_DATA_H #include struct Sales_data { std::string bookNo; unsigned units_sold = 0; double revenue = 0.0; }; #endif SALES_DATA_H can be any name, just it has to be unique throughout the program. preprocessor variables usually are written in all uppercase.

C++ Primer Sec. 2.5 Dealing with Types

Dealing with Types type alias typedef double wages; // wages is a synonym for double typedef wages base, *p; // base is a synonym for double, p for double* C++11: alias declaration using SI = Sales_item; // SI is a synonym for Sales_item As usual, a const that appears in the base type modifies the given type. The type of pstring is “pointer to char.” So, const pstring is a constant pointer to char—not a pointer to const char. It can be tempting, albeit incorrect, to interpret a declaration that uses a type alias by conceptually replacing the alias with its corresponding type. typedef char *pstring; const pstring cstr = 0; // cstr is a constant pointer to char const pstring *ps; // ps is a pointer to a constant pointer to char const char *cstr = 0; // wrong interpretation of const pstring cstr C++11: auto. auto tells the compiler to deduce the type from the initializer. The initializers for all the variables in the declaration using auto must have types that a...

C++ Primer Sec.2.4 const qualifier

const Qualifier Because we can’t change the value of a const object after we create it, it must be initialized just as reference const int i = get_size(); // ok: initialized at run time const int j = 42; // ok: initialized at compile time const int k; // error: k is uninitialized const when we use an object to initialize another object, it doesn’t matter whether either or both of the objects are consts. int i = 42; const int ci = i; // ok: the value in i is copied into ci int j = ci; // ok:the value in ci is copied into j By Default, const Objects Are Local to a File When we really need to share const between files, define a single instance of a const variable, we use the keyword extern on both its definition and declaration(s). // file_1.cc defines and initializes a const that is accessible to other files extern const int bufSize = fcn(); // file_1.h extern const int bufSize; // same bufSize as defined in file_1.cc References to const const int ci = 102...