Some Common Syntax Errors and WarningsCreated by Theodora Yeung |
This is just a few of the possible syntax errors you may
have come across when programming in C using MSVC.
Under each of the errors or warnings is listed the possible cause(s)
and a possible fix. Please note that you may get the same error or warning
for totally different reasons.
If you got one or more of the listed warnings, do NOT ignore them.
This usually means that you are missing a close
curly bracket ( }) near the end of the file. Double check that your curly
brackets
are matching.
This error is usually caused by syntax error at printf statements.
Possibilities:
1) missing a close quotation ( " ) at the end of the string that you want
to print.
example: printf ( " string I want to
print );
2) the string is span over
2 or more lines (separated by return character, or you have hit return
to break the string into 2 or more lines)
example: printf ( " continue printing,
don't want a wrap around on printout " ) ;
fix: a) add in
quotation marks
printf ( " constinue printing, "
" don't want to wrap around on printout "
) ;
b) 2 separate printfs
printf ( " continue printing, " ) ;
printf ( " don't want to wrap around on printout " ) ;
error C2017: illegal escape sequence
Chances are you have a misplaced escape character like " \n " or " \t ".
example: "\n" is placed outside quotation
of a printf statement.
printf ( " printing "
\n );
fix:
printf ( " printing \n " );
error C2059:syntax error : 'type' / syntax error : ';'
The cause that I know (more than likely, there will
be billions other causes), you have two function with the same name but
different
return type. These errors follow other error like
" error C2061: syntax error : identifier 'function1' ", where 'function1'
is the name of
the function that caused the error ( in the case
I know )
exmaple: function prototype of 2 functions with
the same name but different return type.
int function1( int );
bool function1( int );
fix: Rename one of them (but chances are you probably only need one of them or one of them is a result of a typo).
error C2059:syntax error : '}'
If
you are not missing a ' } ' at the line that the error message points to,
you are probably missing one somewhere above that point.
You could also get this error message if you have too many ' }'. Double
check that your curly brackets ( ' { ' and ' } ' ) are matching
correctly.
error C2059: syntax error : ')'
You probably got this error because your parenthesis are not matching correctly.
Take a careful look at the line the error message
points to.
error C2143: syntax error : missing ';' before 'type'
If this error message points you to the begining of a function, you are missing a ' } ' before that line.
example:
void function1(int s){
if ( s > 0) {
printf ( " s > 0 " );
}
else {
printf ( " s <= 0 " );
}
/* you are missing a ' }' right here */
/* there are 3 '{ ' in this function, (1
at the begining of the function, 1 from the if statements and
1 from the else statement) but only 2 ' } ' */
void function2 (int i) { /* if
the error message points to this line */
printf( " CSE /ENGR 142 " ) ;
}
error C2146: syntax error : missing ')' before identifier 'i' ( at printf statements)
You are probably not missing
a ' ) ' but a comma ( , ) instead.
example:
printf ( " printing %d \n " i );
fix:
printf ( " printing %d \n " , i );
error C2296:'%'
: illegal, left operand has type 'const double '
error C2297:
'%' : illegal, right operand has type 'const double '
Both operands of the mod
operator ( % ) have to be of type int. It doesn't work with type double.
example:
int s = 1.00000 % 9.999999;
error C2100: illegal indirection
You probably have de-referenced a variable that is
not a pointer. You may also get this error if you put a pointer as the
index
to an array.
example:
int i = 0, j =0; /* 2 integer variable
*/
int *k; /* pointer to integer */
*k = *i /* i is not a pointer, cannot
be de-referenced */
j = **k ; /* k is a pointer, de-referenced
once (*k) gives an int, de-reference again......... some case as *i */
error C2106: '=' : left operand must be l-value
You are probably assigning
an array to another, you CANNOT do this. If not you are attempting to assign
a #define variable or
a constant variable.
error C2371:
'func' : redefinition; different basic types
see declaration of 'func' (' func' is the name of a function)
warning C4029:
declared formal parameter list different from definition
These errors and warnings usually means that your function prototype and the corresponding function are not matching.
example:
a) return types not matching, result in error C2371
int func(void) ; /* function prototype at the top of the file */
:
:
:
void func(void){ /* implementation of the function func */
/* function body */
}
b) parameters not matching in type and/or number, result in warning C4029
void func(void) ; /* function prototype at
the top of the file */
:
:
:
void func(int, int){
/* implementation of the function func */
/* function body */
}
warning C4013:
'func' undefined; assuming extern returning int
('func' is the name of a function)
This probably means you are missing a matching function prototype.
warning C4715:'func'
: not all control paths return a value
('func' is the name of a function)
If your function has a return type other void, you
have to make sure that every single path has a return statement. This happens
when you
have loops, if and/or switch statements.
example:
int func (void) {
while ( condition_1 is true ){
/* when the program is compiled, the compiler does not konw whether condition_1
will
true or not. If it is never true, the body of the loop will not be executed.
Therefore, nothing
will return, the function got stuck */
if( condition_2 ) {
/* Even if it did get to execute this loop, same thing apply to condition_2
*/
/* do something */
return 0;
}
}
return 0; /* fix:
Sometimes, you design the function to have condition_1
and condition_2 be true at execution, but the compiler does not know that.
Put a return statement at the end of the function to get rid of the warning.
Even all the condition fails, something is returned. Sometimes, you
are really missing a return statement*/
}
warning C4700:local variable 'i' used without having been initialized
example:
int v;
/* at this point, v holds some junk number got left in memory*/
s = 9 + v; /* with this
assignment ( junk number + 9 ), s is now some junk number */
warning C4047: '=' : differs in levels of indirection
You are probably assigning
one variable to another which are of different type. You get this when
you are using arrays and pointers.
Another possibility is when
you make a function call, you pass in parameters that do not match the
types of the parameters declared.
Check your function prototype.
example:
a) int i;
int* ptrToInt;
:
:
i = ptrToInt; /*
assining pointer to int, you really don't need to do this in cse/engr142
*/
b) int arrayOfInt[10];
int* ptrToInt;
:
:
arrayOfInt = ptrToInt;
c) void function1 (int* intPointer);
:
:
int arrayOfInt[10];
function1(arrayOfInt);
warning C4005: macro redefinition
This warning has to do with #define variables.
Possibilities of getting this warning:
1) The straight forward reason (usually not the case though)
You have #defined the same
variable twice or more.
2) Perhaps a MSVC problem
If you have #define a variable,
the first time you won't get this
warning, not until you change
the value of the #define , perhaps
the third time. Since then,
you will get this warning every single
time you compile.
fix:
use Rebuild All or Clean (under the menu bar Build)