Chapter 03
Chapter 03
IT1134
Primary Data Types
A program can use several types of data to solve a given problem, for example, characters, integers, or
floating-point numbers. Since a computer uses different methods for processing and saving data, the data
type must be known.
Character types: They can represent a single character, such as 'A' or '$'.
Integer types: They can store a whole number value, such as 7 or -1024. They exist in a variety of sizes,
and can either be signed or unsigned, depending on whether they support negative values or not.
Floating-point types: They can represent real values, such as 3.14 or -0.01, with different levels of
precision, depending on which of the floating-point types is used.
Boolean type: The Boolean type can only represent one of two states, true or false.
Variables
A variable is a symbolic name for a memory location in which data can be stored and subsequently recalled.
Variables are used for holding data values so that they can be utilized in various computations in a program.
All variables have two important attributes:
A Data type which is established when the variable is defined (e.g., integer, real, character). Once defined,
the type of a C++ variable cannot be changed.
A variable name (Identifier)
Variable name
alignas, alignas, alignof, and, and_eq, asm, auto, bitand, bitor, bool, break, case, catch, char, char16_t,
char32_t, class, compl, const, constexpr, const_cast, continue, decltype, default, delete, do, double,
dynamic_cast, else, enum, explicit, export, extern, false, float, for, friend, goto, if, inline, int, long, mutable,
namespace, new, noexcept, not, not_eq, nullptr, operator, or, or_eq, private, protected, public, register,
reinterpret_cast, return, short, signed, sizeof, static, static_assert, static_cast, struct, switch, template, this,
thread_local, throw, true, try, typedef, typeid, typename, union, unsigned, using, virtual, void, volatile,
wchar_t, while, xor, xor_eq
Declaration of variables
C++ is a strongly-typed language, and requires every variable to be declared with its type before
its first use. This informs the compiler the size to reserve in memory for the variable and how to
interpret its value. The syntax to declare a new variable in C++ is straightforward:
<data type> <variable name>;
int x;
float mynumber;
If declaring more than one variable of the same type, they can all be declared in a single statement by
separating their identifiers with commas. For example:
int x,y,z;
This declares three variables (x, y and z), all of them of type int
Initialization of variables
When the variables are declared, they have an undetermined value until they are assigned a value for
the first time. But it is possible for a variable to have a specific value from the moment it is declared.
This is called the initialization of the variable.
type identifier = initial_value;
int a = 1;
int b = 3, c = 5;
char x = 'x'; // the variable x has the value 'x'.
bool s=false;
Constants
Constants refer to fixed values that the program may not alter and they are called literals. Constants
can be of any of the basic data types and can be divided into Integer Numerals, Floating-Point
Numerals, Characters, Strings and Boolean Values. Again, constants are treated just like regular variables
except that their values cannot be modified after their definition.
Defined constants (#define)
You can define your own names for constants that you use very often without having to resort to memory-
consuming variables, simply by using the #define pre processor directive. Its for mat is:
area = PI * r *r;
cout<< "Area:"<<area;
cout<< NEWLINE;
return 0;
}
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:
const int pathwidth = 100;
const char tabulator = '\t';
Here, pathwidth and tabulator are two typed constants. They are
treated just like regular variables except that their values cannot be
modified after their definition.
Operators in C++
An operator is a symbol that tells the compiler to perform specific mathematical or
logical manipulations. C++ is rich in built-in operators and provides the following
types of operators: Arithmetic Operators, Relational Operators, Logical Operators,
Bitwise Operators, Assignment Operators, and Misc Operators.
Arithmetic Operators
In C++, you can assign a numeric value to a bool variable. Any nonzero value
evaluates to true and zero value evaluates to false. For example, after the following
assignment statements, b1 and b3 become true, and b2 becomes false.
Truth Tables
We can express compound propositions using a truth table that displays the relationships between the truth
values of the simple propositions and the compound proposition.
Logical Operators
Operator Description Example
A=true, B=false
&& Called Logical AND operator. If both the operands are (A && B) is false.
non-zero, then condition becomes true.
Line 3 reads a string to city. This approach to reading a string is simple, but there is a problem.
The input ends with a whitespace character. If you want to enter New York, you have to use an alternative approach.
C++ provides the getline function in the string header file,
Concatenating Strings
C++ provides the + operator for concatenating two strings. The statement shown
below, for example, concatenates strings s1 and s2 into s3:
string s3 = s1 + s2;
message= "Welcome to C++”
message = message+" and programming is fun";
Therefore, the new message is "Welcome to C++ and programming is fun".
Comparing Strings
We can use the relational operators ==, !=, <, <=, >, >= to compare two strings. This is
done by comparing their corresponding characters one by one from left to right.