Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

char - what happens internally?

 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is happening here?

char c = 0;
print(c)
gives no output but
print((int)c)
prints a 0

also you can't assign to it unless you use a literal or a cast or another char. What is happening there?
 
Krishna Srinivasan
Ranch Hand
Posts: 1876
Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,

char 10 is equivalent to \n is a line feed (LF) character. So when you print it it prints a new line in the console which you are not able to see. If you change this value to something like '11', then you could see a some other character. When you are doing typecasting, then it converts to the actual int and displays in the screen.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably anything you need to know about what's happening behind the scenes is very well explained in this topic.
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for looking that up. So "under the hood" its the same for arithmetic purposes (except unsigned), but when it comes time to display the char its pure value is used rather than a printable representation of the value. I was messing with a debugger earlier and even it doesn't show the contents of a char variable.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guillermo Ishi wrote:I was messing with a debugger earlier and even it doesn't show the contents of a char variable.

When I debug this code (using Eclipse)I clearly see the correct contents of each variable (see screenshot). Because c1 is a not representable character, you only see the square when you select the row.
chars-in-debug.jpg
[Thumbnail for chars-in-debug.jpg]
different char values in debug mode
 
Campbell Ritchie
Sheriff
Posts: 51446
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember (char) 0 is called the null character and it has no appearance. So you would not expect to see it.
 
Junilu Lacar
Bartender
Pie
Posts: 8875
81
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Compile and run this from the command line:

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Compile and run this from the command line:

After such an effort (compiling and running from command line), I was expecting to see something spectacular... Must admit I'm a little bit disappointed

(And it's almost 3 hours too late to qualify for April Fools )
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic