error C2143: syntax error : missing ';' before 'type'

原文:http://blog.csdn.net/w343051232/article/details/7074788

由于cpp编译下列代码报错

  1. #include <cstdio>  
  2. #include <cstring>  
  3.   
  4. int main()  
  5. {  
  6.   
  7.    wchar_t wstr[] = L"Hello!";  
  8.    int wlen = strlen(wstr);  
  9.    wprintf(L"%ls",wstr);  
  10.    int len;  
  11.   
  12.       
  13.   
  14.     return 0;  
  15. }  

error C2664: 'strlen' : cannot convert parameter 1 from 'unsigned short [7]' to 'const char *'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

于是将后缀名改成.c

却出现另外一个诡异的编译错误:error C2143: syntax error : missing ';' before 'type' 并且指向 int len;这行

将代码做如下修改却诡异般的编译过了

  1. #include <cstdio>  
  2. #include <cstring>  
  3.   
  4. int main()  
  5. {  
  6.   
  7.    wchar_t wstr[] = L"Hello!";  
  8.    int len;  
  9.    int wlen = strlen(wstr);  
  10.    wprintf(L"%ls",wstr);  
  11.   
  12.       
  13.   
  14.     return 0;  
  15. }  

不知道什么原因!!找了找资料。。。。原来It is a rule! 在纯C中,在一个代码块中变量声明必须在最前面
阅读(773) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
posted on 2016-01-25 16:43  玄冬  阅读(518)  评论(0编辑  收藏  举报