Issue
In "Advanced programming in the UNIX environment", in Figure 7.16, in pr_limits function, the following two lines are written to show resource limit.
lim = limit.rlim_max;
printf("%10lld", lim);
my question is, why the value is first copied to lim variable and then it is printed, and why not directly not printed?
Solution
my question is, why the value is first copied to lim variable and then it is printed, and why not directly not printed?
Probably as you have been pointed to in the comments, there's a conversion issue in the middle. The linux man page states that
struct rlimit {
rlim_t rlim_cur; /* Soft limit */
rlim_t rlim_max; /* Hard limit (ceiling for rlim_cur) */
};
and rlim_t
cannot be warranted to be a specific type, so to be portable, you can either use an intermediate variable to do the conversion (so you can safely use the %10lld
format specifier) or you can use a cast in the call to printf()
to ensure proper formatting. Anyway, the use of an extra variable will probably be eliminated by the compiler in the optimization process, so you most probably will not sense any efficiency penalty.
This extra type conversion ensures portability of code.
Answered By - Luis Colorado Answer Checked By - Pedro (WPSolving Volunteer)