for loop weiredness

Started by sunmountain, March 14, 2011, 10:14:58 PM

Previous topic - Next topic

sunmountain

Trivial:

> (for (i 1 10 1) (println i))
1
2
3
4
5
6
7
8
9
10


Now this:

> (import "msvcrt.dll" "printf")
printf<7714C5B9>
> (for (i 1 10 1) (printf "%in" i))
0
0
0
0
0
0
0
0
0
0


Could someone please explain what is going on here ?

HPW

#1
My guess: You import the DLL-function printf and call it. The 0 is the returned value from the function-call.
Hans-Peter

kosh

#2
The variable type of i is maybe `double' (newlisp's float type).

printf %i (dll or libc.so) requires a `int' argument.

Therefore, the output will be broken because of type mismatches.


(for (i 1 10 1) (println (float? i))) ;-> true ...

The solution is use "int" function, or %f, %g format.


(printf "%in" (int i))
(printf "%gn" i)


# P.S.

If num-step (`for' primive argument) is not specified, the variable type i will be `int' .


(for (i 1 10) (println (integer? i))) ;-> true ...