介紹 C 語言中的字元操作


在 C 程式語言中,字元可以是 (A-Z(或)a-z)、數字 (0-9)、一個空格或一個特殊符號。

宣告

以下是 C 程式設計中字元操作的宣告 -

char a= ‘A’; using a character constant.

字元輸入/輸出函式

字元輸入/輸出函式的解釋如下 -

示例 - char a;

scanf("%c", &a); printf ("%c", &a);
a = getchar ( ); putchar (a);
a = getch ( ); putch (a);

示例

以下是使用 getchar() 進行行計數的 C 程式 -

 即時演示

#include <stdio.h>
/* count lines in input */
main(){
   int count, num;
   printf("enter multiple statements and Press cntrl+z:
");    num = 0;    while ((count = getchar()) != EOF)       if (count == '
')       ++num;    printf("%d
", num); }

輸出

以下為程式的輸出 -

enter multiple statements and Press cntrl+z:
Hi
Hello
Welcome To
My World
^Z
4

更新於: 2021-03-24

523 次瀏覽

啟動您的職業

完成課程獲得認證

入門
廣告
© . All rights reserved.