#include <iostream>
using std::cout;
using std::endl;
int a = 0; // global a
int main()
{
cout << "a = " << a << endl; // global `a'
cout << "::a = " << ::a << endl; // global `a'
int a = 1; // hides global `a'
cout << "a = " << a << endl; // local `a' (main)
cout << "::a = " << ::a << endl; // global `a'
{ // inner scope
int a = 2; // hides global `a' and `a' from main()
cout << "a = " << a << endl; // local `a' (inner scope)
cout << "::a = " << ::a << endl; // global `a'
}
cout << "a = " << a << endl; // local `a' (main)
cout << "::a = " << ::a << endl; // global `a'
return 0;
}
/*
g++ Scope.cpp -o Scope
./Scope
a = 0
::a = 0
a = 1
::a = 0
a = 2
::a = 0
a = 1
::a = 0
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <stdio.h> // for printf()
#include <stdbool.h> // for bool, false, true
// /usr/lib/gcc/x86_64-linux-gnu/9/include/stdbool.h
int main()
{
char c = false; // no error, no warning
bool b = false;
// b = 0; // no error, no warning
printf("(int) c = %d\n", (int) c); // 0
printf("(bool) b = %d\n", (bool) b); // 0
// b--; // compile error
c++;
b++; // no warning
// c = true; // no error, no warning
printf("(int) c = %d\n", (int) c); // 1
printf("(bool) b = %d\n", (bool) b); // 1
c++;
b++; // no warning
// b = -10; // no error, no warning, but `b' remains 1
// b = 10; // no error, no warning, but `b' remains 1
// c = true + 10; // c == 11
printf("(int) c = %d\n", (int) c); // 2
printf("(bool) b = %d\n", (bool) b); // 1
b = 10; // b == 1
printf("(bool) c = %d\n", (bool) c); // 1
printf("(int) b = %d\n", (int) b); // 1
c = 65, b = 0;
printf("c = '%c'\n", c); // 'A' (ASCII 65)
printf("(int) c = %d\n", (int) c); // 65
printf("(bool) c = %d\n", (bool) c); // 1
printf("(int) b = %d\n", (int) b); // 0
printf("(bool) b = %d\n", (bool) b); // 0
return 0;
}
/*
gcc types.c -o types
./types
(int) c = 0
(bool) b = 0
(int) c = 1
(bool) b = 1
(int) c = 2
(bool) b = 1
(bool) c = 1
(int) b = 1
c = 'A'
(int) c = 65
(bool) c = 1
(int) b = 0
(bool) b = 0
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <iostream>
using std::cout;
using std::endl;
int main()
{
char c = false; // no error, no warning
bool b = false;
// b = 0; // no error, no warning
cout << "(int) c = " << (int) c << endl; // 0
cout << "(bool) b = " << (bool) b << endl; // 0
// b--; // compile error
c++;
b++; // warning: deprecated
// c = true; // no error, no warning
cout << "(int) c = " << (int) c << endl; // 1
cout << "(bool) b = " << (bool) b << endl; // 1
c++;
b++; // warning: deprecated
// b = -10; // no error, no warning, but `b' remains 1
// b = 10; // no error, no warning, but `b' remains 1
// c = true + 10; // c == 11
cout << "(int) c = " << (int) c << endl; // 2
cout << "(bool) b = " << (bool) b << endl; // 1
b = 10; // b == 1
cout << "(bool) c = " << (bool) c << endl; // 1
cout << "(int) b = " << (int) b << endl; // 1
c = 65, b = 0;
cout << "c = " << '\'' << c << "\'" << endl; // 'A' (ASCII 65)
cout << "(int) c = " << (int) c << endl; // 65
cout << "(bool) c = " << (bool) c << endl; // 1
cout << "(int) b = " << (int) b << endl; // 0
cout << "(bool) b = " << (bool) b << endl; // 0
return 0;
}
/*
g++ Types.cpp -o Types
Types.cpp: In function ‘int main()’:
Types.cpp:14:4: warning: use of an operand of type ‘bool’ in
‘operator++’ is deprecated [-Wdeprecated]
14 | b++; // warning: deprecated
| ^~
Types.cpp:20:4: warning: use of an operand of type ‘bool’ in
‘operator++’ is deprecated [-Wdeprecated]
20 | b++; // warning: deprecated
| ^~
./Types
(int) c = 0
(bool) b = 0
(int) c = 1
(bool) b = 1
(int) c = 2
(bool) b = 1
(bool) c = 1
(int) b = 1
c = 'A'
(int) c = 65
(bool) c = 1
(int) b = 0
(bool) b = 0
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <iostream>
using std::cout;
using std::endl;
int main() // initializations
{
int a = 0;
int b(1);
int c{2};
cout << "a = " << a << ", b = " << b << ", c = " << c << endl;
return 0;
}
/*
g++ Init.cpp -o Init
./Init
a = 0, b = 1, c = 2
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <iostream>
using std::cout;
using std::endl;
int main() // references and pointers
{
int i = 0;
int &ri = i;
int *pi = &i;
cout << "i = " << i << ", ri = " << ri << ", *pi = " << *pi << endl;
i++;
cout << "i = " << i << ", ri = " << ri << ", *pi = " << *pi << endl;
ri++;
cout << "i = " << i << ", ri = " << ri << ", *pi = " << *pi << endl;
(*pi)++;
cout << "i = " << i << ", ri = " << ri << ", *pi = " << *pi << endl;
return 0;
}
/*
g++ Ref.cpp -o Ref
./Ref
i = 0, ri = 0, *pi = 0
i = 1, ri = 1, *pi = 1
i = 2, ri = 2, *pi = 2
i = 3, ri = 3, *pi = 3
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************