C 庫 - isgraph() 函式



C 的ctypeisgraph() 函式檢查字元是否具有圖形表示。具有圖形表示的字元是所有可以列印的字元,除了空格字元(如 ' '),空格字元不被認為是 isgraph 字元。這包括所有在顯示器上產生可見標記的字元,例如字母、數字、標點符號等,但不包括空格、製表符或換行符等空格字元。

語法

以下是 isgraph() 函式的 C 庫語法:

int isgraph(int c);

引數

此函式接受單個引數:

  • int c : 這是要檢查的字元,作為整數傳遞。字元通常以其 ASCII 值傳遞。

返回值

如果字元是可列印字元(空格字元除外),則該函式返回非零值(true);如果字元不是可列印字元(空格字元除外),則返回 0(false)。

示例 1:檢查字母字元

字元 'A' 是字母字元,並且是可圖形列印的,因此 isgraph() 返回 true。

#include <stdio.h>
#include <ctype.h>

int main() {
   char ch = 'A';

   if (isgraph(ch)) {
      printf("'%c' is a printable character other than space.\n", ch);
   } else {
      printf("'%c' is not a printable character or it is a space.\n", ch);
   }
   return 0;
}

輸出

以上程式碼產生以下結果:

'A' is a printable character other than space.

示例 2:檢查空格字元

空格不被認為是圖形字元。因此,isgraph() 函式返回 false。

#include <stdio.h>
#include <ctype.h>

int main() {
   char ch = ' ';

   if (isgraph(ch)) {
      printf("'%c' is a printable character other than space.\n", ch);
   } else {
      printf("'%c' is not a printable character or it is a space.\n", ch);
   }
   return 0;
}

輸出

執行以上程式碼後,我們得到以下結果:

' ' is not a printable character or it is a space.
廣告