VOOZH about

URL: https://www.codecademy.com/article/cpp-data-types

⇱ Basic Data Types | Codecademy


Skip to Content
Articles

Basic Data Types

Basic Data Types:

C++ provides a rather large number of types. However, you can write perfectly good programs using only five of those:

👁 Basic C++ Data Types

Here are some examples of declaring and initializing variables:

int age =28;
double price =8.99;
char grade ='A';
std::string message ="Game Over";
bool late_to_work =true;
Copy to clipboard
Copy to clipboard

Datatype Modifiers:

As the name implies, datatype modifiers are used with built-in data types to modify the length of data that a particular data type can hold. Data type modifiers in C++ are:

  • signed
  • unsigned
  • short
  • long

We will learn about these in a bit!

Const:

const (constant) variables cannot be changed by your program during execution.

constdouble quarter =0.25;
// and now variable quarter can only be 0.25
Copy to clipboard
Copy to clipboard

Simply add the keyword const before the data type during declaration to make the variable not modifiable.

Type Conversion:

A type cast is basically a conversion from one type to another.

The notation (type) value means “convert value to type“. So for example:

double weight1;
int weight2;
weight1 =154.49;
weight2 =(int) weight1;
// weight2 is now 154
Copy to clipboard
Copy to clipboard

Note: Going from a double to an int simply removes the decimal. There’s no rounding involved.

Codecademy Team

'The Codecademy Team, composed of experienced educators and tech experts, is dedicated to making tech skills accessible to all. We empower learners worldwide with expert-reviewed content that develops and enhances the technical skills needed to advance and succeed in their careers.'

Meet the full team

Learn more on Codecademy

  • Learn advanced techniques to narrow complex types in TypeScript. Discover TypeScript’s power inference through type guards and type narrowing.
    • With CertificateWith Certificate
    • Intermediate.
      1 hour1 hour
  • BI Data Analysts use Python and SQL to query, analyze, and visualize data — and Tableau and Excel to communicate findings.
    • Includes 18 CoursesIncludes 18 Courses
    • With CertificateWith Certificate
    • Beginner Friendly.
      50 hours50 hours
  • Data Analysts and Analytics Data Scientists use Python and SQL to query, analyze, and visualize data — and communicate findings.
    • Includes 22 CoursesIncludes 22 Courses
    • With Professional CertificationWith Professional Certification
    • Beginner Friendly.
      70 hours70 hours