Menu
CoddyTech

Type Declaration

Part of the Fundamentals section of Coddy's C++ journey. Lesson 11 of 74.

In C++, once a variable is declared with a certain type, it can only hold values of that type. For instance, an int variable can only hold integer values, and a std::string variable can only hold text.

For example:

int age = 25;          // Can only hold whole numbers
string str = "abc";  // Can only hold text

These would cause errors:

age = "defg";  // Error: can't put text in an int variable
std::string s = 25;  // Error: no std::string constructor accepts a number

These are valid:

age = 26;        // OK: assigning a new integer
str = "Jane";    // OK: assigning a new text string
challenge icon

Challenge

Beginner

Declare the following variables with their corresponding types and values:

  • An int variable named count with the value 10.
  • A double variable named total with the value 150.75.
  • A char variable named grade with the value 'A'.
  • A bool variable named isActive with the value false.
  • A string variable named userName with the value "Bob123".

The lines below your code print them in this format:

Count: [value of count]
Total: [value of total]
Grade: [value of grade]
Active: [value of isActive]
User Name: [value of userName]

Try it yourself

    // Declaring variables


quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals

Practice on your own: Online C++ compiler