In some cases, NULL values are reported as 0 (zero)

printf() does not change anything. The effect that you see is due to the way how you wrote your code. In your code you are adding string to numerical array:

"bb = " + bb + "\n"; // where bb is a numerical array with Nulls

Despite innocent look 'plus' operator has different semantics depending on type of arguments. And you've got incompatible types here. For strings 'plus' means concatenate, for numbers it has mathematical meaning so if you write such code, AmiBroker must run some circles to make it possible. To do so it has to take SelectedValue() of array bb and convert it to string and then concatenate it with remaining strings.

But SelectedValue() the same way as LastValue() converts all Nulls to zero. You can check this:

x = SelectedValue( Null ); // x will be 0

That is why when you add a string to ARRAY filled with Nulls you will get them treated as zero.

Proper way to use printf() to print the numerical value is

printf("value is = %g\n", bb );

This will give you -1e10 for Nulls. Why? Because you are NOT adding string to numerical array here.

First string is a formatting string. It gets interpreted by printf(). Values should come afterwards. It is much faster and safer than "adding" strings to numerical arrays the way you do. If first string in printf() has any special characters like % it would get interpreted as formatting command and in case of absence of matching arguments would result in runtime errors. For these reasons, formatting string in printf() should be constant literal.

More read about types and coercion:

3 Likes