-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvariable.cpp
More file actions
162 lines (110 loc) · 5.43 KB
/
Copy pathvariable.cpp
File metadata and controls
162 lines (110 loc) · 5.43 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <iostream> //all preprocessor start with "#"" and does not eand with ";"
using namespace std;
//global variable
int g1; // global default initialization → 0
int g2 = 10; // global copy initialization
static int g3; // global static → 0
int main()
{
// ========================================================
// 🔷 LOCAL VARIABLES
// ========================================================
int a; // local default initialization → garbage value
static int b; // local static → 0
// ========================================================
// 🔷 INITIALIZATION TYPES
// ========================================================
int c = 10; // copy initialization
int d(20); // direct initialization
int e{30}; // list initialization (no narrowing)
int f{}; // value initialization → 0
int g = {40}; // copy list initialization
int h = int(50); // temporary + copy init
int i = int{}; // value init via temporary → 0
// ========================================================
// 🔷 CONSTANT VARIABLES (NAMED CONSTANTS)
// ========================================================
const int MAX1 = 100; // constant variable (datatype CONST_NAME)
int const MAX2 = 200; // constant variable (const datatype NAME)
// ========================================================
// 🔷 LITERAL CONSTANTS
// ========================================================
int intLiteral = 10; // integer literal
int hexLiteral = 0x1A; // hexadecimal literal
int octLiteral = 012; // octal literal
float floatLiteral = 3.14f; // float literal
double doubleLiteral = 3.14; // double literal
char charLiteral = 'A'; // character literal
bool boolLiteral = true; // boolean literal
const char *strLiteral = "Hello"; // string literal
// ========================================================
// 🔷 TYPE CONVERSION
// ========================================================
double d1 = 10; // implicit conversion → int to double
int x = 10, y = 3;
double d2 = x / y; // implicit but integer division first → result 3
// ---------- Explicit Conversion ----------
double d3 = (double)x / y; // C-style cast (cast notation)
double d4 = double(x) / y; // function notation
double d5 = static_cast<double>(x) / y; // named cast (C++ style, safest)
//try as operator as well
// ========================================================
// 🔷 AUTO & DECLTYPE
// ========================================================
auto a1 = 10; // auto → int
auto a2 = 3.14; // auto → double
decltype(a2) a3 = 5.5; // decltype → same type as a2 (double)
// ========================================================
// 🔷 ARRAYS
// ========================================================
int arr1[3]; // garbage values
int arr2[3]{}; // all 0
int arr3[3]{1, 2, 3}; // list initialized
// ========================================================
// 🔷 COMMA OPERATOR
// ========================================================
int z = (x = 5, y = 10, x + y); // evaluates left→right, result = last expression (15)
// ========================================================
// 🔷 LVALUE & RVALUE
// ========================================================
int a = 5; // 'a' is lvalue (has memory location)
int b = a + 10; // (a + 10) is rvalue (temporary)
int &ref = a; // lvalue reference → binds to lvalue
int &&rref = 20; // rvalue reference → binds to temporary
// int &&wrong = a; // ❌ error → rvalue ref cannot bind to lvalue
// ========================================================
// 🔷 REFERENCE VARIABLE (&)
// → alias of existing variable (must be initialized)
// ========================================================
int a = 10;
int &ref = a; // reference → ref is another name for a
ref = 20; // modifies 'a'
// ========================================================
// 🔷 POINTER VARIABLE (*)
// → stores address, can be reassigned
// ========================================================
int b = 30;
int *ptr = &b; // pointer to b
*ptr = 40; // modifies b
// ========================================================
// 🔷 OUTPUT (SAFE VARIABLES ONLY)
// ========================================================
cout << "Global g1: " << g1 << endl;
cout << "Global g2: " << g2 << endl;
cout << "Static g3: " << g3 << endl;
cout << "b (static local): " << b << endl;
cout << "c,d,e,f: " << c << " " << d << " " << e << " " << f << endl;
cout << "Constants: " << MAX1 << " " << MAX2 << endl;
cout << "Literals: " << intLiteral << " " << floatLiteral << " " << charLiteral << endl;
cout << "Conversions: " << d1 << " " << d2 << " " << d3 << " " << d5 << endl;
cout << "Auto/Decltype: " << a1 << " " << a2 << " " << a3 << endl;
cout << "Comma operator result: " << z << endl;
cout << ref << " " << rref << endl;
ref = 20; // modifies 'a'
cout << a << endl; // 20
cout << b << endl; // 40
int c = 50;
ptr = &c; // pointer can point to another variable
cout << *ptr << endl; // 50
return 0;
}