char類型的值占用所有數(shù)據(jù)類型的最小內(nèi)存量。
它們通常只需要一個字節(jié)。
您可以通過字符常量為char類型的變量指定初始值。
字符常數(shù)可以只是單引號之間的字符。這里有些例子:
char letter = "A"; char digit = "9"; char exclamation = "!";
您可以使用一對單引號之間的轉(zhuǎn)義序列來指定字符常量:
char newline = "\n"; char tab = "\t"; char single_quote = "\"";
您也可以使用整數(shù)值初始化char類型的變量,只要該值適用于您的編譯器的char類型的范圍,如下例所示:
char character = 74; // ASCII code for the letter J
char類型的變量具有一種雙重個性:您可以將其解釋為字符或整數(shù)。
下面是一個值為char類型的算術(shù)運(yùn)算的例子:
char letter = "C"; // letter contains the decimal code value 67 letter = letter + 3;// letter now contains 70, which is "F"
因此,您可以對char類型的值執(zhí)行算術(shù),并將其視為字符。
您可以從鍵盤讀取單個字符,并使用格式說明符%c的scanf()函數(shù)將其存儲在char類型的變量中,例如:
char ch = 0; scanf("%c", &ch); // Read one character
要使用printf()函數(shù)將單個字符寫入命令行,請使用相同的格式說明符%c
:
printf("The character is %c\n", ch);
當(dāng)然,也可以輸出一個字符的數(shù)值:
printf("The character is %c and the code value is %d\n", ch, ch);
該語句將以ch作為字符和數(shù)值輸出值。
#include <stdio.h>
int main(void)
{
char first = "A";
char second = 63;
printf("The first example as a letter looks like this - %c\n", first);
printf("The first example as a number looks like this - %d\n", first);
printf("The second example as a letter looks like this - %c\n", second);
printf("The second example as a number looks like this - %d\n", second);
return 0;
}
上面的代碼生成以下結(jié)果。
您還可以使用格式說明符%x而不是%d將char類型的變量的整數(shù)值輸出為十六進(jìn)制值。
我們來看另外一個例子,你將算術(shù)運(yùn)算應(yīng)用到char類型的值中:
#include <stdio.h>
int main(void)
{
char first = "A";
char second = "B";
char last = "Z";
char number = 40;
char ex1 = first + 2; // Add 2 to "A"
char ex2 = second - 1; // Subtract 1 from "B"
char ex3 = last + 2; // Add 2 to "Z"
printf("Character values %-5c%-5c%-5c\n", ex1, ex2, ex3);
printf("Numerical equivalents %-5d%-5d%-5d\n", ex1, ex2, ex3);
printf("The number %d is the code for the character %c\n", number, number);
return 0;
}
上面的代碼生成以下結(jié)果。
改變輸入,保留非字母
#include <stdio.h>
#include <ctype.h> // for isalpha()
int main(void)
{
char ch;
while ((ch = getchar()) != "\n")
{
if (isalpha(ch)) // if a letter,
putchar(ch + 1); // display next letter
else // otherwise,
putchar(ch); // display as is
}
putchar(ch); // display the newline
return 0;
}
上面的代碼生成以下結(jié)果。
改變輸入,保留空格
#include <stdio.h>
#define SPACE " " // that"s quote-space-quote
int main(void)
{
char ch;
ch = getchar(); // read a character
while (ch != "\n") // while not end of line
{
if (ch == SPACE) // leave the space
putchar(ch); // character unchanged
else
putchar(ch + 1); // change other characters
ch = getchar(); // get next character
}
putchar(ch); // print the newline
return 0;
}
上面的代碼生成以下結(jié)果。
以下代碼顯示字符的代碼。
#include <stdio.h>
int main(void)
{
char ch;
printf("Please enter a character.\n");
scanf("%c", &ch); /* user inputs character */
printf("The code for %c is %d.\n", ch, ch);
return 0;
}
上面的代碼生成以下結(jié)果。
以下代碼使用轉(zhuǎn)義字符。
#include <stdio.h>
int main(void)
{
float salary;
printf("\aEnter your desired monthly salary:");/* 1 */
printf(" $_______\b\b\b\b\b\b\b"); /* 2 */
scanf("%f", &salary);
printf("\n\t$%.2f a month is $%.2f a year.", salary,
salary * 12.0); /* 3 */
printf("\rGee!\n"); /* 4 */
return 0;
}
上面的代碼生成以下結(jié)果。
更多建議: