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);.
Which country is partnering with India in the Infrastructure Financing Bridge aimed at reducing the cost of capital for infrastructure projects?
How many new joint India–Australia research projects will be launched under SPARC during the AIESC meeting?Â
Who has been honoured with the ASME Holley Medal 2025 for engineering innovation with public impact?Â
- What is the financial outlay of the 'Lakhpati Baideo' scheme launched in Assam?
What major cybersecurity capability was enabled by the MoU between DoT and FIU-IND?Â
What does the strategic importance of the Nimmu-Padam-Darcha road signify for Ladakh?
Which bank has issued its maiden 10-year Sustainability Bond and raised USD 1 billion under its Environmental Social Governance (ESG) Framework?
Which country topped the Global Investment Risk and Resilience Index 2025 as the most resilient for investors?Â
What is the total amount awarded to ALICE collaboration as part of the Breakthrough Prize 2025?
Why are commemorative coins being issued for Prof. M.S. Swaminathan and Make in India?Â