0% found this document useful (0 votes)
12 views13 pages

iii_lesson proper (1)

The document provides an overview of C++ data types, including fundamental types like int, float, char, and double, along with their sizes and examples. It also covers type modifiers, various operators such as arithmetic, relational, logical, assignment, increment/decrement, and bitwise operators, with code examples demonstrating their usage. The document emphasizes the importance of understanding data types and operators for effective programming in C++.

Uploaded by

talidong885
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views13 pages

iii_lesson proper (1)

The document provides an overview of C++ data types, including fundamental types like int, float, char, and double, along with their sizes and examples. It also covers type modifiers, various operators such as arithmetic, relational, logical, assignment, increment/decrement, and bitwise operators, with code examples demonstrating their usage. The document emphasizes the importance of understanding data types and operators for effective programming in C++.

Uploaded by

talidong885
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

C++ Data Types

What kind of data a variable can store is called its data type. For instance, an integer
variable can store integer data, and a Boolean variable can contain Boolean data.

We must utilize distinct variables when coding in order to store various types of data.
Simply put, variables are places set aside for storing values. As a result, you set aside RAM
when you create the variable. Information for many data kinds, including texts, floats, integers,
and Boolean values, can be something you wish to store. Memory is allotted according to the
data type of the variable. The type of data determines how much memory is needed.

For example,

int age = 18;

age here is an int datatype variable. The variable score can only store 2-byte or 4-byte integers,
depending on the compiler/system.

C++ Fundamental Data Types


The table below shows the fundamental data types, their meaning, and their sizes (in bytes):

Data Type Meaning Size (in Bytes)

int Integer 2 or 4

float Floating-point 4

double Double Floating-point 8

char Character 1

Now, let us discuss these fundamental data types in more detail.


1. Integer
 The keyword int can represent integer data types.
 The range of integers is -2147483648 to 2147483647, and they take up 4 bytes
of memory.

For example

int age = 21;

2. Character
 The keyword char represent characters.
 It is 1 byte in size.
 Single quotes ' ' are used to enclose characters in C++
For example

Char test = ‘k’;

3. Floating
 float is the keyword used to hold floating-point numbers (decimals and
exponentials).
 The float variable has a size of 4 bytes.

For example

float area 75.01;

4. Double
 double is the keyword used to hold floating-point numbers (decimals and
exponentials) with double precision.
 The double variable has a size of 8 bytes.

double volume = 135.64534;

Note: In C++, an integer value is stored in a char variable rather than the character itself. To
learn more, visit C++ characters.

C++ Type Modifiers


We can further modify some of the fundamental data types by using type modifiers. There are 4
type modifiers in C++.

They are:
1. signed
2. unsigned
3. short
4. long
We can modify the following data types with the above modifiers:
 int
 double
 char

C++ Operators
In programming, an operator is a symbol (or collection of symbols) that indicates a
specific action or operation to be performed on data. By modifying variables and values using
operators, programmers are able to carry out operations such as value comparisons,
assignments, modifications, and mathematical computations.

The C++ language's operators are essential to our ability to perform a wide range of
operations on data. The many types of operators that are most frequently used in C++
applications are listed in the table below, along with their kinds.

1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Increment and Decrement Operators
6. Bitwise Operators

1. Arithmetic Operators
To perform mathematical operations and arithmetic manipulations on numerical
quantities, we employ the arithmetic operators in C++. Addition, subtraction,
multiplication, and division are among these operations.

Here is a table listing all the C++ arithmetic operators and their associated
syntax:

Now, let's take a look at a simple C++ program that illustrates how to use these
operators in action.

Example 1: Arithmetic Operators

#include <iostream>
using namespace std;

int main() {
int a, b;
a = 7;
b = 2;

// printing the sum of a and b


cout << "a + b = " << (a + b) << endl;
// printing the difference of a and b
cout << "a - b = " << (a - b) << endl;

// printing the product of a and b


cout << "a * b = " << (a * b) << endl;

// printing the division of a by b


cout << "a / b = " << (a / b) << endl;

// printing the modulo of a by b


cout << "a % b = " << (a % b) << endl;

return 0;
}

Output
a+b=9
a-b=5
a * b = 14
a/b=3
a%b=1

Here, the operators +, - and * compute addition, subtraction, and multiplication


respectively as we might have expected.

/ Division Operator
Note the operation (a / b) in our program. The / operator is the division operator.

As we can see from the above example, if an integer is divided by another


integer, we will get the quotient. However, if either divisor or dividend is a floating-point
number, we will get the result in decimals.
In C++,

7/2 is 3
7.0 / 2 is 3.5
7 / 2.0 is 3.5
7.0 / 2.0 is 3.5

% Modulo Operator
The modulo operator % computes the remainder. When a = 9 is divided by b = 4, the
remainder is 1.
Note: The % operator can only be used with integers.

