• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests

C Quiz - 111 | Question 2

Pick the best statement for the following program snippet: C
#include \"stdio.h\"
void foo(void)
{
 static int staticVar;
 staticVar++;
 printf(\"foo: %d\\n\",staticVar);
}

void bar(void)
{
 static int staticVar;
 staticVar++;
 printf(\"bar: %d\\n\",staticVar);
}

int main()
{
 foo(), bar(), foo();
 return 0;
}

(A)

Compile error because same static variable name is used in both foo and bar. Since these static variables retain their values even after function is over, same name can’t be used in both the functions.

(B)

Compile error because semicolon isn’t used while calling foo() and bar() in side main function.

(C)

No compile error and only one copy of staticVar would be used across both the functions and that’s why final value of that single staticVar would be 3.

(D)

No compile error and separate copies of staticVar would be used in both the functions. That’s why staticVar in foo() would be 2 while staticVar in bar() would be 1.

Answer

Please comment below if you find anything wrong in the above post
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Last Updated :
Share your thoughts in the comments