Wednesday, July 29, 2026
HomeC ProgrammingCtype Capabilities: isascii() and iscntrl()

Ctype Capabilities: isascii() and iscntrl()



I virtually consider these two capabilities to be ineffective, however they do play an essential position within the ctype pantheon: isascii() determines whether or not an integer worth is an ASCII character, and iscntrl() lets this system know additional whether or not an integer worth can be an ASCII management code. This pair provides to my persevering with exploration of the ctype capabilities.

Listed below are the precise definitions:

The isascii() perform returns TRUE when the worth of its integer argument is within the vary of zero by 127, the ASCII code values. (ASCII represents unsigned char values, however the perform’s argument is an int information sort.)

The iscntrl() perform returns TRUE when the worth of its integer argument is within the vary of ASCII codes zero by 31 plus the DEL character, code 127. These are all unprintable characters, although some have an effect on the cursor’s place.

To check these two capabilities, I wrote code that generates 20 random values within the vary of zero by 255. For every worth generated, a line is output displaying its decimal and hexadecimal values, in addition to its Unicode character illustration. Then the code explains whether or not the worth is ASCII, non-ASCII, or a management code. It’s assumed that each one management codes are ASCII values by definition.

2026_06_13-Lesson-a.c


#embrace <locale.h>
#embrace <wchar.h>
#embrace <stdlib.h>
#embrace <time.h>
#embrace <ctype.h>

int predominant()
{
    int x,r;

    
    srand( (unsigned)time(NULL) );

    
    setlocale(LC_ALL,"");

    
    for( x=0; x<20; x++ )
    {
        r = rand() % 0xFF;    
        wprintf(L"%03d %02X ",r,r);
        if( isascii(r) )
        {
            if( iscntrl(r) )
            {
                
                if( r==127 )
                    wprintf(L"%lc - management code",9249);
                
                else
                    wprintf(L"%lc - management code",r+9216);
            }
            else
                wprintf(L"%lc - ASCII",r);
        }
        else
        {
            wprintf(L"%lc - non-ASCII",0xB7);    
        }
        putwchar(L'n');
    }

    return 0;
}

Testing for ASCII and management code values takes place within the for loop. An if-else choice determines whether or not the character is ASCII. If not (else), a dot character is output together with the textual content “non-ASCII.” When an ASCII character is discovered, the iscntrl() perform checks for a management code worth. If true, the management code’s Unicode character is output, in any other case the ASCII character is output.

Here’s a pattern run:

173 AD · - non-ASCII
054 36 6 - ASCII
038 26 & - ASCII
038 26 & - ASCII
218 DA · - non-ASCII
065 41 A - ASCII
218 DA · - non-ASCII
247 F7 · - non-ASCII
097 61 a - ASCII
160 A0 · - non-ASCII
075 4B Ok - ASCII
072 48 H - ASCII
012 0C ␌ - management code
122 7A z - ASCII
070 46 F - ASCII
035 23 # - ASCII
164 A4 · - non-ASCII
157 9D · - non-ASCII
002 02 ␂ - management code
110 6E n - ASCII

Emulating these capabilities requires a spread check. For ASCII characters, the integer vary is from zero by 127; for management codes the vary is from zero by 31. My isascii() and iscntrl() capabilities use this vary and return values true or false accordingly:

2026_06_13-Lesson-b.c


#embrace <locale.h>
#embrace <wchar.h>
#embrace <stdlib.h>
#embrace <time.h>
#embrace <stdbool.h>


int isascii(int c)
{
    if( c>=0 && c<=127 )
        return true;
    else
        return false;
}


int iscntrl(int c)
 c==127 )
        return true;
    else
        return false;


int predominant()
{
    int x,r;

    
    srand( (unsigned)time(NULL) );

    
    setlocale(LC_ALL,"");

    
    for( x=0; x<20; x++ )
    {
        r = rand() % 0xFF;    
        wprintf(L"%03d %02X ",r,r);
        if( isascii(r) )
        {
            if( iscntrl(r) )
            {
                
                if( r==127 )
                    wprintf(L"%lc - management code",9249);
                
                else
                    wprintf(L"%lc - management code",r+9216);
            }
            else
                wprintf(L"%lc - ASCII",r);
        }
        else
        {
            wprintf(L"%lc - non-ASCII",0xB7);    
        }
        putwchar(L'n');
    }

    return 0;
}

This system’s output is similar.

These capabilities are essential when processing information to search for and weed out ASCII characters in addition to management codes. Even so, I don’t assume I’ve ever used them. I simply use the identical vary verify as proven in my capabilities (above).

For subsequent week’s Lesson, I cowl the ctype perform ispunct(), which isn’t the identical is ispunked(), which isn’t a C language perform in any respect (although it may be if you wish to code it).

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments