Consider the following C program:
#include<stdio.h> int r(){ int static num=7; return num--; } int main() { for (r();r();r()) { printf ( "%d " ,r()); }; return 0; } |
Which one of the following values will be displayed on execution of the programs?
(A) 41
(B) 52
(C) 63
(D) 630
Answer: (B)
Explanation: According to “for” loop in C,
for (initialization expr; test expr; update expr) { // body of the loop // statements we want to execute }
Code will execute as following flow chart,
Also, note that there is postdecrement (num – –) in given return statement, so it will return previous value of “num” then decrements it by 1.
Static will assign memory once for variable int “num” and all the changes will happen here.
#include<stdio.h> int r(){ int static num=7; return num--; } int main() { for (r();r();r()) { printf ( "%d " ,r()); }; return 0; } |
Therefore, given code will print: 5 2.
Option (B) is correct.
Quiz of this Question