Question
Consider the following C code snippet for calculating
base raised to the power of exp:   #include   double power(double base, int exp) {     if (exp == 0) {       return 1;     }     // Missing handling for negative exponent     return base * power(base, exp - 1);   }   int main() {     printf("%.2f\n", power(2.0, -2));     return 0;   }   What will be the output or behavior of the program when power(2.0, -2) is called?Solution
The power function correctly handles exp == 0 and positive exp. However, it lacks a specific base case or handling for negative exponents. When power(2.0, -2) is called, exp is -2, which is not 0. The function then calls base power(base, exp - 1), which becomes 2.0Â power(2.0, -3). This process continues indefinitely, with exp becoming more and more negative (-4, -5, etc.). The exp will never reach the base case of 0, leading to infinite recursion, a stack overflow, and typically a segmentation fault. To fix this, a condition for exp < 0 should be added, typically return 1.0 / power(base, -exp);.
According to investment commitments announced during the AI Impact Summit, what was the total value of AI infrastructure investment commitments secured?
Non-basmati rice exports from India increased by what percentage in 2025?
Who announced the renaming of the Bandra-Versova sea route in Mumbai as Swatantra Veer Savarkar Bandra-Versova Sagari Setu?
Where did ISRO successfully conduct its second short hot test of the semicryogenic engine in April 2025?
A Governor gives its resignation to whom?
What percentage of GST has been fixed on 'Online Gaming' in the 50th meeting of the GST Council?
In May 2026, BIT Mesra signed an MoU with NESAC to advance research in which field?
Under which area was the MoU for the National Maritime Heritage Complex (Lothal) signed between India and Germany?
Which set correctly identifies the Partner Countries for World Food India 2025?
According to the UNICEF report "Prospects for Children in 2025," what is the estimated number of children living in conflict zones as of 2023?