Understanding The Extern Keyword In C

Variables in C -1

Example

// Here we are writing down the C programming language code to demonstrate
// the concept of Understanding the "extern" keyword in C with its relevant code
// and output supported by valid syntax where ever it's necessary
#include <stdio.h>
// the main driver code functionality starts from here
int main()
{
    // declaration and definition of variable 'a123.'
// As we can see, there are character data type variables and definitions
// declared below as a_123
    char a_123 = 'b';
 
   
// The variable 'b' is declared, and the memory has been allocated 
// until an explicit usage is mad,e it is initialized with some garbage 
// value
    float a; 
 

    // As we can see, there are integer data type variables and definitions
    // declared below as c, e, and _d45, respectively
    int _c, _d45, e;
 
  
// The below code helps us in printing down the variables declared
    printf("%c \n", a_123);
 
    return 0;
// the main driver code functionality ends from here
}

Output:

Variables in C -2

Example

// Here we are writing down the C programming language code to demonstrate
// the concept of Understanding the "extern" keyword in C with its relevant 
// code
// and output supported by valid syntax where ever it's necessary
#include <stdio.h>
void function(){
//here integer x is a local variable
int x = 120;
//here, integer y is a static variable
static int y = 130;
// below, we are performing operations over integer and static variables
x = x + 101;
y = y + 101;
// here, we are trying to print the variables after the operation 
printf("\n%d,%d",x,y);
}
// The main driver code functionality starts from here
int main() {
// calling the function multiple times to verify the results
function();
function();
function();
return 0;
// The main driver code functionality ends from here
}

Output:

Output

221,231
221,332
221,433

Variables in C -3

Example

// Here we are writing down the C programming language code to demonstrate
// the concept of Understanding the "extern" keyword in C with its relevant 
// code
// and output supported by valid syntax where ever it's necessary
#include <stdio.h>
void function()
{
int x=10;//local variable (also automatic)
auto int y=20;//automatic variable
}
// the main driver code functionality starts from here
int main() {

	function();
	return 0;
// the main driver code functionality ends from here
}

Output:

Output

Compiles without any error

Extern keyword

Example

// here we have written the c code of extern keyword declaration
#include <stdio.h>

extern int a;	 
				
// the main driver code functionality starts from here
int main()
{
	printf("%d", a);

return 0;
// the main driver code functionality ends from here
}

Output:

Output

/usr/bin/ld: cannot open output file a.out: Permission denied
collect2: error: ld returned one exit status

The following code compiles without any errors and executes successfully.

Example

// here, we have written multiple cases where the C compiler runs 
// successfully without throwing any error (example-1)
extern int var;
// the main driver code functionality starts from here
int main(void)
{
    // no var variable declared it is declared outside the main driver code
return 0;
}

// example-2
extern int var;
// the main driver code functionality starts from here
int main(void)
{
    // the var variable here holds the value of 10
var = 10;
return 0;
}
// example-3
#include "somefile.h"
extern int var;
// the main driver code functionality starts from here
int main(void)
{
     // the var variable here holds the value of 20
var = 20;
return 0;
}
// example-4
extern int var = 0;
// the main driver code functionality starts from here
int main(void)
{
     // the var variable here holds the value of 100
var = 100;
return 0;
}

Input Required

This code uses input(). Please provide values below: