Will "long i = 1;" cause implicit type conversion in C?

If I write " long i = 1;" instead of " long i = 1l;" will be recognized 1as int, and then implicitly converted to long?

Edit: Thanks everyone. I do not see type conversion. Does this also have a suffix u(e.g. 10u)? Then what use are these land u?

+3
source share
5 answers

The compiler will see what you are trying to assign, and immediately set the value to 1. Type conversion does not occur with a literal. Even if you said long x = 1.0, you won’t see a runtime type conversion.

, Windows, long int , .

[: , Windows; ]

-1

1 - int, , .

:

int main(void)
{
    long long i = -2147483648;
    long long j = -2147483647 - 1;

    printf( " i is %lld, j is %lld\n", i, j);

    return(0);
}

:

  • MSCV 9 ( 15.00.21022.08):

                i is 2147483648, j is -2147483648
    
  • GCC (3.4.5):

                i is -2147483648, j is 0
    
  • (4.3.10.1):

                i is 2147483648, j is -2147483648
    
  • :

                i is -2147483648, j is -2147483648
    

, . :

  • C90 C99 ( long long "- C99, C90 " long long " )
  • undefined

FWIW, MSVC Comeau - , - , . ( ) :

  • -2147483648 '-' 2147483648
  • 2147483648 int ( int - , C99 )
  • "-" 2147483648 -
  • .

:

  • -2147483647 '-' 2147483647
  • 2147483647 - int
  • 1 -2147483648, ,
  • .
+3

, , long, .

+1

, , , = 1l; .

,

long = (unsigned int) -1;

, , , .

+1

.

0

Source: https://habr.com/ru/post/1718671/


All Articles