iscntrl() 函數(shù)用來檢測一個(gè)字符是否是控制字符(Control Character)。
“控制字符”是指那些具有某種特殊功能、不會(huì)顯示在屏幕上、不會(huì)占用字符位置的特殊字符。無法在屏幕上打印的字符稱為控制字符。例如,退格鍵,轉(zhuǎn)義符,換行符等。
控制字符和可打印字符是相對(duì)的,可打印字符是指那些會(huì)顯示在屏幕上、會(huì)占用字符位置的“普通”字符。要檢測一個(gè)字符是否是可打印字符,請(qǐng)使用 isprint() 函數(shù)。
iscntrl()函數(shù)檢查一個(gè)字符(傳遞給該函數(shù)的字符)是否為指定字符。如果傳遞的字符是指定字符,則返回非零整數(shù)。如果不是,則返回0
此函數(shù)在ctype.h 頭文件中定義。
int iscntrl(int argument);
isntrl()函數(shù)采用單個(gè)參數(shù)并返回一個(gè)整數(shù)。
當(dāng)將字符作為參數(shù)傳遞時(shí),將傳遞字符的相應(yīng)ASCII值,而不是傳遞該字符本身。
#include <stdio.h>
#include <ctype.h>
int main()
{
char c;
int result;
c = 'Q';
result = iscntrl(c);
printf("當(dāng)%c被傳遞給iscntrl()時(shí) = %d\n", c, result);
c = '\n';
result = iscntrl(c);
printf("當(dāng)%c被傳遞給iscntrl()時(shí) = %d", c, result);
return 0;
}輸出結(jié)果
當(dāng)Q被傳遞給iscntrl()時(shí) = 0 當(dāng) 被傳遞給iscntrl()時(shí) = 1
#include <stdio.h>
#include <ctype.h>
int main()
{
int i;
printf("所有控制字符的ASCII值是 ");
for (i=0; i<=127; ++i)
{
if (iscntrl(i)!=0){
printf("%d ", i);
}
}
return 0;
}