You are not logged in.
good morning everyone,
i have a question...
i know, that i can define variables so ...
long varname = 57 ;
but in my book in c++ stays, that i can write variables so ...
12L
the L is for long stays in my book... but my book has no example for that...
how does this write method work, can you show me an example please? how should i write to define a variable "varname" with the value "57" in long in that method in second quote?
Last edited by lo7777799 (Yesterday 09:03:22)
Offline
#include <iostream>
#include <typeinfo>
int main()
{
const auto a = 12;
std::cout << typeid(a).name() << std::endl; // "i" = int
const auto b = 12L;
std::cout << typeid(b).name() << std::endl; // "l" = long
}
Offline
ok thanks ...
is this write-method "12L" only with the c++-command "auto" possible or is that also otherwise possible?
is 12 in this method the value? should i write this ?
auto varname = 57L ?
to create a variable called varname with type long and the value 57 ?
Offline
is this write-method "12L" only with the c++-command "auto" possible or is that also otherwise possible?
C++ has no "commands".
Integer literal suffixes are clearly explained here: https://en.cppreference.com/w/cpp/language/integer_literal
Offline
The suffix determines the type of the value itself. `57L` is of type `long int`, the `57L` literal itself. It’s unrelated to what you assign it to later.
`auto` in bobbes’ case is used only to make expressions clearer.
You may do this directly too:
#include <iostream>
using ::std::cout;
int main()
{
cout << typeid(12).name() << '\n'; // i → signed int
cout << typeid(12L).name() << '\n'; // l → signed long
cout << typeid(12LL).name() << '\n'; // x → signed long long
cout << typeid(12U).name() << '\n'; // j → unsigned int
cout << typeid(12ULL).name() << '\n'; // y → unsigned long long
// … or, alternatively …
cout << '\n' << (typeid(12) == typeid(int)) << '\n';
cout << (typeid(12L) == typeid(long)) << '\n';
cout << (typeid(12LL) == typeid(long long)) << '\n';
cout << (typeid(12U) == typeid(unsigned int)) << '\n';
cout << (typeid(12ULL) == typeid(unsigned long long)) << '\n';
return 0;
}
Sometimes I seem a bit harsh — don’t get offended too easily!
Offline