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

7
  • 4
    '10' is not a string?! Commented Jun 8, 2018 at 10:08
  • 3
    @InAFlash Enable more compiler warnings. Commented Jun 8, 2018 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 Commented Jun 8, 2018 at 10:12
  • 2
    Side note: circleArea() would be much better as a function rather than a macro. Commented Jun 8, 2018 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. Commented Jun 8, 2018 at 10:24

1 Answer 1

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.

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

5 Comments

what do you mean by implementation defined? is it like, its dependent on compiler implementation?
Read as: not something to depend on for portable code, or in this case, ever really.
@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 stackoverflow.com/questions/45550674/…
@Bathsheba As usual precise answer +1 :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.