Click here to Skip to main content
15,886,963 members
Please Sign up or sign in to vote.
1.66/5 (6 votes)
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.
Posted
Updated 13-Jul-23 0:43am
v5

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
C++
char x = <whatever>; //you are reserving 1 byte
int x = <whatever>; //you are reserving (let's say) 4 bytes


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: ?


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; (that is good, no conversion required)

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


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

100: 48d (48 is decimal ascii code for characted '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'; 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
 
Share this answer
 
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
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;
 
Share this answer
 
v2
Comments
Patrice T 27-Aug-16 21:49pm    
I think you missed the point of the question.
The OP said:
i saw this '0' kind of writing mostly in while loop,for example:
while(x!='0')
Member 12702056 28-Aug-16 6:21am    
BillWoodruff,thank you for your explanation :),but actually i can't understand what you meant exactly.
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[^]

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

[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.
 
Share this answer
 
v7
Comments
Member 12702056 27-Aug-16 17:54pm    
i saw this '0' kind of writing mostly in while loop,for example:
while(x!='0')

i got from your explanation that '0' is a character but it is same with another character char x=48
yes?
i wrote it in the program and checked it,yes,it is same char x=48/but you know this from ASCII,yes?
Member 12702056 27-Aug-16 18:51pm    
but in c++,are they as same as you explained?
BillWoodruff 27-Aug-16 21:12pm    
My vote of #1. Standard encoding in .NET is not ASCII; it's Unicode. The code you posted will never compile: the reason why is obvious.
Patrice T 27-Aug-16 21:44pm    
I would have been happy with a vote of 3 :)
The question was "especially i mean in c++", not .NET.
The OP is confused about the difference in usage of 0 and '0' in code. Using ASCII or UniCode in explanation do not change the explanation.
The size of char will depend on age of compiler, Which version was not given.

The piece of code is not meant to be compiled as per the comments, the 2 lines are the same.
Member 12702056 28-Aug-16 6:27am    
ppolymorphe,you mean char x='0' equal to int x=48 ?! Is it like this? Could i get it correctly?
//__________
And about char x=0;
Is this correct writing?or int 0 can't be equal to char ?
My vote:5 :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900