2

I am not very familiar with C so I am a bit confused on the type safety of the language.

For example.

char* my_pointer;

my_pointer = malloc(sizeof(char));

if (*my_pointer == 0b0000)
{
    // this might be true or false, doesn't matter
}

How come the code runs? Why doesn't it just blow up at *my_pointer == 0b0000?

Shouldn't *my_pointer return a char?

So technically shouldn't only something like *my_pointer == 'a' be able to work?

Martymoose
  • 68
  • 12
AlanSTACK
  • 5,525
  • 3
  • 40
  • 99
  • 1
    when you dereference my_pointer you get a char, but then it's promoted to int same goes with the other value – Martin Chekurov Feb 02 '18 at 07:42
  • @martin Woah, I think you might be on to something. Unfortunately as I am a total rookie at C, perhaps a more in depth explanation of what you mentioned would be an answer I would happily accept. – AlanSTACK Feb 02 '18 at 07:43
  • 3
    Standard C rules do not apply here as binary integer constants `0b0000` are not part of the language. – user694733 Feb 02 '18 at 07:43
  • SO cannot replace a beginner's course. You should grab a C text book and learn about basic types and conversion rules. – Gerhardh Feb 02 '18 at 07:49
  • 2
    [Implicit type promotion rules](https://stackoverflow.com/questions/46073295/implicit-type-promotion-rules) – Lundin Feb 02 '18 at 07:53
  • Interestingly, `'a'` is also an `int`. [Why sizeof('a') is 4 in C?](https://stackoverflow.com/questions/8654837/why-sizeofa-is-4-in-c?noredirect=1&lq=1) – Bo Persson Feb 02 '18 at 11:18

3 Answers3

3

C language does not have a dedicated "character" type with some isolated "character-only" semantics. char is just another integer type in C, just like, short or int. It just happens to be the smallest integer type. You can use char for integer arithmetic computations in the very same way you'd use any other integer type (albeit using char in that role is not a very good idea).

When you are comparing char and int you are simply comparing two integer values. Nothing unusual about it.

The rules of integer promotion say that char gets implicitly converted to int (provided the range fits) and then two int values are compared.

Your *my_pointer == 'a' example is actually no different from your original one. In C 'a' is a character constant which represents an integer value of type int. I.e. there's no need to even promote 'a' to int since it is already an int.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • Does integer promotion happen for all in-built variable types in C? – AlanSTACK Feb 02 '18 at 07:44
  • 1
    @AlanSTACK: Integer promotion happens to small integer types `[signed/unsigned] char` and `[signed/unsigned] short` in those contexts where the language specification says it should happen. Operator `==` happens to be one of such contexts. – AnT stands with Russia Feb 02 '18 at 07:45
1

First of all, 0b is not a valid standard C prefix for integer constants. So you need to make sure you are following the specifics for the platform you are using.

That being said, as per the rules for Equality operators (==), arithmetic conversion (integer promotion) rules are applicable for the operands, if both of them are of arithmetic type.

Quoting C11, chapter §6.5.9,

If both of the operands have arithmetic type, the usual arithmetic conversions are performed. [...]

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
0

You are simply comparing 2 integer values.This happens becouse 0b0000 is an integer literal and the value you get after dereferencing my_pointer is being promoted to int.

If an int can represent all values of the original type, the value is converted to an int; otherwise, it is converted to an unsigned int

Martin Chekurov
  • 733
  • 4
  • 15