In my process of C code optimization*/
return (curr < 0 ? : ((curr > 255) ? 255 : curr));
/* I want the int value to be clipped between 0 to 255.
Is there any other way to optimize above code snippet? */
@Banfa: That will wrap a value like 256 to 0 where I beleve the op wants to have it be 255 for all values over 255.
@tinstaafl: Why require cmath for specific versions of min/max which have an already templated form?
Building from tinstaafls approach:
val = std::min ( 255, std::max (0, val) );
As an aside, it really doesn't make much sense to try and 'optimize' such statements. Small expressions are - in almost all cases - best handled by the optimization steps of the compiler. Things you do in source have very limited effect on the generated machine code.
Aim for readability; optimize only out of necessity.
Hard for it to be optimal when it doesn't do what you want. Both methods, using fmin/fmax and using MIN/MAX will do what you want. But as was pointed out anything you do to optimize something that's relatively straight forward, will get re-optimized by the compiler. So readability becomes more important in this case.