C语言中如何区分可打印字符和控制字符?

给定一个字符, 我们需要查找它是否可打印。我们还需要查找它是否是控制字符。如果一个字符占用打印空间, 则称为可打印字符。

对于标准ASCII字符集(由” C”语言环境使用), 控制字符是ASCII代码0x00(NUL)和0x1f(US)加上0x7f(DEL)之间的字符。

例子:

Input : a
Output :a is printable character
        a is not control character
        
Input :\r
Output : is not printable character
         is control character

为了找到可打印字符和控制字符之间的区别, 我们可以使用一些预定义的函数, 这些函数在” ctype.h”头文件中声明。

isprint()函数检查字符是否为可打印字符。 isprint()函数采用整数形式的单个参数, 并返回int类型的值。我们可以在内部传递一个char类型的参数, 它们通过指定ASCII值充当int。

iscntrl()函数用于检查字符是否为控制字符。 iscntrl()函数还采用单个参数并返回整数。

//C program to illustrate isprint() and iscntrl() functions.
#include <stdio.h>
#include <ctype.h>
int main( void )
{
     char ch = 'a' ;
     if (isprint(ch)) {
         printf ( "%c is printable character\n" , ch);
     } else {
         printf ( "%c is not printable character\n" , ch);
     }
  
     if ( iscntrl (ch)) {
         printf ( "%c is control character\n" , ch);
     } else {
         printf ( "%c is not control character" , ch);
     }
     return (0);
}

输出如下:

a is printable character
a is not control character

如果发现任何不正确的地方, 或者想分享有关上述主题的更多信息, 请写评论。

来源:

https://www.srcmini02.com/69444.html

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_39613.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?