title: Global variables in C on STM32
body: <p>I'm working on a project where I need to use one variable declared in a certain file (say mylib.c) in the main function using 'extern'. All headers are included with guard words to avoid multiple inclusions. 
  The variable is a structure(defined in mylib.h) which members are only floats and integers.It's initialized at the beginning of the main function.</p>
  
  <p>After entering the main's loop, and doing some work, some members that aren't concerned get random values.</p>
  
  <p>So,I removed extern from the declaration in main, and instead placed it in the declaration in mylib.c. And it worked.</p>
  
  <p>Sim808.h</p>
  
  <pre><code>#ifndef _SIM808_H
  #define _SIM808_H
  typedef struct{
  uint8_t GPRS_Active;
  
  float gsm_latitude;
  float gsm_longitude;
  }SIM808;
  void sendCmd(const char cmd[]);
  void sim808_init(void);
  void parse_gsm_location(uint8_t* line);
  #endif
  </code></pre>
  
  <p>Sim808.c</p>
  
  <pre><code>#include "sim808.h"
  SIM808 sim808;
  void parse_gsm_location(uint8_t* line)
  {
  uint8_t commas=0,index=0;
  uint16_t err;
  if((err=atoi((const char*)line+12))!=0)
  {
      printf("No coordinates received\n");
      if(err==404 || err==601)
          sim808.GPRS_Active=0;
  
      return;
  }
  while (line[index]!= '\0' &amp;&amp; index &lt;50)
      {
      if(line[index]==',')
      {
          commas++;
          switch (commas)
          {
              case 1:
                  sim808.gsm_longitude=atof((const char*)(line+index+1));
                  printf("Long:%f\n",sim808.gsm_longitude);   
              break;
              case 2:
                  sim808.gsm_latitude=atof((const char*)(line +index+1));
                  printf("Longitude%f     Latitude%f\n",sim808.gsm_longitude,sim808.gsm_latitude);
              break;
              case 3:
                  sscanf((const char*)(line+index+1),"%4d/%2d/%2d",    (int*)&amp;sim808.gsmDate.year,(int*)&amp;sim808.gsmDate.month,
                      (int*)&amp;sim808.gsmDate.day);
              break;
              case 4:
                  sscanf((const char*)(line+index+1),"%2d/%2d/%2d",
                      (int*)&amp;sim808.gsmTime.hours,(int*)&amp;sim808.gsmTime.minutes,(int*)&amp;sim808.gsmTime.seconds);
              break;
          }
      }
          index++;
      }
  }
  </code></pre>
  
  <p>main.c</p>
  
  <pre><code>#include "sim808.h"
  extern SIM808 sim808;
  
  int main(void)
  {
    uint8_t response[150];
   //init functions
   while(1)
   {
    if(sim808.GPRS_Active==1)
      {
          sendCmd("AT+CIPGSMLOC=1,1\r\n");
          HAL_UART_Receive(&amp;huart4,response,2,60000);//max response time is 1 min
          HAL_UART_Receive(&amp;huart4,response,150,1000);//we dont need first 2 chars
          parse_gsm_location(response);
          memset((void*)response,0,150);
      }
      else
          sim808_init();
    }
  }
  </code></pre>
  
  <p>As you can see,the member GPRS_Active can only receive 1 or 0 in my code.
  Using printf, it turned to become 242 after the first iteration.
  Can someone explain? Can this be a compiler bug?
  Thanks.</p>