CodeBlanks Logo Icon CodeBlanks

Summary - Data types

Combine integer, floating-point and string types to write a calculation card.

Summary - Data types

In this section you learned the basic C++ data types:

  • int, short, long, long long integer types
  • float, double, long double floating-point types
  • bool for true/false, char for single characters
  • std::string for text
  • sizeof to query memory size

Now you will combine these types in two tasks to reinforce what you learned.

Practice Print three types

Add the #include <string> header. Inside main, declare an int (value 42), a double (value 3.14) and a std::string (value "Test"). Print all three in a single std::cout line with spaces (" ") between them and end with std::endl.

Starter code

main.cpp
#include <iostream>

int main() {
    // Declare int, double and std::string
    // Print all three with std::cout
    return 0;
}

Sample solution

main.cpp
#include <iostream>
#include <string>

int main() {
    // Declare int, double and std::string
    int count = 42;
    double rate = 3.14;
    std::string name = "Test";
    // Print all three with std::cout
    std::cout << count << " " << rate << " " << name << std::endl;
    return 0;
}

Expected output

42 3.14 Test

Practice Print a variable's size

Inside main, declare int value = 100;. Use sizeof(value) to print the byte size of value with std::cout using std::endl.

Starter code

main.cpp
#include <iostream>

int main() {
    // Declare int value
    // Print the size of value with sizeof
    return 0;
}

Sample solution

main.cpp
#include <iostream>

int main() {
    // Declare int value
    int value = 100;
    // Print the size of value with sizeof
    std::cout << sizeof(value) << std::endl;
    return 0;
}

Expected output

4

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Match types with categories

Term Meaning
int Integer
double Floating-point
char Character
std::string Text

2 Querying size

Question
std::cout << ____(double);
Answer
std::cout << sizeof(double);

Explanation: sizeof(double) gives the byte size of double.

All lessons in Beginner