1

When we compare numbers in a string/character format, how does the c++ compiler interpret it? The example below will make it clear.

#include <iostream>
using namespace std;

int main() {
// your code goes here
    if ('1'<'2')
        cout<<"true";
    return 0;
}

The output is

true

What is happening inside the compiler? Is there an implicit conversion happening from string to integer just like when we refer an index in an array using a character,

   arr['a']
=> arr[97]
gitartha
  • 95
  • 9
  • It is exactly due to the numeric representation of characters. Any character has a numeric value, that's why you can compare them. And `'1'` is a `char` not a "string" – UnholySheep Sep 04 '20 at 10:54
  • See [ascii chart](https://en.cppreference.com/w/cpp/language/ascii) may be help you – srilakshmikanthanp Sep 04 '20 at 10:55
  • @UnholySheep yaa its a 'char' sorry, but which numeric values are being referred here, could you share? – gitartha Sep 04 '20 at 10:55
  • [This](https://godbolt.org/z/vE73a3) could perhaps open some doors. – Ted Lyngmo Sep 04 '20 at 11:00
  • 2
    "It is not due to ASCII values I guess." why not? – 463035818_is_not_an_ai Sep 04 '20 at 11:02
  • 3
    The C++ standard specifies a set of rules for parsing the source file, including interpreting literal values as having particular types, etc. Code within the compiler implements those rules. Also, in all currently standardised character sets, the set of arabic numerals (`'0'`, `'1'`, .... `'9'`) is a contiguous monotonically increasing sequence of integral values (i.e. `'0' + 1 == '1'`, `'1' + 1 == '2'`, ..... `'8' + 1 == '9'` is guaranteed, even if the particular numeric values vary between standardised character sets). – Peter Sep 04 '20 at 11:07
  • @idclev463035818 yes it is, my bad. – gitartha Sep 04 '20 at 15:49

2 Answers2

5

'1' is a char type in C++ with an implementation defined value - although the ASCII value of the character 1 is common, and it cannot be negative.

The expression arr['a'] is defined as per pointer arithmetic: *(arr + 'a'). If this is outside the bounds of the array then the behaviour of the program is undefined.

Note that '1' < '2' is true on any platform. The same cannot be said for 'a' < 'b' always being true although I've never come across a platform where it is not true. That said, in ASCII 'A' is less than 'a', but in EBCDIC (in all variants) 'A' is greater than 'a'!

The behaviour of an expression like "ab" < "cd" is unspecified. This is because both const char[3] constants decay to const char* types, and the behaviour of comparing two pointers that do not point to objects in the same array is unspecified.

(A final note: in C '1', '2', and 'a' are all int types.)

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • I also tested `"12"<"32"` which evaluated to true, on what basis is the compiler making the decision which one is greater or smaller, that's my question? – gitartha Sep 04 '20 at 11:04
  • 1
    last paragraph "&c" ?!? – 463035818_is_not_an_ai Sep 04 '20 at 11:04
  • @idclev463035818: and company. – Bathsheba Sep 04 '20 at 11:04
  • 1
    Now try `"32"<"12"` (and not in the same program as one which already used those string literals somewhere else). – aschepler Sep 04 '20 at 11:06
  • 1
    @Py_g: That's a hornets nest you've opened up. I've added to the answer. – Bathsheba Sep 04 '20 at 11:08
  • 1
    Re “'a' is *widened* to an int prior to the index being evaluated”: Subscripts do not undergo integer promotion. Subscripting is of course defined via pointer arithmetic, and the clause on addition specifies the usual conversions for adding two arithmetic types but no conversions or promotions for adding to a pointer. – Eric Postpischil Sep 04 '20 at 11:09
  • @Ron: `'1'` is an `int` type in C, and a `char` type in C++. – Bathsheba Sep 04 '20 at 11:18
  • @Bathsheba could you suggest some additional resources for the last part, hornets nest, like how and why does the string decay to `const char *` types – gitartha Sep 04 '20 at 11:31
3

The operands '1' and '2' are not strings, they're char literals.

The characters represent specific numbers of type char, typically defined by the ASCII table, specifically 49 for '1' and 50 for '2'.

The operator < compares those numbers, and since the number representation of '1' is lesser than that of '2', the result of '1'<'2' is true.