0

I would like to know if it was possible to compare an integer and a char. Something that could be the "===" from Javascript. Because

'a' == 97

Will ouput

1

Edit : I would like something like

compare('a', 97)

Which would ouput

0

How i can do ?

Thanks

Extaze
  • 1,117
  • 2
  • 10
  • 18
  • 2
    Problem there is that the type of `'a'` is `int` too. You can only distinguish `'a'` from `97` if the source character set is ASCII-incompatible. – Daniel Fischer May 15 '13 at 20:18
  • No 'a' is not an int. Check sizeof(char) and sizeof(int)a – Extaze May 15 '13 at 20:25
  • 1
    Well, the size of a `char` and the size of an `int` usually are different (no guarantee, though), but an integer character constant has type `int` in C. Try `printf("%zu\n", sizeof 'a');`. – Daniel Fischer May 15 '13 at 20:27
  • "An integer character constant has type `int`. The value of an integer character constant containing a single character that maps to a single-byte execution character is the numerical value of the representation of the mapped character interpreted as an integer. The value of an integer character constant containing more than one character (e.g., `'ab'`), or containing a character or escape sequence that does not map to a single-byte execution character, is implementation-defined." – Daniel Fischer May 15 '13 at 20:31
  • 1
    @Extaze yes it it, the standard clearly states `An integer character constant has type int.` (paragraph 10, §6.4.4.4) – jerry May 15 '13 at 20:32
  • By the way, are you actually comparing literals or is at least one a variable? If one is a variable, in what manner are you doing these comparisons so that you've lost the type information? Remember that JavaScript is dynamically and weakly typed. `var x` may be an integer first, a string second, and a function third in JavaScript, whereas `char x` is always a `char` in C. – jerry May 15 '13 at 21:10

3 Answers3

2

Yes, it's possible. Not only is char is just another integral type, but 'a' is actually itself a literal of type int. So 'a' == 97 is a perfectly valid logical expression. Its value is implementation-defined.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • I think OP is asking how to check whether `'a'` and `97` are the same object. Strict comparison would return `false` if they aren't of the same type. – Blender May 15 '13 at 20:12
  • 1
    @Blender: They're both temporary values, so they can't be "the same object", no more than `2 == 2`. In fact, since the operands aren't lvalues, I don't think it even makes sense to ask whether "they're the same object". – Kerrek SB May 15 '13 at 20:18
  • @KerrekSB: I've addressed this in my answer though I'm still not entirely sure if that's what the OP was asking for... – R.. GitHub STOP HELPING ICE May 15 '13 at 20:21
  • @Blender: Let me make this more formal: Object identity is by address and by type (i.e. two objects are the same if and only if they have the same address and the same type). Since only lvalues can have an address, only lvalues can have an identity. – Kerrek SB May 15 '13 at 20:21
  • @R..: Yeah, your answer is fine. I think the OP probably can't be helped anyway, since she's probably expecting some PHP-like nebulous "object model" (I shudder to even call it that). – Kerrek SB May 15 '13 at 20:22
  • 1
    @Blender: Further to my last point, I should like to say that C and C++ have heavily *value-based* languages (and C++ much moreso even), and the notion of object identity isn't really all that useful in them. Value equality is a far more idiomatic relation. Languages which are fundamentally reference-semantic have very different idioms (e.g. there's a single, untouchable integer "2", and all variables are just references to it). – Kerrek SB May 15 '13 at 20:23
1

'a' and 97 are values, not objects, so there is no sense in asking if they're the same object. If you had these values stored in objects and had pointers to those objects, you would compare the pointers.

If on the other hand you wanted a comparison that distinguishes that 'a' is a char and 97 is an int, you're also out of luck. In C, 'a' is an int, not a char.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
  • Check sizeof(char) and sizeof(int) – Extaze May 15 '13 at 20:26
  • 2
    @Extaze Check out `sizeof('a')` and `sizeof(97)`. `'a'` is not a *char* in the first place. So saying `sizeof(char)` is different from `sizeof(int)` not a fair comparison ;) – P.P May 15 '13 at 20:34
0

In C (contrarily to PHP) you know at compile time if a variable is of type char or int, so there is no point in having == and === operators.

I would like something like compare('a', 97)

Is that a function? If so, the type of the arguments are fixed at compile time, so you couldn't implement that, and anysay it would not make sense to call that function. You know, at compile time, if the arguments are of the same type or not.

Further, if we have

 c = (char)40;
 i = 40;

what should that compare(c,i) return?

Perhaps you can give us some motivation, why do you believe you need such a function?

You could write a macro that puts a sizeof() comparison before the == , but, this would not distinguish different types of same size, and, again, it would not really make much sense.

leonbloy
  • 73,180
  • 20
  • 142
  • 190