LoginSignup
1
1

More than 5 years have passed since last update.

mistake > return (i++);

Last updated at Posted at 2015-06-20

引用: C++プログラミングの落とし穴 by Steve Oualline

int second(void)
{
    static int i = 0;
    return (i++);
}

上記のsecond()を3回まわして、1,2,3が出るのを期待していた。結果は0,1,2だった。

何故か?

return (i++);

はiの値を返してから++するから。

i++;は単独行で実行してからreturn iしましょう、とのこと。

1
1
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1