Visual Studio2013终于开始比较良好地支持C99特性了。在此之前,如果用C语言写代码的话,变量名都需要放到函数体的前面部分,代码写起来十分别扭。
而Visual Studio2013中的C编译器已经支持了不少C99标准,让我来为大家盘点一下。
现在仍然不支持的语法特性有:
1、inline关键字:在VC中,仍然需要用微软自己定义的__inline,而尚不支持inline,尽管inline在C++中是支持的。
2、restrict关键字。
3、_Complex与_Imaginary:尽管VS2013的C语言编译器可以用complex.h库,不管这两个关键字不支持。库的实现用的是描述复数的结构体。
4、变长数组
除了上述四点,其它主要特性都予以了支持。下面给出一个示例代码来给出支持特性的描述:
#include <stdio.h>
#define MY_PRINT(...) printf(__VA_ARGS__)
typedef __int64 tb_int64_t;
typedef tb_int64_t tb_long_t;
static __inline int MyGetMax(int x, int y)
{
return x > y ? x : y;
}
int g_charset_ucs4_to_gb2312_table_data[]={1,2,3};
static int tb_charset_gb2312_from_ucs4(int ch)
{
// is ascii?
//if (ch <= 0x7f) return ch;
// find the gb2312 character
tb_long_t left = 0;
tb_long_t right = (ch / sizeof(g_charset_ucs4_to_gb2312_table_data[0])) - 1;
while (left <= right)
{
}
return 0;
}
void test_vs2013_for_c99()
{
tb_charset_gb2312_from_ucs4(23);
MY_PRINT("%s\n", "Hello, world!");
int arr[] = { [0] = 100, [2] = 200, [8] = 400 };
MY_PRINT("The value is: %d\n", arr[2] + arr[3]);
struct Test
{
int x;
float f;
_Bool b;
long long ll;
}test = {.x = 10, .f = -0.5f, .b = 0};
struct Test t = (struct Test){ .ll = 100LL };
for (int i = 0; i < test.x; i++)
t.x += test.x;
int *p = (int[]){ [1] = t.x, [3] = test.b + test.ll };
int a=1;
int b=2;
int c=a+b;
printf("c=%d\n",c);
int d=c*b;
printf("d=%d\n",d);
MY_PRINT("The value is: %d\n", p[0] + p[1] + p[2] + p[3]);
}
//测试vs2013对c99的支持情况和解决“将此类型用作表达式非法”的问题
//使用纯C语言写代码的话,变量名都需要放到函数体的前面部分,现在只需要在if语句后面添加块号{}即可
int main(void)
{
test_vs2013_for_c99();
int ch=56;
// is ascii?
// if (ch <= 0x7f)return -1; 这样子在vs2013编译出错,需要添加块{}
if (ch <= 0x7f)
{
return -1;
}
// find the gb2312 character
tb_long_t left = 0;
tb_long_t right = (ch / sizeof(g_charset_ucs4_to_gb2312_table_data[0])) - 1;
while (left <= right)
{
}
if (ch <= 0x7f)
return 0;
return 0;
}