Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

Data Types

The data type decides what values a variable can hold. So far, you've used int. To start with, these five types are all you need:

#include <iostream>
#include <string>

int main() {
    int students = 28;
    double price = 12.5;
    char group = 'B';
    bool isWeekend = true;
    std::string city = "London";

    std::cout << students << "\n";
    std::cout << price << "\n";
    std::cout << group << "\n";
    std::cout << isWeekend << "\n";
    std::cout << city << "\n";
}

This prints:

28
12.5
B
1
London
  • int is a whole number, positive or negative: 28, -5, 0.
  • double is a decimal number, written with a decimal point: 12.5, -0.25. std::cout prints it without extra zeros: 2.50 as 2.5 and 3.0 as 3.
  • char is one character in single quotes: 'B', '7', '?'.
  • bool is a truth value (a Boolean): either true or false. It's useful for questions whose answer is yes or no: is it the weekend? does the player have the key? std::cout prints true as 1 and false as 0, which is why you see 1 in the output above.
  • std::string is text (a string, as in a string of characters) in double quotes: "London". It comes from the standard library, so it's written with std::, just like std::cout. It needs #include <string> at the top of the program, just as std::cout needs #include <iostream>.

You choose the type by what the variable will hold: the number of students is always a whole number, a price can have cents, and a name is text.

Typical mistakes:

  • One character in double quotes: char group = "B"; ends with the error cannot initialize a variable of type 'char' with an lvalue of type 'const char[2]'. The compiler is saying that the text "B" can't be stored in a char. One character goes in single quotes: 'B'.
  • A decimal comma: in many countries, people write 12,5. In C++, double price = 12,5; ends with the error expected unqualified-id, because the comma means something else in C++. Write decimal numbers with a point.
  • A decimal number in an int: int price = 12.5; compiles, but only 12 is stored. The decimal part is thrown away.
  • If you forget std::, string city = "London"; ends with the error unknown type name 'string'; did you mean 'std::string'?

Instructions

  1. 1

    The program doesn't compile yet, because it prints variables that don't exist. Below the comment, create them, each with the right data type:

    • name with the value "Emma"
    • age with the value 16
    • height with the value 1.68
    • group with the value 'B'
    • isStudent with the value true
  2. 2

    Run the program. It should print:

    Name: Emma
    Age: 16
    Height: 1.68 m
    Group: B
    Is student: 1
  3. 3

    Below the prints, create a variable isAdult of type bool with the value false, and print it like this:

    Is adult: 0
Back

4/11

  1. 1. Creating Variables
  2. 2. Printing Variables
  3. 3. Initialization and Assignment
  4. 4. Data Types
  5. 5. Arithmetic
  6. 6. Integer Division
  7. 7. Quiz: Integer Division
  8. 8. Updating Variables
  9. 9. Joining Text
  10. 10. Quiz: Joining Text
  11. 11. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.