4

I wrote the following code

#include <iostream>

#define  circleArea(r) (3.1415*r*r)
int main() {
    std::cout << "Hello, World!" << std::endl;
    std::cout << circleArea('10') << std::endl;
    std::cout << 3.1415*'10'*'10' << std::endl;
    std::cout << 3.1415*10*10 << std::endl;

    return 0;
}

The output was the following

Hello, World!
4.98111e+08
4.98111e+08
314.15

The doubt i have is why is 3.1415 * '10'*'10' value 4.98111e+08. i thought when i multiply a string by a number, number will be converted to a string yielding a string.Am i missing something here?

EDIT: Rephrasing question based on comments, i understood that single quotes and double are not same. So, '1' represents a single character. But, what does '10' represent

rawwar
  • 4,834
  • 9
  • 32
  • 57
  • 4
    `'10'` is not a string?! – DeiDei Jun 08 '18 at 10:08
  • 3
    @InAFlash Enable more compiler warnings. – Biffen Jun 08 '18 at 10:11
  • @DimChtz, i just printed, '10' and it was showing a value of 12592, can you help me understand how c++ evaluated this value – rawwar Jun 08 '18 at 10:12
  • It's 3130 hex as a decimal. This is because you've combined 31 (hex ascii for 1) and 30 (hex ascii for 0) next to each other in memory, which, when read as an int, evaluates to an integer 12592. – Baldrick Jun 08 '18 at 10:14
  • 2
    Side note: `circleArea()` would be much better as a function rather than a macro. – Galik Jun 08 '18 at 10:17
  • 3
    @InAFlash: Personally I don't understand the downvote. You supply a compilable example, stating clearly the areas that you don't understand. The numerical values are not obvious. I've upped it. – Bathsheba Jun 08 '18 at 10:24
  • @Galik, i was actually trying to see how macros work. and then i noticed this and wrote couple of lines to see what's actually being printed – rawwar Jun 08 '18 at 11:06

1 Answers1

10

'10' is a multicharacter literal; note well the use of single quotation marks. It has a type int, and its value is implementation defined. Cf. "10" which is a literal of type const char[3], with the final element of that array set to NUL.

Typically its value is '1' * 256 + '0', which in ASCII (a common encoding supported by C++) is 49 * 256 + 48 which is 12592.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • 1
    what do you mean by implementation defined? is it like, its dependent on compiler implementation? – rawwar Jun 08 '18 at 10:18
  • 3
    Read as: not something to depend on for portable code, or in this case, ever really. – Baldrick Jun 08 '18 at 10:19
  • 1
    @InAFlash: See https://stackoverflow.com/questions/2397984/undefined-unspecified-and-implementation-defined-behavior – Bathsheba Jun 08 '18 at 10:20
  • 1
    @InAFlash: Some folk even use multicharacters as `case` labels in `switch` blocks, although I'd advise against it due to the implementation defined nature of the label values. See https://stackoverflow.com/questions/45550674/this-source-code-is-switching-on-a-string-in-c-how-does-it-do-that/45550696#45550696 – Bathsheba Jun 08 '18 at 10:22
  • 3
    @Bathsheba As usual precise answer +1 :) – JeJo Jun 08 '18 at 12:04