I'm trying to understand a piece of code which uses __ctype_b_loc()
, problem is that I don't know what is the purpose of this function.
So far, I found it is defined in the ctype.h
. I also found its prototype and an implementation. Still I have no idea of what this function is for.
Can someone enlight me?
Alessandro's own answer is very informative, but I would like to add some information.
As stated by Alessandro, the
__ctype_b_loc(void)
function returns an array where each element contains the features of one ASCII character. For instance by looking up in the table we can learn that the character 'A' is uppercase, hexadecimal, graphical, printing, alphanumeric.To be precise, the
__ctype_b_loc()
function returns aconst unsigned short int**
which is a pointer to an array of 384unsigned short int*
. The reason there ara 384 elements is so the table can be indexed by:unsigned char
value [0,255] (so 256 elements)signed char
value [-128,-1) (so 127 elements)This table is used by the functions :
However these functions are defined as macros, so you will never see them called in an assembly code. What you will see is a call to
__ctype_b_loc()
to get the table, some code to retrieve the correct entry and the usage of a bit mask to see if the property we are checking is set. For instance if we want to see if a character is uppercase, we have to check if the bit 0 is set.Here is the assembly code generated by calling
isupper('A');
:After a fair research, I think I can answer myself this question.
unsigned short int __ctype_b_loc (void)
is a function which returns a pointer to a 'traits' table containing some flags related with the characteristics of each single character.
Here's the enum with the flags:
From
ctype.h
To make an example, if you make a lookup to the table
__ctype_b_loc()
returns for the character whose ascii code is0x30
('0
') you will have0x08d8
.The table is connected with the
localchar
of the locale installed on the machine, so the example might not be accurate, compared with results you may have on your system.