Bird
Raised Fist0
C++programming~10 mins

Built-in data types in C++ - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Built-in data types
Start
↓
Declare variable with built-in type
↓
Assign value
↓
Use variable in operations
↓
Output or store result
↓
End
This flow shows how built-in data types are declared, assigned values, used, and then output or stored.
Execution Sample
C++
#include <iostream>

int main() {
    int a = 5;
    float b = 3.2f;
    char c = 'X';
    bool d = true;
    std::cout << a << b << c << d;
    return 0;
}
This code declares variables of built-in types, assigns values, and prints them.
Execution Table
StepActionVariableValueOutput
1Declare int aaundefined
2Assign 5 to aa5
3Declare float bbundefined
4Assign 3.2 to bb3.2
5Declare char ccundefined
6Assign 'X' to cc'X'
7Declare bool ddundefined
8Assign true to ddtrue
9Output variablesa,b,c,d5,3.2,'X',true53.2X1
10End of program
💡 Program ends after outputting all variable values.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6After Step 8Final
aundefined55555
bundefinedundefined3.23.23.23.2
cundefinedundefinedundefined'X''X''X'
dundefinedundefinedundefinedundefinedtruetrue
Key Moments - 3 Insights
Why does the variable 'a' have 'undefined' before assignment?
Before assigning a value, variables hold garbage or undefined data, as shown in steps 1 and 2 in the execution_table.
Why is the output '53.2X1' when printing variables?
The output concatenates values: int 5, float 3.2, char 'X', and bool true (printed as 1), as shown in step 9.
Why do we use 'f' after 3.2 in float assignment?
The 'f' tells the compiler this is a float literal, not double, ensuring correct type assignment (step 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'c' after step 6?
A5
Bundefined
C'X'
Dtrue
💡 Hint
Check the 'Value' column for variable 'c' at step 6 in execution_table.
At which step does the variable 'b' get assigned a value?
AStep 4
BStep 3
CStep 2
DStep 6
💡 Hint
Look for the assignment action for 'b' in execution_table.
If we change 'bool d = true;' to 'bool d = false;', what would be the output at step 9?
A53.2X1
B53.2X0
C53.2Xtrue
D53.2Xfalse
💡 Hint
Boolean true prints as 1 and false as 0, check output in execution_table step 9.
Concept Snapshot
Built-in data types in C++ include int, float, char, bool.
Declare variables with type, assign values.
Use 'f' suffix for float literals.
Bool prints as 1 (true) or 0 (false).
Variables hold undefined data before assignment.
Full Transcript
This visual execution traces how built-in data types in C++ work. We start by declaring variables of types int, float, char, and bool. Initially, variables hold undefined values. Then we assign values: 5 to int, 3.2f to float, 'X' to char, and true to bool. Finally, we output all variables, which prints 5, 3.2, X, and 1 (for true). The flow shows declaration, assignment, usage, and output. Key points include understanding undefined initial values, the float literal suffix 'f', and how bool prints as 1 or 0. The quiz checks understanding of variable values at steps and output changes if bool changes.

Practice

(1/5)
1.

Which built-in data type in C++ is used to store whole numbers without decimals?

easy
A. int
B. double
C. char
D. bool

Solution

  1. Step 1: Understand data types for numbers

    Whole numbers without decimals are stored in integer types.
  2. Step 2: Identify the integer type in C++

    The int type stores whole numbers like 1, 2, -5.
  3. Final Answer:

    int -> Option A
  4. Quick Check:

    Whole numbers = int [OK]
Hint: Whole numbers use int type in C++ [OK]
Common Mistakes:
  • Choosing double for whole numbers
  • Using char for numbers
  • Confusing bool with numbers
2.

Which of the following is the correct way to declare a variable of type char in C++?

?
easy
A. char letter = 65
B. char letter = "A";
C. char letter = A;
D. char letter = 'A';

Solution

  1. Step 1: Understand char initialization

    Characters must be enclosed in single quotes in C++.
  2. 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.
  3. Final Answer:

    char letter = 'A'; -> Option D
  4. Quick Check:

    char uses single quotes for characters [OK]
Hint: Use single quotes for char values [OK]
Common Mistakes:
  • Using double quotes for char
  • Missing quotes around character
  • Assigning int without cast
3.

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;
}
medium
A. True
B. False
C. 0
D. 1

Solution

  1. Step 1: Understand bool initialization

    The variable flag is set to 0, which means false in boolean context.
  2. Step 2: Check the if condition

    Since flag is false, the else block runs, printing "False".
  3. Final Answer:

    False -> Option B
  4. Quick Check:

    bool 0 means false [OK]
Hint: bool 0 means false, else block runs [OK]
Common Mistakes:
  • Thinking 0 is true
  • Expecting numeric output
  • Confusing bool with int
4.

Identify the error in this C++ code snippet:

double price = 9.99;
char letter = "B";
bool isAvailable = true;
medium
A. Incorrect bool value assignment
B. Missing semicolon after double declaration
C. Using double quotes for char variable
D. No errors in the code

Solution

  1. Step 1: Check char variable initialization

    The variable letter is declared as char but assigned with double quotes, which is incorrect.
  2. Step 2: Verify other lines

    The double and bool declarations are correct with semicolons and proper values.
  3. Final Answer:

    Using double quotes for char variable -> Option C
  4. Quick Check:

    char needs single quotes, not double [OK]
Hint: char must use single quotes, not double quotes [OK]
Common Mistakes:
  • Using double quotes for char
  • Missing semicolons
  • Wrong bool literals
5.

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++?

medium
A. int for age, char for initial, bool for student status
B. double for age, string for initial, int for student status
C. char for age, int for initial, bool for student status
D. bool for age, char for initial, double for student status

Solution

  1. Step 1: Choose type for age

    Age is a whole number, so int is suitable.
  2. Step 2: Choose type for initial letter

    Initial is a single character, so char fits.
  3. Step 3: Choose type for student status

    Student status is yes/no, so bool is best.
  4. Final Answer:

    int for age, char for initial, bool for student status -> Option A
  5. Quick Check:

    Whole number = int, letter = char, yes/no = bool [OK]
Hint: Use int for numbers, char for letters, bool for yes/no [OK]
Common Mistakes:
  • Using double for age
  • Using string for single char
  • Using int for boolean values
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard