2013-12-18 54 views
-5
‪#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); 
} 

当我用这段代码编译时, 出现语法和其他错误。下面的C代码编译错误的原因

这段代码有什么问题?

错误消息:

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 
+1

这些错误是什么? – exexzian

+0

'char * string =“korea”;''char *'后的空格不会杀死你。 – Maroun

+1

提到所有的错误....... –

回答

1

两个错误:

  1. main必须返回一个int
  2. 可变1Value非法命名。标识符不能以字母或下划线以外的任何字符开头。

当您试图弄清楚语法错误是什么时,从顶部开始。考虑错误:

76\76.c(7) : error C2059: syntax error : 'bad suffix on number'

进入到第7行检查它:

long 1Value=7890123; 

什么都不可能是错的呢?不要从这一行开始,直到找出结果!当你这样做。重新编译,看看是否修复了所有其他错误。如果没有,解决下一个第一个错误。

+1

+1用于调试提示:'从顶部开始'和'不要从该行移动,直到找出结果!'。编译器在语法错误将它们抛入gungeland时发出巨大的垃圾而臭名昭着。 –

6
long 1Value=7890123; 

不能使用数字作为变量名的第一个字符。

+0

似乎OP想将它命名为“lValue”,在检查int值后是“iValue” – exexzian

+2

最好不要单独使用l(ell),即没有上下文。 –