You are not logged in.

#1 Yesterday 09:00:46

lo7777799
Member
Registered: 2023-07-25
Posts: 57

C++ - how can i write variables as "57L" = long?

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?  smile

Last edited by lo7777799 (Yesterday 09:03:22)

Offline

#2 Yesterday 12:23:34

bobbes
Member
Registered: 2025-04-13
Posts: 3

Re: C++ - how can i write variables as "57L" = long?

#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

#3 Yesterday 15:43:42

lo7777799
Member
Registered: 2023-07-25
Posts: 57

Re: C++ - how can i write variables as "57L" = long?

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

#4 Yesterday 18:29:59

dimich
Member
From: Kharkiv, Ukraine
Registered: 2009-11-03
Posts: 353

Re: C++ - how can i write variables as "57L" = long?

lo7777799 wrote:

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

#5 Yesterday 21:41:42

mpan
Member
Registered: 2012-08-01
Posts: 1,398
Website

Re: C++ - how can i write variables as "57L" = long?

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

Board footer

Powered by FluxBB