isupper macro defined in ctype.h

T

Tagore

I checked ctype.h header file to see how isupper macro is defined in
it. I foung following implementation:
#define isupper(c) (_ctype[(c) + 1] & _IS_UPP)

where _IS_UPP is defined as:
#define _IS_UPP 4

please help me in understanding working of it.

Actually I was trying to define my own macro for isupper as
#define isupper(c) (((c) >= 'A') && ((c) <= 'Z')))
but it would create problem if I call it as isupper(ch++)..
so I tried to understand how it is defined in ctype.h and could not
understand its working.
 
L

Lew Pitcher

I checked ctype.h header file to see how isupper macro is defined in
it. I foung following implementation:
#define isupper(c) (_ctype[(c) + 1] & _IS_UPP)

where _IS_UPP is defined as:
#define _IS_UPP 4

please help me in understanding working of it.

Your implementation evidently uses a fixed array to store the testable
attributes of char entities. Each array element stores the attributes of
one single char value, with each attribute coded as a single bit within the
array element. Thus, for any char value, the array element selected by that
value will contain flags that indicate whether or not the corresponding
char value is alphabetic, numeric, special character, space, upper case,
ascii, a control character, punctuation, hexidecimal digit, etc.

For example
the array element
_ctype['A'] would contain a value that would indicate that 'A' is
alphanumeric
alphabetic
graphic
upper case
printable
a hexidecimal digit
while
_ctype['0'] would contain a value that would indicate that '0; is
alphanumeric
digit
graphic
printable
a hexidecimal digit
Actually I was trying to define my own macro for isupper as
#define isupper(c) (((c) >= 'A') && ((c) <= 'Z')))
but it would create problem if I call it as isupper(ch++)..

And, it doesn't work properly with
a) EBCDIC variant charactersets, or
b) non-latin charactersets (i.e. greek, russian, kata-kana)
so I tried to understand how it is defined in ctype.h and could not
understand its working.

--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
 
B

Ben Bacarisse

Richard Heathfield said:
Tagore said:
I checked ctype.h header file to see how isupper macro is defined
in it. I foung following implementation:
#define isupper(c) (_ctype[(c) + 1] & _IS_UPP)

Yes, that would work, provided EOF is defined as -1 and _ctype[0] is
EOF. It isn't the only way isupper can be defined, however.

Looks like finger trouble: If EOF is -1, _ctype[0] should 0 not EOF.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top