Lesson 2A Fundamental Types Constant and Variable
Lesson 2A Fundamental Types Constant and Variable
Types, Constant
and Variable
GEADLITE – LIVING IN THE INFORMATION
TECHNOLOGY ERA
Content
C++ Data Type
◦ Bool Type
◦ The char and wchar_t Type
◦ Integral Type
◦ Floating Point Type
Variable
◦ Valid Names/Identifier
◦ Keyword in C++
◦ Example of names
◦ Declaration of Variables
◦ Initilization
Scope of Variable
◦ Local
◦ Global
◦ Constant
Fundamental Types
When programming, we store the variables in our
computer's memory, but the computer has to know what
kind of data we want to store in them, since it is not going to
occupy the same amount of memory to store a simple
number than to store a single letter or a large number, and
they are not going to be interpreted the same way.
The memory in our computers is organized in bytes. A byte
is the minimum amount of memory that we can manage in
C++. A byte can store a relatively small amount of data: one
single character or a small integer (generally an integer
between 0 and 255). In addition, the computer can
manipulate more complex data types that come from
grouping several bytes, such as long numbers or non-
integer numbers.
Fundamental Data Type
Data Types
Floating
Boolean Character Integer
Points
long
long
double
Name Description Size Range
C++ Data Type
char Character or small integer 1 byte signed: -128 to 127 unsigned: 0 to
255
short int Short Integer 2 bytes signed: -32768 to 32767
(short) unsigned: 0 to 65535
int Integer 4bytes signed: -2147483648 to
2147483647
unsigned: 0 to 4294967295
long int Long Integer 8bytes signed: -2147483648 to
(long) 2147483647
unsigned: 0 to 4294967295
bool Boolean value (true or 1byte true or false
false)
float Floating point number 4bytes +/- 3.4e +/- 38 (~6 digits of
decimal)
double Double precision floating 8bytes +/- 1.7e +/- 308 (~15 digits of
point number decimal)
long double Long double precision 10bytes +/- 1.7e +/- 308 (~19 digits of
floating point number decimal)
wchar_t Wide character 2 or 4 1 wide character
bytes
The Type bool
The result of a comparison or a logical association using AND or OR is a
boolean value, which can be true or false.
C++ uses the bool type to represent boolean values.
An expression of the type bool can either be true or false, where the
internal value for true will be represented as the numerical value 1 and
false by a zero.
The char and wchar_t Types
These types are used for saving character codes.
A character code is an integer associated with each character.
The letter A is represented by code 65, for example.
The character set defines which code represents a certain character.
When displaying characters on screen, the applicable character codes are
transmitted and the “receiver,” that is the screen, is responsible for
correctly interpreting the codes.
The C++ language does not stipulate any particular characters set, although
in general a character set that contains the ASCII code (American Standard
Code for Information Interchange) is used.
This 7-bit code contains definitions for 32 control characters (codes 0 – 31)
and 96 printable characters (codes 32 – 127).
.
The char and wchar_t Types
The char (character) type is used to store character codes in one byte (8
bits).
This amount of storage is sufficient for extended character sets, for
example, the ANSI character set that contains the ASCII codes and
additional characters such as German umlauts.
The wchar_t (wide character type) type comprises at least 2 bytes (16
bits) and is thus capable of storing modern Unicode characters.
Unicode is a 16-bit code also used in Windows NT and containing codes
for approximately 35,000 characters in 24 languages
Mostly the wchar_t datatype is used when international languages like
Japanese are used
https://www.geeksforgeeks.org/wide-char-and-library-functions-in-c/
Integral Types
The types short, int, and long are available for operations with integers.
These types are distinguished by their ranges of values. The table on the
opposite page shows the integer types, which are also referred to as
integral types, with their typical storage requirements and ranges of
values.
Floating-Point Type
Numbers with a fraction part are indicated by a decimal point in C++ and
are referred to as floating-point numbers. In contrast to integers, floating-
point numbers must be stored to a preset accuracy. The following three
types are available for calculations involving floating-point numbers:
float for simple accuracy
double for double accuracy
long double for high accuracy
The signed and unsigned
Modifiers
The short, int, and long types are normally interpreted as signed with
the highest bit representing the sign.
However, integral types can be preceded by the keyword unsigned.
The amount of memory required remains unaltered but the range of
values changes due to the highest bit no longer being required as a sign.
The keyword unsigned can be used as an abbreviation for unsigned int.
Variable
Data such as numbers, characters, or even complete records are stored
in variables to enable their processing by a program. Variables are also
referred to as objects, particularly if they belong to a class.
Note: The C++ language is a "case sensitive" language. That means that
an identifier written in capital letters is not equivalent to another one with
the same name but written in small letters. Thus, for example, the
RESULT variable is not the same as the result variable or the Result
variable. These are three different variable identifiers.
Declaration of variables
In order to use a variable in C++, we must first declare it specifying which
data type we want it to be. The syntax to declare a new variable is to write
the specifier of the desired data type (like int, bool, float...) followed by a
valid variable identifier. For example:
int num;
float mynumber;
char c;
double x,y,z
Declaration of Variables
If you are going to declare more than one variable of the same
type, you can declare all of them in a single statement by
separating their identifiers with commas. For example:
int x,y,z;
Declaration of Variables
This declares three variables (x, y and z), all of them of type int,
and has exactly the same meaning as:
int x;
int y;
int z;
Initialization
A variable can be initialized, i.e. a value can be assigned to the
variable, during its definition. Initialization is achieved by placing
the following immediately after the name of the variable:
• an equals sign ( = ) and an initial value for the variable or
• round brackets containing the value of the variable.
Example 1
int x=1;
char c= ‘a’;
float x(1.875)
Example 2
int number; // Declare a variable named "number" of the type "int" (integer)
number = 99; // Assign an integer value of 99 to the variable "number"
number = 88; // Re-assign a value of 88 to "number"
number = number + 1; // Evaluate "number + 1", and assign the result back to "number"
int sum = 0; // Declare an int variable named sum and assign an initial value of 0
sum = sum + number; // Evaluate "sum + number", and assign the result back to
"sum", i.e. add number into sum
int num1 = 5, num2 = 6; // Declare and initialize two int variables in one statement,
separated by a comma
double radius = 1.5; // Declare a variable name radius, and initialize to 1.5
int number; // ERROR: A variable named "number" has already been declared
sum = 55.66; // WARNING: The variable "sum" is an int. It shall not be assigned a
floating-point number
sum = "Hello"; // ERROR: The variable "sum" is an int. It cannot be assigned a text
string
Example 3
Example 4
Screen Output
Take note that:
• Each declaration statement is terminated with a semicolon (;).
• In multiple-variable declaration, the names are separated by commas
(,).
• The symbol =, known as the assignment operator, can be used to
assign an initial value (of the declared type) to the variable.
• Each variable can only be declared once.
• You can declare a variable anywhere inside the program, as long as it
is declared before it is being used.
• Once the type of a variable is declared, it can only store a value
belonging to this particular type. For example, an int variable can hold
only integer such as 123, and NOT floating-point number such as -
2.17 or text string such as "Hello".
• The type of a variable cannot be changed inside the program.
Scope of Variables
All the variables that we intend to use in a program must have been
declared with its type specifier in an earlier point in the code, like we did in
the previous code at the beginning of the body of the function main when
we declared that a, b, and result were of type int.
Example
#define PI 3.14159
#define NEWLINE ‘\n’
This defines two new constants: PI and NEWLINE. Once they are
defined, you can use them in the rest of the code as if they were any
other regular constant, for example:
Declared constants (const)
With the const prefix you can declare constants with a specific type in the
same way as you would do with a variable: