Sunday, 8 September 2013

Why is my output wrong with VS 2012 but right with g++?

Why is my output wrong with VS 2012 but right with g++?

When I run the g++ compiled version of my program, the correct sum of
18510000000 is returned. When I run the VS 2012 compiled program,
1330130816 is returned. Can anyone explain why this might be and how to
fix the code to work with both compilers? Thanks.
#include <cstdio>
#include <iostream>
#include <time.h> //includes <ctime> library
using namespace std;
int main() {
clock_t START1, END1;
int n1 = 15000000;
long sum1 = 0;
START1 = clock();
for(int i = 0; i < n1; i++) {
sum1 = sum1 + 1234;
}
END1 = clock();
long ExecutionTime1 = END1 - START1;
cout << "sum = " << sum1 << " Execution time = ";
printf("%.04f\n", (float)ExecutionTime1/CLOCKS_PER_SEC);
return 0;
}

No comments:

Post a Comment