isspace函数

isspace函数

用法

#include 
#include 

void main(){
    char s[] = "  ff  ";
    int i;
    for(i = 0; isspace(s[i]); i++);
    printf("%d",i);
}

不出所料结果输出2

isspace 过滤空字符方法

看一下ctype.h里面的定义

# define isspace(c) __isctype((c), _ISspace)

#ifndef __cplusplus
# define __isctype(c, type) \
  ((*__ctype_b_loc ())[(int) (c)] & (unsigned short int) type)
#elif defined __USE_EXTERN_INLINES

_ISspace = _ISbit (5),  /* Whitespace.  */

#  define _ISbit(bit)   ((bit) < 8 ? ((1 << (bit)) << 8) : ((1 << (bit)) >> 8))

讲方法展开最终表达式

(((*(__ctype_b_loc)())[i]) & (((bit) < 8 ? ((1 << (bit)) << 8) : ((1 << (bit)) >> 8)))

对结果进行了分析

二进制         value   key        
// 10000000000000  8192   _ISbit (5)   char     //关键
//110000000000001  24577  32            ' '
// 10000000000011  8195   9             '\t'
// 10000000000010  8194   10            '\n'
// 10000000000010  8194   11            
// 10000000000010  8194   12
// 10000000000010  8194   13            '\r' 

_ISspace = _ISbit (5) 得到的是一个13个比特位二进制数字只有第13位为1

(*(__ctype_b_loc)()) 返回的是一个int数组key为ask码值位一个特殊的数字
两个结果取 &
ctype.h 只是找到 __ctype_b_loc的定义 我下载了一份 c标准库 依然没有找到 作为初学者就看到这里了

再贴一段我调试的时候写的一段代码

#include 
#include 

void atof(char s[]){
    double val, power;
    int i, sign;
    for(i = 0; isspace(s[i]); i++);
        printf("%d", i);

    int bit = 5;
    sign = ((bit) < 8 ? ((1 << (bit)) << 8) : ((1 << (bit)) >> 8));

    int temp;
    temp = 1 << (bit);
    printf("%d", temp);

for(i = -100 ; i < 200; i ++){
    printf("%d - %d \n", (((*(__ctype_b_loc)())[i]) & 8192), i);
}
return ;

printf("%d", ((*(__ctype_b_loc)())[2]) & 8192);
    return;
    int x = ' ';
    int y = '\n';
    int z = '\t';
    int b = '\r';
    printf("%d \n", (int) x); //32
    printf("%d \n", (int) y); //10
    printf("%d \n", (int) z); //9
    printf("%d \n", (int) b); //13
    printf("%d \n", sizeof(((*__ctype_b_loc ()))));

    printf("%d \n",((*__ctype_b_loc ())[(int) x]));
    printf("%d \n",((*__ctype_b_loc ())[(int) y]));
    printf("%d \n",((*__ctype_b_loc ())[(int) z]));

printf("%d \n", (((*__ctype_b_loc ())[(int) x]) & (unsigned short int) 8192 ));
printf("%d \n", (((*__ctype_b_loc ())[(int) y]) & (unsigned short int) 8192 ));
printf("%d \n", (((*__ctype_b_loc ())[(int) z]) & (unsigned short int) 8192 ));
printf("%d \n", (((*__ctype_b_loc ())[(int) b]) & (unsigned short int) 8192 ));
printf("%d \n", (((*__ctype_b_loc ())[(int) 1]) & (unsigned short int) 8192 ));

for(i = 0; i< 30; i ++ ){
    printf("%d \n",((*__ctype_b_loc ())[32]));
}
for(i = 0; i < 10; i ++){
    ((*__ctype_b_loc ())[(int) ( s[i])] & (unsigned short int) 8192);

}
printf("%s", s);
}

void main(){
    char s[] = "   fff   ";
    atof(s);
}

你可能感兴趣的:(isspace函数)