什么是字符常量、字符串常量和格式字符啊??

Marrakech 2007-12-03 11:19:25
什么是字符常量、字符串常量和格式字符啊??请了解的人帮我解说
...全文
3896 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
wizard_tiger 2011-03-09
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 z1090891028 的回复:]
‘8.34’为什么不是字符常量
[/Quote]
'8'这个才是字符常量
"8.34"这个是字符串常量
zklsdfd 2011-03-09
  • 打赏
  • 举报
回复
‘8.34’为什么不是字符常量
ieyecat 2007-12-13
  • 打赏
  • 举报
回复
嗯 懂了 谢谢
wangchangqiao 2007-12-13
  • 打赏
  • 举报
回复
一般的那255个符号只要加上' '都算是字符常量.
而字符串常量就是加了" "就算是字符串常量.
像%c,%s就是控制字符或字符串的输出
wangweiwangxia 2007-12-13
  • 打赏
  • 举报
回复
字符常量类型包括
字母:'a','A'
数字:'1'
符号:'-'
Marrakech 2007-12-06
  • 打赏
  • 举报
回复
非常感谢你们^_^
shaoze5 2007-12-04
  • 打赏
  • 举报
回复
字符常量,只会有一个字符。但是字符串常量,就算这个串只有一个字符,他的后面也有一个'\0'。
r_swordsman 2007-12-04
  • 打赏
  • 举报
回复
摘自 C++ Language Reference 的 C++ Character Constants 部分

Character constants are one or more members of the "source character set," the character set in which a program is written, surrounded by single quotation marks ('). They are used to represent characters in the "execution character set," the character set on the machine where the program executes.

字符常量是由单引号扩起来的一个或多个在编程时所写的“原始字符集”构成,字符常量是用于在程序运行的计算机上表示“运行时字符集”中的字符。

由此可见,字符常量是由单引号扩起来的,如,'x'、'ab'、'abcd' 都为字符常量(虽然可能没意义),
而且字符常量所表示的字符和运行计算机的有关,如字符 'ab' 在某计算机上是 'x',在另外一台计算机上是 'y'。

Microsoft Specific :
For Microsoft C++, the source and execution character sets are both ASCII.

微软特定:对于 Microsoft C++,原始字符集和运行时字符集都是 ASCII。

There are three kinds of character constants:
Normal character constants
Multicharacter constants
Wide-character constants

有 3 种类型的字符常量:标准字符常量、多字符常量、宽字符常量。

Note: Use wide-character constants in place of multicharacter constants to ensure portability.

注意:使用宽字符常量来替代多字符常量可方便(程序在不同计算机上)发行。

Character constants are specified as one or more characters enclosed in single quotation marks. For example:

字符常量是由单引号括起的一个或多个字符指定的,例如:


char ch = 'x'; // Specify normal character constant. 指定标准字符常量
int mbch = 'ab'; // Specify system-dependent multicharacter constant. 指定依赖系统的多字符常量
wchar_t wcch = L'ab'; // Specify wide-character constant. 指定宽字符常量




摘自 C++ Language Reference 的 C++ String Literals 部分

A string literal consists of zero or more characters from the source character set surrounded by double quotation marks ("). A string literal represents a sequence of characters that, taken together, form a null-terminated string.

一个字符串文本是由双引号括起的零或多个原始字符集组成,一个字符串文本表现为一个合在一起的字符序列并以空字符结束的字符串。

C++ strings have these types:
Array of char[n], where n is the length of the string (in characters) plus 1 for the terminating '\0' that marks the end of the string
Array of wchar_t, for wide-character strings

C++ 字符串包括这些类型:
char[n] 的数组,其中 n 为字符串(以字符计算)的长度加上用于标记字符串结束的结束符 '\0' 的(长度) 1
用于宽字符字符串的 wchar_t 类型的数组

The result of modifying a string constant is undefined. For example:
对字符串常量的修改所导致的后果是不明确的,例如:


char *szStr = "1234";
szStr[2] = 'A'; // Results undefined 结果不明确





原文太多,不想翻译

:)


yuyunliuhen 2007-12-04
  • 打赏
  • 举报
回复
一种是用该字符的图形符号,如'b' ,'y','*'。
另外还可以用字符的ASCII码表示,即用反斜符(\)开头,后跟字符的ASCII码,这种方法也称为转义序列表示法,具体方法是:有两种形式:

一种是用字符的八进制ASCII码,表示为:\ddd这里,ddd是八进制值。

另一种使用字符的十六进制ASCII码值,表示为 \xhh  这里hh是两位十六进制值。
如:'A' ,'\101' 和 'x41'都表示同一个字符常量。  
转义序列表示法还可以用来表示一些特殊字符,用来显示特殊符号或控制输出格式。
NemoTian 2007-12-04
  • 打赏
  • 举报
回复
#include<iostream>
#include<string>
using namespace std;
int main()
{
char a='y';
string b="is yes!";
cout<<a<<' '<<b<<endl;
}
结果是y is yes!
其中a是一个字符常量.用char声明.
b是一个字符串常量.用string声明.使用string声明时,要加头文件<string>.
ckt 2007-12-03
  • 打赏
  • 举报
回复
字符常量: 'c' 'a' '*' ......
常量字符串:char* pStr = "abcd";
NKLoveRene 2007-12-03
  • 打赏
  • 举报
回复
'a'字符常量
"a"字符串常量
自定义一个格式就是格式字符串

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