char零和普通零之间有什么区别? [英] What is the difference between char zero and normal zero?

查看:132
本文介绍了char零和普通零之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

c ++中char零和普通零之间有什么区别?特别是我的意思是在c ++

例如,

char x = 0;

int x = 0;

和char x ='0'

或者就像'0'

所有这些之间的区别是什么?它们的确切含义是什么?

请帮帮我:)



我尝试过:



我知道int x = 0是数字零。

What is the difference between char zero and normal zero in c++? especially i mean in c++
for example,
char x=0;
int x=0;
and char x='0'
or just like '0'
what is the difference between all of them?what is the exact meaning of them?
Please,help me :)

What I have tried:

I know that int x=0 is number zero.

推荐答案

我不知道最新的c ++以及现在如何强类型:



概念



概念是内存表示根据需要而变化关于声明类型。



Char通常定义为8位存储单元,int通常定义为32位或64位

Hi, I am not up to date with latest c++ and how it is strongly typed nowadays:

Concept

The concept is that the memory representation changes depending on the declaring type.

Char is usually defined as an 8 bit memory cell, int is usually defined as 32bit or 64bit
char x = <whatever>; //you are reserving 1 byte
int x = <whatever>; //you are reserving (let's say) 4 bytes





C和C ++中的字面值'0'表示字符'0'而不是值0,例如字符'0'的ascii代码是48(十进制), Nul字符由文字\0或\ x0代替。



工作原理



要了解它是如何工作的,想象一下你的变量如何存在于计算机中:它是RAM内存的一部分,假设绑定到变量的内存位于地址100。



在初始化变量之前,这是你的ram,其内容未定义:



The literal '0', in C and C++ represent the character '0' and not the value 0, for example the ascii code for character '0' is 48 (decimal), the Nul character is represented by the literal '\0' or '\x0' instead.

How it works

TO understand how it works, imagine how your variable lives in the computer: it is a portion of the RAM memory, suppose that memory bound to your variable is located at address 100.

Here is your ram before you initialize your variable, its content is undefined:

100: ?
101: ?
102: ?
103: ?





这就是你发布的各种不同版本对你记忆状态的影响:



char x = 0 ;我不喜欢那么多,但是编译器一切都很好。





this is how the various different versions you posted affect the status of your memory:

char x=0; I do not like that so much, but for the compiler is all good.

100: 0
101:  (not allocated to x, cause is char that only use 1 cell)
102:  (not allocated to x)
103:  (not allocated to x)





int x = 0; (这很好,没有需要转换)





int x=0; (that is good, no conversion required)

100: 0
101: 0
102: 0
103: 0





char x ='0'; (很好,无需转换)





char x='0'; (that is good, no conversion required)

100: 48d (48 is decimal ascii code for characted '0')





char x ='\ 0';
(与上面相同,但'\0'是代表ASCII NUL的字符,这是我喜欢将char初始化为0的方式。



char x = '\0';
(same as above but '\0' is a char literal representing ASCII NUL, this is the way I prefer to initialize a char to 0)

100: 0





int x ='0'; 我不喜欢'因为右边部分是文字,我可以容忍char变量的赋值e到一个int。





int x = '0'; I do not like that 'cause right part is a literal, i can tolerate the assignment of a char variable to an int.

100: 48d (in this example the machine is little endian, LSB stored at lower address)
101: 0 
102: 0
103: 0


类型'Char is'是System.Char结构的一个实例.NET Framework用于表示Unicode字符。 Char对象的值是一个16位数字(序数)值。



Unicode字符用于表示全世界大多数书面语言。[ ^ ]。



类型'Int是带符号的32位整数; .NET提供隐式转换其他整数类型当你声明并初始化一个'结尾没有后缀的Int ::取决于你赋值的值时,.NET可能会使变量成为另一个整数类型,比如'uInt,'ulong。参见:> [ ^ ]。



要从'Int转换为'Char或'Char to'Int,需要转换:
Type 'Char is "an instance of the System.Char structure that the .NET Framework uses to represent a Unicode character. The value of a Char object is a 16-bit numeric (ordinal) value.

Unicode characters are used to represent most of the written languages throughout the world." [^].

Type 'Int is a signed 32 bit integer; .NET provides an implicit conversion to other integral Types when you declare and initialize an 'Int without a suffix on the end:: depending on the value you assign, .NET may make the variable another integral Type, like 'uInt, 'ulong. See: >[^].

To convert from 'Int to 'Char or 'Char to 'Int requires casting:
int i = 48;
char c = '0';

char c1 = (char) i;
int i1 = (int)c1;

bool b1 = (int)c == (char)i;   // true
bool b2 = i1 == c1;  // true

因为Unicode字符可以重新复制为16位整数,所以这也可以工作:

Because a Unicode character can be repreented as a 16 bit interger, this would also work:

Int16 i = 48;
char c = '0';

char c1 = (char) i;
Int16 i1 = (Int16)c1;

bool b1 = i1 == c1;
bool b2 = (int)c == (char)i;


字符用于使用名为ASCII的编码存储字母和其他符号或UniCode,UniCode在最新版本的编译器中替代ASCII。

ASCII - 维基百科,免费的百科全书 [ ^ ]



Chars are used to store letters and other signs using a coding named ASCII or UniCode, UniCode superseting ASCII in lastest versions of compilers.
ASCII - Wikipedia, the free encyclopedia[^]

char x='0';
// and
char x=48;
// are the same thing.



[更新]

ASCII / UniCode编码用于输入/输出

当您在键盘上键入0时,您会收到代码48,但它是在代码而不是48中更容易使用'0'。

ASCII / UniCode编码使用C,C ++,C#,VB,Java,JS,任何语言。



[细化]

在计算机中,屏幕(控制台模式),键盘和文件都使用ASCII / UniCode对每个字符进行编码。

这意味着当你在记事本中输入0并保存文件时,该文件将保存1个值,即48。如果输入我的名字是债券,该文件将保存77,121,32,110,97,109,101,32,105,115,32, 66,111,110,100。

当你在代码中看到0,1,2 ......时,这些值意味着计算。

当你看到'0','1'时, '2'...在代码中,它意味着它与输入/输出有关,要么检查键盘输入,要么检查di在屏幕上显示这个字符...

更容易阅读'0','1','2'而不是48,49,50 ...

更容易阅读我的名字是邦德而不是77,121,32,110,97,109,101,32,105,115,32,66,111,110,100。


[Update]
The ASCII/UniCode coding is used for input/output
When you type 0 on keyboard, you receive the code 48, but it is easier to use '0' in code rather than 48.
The ASCII/UniCode coding is used C, C++, C#, VB, Java, JS, any language.

[Refinement]
In a computer, the screen (console mode), keyboard and files are all using ASCII/UniCode to encode every characters.
It mean that when you type 0 in Notepad and save the file, the file will hold 1 value which is 48. If you type "My name is Bond", the file will hold 77,121,32,110,97,109,101,32,105,115,32,66,111,110,100.
When you see 0, 1, 2 ... in code, the values are meant foe computation.
When you see '0', '1', '2' ... in code, it mean that it is related to Input/Output, either to check a keyboard input or to display that char on screen ...
It is easier to read '0', '1', '2' rather than 48, 49, 50 ...
It is easier to read "My name is Bond" rather than 77,121,32,110,97,109,101,32,105,115,32,66,111,110,100.


这篇关于char零和普通零之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