¿Producción?
#include <stdio.h> int fun() { static int num = 16; return num--; } int main() { for(fun(); fun(); fun()) printf("%d ", fun()); return 0; }
(A) Bucle infinito
(B) 13 10 7 4 1
(C) 14 11 8 5 2
(D) 15 12 8 5 2
Respuesta: (C)
Explicación: Dado que num es estático en fun() , el antiguo valor de num se conserva para llamadas a funciones posteriores. Además, dado que la declaración return num – es postfijo, devuelve el valor anterior de num y actualiza el valor para la próxima llamada a la función.
fun() called first time: num = 16 // for loop initialization done; In test condition, compiler checks for non zero value fun() called again : num = 15 printf("%d \n", fun());:num=14 ->printed Increment/decrement condition check fun(); called again : num = 13 ---------------- fun() called second time: num: 13 In test condition,compiler checks for non zero value fun() called again : num = 12 printf("%d \n", fun());:num=11 ->printed fun(); called again : num = 10 -------- fun() called second time : num = 10 In test condition,compiler checks for non zero value fun() called again : num = 9 printf("%d \n", fun());:num=8 ->printed fun(); called again : num = 7 -------------------------------- fun() called second time: num = 7 In test condition,compiler checks for non zero value fun() called again : num = 6 printf("%d \n", fun());:num=5 ->printed fun(); called again : num = 4 ----------- fun() called second time: num: 4 In test condition,compiler checks for non zero value fun() called again : num = 3 printf("%d \n", fun());:num=2 ->printed fun(); called again : num = 1 ---------- fun() called second time: num: 1 In test condition,compiler checks for non zero value fun() called again : num = 0 => STOP
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA