retagged by
349 views
5 votes
5 votes

What will be the output of running file main.c?

write.c
***********************
extern int count;

void write_extern()
{
    count +=2;
}
main.c
***********************
#include "write.c"
#include<stdio.h>
int count = 5;

main()
{
    write_extern();
    write_extern();
    printf("%d", count);
}
  1. $0$
  2. $5$
  3. $9$
  4. None of these
retagged by

1 Answer

3 votes
3 votes

Answer: C

Before compilation the pre-processor takes the charge and handles pre-processor directives like:

  • #define,
  • #undef,
  • #include,
  • #ifdef,
  • #endif,
  • #if,
  • #else….

so simply replace the contents of write.c file with the line – #include “write.c”

extern int count;
 
void write_extern()
{
    count +=2;
}

#include<stdio.h>
int count = 5;
 
int main()
{
    write_extern();
    write_extern();
    printf("%d", count);
}

the variable count used in printf() is declared at the top of the program so no compilation error.

extern will look for definition of the variable count in current and global scope which it finds.

Answer:

Related questions