int arrSize = 10;
double* arr = new double [arrSize];
for(int i = 0; i < arrSize; i++){
arr[i] = i;
}
for(int i = 0; i < arrSize; i++){
printf("%d", arr[i]);
//std::cout<<arr[i];
}
Here
printf()prints 0000000000.coutprints 0123456789.
Why ?
Using a wrong format specifier for any particular argument in
printf()invokes undefined behaviour.arris adoublearray, hencearr[i]produces adoubletype value. you need%fformat specifier to print that. If you use%dto print adouble, your program faces UB and the result cannot be justified, in can be anything.OTOH, the
<<used withcoutis an overloaded operator which can adapt based on the type of the supplied variable. So, it prints the output as expected.