Learn C++ from scratch with clear explanations, practical code examples, hands-on exercises, and downloadable PDF lessons. Covers variables, data types, operators, control flow, functions, iostream, and common pitfalls—ideal for students and self-learners.
Question: Write a C++ program to demonstrate different data types (int, float, double, char, bool, string) and print example values for each.
#include <iostream>
using namespace std;
int main() {
// Integer: whole numbers
int age = 20;
cout << "Integer example (age): " << age << endl;
// Floating-point: numbers with decimal
float height = 5.9;
double weight = 70.5;
cout << "Float example (height): " << height << endl;
cout << "Double example (weight): " << weight << endl;
// Character: single letter or symbol
char grade = 'A';
cout << "Character example (grade): " << grade << endl;
// Boolean: true or false
bool isStudent = true;
cout << "Boolean example (isStudent): " << isStudent << endl;
// String: sequence of characters
string name = "Ali";
cout << "String example (name): " << name << endl;
return 0;
}
'A' or 'x'.true or false."Hello".Integer example (age): 20
Float example (height): 5.9
Double example (weight): 70.5
Character example (grade): A
Boolean example (isStudent): 1
String example (name): Ali
Note: Boolean values print as
1(true) or0(false) in C++.
Question: Write a C++ program to demonstrate the use of the char (character) data type and display example values.
#include <iostream>
using namespace std;
int main() {
// Character variable
char grade = 'A';
char symbol = '#';
char letter = 'Z';
cout << "Character example 1 (grade): " << grade << endl;
cout << "Character example 2 (symbol): " << symbol << endl;
cout << "Character example 3 (letter): " << letter << endl;
return 0;
}
char data type is used to store a single character, such as a letter, digit, or symbol.Character example 1 (grade): A
Character example 2 (symbol): #
Character example 3 (letter): Z
Here’s a beginner-friendly C++ example to explain the concept of overflow and underflow in simple terms 👇
Question: Write a C++ program to demonstrate the concept of overflow and underflow using integer data type.
#include <iostream>
#include <limits> // for numeric_limits
using namespace std;
int main() {
// Find the maximum and minimum values an int can hold
int maxValue = numeric_limits<int>::max();
int minValue = numeric_limits<int>::min();
cout << "Maximum value of int: " << maxValue << endl;
cout << "Minimum value of int: " << minValue << endl;
// Overflow: adding 1 to the maximum value
cout << "\nAfter overflow (maxValue + 1): " << (maxValue + 1) << endl;
// Underflow: subtracting 1 from the minimum value
cout << "After underflow (minValue - 1): " << (minValue - 1) << endl;
return 0;
}
numeric_limits<int>::max() gives the largest value an integer can store.numeric_limits<int>::min() gives the smallest value an integer can store.For more details about ‘numeric_limits’, See C++ numeric_limits – Get Min/Max Values for Data Types (Beginner’s Guide)
Maximum value of int: 2147483647
Minimum value of int: -2147483648
After overflow (maxValue + 1): -2147483648
After underflow (minValue - 1): 2147483647
Questions: Write a C++ program to demonstrate variable declaration, initialization
#include <iostream>
using namespace std;
int main() {
// 1. What is a variable?
// A variable is a name that stores a value in memory.
// You can change the value of a variable during program execution.
// 2. Variable declaration and initialization
int age = 20; // 'int' is the type, 'age' is the variable name, 20 is the initial value
float height = 5.9; // float stores numbers with decimals
char grade = 'A'; // char stores a single character
bool isStudent = true; // bool stores true or false
string name = "Ali"; // string stores a sequence of characters
// 3. Printing variable values
cout << "Age: " << age << endl;
cout << "Height: " << height << endl;
cout << "Grade: " << grade << endl;
cout << "Is student? " << isStudent << endl;
cout << "Name: " << name << endl;
// 4. Changing variable values
age = 21; // Value of 'age' is updated
name = "Ahmed"; // Value of 'name' is updated
cout << "\nAfter updating variables:" << endl;
cout << "Age: " << age << endl;
cout << "Name: " << name << endl;
/* 5. Rules for declaring variables:
- Must start with a letter or underscore (_)
- Can contain letters, digits, and underscores
- Cannot start with a digit
- Cannot use C++ keywords (like int, float, return, etc.)
- Should have meaningful names
*/
return 0;
}
✅ Explanation:
int age;).int age = 20;).Question: Write a C++ program to demonstrate the concept of literals, including long literals.
#include <iostream>
using namespace std;
int main() {
// 1. Integer literal
int age = 25;
cout << "Integer literal (age): " << age << endl;
// 2. Long integer literal
long population = 7800000000L; // 'L' specifies a long literal
cout << "Long literal (population): " << population << endl;
// 3. Floating-point literal
float height = 5.9f; // 'f' specifies a float literal
double weight = 70.5; // double literal by default
cout << "Float literal (height): " << height << endl;
cout << "Double literal (weight): " << weight << endl;
// 4. Character literal
char grade = 'A';
cout << "Character literal (grade): " << grade << endl;
// 5. Boolean literal
bool isStudent = true;
cout << "Boolean literal (isStudent): " << isStudent << endl;
// 6. String literal
string name = "Ali";
cout << "String literal (name): " << name << endl;
// 7. Escape sequence literal
cout << "This is a new line\nand this is the next line using a literal." << endl;
return 0;
}
Examples:
25 → integer literal7800000000L → long integer literal5.9f → float literal70.5 → double literal'A' → character literal"Ali" → string literaltrue → boolean literal\n → escape sequence literalL for long and f for float to specify the type explicitly.for more details, see Difference Between Preprocessor and Compiler in C++