Tuesday, 12 December 2017

C interview Question

What does static variable means?
Static variable is a variable which retain its value between the function calls.

They are initialized only once and their scope is within the function where they are defined.

A static variable inside a function has a lifespan as long as  program runs. It won't be allocated every time  function is called and deallocated when  function returns.

#include<stdio.h>
void function ()
{
int a=10;
static int sa=10;
a+=5;
sa+=5;
print("a=%d  sa=%d",a,sa);
}

void main()
{
int i;
for(i=0;i=<3;i++)
function ();
}

Output:
sa:15   a:15
sa:20   a:15
sa:25   a:15
sa:30   a:15

Reference:
1)https://stackoverflow.com/questions/5033627/static-variable-inside-of-a-function-in-c

2)https://youtu.be/VGovIzt-ggM

No comments:

Post a Comment

C interview question 8

What will be the output of the given code? #include <stdio.h> int main(void) { float a = 3.14; printf("%f",a%3); ...