Is not using & with a variable in scanf() a syntax error or a runtime error?

大城市里の小女人 提交于 2019-12-02 02:52:33

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!