Declared but unset variable evaluates as true?

牧云@^-^@ 提交于 2019-12-01 23:07:12

You haven't set the initial value of program, so it defaults to a garbage value which just happens to be non-zero.

Set the initial value of program when you declare it:

BOOL program = NO; // or YES, whichever is appropriate

It is always a good practice to initialize all your variables when you declare them.

Also using scanf for input may be overdoing it, if I were you I would use fgets and then extract the information from the string using strtok. That way even if the user puts his elbow on the keyboard you will not have to worry. Alternatively if you are fond of scanf use sscanf on that string instead of strtok.

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