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.cout
prints 0123456789.
Why ?
Using a wrong format specifier for any particular argument in
printf()
invokes undefined behaviour.arr
is adouble
array, hencearr[i]
produces adouble
type value. you need%f
format specifier to print that. If you use%d
to print adouble
, your program faces UB and the result cannot be justified, in can be anything.OTOH, the
<<
used withcout
is an overloaded operator which can adapt based on the type of the supplied variable. So, it prints the output as expected.