ch2-Types
Chapter_2 Scope | Init |
CONTENTS: types.c Types.cpp
types.c CS, p. 7-8
#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
*/
Types.cpp
#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
*/
Chapter_2 Scope | BACK_TO_TOP | Init |
Comments
Post a Comment