char x='x'; why not char x="x"; ?

  • Thread starter Thread starter Tony!
  • Start date Start date
T

Tony!

char x='x';
Why not char x="x"; ?


I'm guessing double-quotes is incorrect because it signifies a string
value which is not implicitly carried into a char bacause it's a
smaller size and some data may be lost.

Guess what I'm wondering is the single quote used here merely for
proper syntax to just remember 'use it in this case', , or does the
single quote have some meaning that understanding may assist in making
better guesses as to when they should be used over double quotes.

Thanks,

Tony!
 
Tony! said:
char x='x';
Why not char x="x"; ?


I'm guessing double-quotes is incorrect because it signifies a string
value which is not implicitly carried into a char bacause it's a
smaller size and some data may be lost.

Guess what I'm wondering is the single quote used here merely for
proper syntax to just remember 'use it in this case', , or does the
single quote have some meaning that understanding may assist in making
better guesses as to when they should be used over double quotes.

single quotes are used to define a char literal and double quotes a string
literal; its as simple as that.

You can't do any of these:-

char x = "x";
string x = 'y';
string x = 'hello';
char x = 'hello';
 
Tony! said:
char x='x';
Why not char x="x"; ?


I'm guessing double-quotes is incorrect because it signifies a string
value which is not implicitly carried into a char bacause it's a
smaller size and some data may be lost.

Guess what I'm wondering is the single quote used here merely for
proper syntax to just remember 'use it in this case', , or does the
single quote have some meaning that understanding may assist in making
better guesses as to when they should be used over double quotes.

The first one ("use it in this case"). I can't think of any other place in
C# where the apostrophe is used.
 
Tony! said:
char x='x';
Why not char x="x"; ?

I'm guessing double-quotes is incorrect because it signifies a string
value which is not implicitly carried into a char bacause it's a
smaller size and some data may be lost.

Guess what I'm wondering is the single quote used here merely for
proper syntax to just remember 'use it in this case', , or does the
single quote have some meaning that understanding may assist in making
better guesses as to when they should be used over double quotes.

'x' is a char struct
"x" is a string object

It is two completely different types.

In C# and in most other languages (also in VB.NET
where Dim c As Char = "x"C is necessary when Strict is
on instead of Dim c As Char = "x").

Arne
 
You have to look at what the difference between char and string literals
are... Even though 'x' and "x" looks the same without the quotes the quotes
make the difference...

For char x='x'; set the signed 8-bit number by at memory x with the ascii
code (8-bit number) for x...
For char x="x"; this is not allow because of the type difference ambiguity
because "x" is not a ascii code (8-bit number) for x but a ascii code (8-bit
number) for x followed by the ascii code for the NULL character -- there for
"x" is not just the x which will fit in char but also the NULL character
afterwards...

One could do char x="x"[0]; which the "x"[0] part first evaluates to char
type with the value of the ascii code (8-bit number) for x - then the
assignment part now is set char x to ascii code (8-bit number) for x...

Bottom line a char is a char and a string(even "x") is a string and the
complier can implicitly convert string to char. Above I explicitly converted
string to char with "x"[0]...

Hope that explains it...
 
Brandon said:
You have to look at what the difference between char and string literals
are... Even though 'x' and "x" looks the same without the quotes the
quotes make the difference...

For char x='x'; set the signed 8-bit number by at memory x with the
ascii code (8-bit number) for x...
For char x="x"; this is not allow because of the type difference
ambiguity because "x" is not a ascii code (8-bit number) for x but a
ascii code (8-bit number) for x followed by the ascii code for the NULL
character -- there for "x" is not just the x which will fit in char but
also the NULL character afterwards...

I believe the group is about C# not C/C++ ...

Arne
 
Brandon said:
You have to look at what the difference between char and string literals
are... Even though 'x' and "x" looks the same without the quotes the
quotes make the difference...

For char x='x'; set the signed 8-bit number by at memory x with the ascii
code (8-bit number) for x...

Note char is 16bits wide, its a unicode character.
 
Back
Top