Chapter 4 Literals, Variables, and Constants


Variable’s Initial Value – Initialization


Download 0.54 Mb.
bet3/6
Sana19.06.2023
Hajmi0.54 Mb.
#1626290
1   2   3   4   5   6
Bog'liq
Chapter-04

4.3.1 Variable’s Initial Value – Initialization

  • Initialization
    • Process of giving a variable a value during its declaration
    • Results in the variable always being in a known state
  • When declared, a variable’s initial value is unknown
  • Important to provide an initial value for all variables
  • int sum = 0;

    int Calvins_age = RETIREMENT_AGE;

    int base_salary = 30000;

    int num_dependents, staff_salary = base_salary;

4.3.1 Variable’s Initial Value – Alternate Forms

int sum ( 0 );

int Calvins_age( RETIREMENT_AGE);

  • Fully uniform type initialization
    • Introduced in C++11
    • int base_salary { 30000 };

      int num_dependents, staff_salary { base_salary };

4.3.1 Variable’s Initial Value – Good Practices

  • Always know the state, or value, of all variables
  • Variables should always be initialized
  • Variables, even characters, are usually initialized to 0

4.3.2 Data Types – Definition

  • Data type
    • Specifies how much memory a variable will take up in memory
    • Indicates operations that can be performed on the variable
  • Primitive data type
    • Data type whose definition is built into the language

4.3.2 Data Types – Selected Types


C++ Data Type

Description of Data

Memory Allocated

Range

char

Character

1 byte

-128 to 127

int

Integer

4 bytes

–2,147,483,648 to 2,147,483,647

float

Floating point (decimal)

4 bytes

+/-3.4 x 1038 with 7 digits of accuracy

double

Double precision floating point

8 bytes

+/-1.7 x 10308 with 15 digits of accuracy

bool

Boolean data

1 byte

true or false

short (or short int)

Smaller integer

2 bytes

–32,768 to 32,767

long long (C++11)

Largest integer

8 bytes

+/-9.22 x 1018

Download 0.54 Mb.

Do'stlaringiz bilan baham:
1   2   3   4   5   6




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling