The manual for this Tcl C API function is at [L1 ].
A simple Tcl code snippet to produce a random integer from 0 to 255 is:
return [expr {int(rand()*(255+1))}]
The C function Tcl_ExprLongObj can be used to code this in C like this (assuming this is called from a Tcl interpreter 'interp'):
Tcl_Obj *exprPtr; /* pointer to hold the expression we want to evaluate */ long myresult; /* the integer holding the result of the expression */ exprPtr = Tcl_NewStringObj("int(rand()*255+1)",-1); /* this prepares the expression, which must be given as a Tcl_Obj */ /* now evaluate the expression in exprPtr and put the result into myresult */ if (Tcl_ExprLongObj(interp,exprPtr,&myresult) != TCL_OK) { return TCL_ERROR; }
DGP Perhaps worth noting that because Tcl_ExprLongObj() writes its result to a variable of type **long**, it implicitly performs the **int(.)** function as part of its nature. There's no need to include the **int(.)** call in the expression string.
Similar functions are Tcl_ExprDoubleObj, Tcl_ExprBooleanObj, and Tcl_ExprObj.
Category Tcl library |
---|