Shop OBEX P1 Docs P2 Docs Learn Events
Must be getting rusty with C — Parallax Forums

Must be getting rusty with C

#include "simpletools.h"                      
#include "adcDCpropab.h"


int main()                                    // Main function
{
  adc_init(21, 20, 19, 18);
  
  set_directions(15, 0, 0b1111111111111111);  // pins 15 through 0 are outputs
  
  int d[] = {0b1110111, 0b1000100, 0b1101011, 0b1101110, 0b1011100,       //digits 0 through 9
             0b0111110, 0b0011111, 0b1100100, 0b1111111, 0b1111100};
  
  
       
        
      

 
  while(1)
  {
    for (int sample = 0; sample <= 1000; sample++)
    {         
        float i =  adc_volts(2);
        
        float j = adc_volts(2);
        
        if (j > i)
          {
            i = j;
          }
    }                       
     
                        
       int a = (int)i;            // intger part
       float b = (10 * (i - a));                
       int c = (int)b;            // frac part
       
       //print("%f, %d, %d\n", i, a, c);
       
       set_outputs(15, 9, d[c]);  // c is the decimal value
       set_output(1,1);           // pin 1 is the decimal digit driver
       pause(8);
       set_output(1,0);
       set_outputs(15, 9, d[a]);  // a is the integer value
       set_output(2,1);           // pin 2 is the integer digit driver
       pause(8);
       set_output(2,0);
           
    
  }  
}


I Cant figure out why this won't compile. When attempting, I becomes undeclared after I = j;
any Ideas?

Comments

  • the variable "i" is declared inside your for-loop. "i" no longer exists once the for-loop exits. A for-loop has its own "scope" much like a function, so any variables created inside the for-loop can not be accessed outside it.

    Solution: move "float i;" above the for-loop (right underneath the opening brace for the while-loop).
  • float i is local to the for loop, I made float i global, compiles ok.
  • Beavis3215Beavis3215 Posts: 229
    edited 2017-07-28 01:33
    I may have never known that. How would I globalize "I" so that it is available in other parts of the progam?

  • Beavis3215Beavis3215 Posts: 229
    edited 2017-07-28 01:42
    I think I figured it out,

    int main(I)
  • Any variable declared outside of main() becomes global in scope.
  • Beavis3215 wrote: »
    I think I figured it out,

    int main(I)

    This would be passing an argument to main and would have local scope to the main function and not global to the entire file and other functions in the file. But, would it compile as is?

    To make it global to the entire file and any functions in the file, you would declare it above the main under the includes. To ensure the compiler does not optimize the value out, you could add volatile to the variable.

    #include "adcDCpropab.h"
    
    volatile float i =  0.0f;
    
    int main()                                    // Main function
    
  • TorTor Posts: 2,010
    int fully_global; /* Visible to the whole program, any source file */
    static int locally_global; /* Visible to everything inside the same source file */
    
    void my_function(void)
    {
        int only_this_function; /* Only visible inside my_function */
    }
    
    int main (int argc, char *argv[])
    {
        int visible; /* Visible everywhere in the main() function */
        while (something()) 
        {
             int loop_var; /* Visible inside the while() loop only */
             for (int k = 0; k < 10; k++) /* k only exists inside this for loop */
             {
                 ;
             }
             
             {
                  int very_local; /* Creating a block scope purely to isolate very_local from the rest of main() */
                  very_local = 0;
             }
        }    
        
        from_here = 0; /* This will fail */
        int from_here; /* Only visible in main() from here and below, not above */
        from_here = 0; /* This will work */
        return (0);
    }
    

  • When you write C code, declaring a var inside a function/scope, the compiler (hopefully) will use a registers like R12 etc instead of RAM.
    That speeds up the code, especially on a ARM as it's a load-and-store type mcu.
    Registers in C are temporary and will be trashed on exit of function.
Sign In or Register to comment.