0

I have a c file and a header file. I wanted to use the variable explicity using C. How to use extern keyword for the case below :

glob.h

#ifndef GLOB_H
#define GLOB_H
static int a=10;
#endif

Main program

#include"glob.h"

int c = 20;

void loop()
{
  if(c>a)
  {
    Serial.printf("welcome");
  }
}
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
RKNAYAK
  • 55
  • 1
  • 3
  • 9
  • 2
    1. If you can't even bother to look at the question preview before you post (caps lock, indentation), why should we spend our time looking at it and answering it? 2. What is the problem you are encountering? – Angew is no longer proud of SO Mar 27 '14 at 10:25
  • 1
    what problem are you getting? and c/c++? – Jayesh Bhoi Mar 27 '14 at 10:45
  • You probably should have a look at [this SO Question](http://stackoverflow.com/questions/1433204/how-do-i-share-a-variable-between-source-files-in-c-with-extern-but-how/). – Jabberwocky Mar 27 '14 at 11:00

2 Answers2

0

Simply add the extern declaration to your main program

#include"glob.h"

int c = 20;
extern int a;

void loop()
{
  if(c>a)
  {
    Serial.printf("welcome");
  }
}
stakri
  • 1,357
  • 1
  • 11
  • 14
0

extern is used to tell the compiler to not redefine a variable as it exists in another compiled object file and to not redefine it in the current object file but reference the existing variable in the other object file instead. In your code above you don't redefine your a variable in your main program so you don't need it as long as we're talking about the same compiled object file.