edited by
391 views
3 votes
3 votes

Which of the following is the correct replacement for "<my_decl>" to define a global integer variable?

#include<stdio.h>
<my_decl> my_global;
int main()
{
    printf("%d", my_global);
}
  1. global int
  2. extern int
  3. static int
  4. int
edited by

1 Answer

Best answer
15 votes
15 votes

A. global int my_global; is not a valid definition.

B.extern int my_global; Extern storage class simply tells us that the variable is defined elsewhere and not within the same block where it is used. Not required for this program.

  • When a variable is defined outside of any function (including main). It is a global variable and is placed in either the data or bss sections of memory.

  • Any function in a the file where it is declared has access to a global variable.

  • Using the keyword extern in another file gives access to the variable in the second file. If the extern declaration is inside a function, only that function has access to the global variable. If it is outside of any function, then all functions in the file have access to the variable.

  • The extern statement does not declare a variable, but just references a global variable which was declared in another file.

/* File1.c: */
int Global;

extern void f( int ); /* functions as well as variables can be extern */
extern void g( int );
int main(void)
{
   ....
   f( i );
   g( j );
}
---------------------------------------
/* File2.c  */
extern int Global;  /* now f() and g() have access to Global */
void f( int );
void g( int );

void f( int i )
{
   ....
}

void g( int j )
{
   ....
}

 

C.static int my_global; Not required for this program.

  • static variable can be either a global or local variable. Both are created by preceding the variable declaration with the keyword static.
  • local static variable is a variable that can maintain its value from one function call to another and it will exist until the program ends.
  • When a local static variable is created, it should be assigned an initial value. If it's not, the value will default to $0.$
  • global static variable is one that can only be accessed in the file where it is created. This variable is said to have file scope.

The keyword static is used in two ways.

  • static variable declared inside a function is placed in the data or bss portion of memory and can retain it’s value between calls to the same function. In this way it is similar to a global variable, except it is only used in one function. The restriction on the scope of static variables is a compiler implemented restriction; being that, an assembly language program could easily access any data stored in either the data or bss sections of memory.

  • static variable declared as a global variable is also called a static external variable. Here the keyword static produces the opposite results as the extern keyword. The variable is global to the file where it is declared, but may not be referenced in any other files. Thus the keyword static can produce a form of data hiding.Static variables provide a simple means to hide information in a C program. In other words, a variable may need to be persistent or global, but we may not want other programmers to know much about this variable - least they try to change it.

D. int my_global; It is a valid definition of a global integer variable.

So, the correct answer is $(D).$

References:

selected by
Answer:

Related questions

9 votes
9 votes
1 answer
2
gatecse asked Jul 26, 2020
640 views
What will be the output of the following C program:#include<stdio.h int main() { char* disease = "COVID-19"; (*disease)++; printf("%s",disease); }DPWJE-$20$DOVID-$19$COVI...