Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String and integer multiplication in C++

Tags:

c++

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

like image 933
InAFlash Avatar asked Dec 24 '22 06:12

InAFlash


1 Answers

'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.

like image 164
Bathsheba Avatar answered Dec 25 '22 20:12

Bathsheba