2. Relational Operators

By comparing two values, the relational operators are used to evaluate the
relationship between them. After performing a relational comparison between the left
and right operands, these operators yield boolean values, or true or false. The
different relational operators in C++ are listed in the table below, along with the
matching syntax.

Now, let's look at an example showcasing the implementation of these relational


operators in C++.

#include <iostream>
using namespace std;

int main() {
int a, b;
a = 3;
b = 5;
bool result;

result = (a == b); // false


cout << "3 == 5 is " << result << endl;

result = (a != b); // true


cout << "3 != 5 is " << result << endl;

result = a > b; // false


cout << "3 > 5 is " << result << endl;

result = a < b; // true


cout << "3 < 5 is " << result << endl;

result = a >= b; // false


cout << "3 >= 5 is " << result << endl;

result = a <= b; // true


cout << "3 <= 5 is " << result << endl;

return 0;
}

Output
3 == 5 is 0
3 != 5 is 1
3 > 5 is 0
3 < 5 is 1
3 >= 5 is 0
3 <= 5 is 1

Note: Relational operators are used in decision making and loops.

3. Logical Operators

When working with boolean values, C++ logical operators are used (true or
false). With the help of a truth table, you can use these operators to combine many
conditions and find the truth value of each one. In C++, there are three different kinds
of logical operators: logical AND, logical OR, and logical NOT. For every operator,
the truth tables define the outcome of the logical operators. They define how the
outcome of the whole expression depends on the outcomes of the individual
expressions on the left-hand side and the right-hand side.

The table below shows the three logical operators in C++, along with the symbol
and syntax.

Let's take a code sample to show how logical operators are used in C++
programming language.

For example
#include <iostream>

