|
To use the predicates in the following table you will need to include <cctype>.
Note: Predicates such as isdigit and isalpha can return integer results other than 0 or 1, with 0 meaning false and any other integer meaning true. The coding standard requiring all tests to yield either true or false are relaxed for these expressions. |
Note: Functions such as isdigit and isalpha are actually pseudo-functions. They are converted into expressions that select a value from an array, so that no function call is done for them. It is important for an array index not to be negative. So convert a character to type unsigned or unsigned char to use it as a parameter to these predicates. |
c − '0'
Use this to convert a digit to the number that it represents. For example, '8' − '0' yields 8. |
d + '0'
Use this to convert a single-digit number d to a character. For example, 8 + '0' yields '8'. |
c − ('a' − 'A')
This is the result of converting letter c from lower case to upper case. For example, 'q' − ('a' − 'A') yields 'Q'. This only works if c is a lower case letter. |
c + ('a' − 'A')
This is the result of converting letter c from upper case to lower case. For example, 'Q' + ('a' − 'A') yields 'q'. This only works if c is an upper case letter. |
(int)(c)
This is character c treated as having type int. |
(char)(n)
This is integer n treated as having type char. |
This is nonzero if c is a digit. |
This is nonzero if c is a letter |
This is nonzero if c is an upper-case letter |
This is nonzero if c is a lower-case letter |
This is nonzero if c is a white-space character (space, tab, newline, carriage-return, etc.). |
isprint((unsigned) c) is mostly
nonzero if character c can be printed.
For example, isprint('x') is nonzero,
but isprint('\0') is 0.
But isprint has a peculiarity. Isprint(c) is nonzero if c is a space, but is 0 for other characters, such as '\n' and '\t', where isspace(c) is nonzero. |
Write an expresion that is true (or nonzero) if character c is a letter. Answer
Write an expresion that is true if character c is an upper case letter. Answer
Suppose that c is a digit (one of the characters '0', '1', ..., '9'). Write an expression that yields the integer that corresponds to that digit. For example, if c is '4' then your expression should yield 4. Answer
Look at the coding standards for writing constants. Suppose that c has type char. Is
if(c == 10) { … }acceptable by the coding standards? If not, how should it be written? Answer
|