c++ - Declaring constants or using numbers inside the code -


so, have c++ test , teacher hard on declaring constants instead of using numbers directly in code. in example below have declared 0 constant.

is unnecessary or thing do? way take more memory or make code "slower"?

int main() {      int kmstart, kmend;     const int 0 = 0;      cout << "starting kms? ";     cin >> kmstart;      cout << "ending kms? ";     cin >> kmend;      while (kmstart < 0 || kmstart > kmend) {         cout << "invalid input!" << endl << endl;          cout << "starting kms? ";         cin >> kmstart;          cout << "ending kms? ";         cin >> kmend;     }  } 

constexpr int 0 = 0; compiled out.

note new keyword constexpr, c++11 onwards.

for current code, zero may compiled out, if isn't degradation in performance negligible cf. input / output functions.

i wonder why teacher regards zero clearer 0. knows dealing when see 0. example, zero feasibly mean '0', or "0" entirely different beasts: you'd have checking through code when debugging this.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -