Язык C
Код
#include <stdio.h>
int main(int argc, char *argv[])
{
long long temp1 = 1000 * 60 * 60 * 30 * 24 * 12 * 2007;
long long temp2 = 1000ll * 60 * 60 * 30 * 24 * 12 * 2007;
printf("%I64d\n%I64d\n", temp1, temp2);
return 0;
}
Компилятор от майкрософт, из Windows SDK 2008. Выдаёт предупреждение, неплохо.
Код
C:\temp>cl /Wall main.c
Microsoft (R) C/C++ Optimizing Compiler Version 14.00.50727.762 for x64
Copyright (C) Microsoft Corporation. All rights reserved.
main.c
L:\Program Files\Microsoft SDKs\Windows\v6.0\VC\Include\stdio.h(62) : warning C4820: '_iobuf' : '4' bytes padding added after data member '_cnt'
L:\Program Files\Microsoft SDKs\Windows\v6.0\VC\Include\stdio.h(360) : warning C4255: '_get_printf_count_output' : no function prototype given: converting '()' to '(void)'
main.c(5) : warning C4307: '*' : integral constant overflow
main.c(5) : warning C4307: '*' : integral constant overflow
main.c(5) : warning C4307: '*' : integral constant overflow
main.c(3) : warning C4100: 'argv' : unreferenced formal parameter
main.c(3) : warning C4100: 'argc' : unreferenced formal parameter
Microsoft (R) Incremental Linker Version 8.00.50727.762
Copyright (C) Microsoft Corporation. All rights reserved.
/out:main.exe
main.obj
C:\temp>main.exe
-1621647360
62425728000000
Компилятор GCC из набора i686-8.1.0-posix-dwarf-rt_v6-rev0. Тоже выдаёт предупреждение.
Код
C:\temp>gcc -Wall main.c
main.c: In function 'main':
main.c:5:40: warning: integer overflow in expression of type 'int' results in '-1702967296' [-Woverflow]
long long temp1 = 1000 * 60 * 60 * 30 * 24 * 12 * 2007;
^
C:\temp>a.exe
-1621647360
62425728000000
Страшно нелюбимый многими компилятор от фирмы борланд из C++ Builder 6. Так-же выдаёт предупреждение.
Код
Borland C++ 5.6.4 for Win32 Copyright (c) 1993, 2002 Borland
main.c:
Warning W8056 main.c 5: Integer arithmetic overflow in function main
Warning W8056 main.c 5: Integer arithmetic overflow in function main
Warning W8056 main.c 5: Integer arithmetic overflow in function main
Warning W8057 main.c 11: Parameter 'argc' is never used in function main
Warning W8057 main.c 11: Parameter 'argv' is never used in function main
Turbo Incremental Link 5.64 Copyright (c) 1997-2002 Borland
C:\TEMP>main
-1621647360
62425728000000
C#, компилятор из Windows SDK 2008.
Код
using System;
class CMain
{
static void Main(string[] args)
{
long temp = 1000 * 60 * 60 * 30 * 24 * 12 * 2007;
Console.WriteLine(temp);
}
}
Критическая ошибка, уже лучше.
Код
E:\csc>csc overflow.cs
Версия компилятора Microsoft (R) Visual C# 20058.00.50727.7905
для Microsoft (R) Windows (R) 2005 Framework версии2.0.50727
Авторские права (C) Microsoft Corporation 2001-2005. Все права защищены.
overflow.cs(7,15): error CS0220: Переполнение при выполнении операции во время
компиляции в режиме с проверкой
overflow.cs(7,15): error CS0220: Переполнение при выполнении операции во время
компиляции в режиме с проверкой
overflow.cs(7,15): error CS0220: Переполнение при выполнении операции во время
компиляции в режиме с проверкой
Java, компилятор openjdk-13.0.1.
Код
class Overflow
{
public static void main(String[] args)
{
long temp1 = 1000 * 60 * 60 * 30 * 24 * 12 * 2007;
long temp2 = 1000L * 60 * 60 * 30 * 24 * 12 * 2007;
System.out.println(temp1);
System.out.println(temp2);
}
}
Не выдаёт даже предупреждения. Ну как же так?
Код
C:\temp>javac -version -Xlint:all Overflow.java
javac 13.0.1
C:\temp>java Overflow
-1621647360
62425728000000
Delphi.
Код
program overflow;
var
temp : Int64;
begin
temp := 1000 * 60 * 60 * 30 * 24 * 12 * 2007;
WriteLn(temp);
end.
Компилятор Codegear Delphi 2007, критическая ошибка. Нужен явный каст к Int64.
Код
C:\temp>dcc32 -CC overflow.pas
CodeGear Delphi for Win32 compiler version 18.5
Copyright (c) 1983,2007 CodeGear
overflow.pas(6) Error: E2099 Overflow in conversion or arithmetic operation
overflow.pas(6) Error: E2099 Overflow in conversion or arithmetic operation
overflow.pas(8)
Компилятор FreePascal 3.0.4 x64.
Код
C:\temp>ppcrossx64 -vw -WC -Mdelphi overflow.pas
C:\temp>overflow
62425728000000
Никаких ошибок и предупреждений, и результат верный. Я и не знал, какой уникально хороший компилятор я использую.