Saturday, April 19, 2025

C++ Data Types

C++ is one of those programming languages where we must extremely precise while creating variables because end result of operations heavely depend on us thinking in advance about inputs for programs and functions we write.

At the moment we are familiar with whole numbers (integers) where we use int as data type:

int a = 5;

If there's need to be more precise float is used as data type: 

float b = 4323.4323;

And if some operations demands extra precision we will use double as data type:

double z = 4323.4323546;

To use individual characters we must use single quotes only, and data type will be char:

char let = 'A';

For simplicity reasons we wil just print our variables using this source:

cout << "Number from b: " << b << endl;

cout << "Character from container let: " << let << endl;

cout << "Number from z: " << z << endl;

This is full working source:

#include <iostream>

using namespace std;

int main() {

    int a = 5;
    float b = 4323.4323;
    double z = 4323.4323546;

    char let = 'A';

    cout << "Number from b: " << b << endl;

    cout << "Character from container let: " << let << endl;

    cout << "Number from z: " << z << endl;

    return 0;

}

Result:

Number from b: 4323.43
Character from container let: A
Number from z: 4323.43

Process returned 0 (0x0)   execution time : 0.050 s
Press any key to continue.

These are basic data types in C++, but over time you will learn many more

If you have same time there's nice page on Wiki about C++ data types. You don't need to memorize ranges of values, just make note that you can use it, if you need it.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...