下面的代碼編譯細Linux上使用gcc -std = C99,但得到的Visual Studio 2010中C編譯器如下錯誤:爲什麼Visual Studio C編譯器不這樣?
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86 Copyright (C) Microsoft Corporation. All rights reserved. fib.c fib.c(42) : error C2057: expected constant expression fib.c(42) : error C2466: cannot allocate an array of constant size 0 fib.c(42) : error C2133: 'num' : unknown size
用戶輸入斐波那契數來生成的量。我很好奇爲什麼Microsoft編譯器不喜歡這個代碼。
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
void fib(int max);
int main(int argc, char *argv[])
{
int argument;
if (argc != 2)
{
puts("You must supply exactly one command line argument.");
return 0;
}
argument = atoi(argv[1]);
if (argument == 0)
{
puts("You gave us 0 or an invalid integer.");
return 0;
}
else if (argument < 0)
{
puts("You gave us a negative integer.");
return 0;
}
else if (argument == INT_MAX)
{
puts("You gave us a number that's too big to fit in an integer.");
return 0;
}
printf("%d\n", argument);
fib(argument);
return 0;
}
void fib(int max)
{
int num[max]; /// <- Line 42
int i;
for (i = 0; i < max; i++)
{
if (i == 0)
num[i] = 0;
else if (i == 1)
num[i] = 1;
else
num[i] = num[i-1] + num[i-2];
printf("%d\t%d\n", i, num[i]);
}
}
見http://stackoverflow.com/questions/146381/visual-studio-support-for-new-c-c-standards –