Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C - isgraph() function

Tags:

c

Does anyone know how the isgraph() function works in C? I understand its use and results, but the code behind it is what I'm interested in.

For example, does it look at only the char value of it and compare it to the ASCII table? Or does it actually check to see if it can be displayed? If so, how?

like image 885
Rubber Duck Avatar asked Feb 25 '23 22:02

Rubber Duck


1 Answers

The code behind the isgraph() function varies by platform (or, more precisely, by implementation). One common technique is to use an initialized array of bit-fields, one per character in the (single-byte) codeset plus EOF (which has to be accepted by the functions), and then selecting the relevant bit. This allows for a simple implementation as a macro which is safe (only evaluates its argument once) and as a simple (possibly inline) function.

#define isgraph(x) (__charmap[(x)+1]&__PRINT)

where __charmap and __PRINT are names reserved for the implementation. The +1 part deals with the common situation where EOF is -1.


According to the C standard (ISO/IEC 9899:1999):

§7.4.1.6 The isgraph function

Synopsis

#include <ctype.h>
int isgraph(int c);

Description

The isgraph function tests for any printing character except space (' ').

And:

§7.4 Character handling <ctype.h>

¶1 The header declares several functions useful for classifying and mapping characters.166) In all cases the argument is an int, the value of which shall be representable as an unsigned char or shall equal the value of the macro EOF. If the argument has any other value, the behavior is undefined.

¶2 The behavior of these functions is affected by the current locale. Those functions that have locale-specific aspects only when not in the "C" locale are noted below.

¶3 The term printing character refers to a member of a locale-specific set of characters, each of which occupies one printing position on a display device; the term control character refers to a member of a locale-specific set of characters that are not printing characters.167) All letters and digits are printing characters.

166) See ‘‘future library directions’’ (7.26.2).

167) In an implementation that uses the seven-bit US ASCII character set, the printing characters are those whose values lie from 0x20 (space) through 0x7E (tilde); the control characters are those whose values lie from 0 (NUL) through 0x1F (US), and the character 0x7F (DEL).

like image 62
Jonathan Leffler Avatar answered Mar 12 '23 14:03

Jonathan Leffler