int main() {
int x = 5;
int y = 10;

bool condition1 = (x > 0) && (y < 20);


bool condition2 = (x == 5) || (y > 15);
bool condition3 = !(x > 0);

std::cout << "Condition 1: " << condition1 << std::endl;


std::cout << "Condition 2: " << condition2 << std::endl;
std::cout << "Condition 3: " << condition3 << std::endl;

return 0;

Output

Condition 1: 1
Condition 2: 1
Condition 3: 0

Explanation:

1. We begin the C++ example by initializing two integer variables, x and y, with
values 5 and 10 (respectively) inside main().
2. Next, we use logical operators to connect expressions that are made up of
other operators and compare the values as stipulated.
3. We use the AND logical operator in the first operation, i.e., (x > 0) && (y <
20). It checks if x is greater than 0 and y is less than 20.
4. If both the expressions return true (yes in this case), then the AND operation
also returns true, represented by the value 1. The outcome is stored in the
condition1 variable.
5. After that, we use the OR logical operator, which returns true even if one of
the expressions is true. The second operation, i.e., (x == 5) || (y > 15) we
determines if x equals 5 OR y exceeds 15.
6. The result is stored in the condition2 variable, whose value is true (i.e., 1)
since x is actually equal to 5, meaning at least one of the requirements is
met.
7. Next, we use the NOT logical operator, which inverses the value of the initial
condition. Here, in the expression !(x > 0), we check if x is greater than 0, and
the outcome of this logical operation will be the opposite.
8. The initial condition (x>0) is true since x is bigger than 0 and 5 in number.
The outcome of the logical NOT is stored in the condition3 variable, which is
false, denoted by 0.
9. Finally, we use a set of cout commands to print the outcome of each
operation to the output console.
4. Assignment Operators

Assignment operators are used to assign values to variables. They enable you to
perform an operation and simultaneously modify or store a value in a variable.
Assignment operators can be further divided into a number of groups based on the
activities they perform.
The many assignment operators available in C++ are shown in the following
table:

Let's examine some code to illustrate how assignment operators in C++ are
used.

For example:

#include <iostream>

int main() {

int x = 5; // Simple assignment


x += 3; // Addition assignment
std::cout << "x += 3: " << x << std::endl;

x -= 2; // Subtraction assignment
std::cout << "x -= 2: " << x << std::endl;

x *= 4; // Multiplication assignment
std::cout << "x *= 4: " << x << std::endl;

x /= 2; // Division assignment
std::cout << "x /= 2: " << x << std::endl;
x %= 3; // Modulo assignment
std::cout << "x %= 3: " << x << std::endl;

return 0;
}

Output:

x += 3: 8
x -= 2: 6
x *= 4: 24
x /= 2: 12
x %= 3: 0

Explanation:

In the C++ code example-

1. Inside the main() function, we declare an integer variable x and assign the
value 5 to it using the basic assignment operator (=).
2. Then, we use other assignment/ compound assignment operators to carry out
various operations on this variable and assign the value back to the variable
x.
3. As mentioned in the code comments, we first use the addition assignment
operator (+=) to add the right-hand operand/ value to the initial value of x (5)
and assign the result back to x.
4. We then use the cout command to print the value to the console. Note that at
this point, the updated value of x is 8, so the next operation will be carried out
on this value and not the initial value of x.
5. Then, we use the subtraction assignment operator (-=) to subtract the value
on the right-hand side (2) from the current value of x (8). The updated value
of x (6) is then printed to the console.
6. After that, we use the multiplication assignment operator (*=) to multiply the
value of x with the right-hand operand (4) and assign the updated value (24)
back to x.
7. Next, we use the division assignment operator (/=) and divide the value of x
(24) by 2, then assign the updated value back to x (12).
8. Lastly, we use the modulo assignment operator (%=) to find the remainder of
the division of x by 3. This value is then assigned back to x (0), as shown in
the output console.

5. Increment and Decrement Operators

C++ also provides increment and decrement operators: ++ and -- respectively.


++ increases the value of the operand by 1, while -- decreases it by 1.
For example,
int num = 5;

// increasing num by 1
++num;
Here, the value of num gets increased to 6 from its initial value of 5.

For example:
// Working of increment and decrement operators

#include <iostream>
using namespace std;

int main() {
int a = 10, b = 100, result_a, result_b;

// incrementing a by 1 and storing the result in result_a


result_a = ++a;
cout << "result_a = " << result_a << endl;

// decrementing b by 1 and storing the result in result_b


result_b = --b;
cout << "result_b = " << result_b << endl;

return 0;
}

Output:

result_a = 11
result_b = 99

In the above program, we used ++ and -- operator as prefixes. We can also use
these operators as postfix.
There is a slight difference when these operators are used as a prefix versus
when they are used as a postfix.
To learn more about these operators, visit increment and decrement operators.

6. Bitwise Operators

The bitwise operators in C++ are used to change operand values at the bit level,
as their name implies. They perform necessary operations on the binary
representation of the data values and compare, shift, or otherwise work with the
operand value bits.
The C++ bitwise operators are shown in the following table:

Here is a sample code that shows how to use binary bitwise operators in C++.

For example:

#include <iostream>
using namespace std;

int main() {
int x = 5; // Binary: 0101
int y = 3; // Binary: 0011
int result;

// Bitwise AND
result = x & y; // Binary: 0001
cout << "Bitwise AND: " << result << endl;

// Bitwise OR
result = x | y; // Binary: 0111
cout << "Bitwise OR: " << result << endl;

// Bitwise XOR
result = x ^ y; // Binary: 0110
cout << "Bitwise XOR: " << result << endl;

// Bitwise NOT
result = ~x; // Binary: 1010 (2's complement representation)
cout << "Bitwise NOT: " << result << endl;

// Bitwise left shift


result = x << 2; // Binary: 10100
cout << "Bitwise left shift: " << result << endl;

// Bitwise right shift


result = y >> 1; // Binary: 0001
cout << "Bitwise right shift: " << result << endl;

return 0;
}

Output:

Bitwise AND: 1
Bitwise OR: 7
Bitwise XOR: 6
Bitwise NOT: -6
Bitwise left shift: 20
Bitwise right shift: 1

Explanation:

In the C++ code example-

1. Inside the main() function, we declare and initialize two integer variables, x
and y, with values of 5 (binary equivalent 0101) and 3 (binary equivalent
0011), respectively.
2. We then use bitwise operators to carry out bitwise operations on these
variables.
3. First, we use the bitwise AND (&) to compare individual bits of the right and
left operand values. The operator returns true if either both the bits are 1 or 0.
4. The outcome of the operation, i.e., 0001 or integer 1, is stored in the result
variable and printed to the console using the cout command.
5. Next, we use the bitwise OR (|) to compare the bits of operands and return 1
even if either of the bits is 1 and 0 otherwise.
6. The outcome of this operation is binary number 0111 with integer
representation 7, which is stored in the result variable and printed to the
console.
7. After that, we use the bitwise XOR (^), which returns 1 only if the bits of the
operands are different and 0 otherwise.
8. The outcome of this operation is 0110, whose integer representation (6) is
stored in the result variable and displayed in the output console.
9. Then, we use the bitwise NOT (~), which reverses the bit values at every
position and provides a complement.
10. Because the two's complement representation of x is subjected to the bitwise
NOT operation, the outcome is -6.
11. Next, we use the bitwise Left Shift (<<), which shifts the bits of the operand
by the specified number. Here, x is essentially increased by 2 to the power of
the shift count (2). The outcome is 20.
12. Lastly, we use bitwise Right Shift (>>) to move the bits of y one bit to the
right. By doing this, y is divided by 2 increased to power of shift count (1). The
outcome is 1.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy