Thread: Subtracting a decimal value from a Char?

  1. #1
    Registered User
    Join Date
    May 2021
    Posts
    2

    Subtracting a decimal value from a Char?

    I'm looking at Muller's Micro-Max chess engine, trying to port it to z80 ASM just as a fun challenge. https://home.hccnet.nl/h.g.muller/umax4_8.c
    While I consider myself well-versed in z80 as a hobby, I have little experience with C.

    I'm looking at this line of code:


    Code:
    b[16*L+K+8]=(K-4)*(K-4)+(L-3.5)*(L-3.5)
    If I'm correct, L was / has been declared as a variable of "char" type. So I'm confused about subtracting 3.5 from a "char" variable. Let's say that K = 7 and L = 7. What would the result be?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    L will be promoted to double for that part of the calculation, which will result in the whole right hand side being promoted to double for evaluation.
    The assignment will then truncate the whole thing back to char.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2021
    Posts
    2
    Thanks, that answers my question and fixes the problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about subtracting char number
    By Valentas in forum C++ Programming
    Replies: 1
    Last Post: 02-19-2013, 01:24 PM
  2. printing char in hexa decimal
    By sagar474 in forum C Programming
    Replies: 5
    Last Post: 03-21-2012, 01:35 PM
  3. Decimal to binary unsigned char
    By kevinstrijbos in forum C Programming
    Replies: 6
    Last Post: 01-27-2012, 03:43 PM
  4. conversion of char to decimal
    By mapleleafblue in forum C Programming
    Replies: 4
    Last Post: 01-28-2009, 09:37 AM
  5. Replies: 10
    Last Post: 08-17-2005, 11:17 PM

Tags for this Thread