C++ Data Types & I/O
C++ extends C's type system with bool, string, auto, references, and type-safe I/O streams. These improvements make C++ safer and more expressive than C while maintaining performance.
35 min•By Priygop Team•Last updated: Feb 2026
C++ Data Types Code
Example
#include <iostream>
#include <string>
using namespace std;
int main() {
// Fundamental types (same as C + additions)
int age = 25;
double pi = 3.14159;
char grade = 'A';
bool isActive = true; // C++ native bool
string name = "Alice"; // C++ string (not char array!)
// auto keyword (compiler infers type)
auto count = 42; // int
auto price = 9.99; // double
auto greeting = "Hello"s; // string (with s suffix)
// cout: type-safe output (no format specifiers!)
cout << "Name: " << name << ", Age: " << age << endl;
cout << "Pi: " << pi << ", Active: " << boolalpha << isActive << endl;
// cin: type-safe input
cout << "Enter your name: ";
getline(cin, name); // Read full line (including spaces)
int userAge;
cout << "Enter age: ";
cin >> userAge;
// References (alias for existing variable)
int x = 10;
int &ref = x; // ref IS x (not a copy, not a pointer)
ref = 20;
cout << "x = " << x << endl; // 20
// const reference (safe, no copy)
const string &nameRef = name; // Can read, cannot modify
// C++ strings vs C strings
string s1 = "Hello";
string s2 = " World";
string s3 = s1 + s2; // Concatenation with +
cout << s3.length() << endl; // 11
cout << s3.substr(0, 5) << endl; // "Hello"
// Range-based initialization
int arr[] = {1, 2, 3, 4, 5};
for (int val : arr) { // Range-based for loop
cout << val << " ";
}
return 0;
}