We have two simple function.
#include <stdio.h>
/* first approach */
int power1(int *ptr)
{
return *ptr * *ptr;
}
/* second approach */
int power2(int *ptr)
{
int tmp = *ptr;
return tmp*tmp;
}
int main()
{
int val = 5;
printf("%d\n", power1(&val));
printf("%d\n", power2(&val));
return 0;
}
Which one is better? power1 is a little bit faster, but I have heard that power2 is more safety. I do not remember why? As far as I remember there is one case when power1(first approach) has bottleneck. Could you explain it? Does safety critical systems use second approach?
None is good. You want this:
Now concerning your two approaches:
power2is in no way "safer" thanpower1.BTW:
A correct way to declare
mainisint main(void)and thereturn 0;at the end ofmainis not necessary, ifmaindoesn't containt areturnstatement, there is an implicitreturn 0;at the end ofmain.