• Post Reply Bookmark Topic Watch Topic
  • New Topic

char program  RSS feed

 
J Mohandos
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am aware that the default value of char is 0. But in this program I am getting some unexpected output



Output:
ch: ia[ch]:0

If you see the above output ch is printed as a blank space instead of 0, while the array default value is correctly printed by taking the char default value as index. Why is ch printed as blank space?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't print a space - that space is put there by your code.

the default value of char is 0

Not so - the default value is the UTF-8 character \u0000 (or ASCII NUL) - the textual representation of which is not "0" (that would be \u0030 - the digit '0').
 
Campbell Ritchie
Marshal
Posts: 56540
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
J Mohandos wrote:I am aware that the default value of char is 0. . . .
You mean the default value of a char field is (as Ulf points out) the null character. If you want a 0 change the declaration to int. And remember that local variables don't have default values.
 
Robert D. Smith
Ranch Hand
Posts: 221
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:. And remember that local variables don't have default values.

Now that I was not aware of, although it makes sense. If I recall correctly, C is quite similar in that an uninitialised variablecould contain anything.

Regards,
Robert
 
Campbell Ritchie
Marshal
Posts: 56540
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the difference is that in C, you might get it past the compiler and use an uninitialised local variable. In Java® it will fail to compile. Even if you think you have initialised it, you still have to convince the compiler. Try this crappy version of a signum method:-
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!