Built-in data types let you store different kinds of information in your program, like numbers or letters.
Built-in data types in C++
type variableName; // Examples: int age; double price; char letter; bool isOpen;
type is the kind of data you want to store.
variableName is the name you give to your storage box.
int count = 10;
double temperature = 36.5;
char grade = 'A';
bool isRaining = false;This program creates variables of different built-in types and prints their values.
#include <iostream> int main() { int apples = 5; double price = 1.25; char firstLetter = 'A'; bool isOpen = true; std::cout << "Apples: " << apples << "\n"; std::cout << "Price: $" << price << "\n"; std::cout << "First letter: " << firstLetter << "\n"; std::cout << "Store open? " << (isOpen ? "Yes" : "No") << "\n"; return 0; }
Use int for whole numbers without decimals.
Use double for numbers with decimals.
Use char for single characters inside single quotes.
Use bool for true or false values.
Built-in data types store basic kinds of information.
Common types include int, double, char, and bool.
Choose the type based on the kind of data you want to keep.
Practice
Which built-in data type in C++ is used to store whole numbers without decimals?
Solution
Step 1: Understand data types for numbers
Whole numbers without decimals are stored in integer types.Step 2: Identify the integer type in C++
Theinttype stores whole numbers like 1, 2, -5.Final Answer:
int -> Option AQuick Check:
Whole numbers = int [OK]
- Choosing double for whole numbers
- Using char for numbers
- Confusing bool with numbers
Which of the following is the correct way to declare a variable of type char in C++?
?Solution
Step 1: Understand char initialization
Characters must be enclosed in single quotes in C++.Step 2: Check each option
char letter = 'A'; uses single quotes correctly. char letter = "A"; uses double quotes (string). char letter = A; misses quotes. char letter = 65; uses an int, which is allowed but not typical for char.Final Answer:
char letter = 'A'; -> Option DQuick Check:
char uses single quotes for characters [OK]
- Using double quotes for char
- Missing quotes around character
- Assigning int without cast
What will be the output of the following C++ code?
#include <iostream>
int main() {
bool flag = 0;
if (flag) {
std::cout << "True";
} else {
std::cout << "False";
}
return 0;
}Solution
Step 1: Understand bool initialization
The variableflagis set to 0, which means false in boolean context.Step 2: Check the if condition
Sinceflagis false, the else block runs, printing "False".Final Answer:
False -> Option BQuick Check:
bool 0 means false [OK]
- Thinking 0 is true
- Expecting numeric output
- Confusing bool with int
Identify the error in this C++ code snippet:
double price = 9.99;
char letter = "B";
bool isAvailable = true;Solution
Step 1: Check char variable initialization
The variableletteris declared ascharbut assigned with double quotes, which is incorrect.Step 2: Verify other lines
The double and bool declarations are correct with semicolons and proper values.Final Answer:
Using double quotes for char variable -> Option CQuick Check:
char needs single quotes, not double [OK]
- Using double quotes for char
- Missing semicolons
- Wrong bool literals
You want to store a person's age, initial letter of their name, and whether they are a student. Which built-in data types should you use in C++?
Solution
Step 1: Choose type for age
Age is a whole number, sointis suitable.Step 2: Choose type for initial letter
Initial is a single character, socharfits.Step 3: Choose type for student status
Student status is yes/no, soboolis best.Final Answer:
int for age, char for initial, bool for student status -> Option AQuick Check:
Whole number = int, letter = char, yes/no = bool [OK]
- Using double for age
- Using string for single char
- Using int for boolean values
Ready to prove it?
Pass the test and this pattern moves unknown
