Can anyone help me to know more about Data Types? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can anyone help me to know more about Data Types?

https://code.sololearn.com/czaGTBED3kCn/?ref=app 👆👆👆 This code returns summation of a long and int.. what will be the data type of the summation?? long?? And there is a multi-line comment.. I have attached the link of the writer.. Since he is not active for a long time I am asking you.. Can you explain that comment?. I mean 32 bits?? In Java I learned long takes 8 bits.. may be.. I am sorry I forgot.. 8 or 16 bits.. But 32 bits?Above, Does the variable a take 32 bits?? And what about others.. I mean String, float, double, char in C++?? And yeah, How to check data type of a variable??

12th Apr 2021, 4:48 PM
Mohammad Mehedi Hasan
Mohammad Mehedi Hasan - avatar
5 Answers
+ 1
Yes, the result will be a long value, which can be confirmed by including <type_traits> and printing cout << is_same_v< common_type_t< int, long >, long >; The gist is that the result should be at least as wide as the wider operand, but for more detailed information, have a look at: https://www.nayuki.io/page/summary-of-c-cpp-integer-rules https://en.cppreference.com/w/cpp/language/operator_arithmetic An integer is at least 2 byte or 16 bit wide, as guaranteed by the standard. Remember, the writer talked about bits, while you probably learned that in Java, a long has 8 bytes (1 byte = 8 bits). The size of a data type may vary between platforms. You can query the size of any type via the sizeof() operator. Typical values are sizeof( float ) = 4, sizeof( double ) = 8. sizeof( char ) is always 1. You can obtain information about the type of something via typeid(), but this shouldn't be necessary. Since C++ is a strongly typed language, every data type is fixed and known at compile time.
12th Apr 2021, 5:36 PM
Shadow
Shadow - avatar
+ 1
Do you mean this? cout << is_same_v< common_type_t< int, long >, long >; It's a short code snippet to verify the result is indeed a long. To be simple, std::common_type<> determines the type all its arguments can be implicitely converted to, and std::is_same<> yields true if both arguments are the same type, false otherwise. This is basic template metaprogramming, but likely to be over your head for now. I just included it to show a means of verification. To be able to use typeid(), you need to include the <typeinfo> header. Reference along with an example can be found here: typeid: https://en.cppreference.com/w/cpp/language/typeid <typeinfo>: https://en.cppreference.com/w/cpp/header/typeinfo
12th Apr 2021, 5:53 PM
Shadow
Shadow - avatar
0
Shadow What did you say in second paragraph.. I mean in 4th and 5th line?
12th Apr 2021, 5:40 PM
Mohammad Mehedi Hasan
Mohammad Mehedi Hasan - avatar
0
Shadow typeid() isn't working :/
12th Apr 2021, 5:45 PM
Mohammad Mehedi Hasan
Mohammad Mehedi Hasan - avatar
0
Shadow Yeah.. Ok then I should try this after learning more about cpp.. Thanks
12th Apr 2021, 5:59 PM
Mohammad Mehedi Hasan
Mohammad Mehedi Hasan - avatar