Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

C 字符数据类型


char 类型

char 数据类型用于存储 **单个** 字符。

字符必须用单引号包围,例如 'A' 或 'c',我们使用 %c 格式说明符来打印它

示例

char myGrade = 'A';
printf("%c", myGrade);
亲自试一试 »

或者,如果您熟悉 ASCII,您可以使用 ASCII 值来显示某些字符。请注意,这些值没有用引号 ('') 包围,因为它们是数字

示例

char a = 65, b = 66, c = 67;
printf("%c", a);
printf("%c", b);
printf("%c", c);
亲自试一试 »

**提示:** 可以在我们的 ASCII 表格参考 中找到所有 ASCII 值的列表。


关于字符的注意事项

如果您尝试存储多个字符,它只会打印最后一个字符

示例

char myText = 'Hello';
printf("%c", myText);
亲自试一试 »

**注意:** 不要使用 char 类型存储多个字符,因为它可能会产生错误。

要存储多个字符(或整个单词),请使用 字符串(您将在后面的章节中学习更多有关字符串的内容)

示例

char myText[] = "Hello";
printf("%s", myText);
亲自试一试 »

现在,只要知道我们使用字符串来存储多个字符/文本,而 char 类型用于单个字符即可。


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.