跟随C代码编译错误的原因

时间:2013-12-18 14:29:16

标签: c compilation syntax-error

‪#include‬ <stdio.h>

void main(void)
{
    char cValue='a';
    int iValue=1234567;
    long 1Value=7890123;
    float fValue=3.141592;
    double dValue=3.141592;
    char*string="korea";
    char buffer[100];

    sprintf(buffer,"char type is %c", cValue);
    puts(buffer);

    sprintf(buffer,"int type is %d", iValue);
    puts(buffer);

    sprintf(buffer,"long type is %1d", 1Value);
    puts(buffer);

    sprintf(buffer,"float type is %f", fValue);
    puts(buffer);

    sprintf(buffer,"double type is %e", dValue);
    puts(buffer);

    sprintf(buffer,"char* type is %s", string);
    puts(buffer);
}

当我使用此代码编译时, 发生了语法和其他错误。

此代码有什么问题?

错误讯息:

76\76.c(7) : error C2059: syntax error : 'bad suffix on number'
76\76.c(7) : error C2143: syntax error : missing ';' before 'constant'
76\76.c(7) : warning C4091: ' ' : ignored on left of 'long ' when no variable is declared
76\76.c(7) : error C2146: syntax error : missing ';' before identifier 'Value'
76\76.c(7) : error C2065: 'Value' : undeclared identifier
76\76.c(8) : error C2143: syntax error : missing ';' before 'type'
76\76.c(9) : error C2143: syntax error : missing ';' before 'type'
76\76.c(10) : error C2143: syntax error : missing ';' before 'type'
76\76.c(11) : error C2143: syntax error : missing ';' before 'type'
76\76.c(13) : error C2065: 'buffer' : undeclared identifier
76\76.c(13) : warning C4047: 'function' : 'char *' differs in levels of indirection from 'int '
76\76.c(13) : warning C4024: 'sprintf' : different types for formal and actual parameter 1
76\76.c(14) : warning C4047: 'function' : 'const char *' differs in levels of indirection from 'int '
76\76.c(14) : warning C4024: 'puts' : different types for formal and actual parameter 1
76\76.c(16) : warning C4047: 'function' : 'char *' differs in levels of indirection from 'int '
76\76.c(16) : warning C4024: 'sprintf' : different types for formal and actual parameter 1
76\76.c(17) : warning C4047: 'function' : 'const char *' differs in levels of indirection from 'int '
76\76.c(17) : warning C4024: 'puts' : different types for formal and actual parameter 1
76\76.c(19) : warning C4047: 'function' : 'char *' differs in levels of indirection from 'int '
76\76.c(19) : warning C4024: 'sprintf' : different types for formal and actual parameter 1
76\76.c(19) : error C2059: syntax error : 'bad suffix on number'
76\76.c(19) : error C2146: syntax error : missing ')' before identifier 'Value'
76\76.c(19) : error C2059: syntax error : ')'
76\76.c(20) : warning C4047: 'function' : 'const char *' differs in levels of indirection from 'int '
76\76.c(20) : warning C4024: 'puts' : different types for formal and actual parameter 1
76\76.c(22) : warning C4047: 'function' : 'char *' differs in levels of indirection from 'int '
76\76.c(22) : warning C4024: 'sprintf' : different types for formal and actual parameter 1
76\76.c(22) : error C2065: 'fValue' : undeclared identifier
76\76.c(23) : warning C4047: 'function' : 'const char *' differs in levels of indirection from 'int '
76\76.c(23) : warning C4024: 'puts' : different types for formal and actual parameter 1
76\76.c(25) : warning C4047: 'function' : 'char *' differs in levels of indirection from 'int '
76\76.c(25) : warning C4024: 'sprintf' : different types for formal and actual parameter 1
76\76.c(25) : error C2065: 'dValue' : undeclared identifier
76\76.c(26) : warning C4047: 'function' : 'const char *' differs in levels of indirection from 'int '
76\76.c(26) : warning C4024: 'puts' : different types for formal and actual parameter 1
76\76.c(28) : warning C4047: 'function' : 'char *' differs in levels of indirection from 'int '
76\76.c(28) : warning C4024: 'sprintf' : different types for formal and actual parameter 1
76\76.c(28) : error C2065: 'string' : undeclared identifier
76\76.c(29) : warning C4047: 'function' : 'const char *' differs in levels of indirection from 'int '
76\76.c(29) : warning C4024: 'puts' : different types for formal and actual parameter 1

2 个答案:

答案 0 :(得分:6)

long 1Value=7890123;

您不能将数字用作变量名的第一个字符。

答案 1 :(得分:1)

两个错误:

  1. main必须返回int
  2. 变量1Value被非法命名。标识符不能以字母或下划线以外的任何内容开头。
  3. 当您试图弄清楚语法错误是什么时,请从顶部开始。考虑错误:

      

    76 \ 76.c(7):错误C2059:语法错误:'数字后缀错误'

    转到第7行并检查它:

    long 1Value=7890123;
    

    它可能有什么问题?在你搞清楚之前不要离开那条线!当你这样做。重新编译并查看是否能修复所有其他错误。如果没有,请解决下一个第一个错误。