Thread: Global variables???

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    3

    Global variables???

    Hi,

    Probably a silly question but I am currently working on a program which contains two particular functions. they are both called from main().
    function one is declared in one source file and the other function is declared in another source file.
    Now both of the fuctions use a variable identifier (an int). The value of this identifier is established in the first function and is used in the second function. where should declare this variable in the program so that it may be used by both functions?
    I hope I have made my problem clear enough for you guys to help!

  2. #2
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    You could declare it in a header file included by both source files

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Why not return this value from function 1, and pass it to function 2?
    Code:
    int main(void)
    {
       int ret = function1();
       function2(ret);
    
       ...

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    3
    Thanks for the help. I returned a value from the first function to the second like you said but I'm getting a "constant expression required" error. Any idea what this could mean?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    > Any idea what this could mean?
    Yeah, post your code so we can tell you exactly what you did wrong.

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    3
    Ok so heres the function from which I'm returning the value. It is a switch-case function for the value of an i/p portD on a microcontroller. it prints the appropriate result on an LCD and returns this value to main(). Following this piece of code is the code from the main() which calls the function:

    Code:
    
    #define A_1  	110
    #define As_1  	116
    #define B_1  	123
    #define C_1  	130
    #define Cs_1  	138
    #define D_1  	147
    #define Ds_1  	145
    #define E_1  	165
    #define F_1  	175
    #define Fs_1 	185
    #define G_1  	196
    #define Gs_1  	207
    
    int print_value_1(void)
    {
    	while(!RD7){}/////WAIT UNTIL BUTTON IS PRESSED////////////
    	
    	RD7 = 0;
       switch( PORTD ) 
       {
         case 0b01000001: lcd_puts("A"); return A_1;
         break;
         case 0b00100001: lcd_puts("A#"); return As_1;
         break;
         case 0b00010001: lcd_puts("B");  return B_1;
         break;
         case 0b01000010: lcd_puts("C"); return  C_1;
         break;
         case 0b00100010: lcd_puts("C#"); return  Cs_1;
         break;
         case 0b00010010: lcd_puts("D"); return  D_1;
         break;
         case 0b01000100: lcd_puts("D#"); return  Ds_1;
         break;
         case 0b00100100: lcd_puts("E"); return  E_1;
         break;
         case 0b00010100: lcd_puts("F"); return  F_1;
         break;
         case 0b01001000: lcd_puts("F#"); return  Fs_1;
         break;
         case 0b00101000: lcd_puts("G"); return  G_1;
         break;
         case 0b00011000: lcd_puts("G#"); return  Gs_1;
         break;
         
        } 
    } //end of print_value
    
    ////////////////////////////// //////////////////////////
    int main()
    {
    print_value_1();
    int ret1 = print_value_1();
    }

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    > case 0b01000001: lcd_puts("A"); return A_1;
    Unless your compiler supports binary constants, this is an error.

    > print_value_1();
    > int ret1 = print_value_1();
    Seems to me like you're calling the function twice.
    Did you mean for the first one to be a prototype, a declaration or a call?
    C doesn't support mixed declarations and statements, so unless the first is a prototype/declaration then the int is out of place.
    int ret;
    // more code
    ret1 = print_value_1();

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 08-06-2008, 09:59 AM
  2. scope of global variables
    By laertius in forum C++ Programming
    Replies: 4
    Last Post: 10-15-2006, 01:59 AM
  3. global variables - okay sometimes...?
    By MadHatter in forum C++ Programming
    Replies: 21
    Last Post: 01-21-2003, 04:23 PM
  4. global variables
    By rdnjr in forum Linux Programming
    Replies: 0
    Last Post: 01-07-2003, 10:28 AM
  5. Global variables? Bad! Yes, but to what extent?
    By Boksha in forum C++ Programming
    Replies: 6
    Last Post: 05-26-2002, 04:37 PM