1

I'm using the ASCII code 219 to print out "█" in a character array. however I would like to check the value of that cell later on in the program using a if statment. this is what i currently have to try to check the value: if(gameboard[playery][playerx] == 219){}. however is vs code it will give a message stating that the comparison is allways faulse due to range limitations for the data type. is there a diffrent way of doing this?

i've tried single and double quotes aswell as using the box as plan text in the program but none of that works. i also tried using the octal value from this suggestion in a diffrent post however the message still appered.

3
  • 2
    Your default char is likely signed so it has values from -128 to 127. The easiest way out is probably a cast like (char)219. Extra explanation: stackoverflow.com/questions/75191/what-is-an-unsigned-char Commented Feb 14, 2024 at 16:08
  • 1
    The first thing to learn is that there is no such thing as "extended ASCII". Commented Feb 14, 2024 at 16:14
  • 219 is not an ASCII code. Commented Feb 14, 2024 at 16:23

2 Answers 2

1

I'm not sure about whether the char is signed or not, but I think that @teapost418 is correct that you just have to type cast 219 as a char:

if(gameboard[playery][playerx] == (char) 219){} 

I think that otherwise 219 would be interpreted as an int.

Sign up to request clarification or add additional context in comments.

1 Comment

Or better, just declare a constant static const char block = 219; so that it has a name as well as the correct type.
1

First, there is no ASCII code 219 as ASCII is 7-bit.

The are various extended ASCII codings with a value of 219.


<string.h> compare routines like strcmp() examine the several characters as if they were unsigned char even when char is signed.

For all functions in this subclause, each character shall be interpreted as if it had the type unsigned char (and therefore every possible object representation is valid and has a different value). C23dr § 7.26.1 4

fgetc() reads a character and returns, as an int, an unsigned char value or EOF.

I recommend to compare individual char in the same fashion to provide consistent functionality when strings are used.

// if(gameboard[playery][playerx] == 219){} if((unsigned char)gameboard[playery][playerx] == 219){} 

... or perhaps make gameboard[][] an array of unsigned char. Yet need to see overall code to fully endorse this approach.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.