11#include < iostream>
22
3- using namespace std ;
3+ using std::cout;
4+ using std::endl;
5+
6+ bool bglob;
7+ static bool bsglob;
8+ const bool bcglob{true };
9+ const static bool bcsglob{true };
10+ auto abglob{true };
11+
12+ char cglob;
13+ static char csglob;
14+ const char ccglob{34 };
15+ const static char ccsglob{34 };
16+ auto acglob{34 };
417
518int iglob;
619static int isglob;
720const int icglob{123 };
821const static int icsglob{234 };
9- auto aglob{345 };
22+ auto aiglob{345 };
23+
24+ float fglob;
25+ static float fsglob;
26+ const float fcglob{12.3 };
27+ const static float fcsglob{23.4 };
28+ auto afglob{34.5 };
1029
1130double dglob;
1231static double dsglob;
1332const double dcglob{12.3 };
1433const static double dcsglob{23.4 };
1534auto adglob{34.5 };
1635
17- // The function doesn't work because global variables sent with copy of value
18- // template <typename T> inline void print_vars(string name, T var, T * pvar, T & rvar)
19- // {
20- // cout << "Value of " << name << ": " << var << endl;
21- // cout << "Address of " << name << ": " << &var << endl;
22- // cout << "Value of p" << name << ": " << pvar << endl;
23- // cout << "Value of p" << name << "+1: " << pvar+1 << endl;
24- // cout << "Address of p" << name << ": " << &pvar << endl;
25- // cout << "Value of variable with address p" << name << ": " << *pvar << endl;
26- // cout << "Size of " << name << ": " << sizeof(var) << endl;
27- // cout << "Size of p" << name << ": " << sizeof(pvar) << endl;
28- // cout << "Size of r" << name << ": " << sizeof(rvar) << endl;
29- // cout << "Address of r" << name << ": " << &rvar << endl;
30- // cout << "Value of r" << name << ": " << rvar << endl;
31- // }
36+ template <typename T>
37+ void print_variable_info (const T& var, const std::string& name = " " ) {
38+ std::string prefix = (name.empty ()) ? " " : (" [" + name + " ] " );
3239
40+ std::cout << " \n " ;
41+ std::cout << prefix << " Value: " << var << " \n " ;
42+ std::cout << prefix << " Address: " << &var << " \n " ; // Q: why it won't print address of var with type "char"? what is needed to do?
43+ std::cout << prefix << " Type: " << typeid (var).name () << " \n " ;
44+ std::cout << prefix << " Sizeof: " << sizeof (var) << std::endl;
45+ }
46+
47+ template <typename T>
48+ void print_variable_info (const T* var, const std::string& name = " " ) {
49+ std::string prefix = (name.empty ()) ? " " : (" [" + name + " ] " );
50+
51+ std::cout << " \n " ;
52+ std::cout << prefix << " Value: " << var << " \n " ;
53+ std::cout << prefix << " Address: " << &var << " \n " ; // Q: why it won't print address of var with type "char"? what is needed to do?
54+ std::cout << prefix << " Type: " << typeid (var).name () << " \n " ;
55+ std::cout << prefix << " Sizeof: " << sizeof (var) << std::endl;
56+ }
3357
3458int main ()
3559{
3660
61+ bool * pbglob = &bglob;
62+ bool * pbsglob = &bsglob;
63+ const bool * pbcglob = &bcglob;
64+ const bool * pbcsglob = &bcsglob;
65+
66+ bool &rbglob = bglob;
67+ bool &rbsglob = bsglob;
68+ const bool &rbcglob = bcglob;
69+ const bool &rbcsglob = bcsglob;
70+
71+ char * pcglob = &cglob;
72+ char * pcsglob = &csglob;
73+ const char * pccglob = &ccglob;
74+ const char * pccsglob = &ccsglob;
75+
76+ char &rcglob = cglob;
77+ char &rcsglob = csglob;
78+ const char &rccglob = bcglob;
79+ const char &rccsglob = bcsglob;
80+
3781 int iloc;
3882 static int isloc;
3983 const int icloc{654 };
@@ -43,14 +87,14 @@ int main()
4387 int * pisglob = &isglob;
4488 const int * picglob = &icglob;
4589 const int * picsglob = &icsglob;
46- auto *paglob=&aglob;
90+ // auto *paglob=&aglob;
4791
4892
4993 int &riglob = iglob;
5094 int &risglob = isglob;
5195 const int &ricglob = icglob;
5296 const int &ricsglob = icsglob;
53- auto &raglob=aglob;
97+ // auto &raglob=aglob;
5498
5599 int * piloc = &iloc;
56100 int * pisloc = &isloc;
@@ -82,102 +126,23 @@ int main()
82126 cout << " c - const" << endl;
83127 cout << " s - static" << endl;
84128 cout << " glob - global" << endl;
85- cout << " loc - local" << endl << endl ;
129+ cout << " loc - local" << endl;
86130
87131 cout << " ---------------------------------------------------------------------" << endl;
88132
89- cout << " Value of iglob: " << iglob << endl;
90- cout << " Address of iglob: " << &iglob << endl;
91- cout << " Value of piglob: " << piglob << endl;
92- cout << " Value of piglob+1: " << piglob+1 << endl;
93- cout << " Address of piglob: " << &piglob << endl;
94- cout << " Value of variable with address piglob: " << *piglob << endl;
95- cout << " Size of iglob: " << sizeof (iglob) << endl;
96- cout << " Size of piglob: " << sizeof (piglob) << endl;
97- cout << " Size of riglob: " << sizeof (riglob) << endl;
98- cout << " Address of riglob: " << &riglob << endl;
99- cout << " Value of riglob: " << riglob << endl;
100- riglob=545 ;
101- cout << " The reference is changed to 545" << endl;
102- cout << " Value of iglob: " << iglob << endl;
103- cout << " Value of riglob: " << riglob << endl;
133+ print_variable_info<bool >(bglob," bglob" );
134+ print_variable_info<bool >(pbglob," pbglob" );
135+ print_variable_info<bool >(rbglob," rbglob" );
104136
105- cout << endl;
137+ print_variable_info<int >(iglob," iglob" );
138+ print_variable_info<int >(piglob," piglob" );
139+ print_variable_info<int >(riglob," riglob" );
106140
107- cout << " Value of isglob: " << isglob << endl;
108- cout << " Address of isglob: " << &isglob << endl;
109- cout << " Value of pisglob: " << pisglob << endl;
110- cout << " Value of pisglob+1: " << pisglob+1 << endl;
111- cout << " Address of pisglob: " << &pisglob << endl;
112- cout << " Value of variable with address pisglob: " << *pisglob << endl;
113- cout << " Size of isglob: " << sizeof (isglob) << endl;
114- cout << " Size of pisglob: " << sizeof (pisglob) << endl;
115- cout << " Size of risglob: " << sizeof (risglob) << endl;
116- cout << " Address of risglob: " << &risglob << endl;
117- cout << " Value of risglob: " << risglob << endl;
118- risglob=656 ;
119- cout << " The reference is changed to 656" << endl;
120- cout << " Value of isglob: " << isglob << endl;
121- cout << " Value of risglob: " << risglob << endl;
141+ print_variable_info<char >(cglob," cglob" );
142+ print_variable_info<char >(pcglob," pcglob" );
143+ print_variable_info<char >(rcglob," rcglob" );
122144
123- cout << endl;
124-
125- cout << " Value of icglob: " << icglob << endl;
126- cout << " Address of icglob: " << &icglob << endl;
127- cout << " Value of picglob: " << picglob << endl;
128- cout << " Value of picglob+1: " << picglob+1 << endl;
129- cout << " Address of picglob: " << &picglob << endl;
130- cout << " Value of variable with address picglob: " << *picglob << endl;
131- cout << " Size of icglob: " << sizeof (icglob) << endl;
132- cout << " Size of picglob: " << sizeof (picglob) << endl;
133- cout << " Size of ricglob: " << sizeof (ricglob) << endl;
134- cout << " Address of ricglob: " << &ricglob << endl;
135- cout << " Value of ricglob: " << ricglob << endl;
136- // ricglob=741;
137- cout << " The reference can't be changed because it is const" << endl;
138- cout << " Value of icglob: " << icglob << endl;
139- cout << " Value of ricglob: " << ricglob << endl;
140-
141- cout << endl;
142-
143- cout << " Value of icsglob: " << icsglob << endl;
144- cout << " Address of icsglob: " << &icsglob << endl;
145- cout << " Value of picsglob: " << picsglob << endl;
146- cout << " Value of picsglob+1: " << picsglob+1 << endl;
147- cout << " Address of picsglob: " << &picsglob << endl;
148- cout << " Value of variable with address picsglob: " << *picsglob << endl;
149- cout << " Size of icsglob: " << sizeof (icsglob) << endl;
150- cout << " Size of picsglob: " << sizeof (picsglob) << endl;
151- cout << " Size of ricsglob: " << sizeof (ricsglob) << endl;
152- cout << " Address of ricsglob: " << &ricsglob << endl;
153- cout << " Value of ricsglob: " << ricsglob << endl;
154- // ricglob=852;
155- cout << " The reference can't be changed because it is const" << endl;
156- cout << " Value of icsglob: " << icsglob << endl;
157- cout << " Value of ricsglob: " << ricsglob << endl;
158-
159- cout << endl;
160-
161- cout << " Value of aglob: " << aglob << endl;
162- cout << " Address of aglob: " << &aglob << endl;
163- cout << " Value of paglob: " << paglob << endl;
164- cout << " Value of paglob+1: " << paglob+1 << endl;
165- cout << " Address of paglob: " << &paglob << endl;
166- cout << " Value of variable with address paglob: " << *paglob << endl;
167- cout << " Size of aglob: " << sizeof (aglob) << endl;
168- cout << " Size of paglob: " << sizeof (paglob) << endl;
169- cout << " Size of raglob: " << sizeof (raglob) << endl;
170- cout << " Address of raglob: " << &raglob << endl;
171- cout << " Value of raglob: " << raglob << endl;
172- cout << " Type of auto variable is - " << typeid (aglob).name () << endl;
173- cout << " Type of auto pointer is - " << typeid (paglob).name () << endl;
174- cout << " Type of auto reference is - " << typeid (raglob).name () << endl;
175- raglob=963 ;
176- cout << " The reference is changed to 963" << endl;
177- cout << " Value of isglob: " << aglob << endl;
178- cout << " Value of risglob: " << raglob << endl;
179-
180- cout << endl;
145+
181146
182147 cout << " ---------------------------------------------------------------------" << endl;
183148 cout << " Value of iloc: " << iloc << endl;
@@ -366,7 +331,7 @@ int main()
366331 // unsigned int * uipointer = &iglob;
367332 // error: invalid conversion from ‘int*’ to ‘unsigned int*’
368333
369- cout << endl << " ---------------------------------------------------------------------" << endl;
334+ // cout << endl << "---------------------------------------------------------------------" << endl;
370335
371336 // int * npointer{nullptr};
372337 // There is no compilation error
@@ -378,18 +343,19 @@ int main()
378343 // }
379344 // ERROR: Segmentation fault
380345
381- cout << endl << " ---------------------------------------------------------------------" << endl;
346+ // cout << endl << "---------------------------------------------------------------------" << endl;
382347
383348 // int * somepointer;
384349
385350 // somepointer=56;
386351 // cout << "somepointer value = " << somepointer << ", value from pointer = " << *somepointer << endl;
387352 // error: invalid conversion from ‘int’ to ‘int*’ [-fpermissive]
388353
389- cout << endl << " ---------------------------------------------------------------------" << endl;
354+ // / cout << endl << "---------------------------------------------------------------------" << endl;
390355
391356 // iglob="Some string.";
392357 // ERROR: error: invalid conversion from ‘const char*’ to ‘int’ [-fpermissive]
393358
359+ std::cout << std::endl;
394360 return 0 ;
395361}
0 commit comments