-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata-type.cpp
More file actions
48 lines (41 loc) · 2.39 KB
/
Copy pathdata-type.cpp
File metadata and controls
48 lines (41 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <string> // Recommended for explicit string usage
using namespace std;
int main() {
// ===================================================
// 1. STANDARD DATA TYPES (Explicit Type Declaration)
// ===================================================
// Declaring variables by explicitly specifying their data type.
int myNum = 5; // Integer: stores whole numbers (4 bytes)
float myFloatNum = 5.99f; // Float: stores fractional numbers, up to 7 decimal digits (4 bytes)
double myDoubleNum = 9.98; // Double: stores fractional numbers, up to 15 decimal digits (8 bytes)
char myLetter = 'D'; // Character: stores a single character/letter in single quotes (1 byte)
bool myBoolean = true; // Boolean: stores true (1) or false (0) (1 byte)
string myText = "Hello"; // String: stores text wrapped in double quotes
cout << "=== STANDARD DATA TYPES ===" << endl;
cout << "Integer (int) : " << myNum << endl;
cout << "Float (float) : " << myFloatNum << endl;
cout << "Double (double) : " << myDoubleNum << endl;
cout << "Character (char) : " << myLetter << endl;
cout << "Boolean (bool) : " << myBoolean << " (true prints as 1)" << endl;
cout << "String (string) : " << myText << endl;
// ===================================================
// 2. AUTOMATIC TYPE DEDUCTION (`auto` Keyword)
// ===================================================
// Introduced in C++11. The compiler automatically deduces the type
// based on the assigned literal value.
auto autoNum = 5; // Deduced as: int
auto autoFloat = 5.99f; // Deduced as: float (note the 'f' suffix)
auto autoDouble = 9.98; // Deduced as: double (default for decimals)
auto autoLetter = 'D'; // Deduced as: char
auto autoBool = true; // Deduced as: bool
auto autoString = string("Hello"); // Deduced as: std::string
cout << "\n=== AUTO TYPE DEDUCTION ===" << endl;
cout << "autoNum (int) : " << autoNum << endl;
cout << "autoFloat (float) : " << autoFloat << endl;
cout << "autoDouble (double) : " << autoDouble << endl;
cout << "autoLetter (char) : " << autoLetter << endl;
cout << "autoBool (bool) : " << autoBool << endl;
cout << "autoString (string) : " << autoString << endl;
return 0;
}