how many times will strlen() be called in this for loop?

后端 未结 6 709
渐次进展
渐次进展 2020-12-03 14:46

Will the strlen() function below get called just once (with the value stored for further comparisons); or is it going to be called every time the comparison is performed?

6条回答
  •  一生所求
    2020-12-03 15:03

    It will be evaluated for every iteration of the loop (edit: if necessary).

    Like Tatu said, if word isn't going to change in length, you could do the strlen call before the for loop. But as Chris said, the compiler may be good enough to realize that word can't change, and eliminate the duplicate calls itself.

    But if word can change in length during the loop, then of course you'll need to keep the strlen call in the loop condition.

提交回复
热议问题