问题
I was taught that a syntax error comes from a single mistake and doesn't execute the program. However, a runtime error still starts the program but crashes in the middle. If I accidentally didn't put & in front of a variable in a call to scanf(), would that considered to be a syntax error or a runtime error?
For example
int main(void)
{
int a;
printf("input a integer number >>");
scanf("%d\n", a);
printf("the input number is %d\n", a);
return 0;
}
In the scanf statement, I don't have & in front of a so that would crash,
but it still executes the first printf statement, so it would show some printed text.
Is it going to be both a runtime error and a syntax error?
回答1:
Depends on compilers intelligence
Some compilers are made intelligent to warn you for such obvious run-time errors
回答2:
Technically it would be both. It doesn't recognize it as an error until runtime. However as stated on this page scanf expects pointers for the formatted variables therefore not preceding the variable 'a' with (&) is syntactically incorrect. Most would call this a runtime error and that is exactly what I would call it but its not completely wrong to call it a syntax error. It is however more thought of as a runtime error since there is no error until it runs.
来源:https://stackoverflow.com/questions/34459879/is-not-using-with-a-variable-in-scanf-a-syntax-error-or-a-runtime-error