+ 1

Why is the output of the program is 0.

#include <iostream> using namespace std; int main() { int x; int y=2; cout<<x%y; return 0; }

4th Oct 2017, 8:16 AM
Anshul Joshi
Anshul Joshi - avatar
1 Answer
+ 14
It seems SL compiler sets variable x (which is uninitialized) to 0. So, 0 % 2 gives you 0. But it's a good practice to initialize your variable to something meaningful. (e.g. int x = 0;)
4th Oct 2017, 8:30 AM
Babak
Babak - avatar