What is the output of the code below? [code] #include<stdio.h> int function() { static int c = 0; c++; return c; } int

Thảo Justin

New member
What is the output of the code below?
Code:
#include<stdio.h> 
int function() 
{ 
    static int c = 0; 
    c++; 
    return c; 
} 
int main() 
{ 
    printf("%d  ", function()); 
    printf("%d ",  function()); 
    return 0; 
}
A. 1 2
B. 1 1
C. null
D. 1 3
 
Answer: A
Explanation: The variable used in function() is declared static which means it will retain the last value after each function call statement which is then demonstrated in the code, when it is called through the print statement. Had the variable not been declared static, then it would print 1 1, since after every function call it’s memory would be wiped.
 

Tìm kiếm

Back
Top