% d with Long Int

Is the following code correct? As far as I understand, it should not work properly, but in the Dev-C ++ compiler it does. Can someone explain in detail?

#include<limits.h>

int main()
{
long int num_case=LONG_MAX;

scanf("%d",&num_case);

printf("%ld",num_case);
return 0;
}

thank

+3
source share
3 answers

Like most things the standard C library tells you not to do, it causes undefined behavior. undefined means that it may work under certain conditions, but fail when you least expect it.

, long int int : , . (, x86-64 Linux) , , , - . , 8- long int .

: " ", . long int C . , , , . . C , , .

+4

RageD, % ld scanf(). , , , (, , ) int long int (, 4), scanf() - ./p >

+1

Typically, on 32-bit systems, long int has 32 bits (same as int), and on 64-bit systems long int has 64 bits (same as long long int). If you want your code to be portable, use scanf with "% ld".

0
source

All Articles