System Programming Topic
C++ Data Types, Variables, and I/O
1. Primitive Data Types
Fundamental Types:
cpp
// Integer Types (whole numbers)
int age = 25; // Typically 4 bytes (-2.1B to 2.1B)
short smallNumber = 100; // 2 bytes (-32,768 to 32,767)
long bigNumber = 100000L; // 4 or 8 bytes
long long hugeNumber = 1000000000LL; // 8 bytes
// Floating-Point Types (decimal numbers)
float price = 19.99f; // 4 bytes (6-7 decimal digits precision)
double pi = 3.1415926535; // 8 bytes (15 decimal digits precision)
long double precise = 3.141592653589793238L; // 10-16 bytes
// Character Types
char grade = 'A'; // 1 byte (-128 to 127)
wchar_t wideChar = L'Ω'; // 2 or 4 bytes (wide character)
char16_t utf16Char = u'字'; // 2 bytes (UTF-16)
char32_t utf32Char = U'😀'; // 4 bytes (UTF-32)
// Boolean Type
bool isReady = true; // 1 byte (true or false)
bool isDone = false;
// Void Type
void noReturn(); // No value (used for functions)
Type Ranges and Memory Usage:
| Type | Size (bytes) | Range | Example |
|---|---|---|---|
bool |
1 | true/false | bool flag = true; |
char |
1 | -128 to 127 | char c = 'A'; |
short |
2 | -32,768 to 32,767 | short s = 100; |
int |
4 | -2.1B to 2.1B | int i = 100000; |
long |
4 or 8 | ±9.2×10¹⁸ | long l = 100000L; |
float |
4 | ±3.4×10³⁸ | float f = 3.14f; |
double |
8 | ±1.7×10³⁰⁸ | double d = 3.14159; |
2. sizeof Operator
cpp
#include <iostream>
using namespace std;
int main() {
cout << "Size of data types:" << endl;
cout << "bool: " << sizeof(bool) << " byte" << endl;
cout << "char: " << sizeof(char) << " byte" << endl;
cout << "short: " << sizeof(short) << " bytes" << endl;
cout << "int: " << sizeof(int) << " bytes" << endl;
cout << "long: " << sizeof(long) << " bytes" << endl;
cout << "float: " << sizeof(float) << " bytes" << endl;
cout << "double: " << sizeof(double) << " bytes" << endl;
// Using sizeof with variables
int num = 100;
double value = 3.14;
cout << "\nSize of variables:" << endl;
cout << "num (int): " << sizeof(num) << " bytes" << endl;
cout << "value (double): " << sizeof(value) << " bytes" << endl;
// Size of expressions
cout << "\nSize of expressions:" << endl;
cout << "5 + 3: " << sizeof(5 + 3) << " bytes" << endl;
cout << "3.14 * 2: " << sizeof(3.14 * 2) << " bytes" << endl;
return 0;
}
Output varies by system/compiler:
text
Size of data types: bool: 1 byte char: 1 byte short: 2 bytes int: 4 bytes long: 8 bytes float: 4 bytes double: 8 bytes
3. Type Modifiers
cpp
#include <iostream>
using namespace std;
int main() {
// SIGNED (default - can be positive or negative)
signed int s1 = -100; // Same as int
signed char sc = -128;
// UNSIGNED (only positive values, larger positive range)
unsigned int ui = 4294967295U; // Max value for 4-byte unsigned
unsigned short us = 65535; // 0 to 65,535
unsigned char uc = 255; // 0 to 255
// SIGNED vs UNSIGNED comparison
signed int si = -10;
unsigned int ui2 = 10;
cout << "Signed int: " << si << endl;
cout << "Unsigned int: " << ui2 << endl;
cout << "Max unsigned int: " << ui << endl;
// Mixing signed and unsigned (caution!)
signed int x = -5;
unsigned int y = 10;
cout << "\nMixing signed/unsigned (dangerous):" << endl;
cout << "x + y = " << x + y << endl; // Unexpected result!
return 0;
}
4. Constants (const, constexpr)
cpp
#include <iostream>
using namespace std;
int main() {
// CONST - value cannot be changed after initialization
const double PI = 3.14159;
const int MAX_STUDENTS = 100;
const string COMPANY_NAME = "MyCorp";
// PI = 3.14; // Error: assignment of read-only variable
cout << "PI: " << PI << endl;
cout << "Max Students: " << MAX_STUDENTS << endl;
// const with pointers
int value = 10;
const int* ptr1 = &value; // Pointer to constant int
int* const ptr2 = &value; // Constant pointer to int
const int* const ptr3 = &value; // Constant pointer to constant int
// *ptr1 = 20; // Error: cannot modify through ptr1
// ptr2 = nullptr; // Error: cannot change ptr2 itself
// CONSTEXPR - evaluated at compile time (C++11+)
constexpr double SPEED_OF_LIGHT = 299792458.0;
constexpr int ARRAY_SIZE = 100;
constexpr int SQUARE_OF_5 = 5 * 5;
// constexpr functions (C++11+)
constexpr int square(int x) {
return x * x;
}
constexpr int squaredValue = square(10); // Computed at compile time
cout << "Speed of light: " << SPEED_OF_LIGHT << endl;
cout << "Square of 10: " << squaredValue << endl;
// const vs constexpr
int input;
cout << "Enter a number: ";
cin >> input;
const int runtimeConst = input; // OK - initialized at runtime
// constexpr int compileTimeConst = input; // Error - needs compile-time value
return 0;
}
5. Input & Output (cin, cout)
cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
// Basic Output
cout << "Hello, World!" << endl; // endl adds newline
cout << "Welcome to C++" << '\n'; // '\n' also adds newline
// Basic Input
int age;
string name;
cout << "\nEnter your name: ";
cin >> name; // Reads single word
cout << "Enter your age: ";
cin >> age;
cout << "Hello " << name << ", you are " << age << " years old." << endl;
// Multiple inputs
int x, y, z;
cout << "\nEnter three numbers: ";
cin >> x >> y >> z;
cout << "Sum: " << (x + y + z) << endl;
// Reading strings with spaces
string fullName;
cout << "\nEnter your full name: ";
cin.ignore(); // Clear newline from previous input
getline(cin, fullName); // Reads entire line with spaces
cout << "Full name: " << fullName << endl;
// Character input
char initial;
cout << "\nEnter your initial: ";
cin >> initial;
cout << "Initial: " << initial << endl;
// Mixed input types
int id;
double salary;
char department;
cout << "\nEnter ID, salary, and department: ";
cin >> id >> salary >> department;
cout << "Employee #" << id << " earns $" << salary
<< " in department " << department << endl;
return 0;
}
6. Formatting Output
cpp
#include <iostream>
#include <iomanip> // For formatting
using namespace std;
int main() {
// Setting precision for floating-point
double pi = 3.141592653589793;
cout << "Default: " << pi << endl;
cout << "Fixed (2 decimal): " << fixed << setprecision(2) << pi << endl;
cout << "Scientific: " << scientific << pi << endl;
// Setting width and alignment
cout << "\nText Formatting:" << endl;
cout << setw(15) << left << "Name"
<< setw(10) << right << "Score" << endl;
cout << setw(15) << left << "Alice"
<< setw(10) << right << 95.5 << endl;
cout << setw(15) << left << "Bob"
<< setw(10) << right << 87.25 << endl;
// Integer formatting
int num = 255;
cout << "\nInteger formatting:" << endl;
cout << "Decimal: " << dec << num << endl;
cout << "Hexadecimal: " << hex << num << endl;
cout << "Octal: " << oct << num << endl;
// Reset to decimal
cout << dec;
// Fill character
cout << "\nFill character:" << endl;
cout << setfill('*') << setw(20) << "Hello" << endl;
// Boolean formatting
bool flag = true;
cout << "\nBoolean formatting:" << endl;
cout << "Default: " << flag << endl;
cout << boolalpha << "As text: " << flag << endl;
// Show positive sign
cout << "\nShowing positive sign:" << endl;
cout << showpos << 42 << " " << -42 << endl;
cout << noshowpos; // Turn off
return 0;
}
7. Escape Sequences
cpp
#include <iostream>
using namespace std;
int main() {
// Common Escape Sequences
cout << "Escape Sequences Examples:\n" << endl;
// Newline
cout << "Line 1\nLine 2\nLine 3" << endl;
// Tab
cout << "\nTabbed Output:" << endl;
cout << "Name:\tAlice" << endl;
cout << "Age:\t25" << endl;
cout << "City:\tNew York" << endl;
// Backspace
cout << "\nBackspace: ABC\b\bD" << endl; // Prints "AD" (ABC then backspace twice)
// Carriage return
cout << "Loading: 0%\rLoading: 50%\rLoading: 100%" << endl;
// Backslash and quotes
cout << "\nPath: C:\\Users\\Name\\Documents" << endl;
cout << "Quote: She said, \"Hello!\"" << endl;
cout << "Single quote: It\'s raining" << endl;
// Alert (beep)
cout << "\aAlert! (You might hear a beep)" << endl;
// Vertical tab and form feed
cout << "Vertical\vTab\vExample" << endl;
// Unicode/Octal/Hexadecimal characters
cout << "\nSpecial characters:" << endl;
cout << "Euro symbol: \u20AC" << endl; // Unicode
cout << "Copyright: \xA9" << endl; // Hexadecimal
cout << "Smiley: \1" << endl; // Octal (may not work on all systems)
// Raw string literals (C++11) - no escape sequences processed
cout << "\nRaw string literal:" << endl;
cout << R"(Path: C:\Users\Name\Documents\nNo newline here)" << endl;
// Multi-line string
cout << "\nMulti-line output:" << endl;
cout << "Line 1\n"
<< "Line 2\n"
<< "Line 3" << endl;
return 0;
}
8. Complete Example Combining Everything
cpp
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
constexpr double TAX_RATE = 0.07;
const int MAX_ITEMS = 10;
int main() {
// Variable declarations with different types
string itemName;
int quantity;
double price, subtotal, tax, total;
// Input with formatting
cout << "=== SHOPPING CART ===\n" << endl;
cout << "Enter item name: ";
getline(cin, itemName);
cout << "Enter quantity: ";
cin >> quantity;
cout << "Enter price per item: $";
cin >> price;
// Calculations
subtotal = quantity * price;
tax = subtotal * TAX_RATE;
total = subtotal + tax;
// Formatted output
cout << fixed << setprecision(2);
cout << "\n" << string(30, '=') << endl;
cout << "RECEIPT" << endl;
cout << string(30, '-') << endl;
cout << left << setw(15) << "Item:" << itemName << endl;
cout << left << setw(15) << "Quantity:" << quantity << endl;
cout << left << setw(15) << "Unit Price:" << "$" << price << endl;
cout << left << setw(15) << "Subtotal:" << "$" << subtotal << endl;
cout << left << setw(15) << "Tax (" << TAX_RATE * 100 << "%):" << "$" << tax << endl;
cout << left << setw(15) << "Total:" << "$" << total << endl;
cout << string(30, '=') << endl;
// Using sizeof
cout << "\nMemory usage:" << endl;
cout << "int: " << sizeof(int) << " bytes" << endl;
cout << "double: " << sizeof(double) << " bytes" << endl;
cout << "string: " << sizeof(string) << " bytes" << endl;
return 0;
}
Comments (0)
No comments yet. Be the first to comment!