Here are some basic variable types in C++:
Integer Types:
short: 2 bytes, min: -32768, max: 32767
short s = 10;
int: 4 bytes, min: -2147483648, max: 2147483647
int i = 20;
long: 4 bytes (usually), min: -2147483648, max: 2147483647
long l = 30L; // 'L' suffix is optional but recommended for clarity.
long long: 8 bytes, min: -9223372036854775808, max: 9223372036854775807
long long ll = 40LL; // 'LL' suffix is optional but recommended for clarity.
Unsigned Integer Types:
Each of the above types has an unsigned counterpart (e.g., unsigned int, unsigned long long) with twice the range.
Floating-Point Types:
float: 4 bytes, decimal fraction
float f = 3.14f; // 'f' suffix is optional but recommended for clarity.
double: 8 bytes, higher precision than float
double d = 3.14;
long double: 10 or more bytes, highest precision
Character Types:
char: 1 byte, stores a single character or ASCII value
char c = 'A';
wchar_t: 2 or 4 bytes (depending on the platform), stores wide characters (e.g., Unicode)
Boolean Type:
bool: 1 byte, can have values true or false
bool b = true;
Pointer Types:
Pointers store memory addresses and are used to manipulate the data at those addresses.
int* ptr; // pointer to an integer
Here’s an example that demonstrates some basic variable declarations and initialization:
#include <iostream>
int main() {
int i = 10; // integer declaration and assignment
double d = 3.14; // floating-point number declaration and assignment
std::cout << "i = " << i << ", d = " << d << std::endl;
bool b = true;
char c = 'A';
char s[20] = "Hello, World!";
std::cout << "b = " << (b ? "true" : "false") << ", c = " << c << ", s = " << s << std::endl;
return 0;
}
This will output:
i = 10, d = 3.14
b = true, c = A, s = Hello, World!
Overflow is the situation where you try to store a number that exceeds the maximum range for the data type. When you try to store too large of a positive or negative number, the binary representation of the number is corrupted and you get a meaningless or erroneous result.
Underflow is the situation where you try to store a number that exceeds the minimum range for the data type. When you try to store too small of a positive or negative number, the binary representation of the number is corrupted and you get a meaningless or erroneous result.
Example:
#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) << "\n";
// printing the difference of a and b
cout << "a - b = " << (a - b) << "\n";
// printing the product of a and b
cout << "a * b = " << (a * b) << "\n";
// printing the division of a by b
cout << "a / b = " << (a / b) << "\n";
// printing the modulo of a by b
cout << "a % b = " << (a % b) << "\n";
return 0;
}
Note the operation (a / b) in our program. The / operator is the division operator.
As we can see from the 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
Example:
#include <iostream>
using namespace std;
int main() {
int x; // Declare an integer variable
x = 20; // Assign the value 20 to x
cout << "The value of x is: " << x << endl; // Output: The value of x is: 20
int a = 10;
a += 5; // Now a is 15
cout << "After addition, a = " << a << endl;
// Output: After addition, a = 15
a -= 3; // Now a is 12
cout << "After subtraction, a = " << a << endl;
// Output: After subtraction, a = 12
a *= 2; // Now a is 24
cout << "After multiplication, a = " << a << endl;
// Output: After multiplication, a = 24
a /= 4; // Now a is 6
cout << "After division, a = " << a << endl;
// Output: After division, a = 6
return 0;
}
Example:
#include <iostream>
using namespace std;
int main() {
int a = 10;
int b = 20;
cout << "a == b: " << (a == b) << endl; // Output: 0 (false)
cout << "a != b: " << (a != b) << endl; // Output: 1 (true)
cout << "a > b: " << (a > b) << endl; // Output: 0 (false)
cout << "a < b: " << (a < b) << endl; // Output: 1 (true)
cout << "a >= b: " << (a >= b) << endl; // Output: 0 (false)
cout << "a <= b: " << (a <= b) << endl; // Output: 1 (true)
return 0;
}
Example:
#include <iostream>
using namespace std;
int main() {
//Logical AND
int age = 25;
bool isStudent = true;
if (age > 18 && isStudent) {
cout << "You are eligible for a student discount." << endl;
} else {
cout << "You are not eligible for a student discount." << endl;
}
//Logical OR
int num = 7;
if (num <= 0 || num >= 10) {
cout << "The number is outside the range of 0 to 10." << endl;
} else {
cout << "The number is between 0 to 10." << endl;
}
//Logical NOT
bool condition = false;
if (!condition) { // Negates the condition
cout << "Condition is false." << endl;
} else {
cout << "Condition is true." << endl;
}
return 0;
}
Example:
#include <iostream>
using namespace std;
int main() {
int a = 7; // Binary: 0111
int b = 5; // Binary: 0101
cout << "Bitwise Operators Example:\n";
cout << "a & b = " << (a & b) << " // Bitwise AND\n"; // Result: 5 (0101)
cout << "a | b = " << (a | b) << " // Bitwise OR\n"; // Result: 7 (0111)
cout << "a ^ b = " << (a ^ b) << " // Bitwise XOR\n"; // Result: 2 (0010)
cout << "~a = " << (~a) << " // Bitwise NOT\n"; // Result: -8 (inverts bits)
cout << "a << 1 = " << (a << 1) << " // Left Shift\n"; // Result: 14 (1110)
cout << "a >> 1 = " << (a >> 1) << " // Right Shift\n"; // Result: 3 (0011)
return 0;
}