How many times will the following loop execute?
unsigned char half_limit = 150;
for (unsigned char i = 0; i < 2 * half_limit; ++i)
{
// do something;
}
An infinite loop is a runtime error, so I was still technically right.
Also, my old C++ textbook, published in 2007, states that char can only be used to store a single symbol; a letter, a number, etc. It also states that char is effectively a single symbol string... and you can't perform arithmetic on strings.
However, it does not do variable declarations as unsigned char... just as char.
So, it looks like this allowance of using char to hold an integer value is a new addition to C++, at least new compared to C++ used in 2007.
For reference: (how the book does it)
char symbol, letter;
symbol = 'a';
letter = '1';
A runtime error would be something like a memory access violation that causes the program to terminate abnormally or crash. That might happen from overstepping the bounds of an array (usually by a lot). You might also use the term when talking about exceptions, particularly the std::runtime_error exception. Exceptions could be caught and handled, preventing a crash, though default behaviour if uncaught is to terminate the program with an error message.
An infinite loop may be intentional (and hence not an error). It may also be a logic error. The program operates abnormally, but doesn't crash.
char can only be used to store a single symbol; a letter, a number, etc.
Exactly, a char can hold a number. You can perform arithmetic on numbers. You can not perform arithmetic on arrays of numbers. A string would be an array of char.
Using the single quotes actually creates a numeric literal. The value relates to the ASCII (http://www.asciitable.com/) values of the characters. You can assign such numbers to an int as well.
int value = 'a';
std::cout << a << std::endl; // 97