C++ Primitive Data Types

Primitive data types in C++ are the basic built-in types used to store simple values such as numbers, characters, and boolean values. They are the foundation of all C++ programs.

1. Types of Primitive Data Types in C++

  • int - Stores whole numbers.
  • float - Stores decimal numbers (single precision).
  • double - Stores decimal numbers (double precision).
  • char - Stores a single character.
  • bool - Stores true or false values.
  • void - Represents no value.

2. int Data Type

The int type stores whole numbers (both positive and negative).

C++
Example of int data type
#include <iostream>
using namespace std;

int main() {
    int age = 25;
    cout << "Age: " << age;
    return 0;
}

3. float and double Data Types

Float and double are used to store decimal numbers. Double provides more precision than float.

C++
Example of float and double
#include <iostream>
using namespace std;

int main() {
    float price = 99.99f;
    double pi = 3.14159265359;

    cout << "Price: " << price << endl;
    cout << "Pi: " << pi;
    return 0;
}

4. char Data Type

The char type stores a single character enclosed in single quotes.

C++
Example of char data type
#include <iostream>
using namespace std;

int main() {
    char grade = 'A';
    cout << "Grade: " << grade;
    return 0;
}

5. bool Data Type

The bool type stores boolean values: true or false.

C++
Example of bool data type
#include <iostream>
using namespace std;

int main() {
    bool isPassed = true;

    cout << "Passed: " << isPassed;
    return 0;
}

6. void Data Type

The void type represents the absence of value. It is mainly used for functions that do not return any value.

C++
Example of void function
#include <iostream>
using namespace std;

void greet() {
    cout << "Welcome to CodeCrown!";
}

int main() {
    greet();
    return 0;
}

7. Complete Example Using All Primitive Types

C++
Program demonstrating primitive data types
#include <iostream>
using namespace std;

int main() {
    int age = 20;
    float height = 5.9f;
    double salary = 50000.75;
    char grade = 'A';
    bool isStudent = true;

    cout << "Age: " << age << endl;
    cout << "Height: " << height << endl;
    cout << "Salary: " << salary << endl;
    cout << "Grade: " << grade << endl;
    cout << "Is Student: " << isStudent << endl;

    return 0;
}

Conclusion

Primitive data types in C++ form the building blocks of any program. Understanding int, float, double, char, bool, and void is essential before moving to advanced topics like arrays, structures, and object-oriented programming.