Reputation: 19
I've read in Stroustrup PPP "Programming Principles and Practice Using C++", 2nd edition in table "useful operators for some common and useful types" here
that type "char" actually does not support operator ++ (or operator ++ actually does not work with type "char"), but that is so strange. I tested in small program and figured out, that char works with ++ there:
#include <iostream>
using namespace std;
int main() {
char ch=24;
cout << "Enter ch: ";
cin >> ch; cout << "ch: " << ch << endl;
ch=ch+1; cout << "ch + 1: " << ch << endl;
ch++; cout << "ch++: " << ch << endl;
ch--; cout << "ch--: " << ch << endl;
return 0;
}
$
$ g++ 2.cpp
$ ./a.out
Enter ch: g
ch: g
ch + 1: h
ch++: i
ch--: h
$ g++ --version
g++ (Debian 12.2.0-14) 12.2.0
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Here I enter char "g" to variable "ch", then add ch+1; and then increment ch++; and finally get char "i", then I decrement ch--; and get from "i" char "h", so it works!
Could you, please, explain, do standards of C++11 and C++14 support operators "++" and "--" for type char? g++ 12.2.0-14 actually supports. But why does Stroustrup wrote that C++ does not support?
typed program, search standards C++ in the web and could not find operators ant types.
Upvotes: 0
Views: 108