0

I have a program that needs two different values of the same variable depending on the state of the program.

Function is declared in header (test.inc):

void function_a(int pr)
{
    if (pr == 1)
    enum{
      a = 5,
      b = 5,
      c = 5,
    };
    else
    enum{
      a = 2,
      b = 2,
      c = 2,
    };
}

And program: (main.c)

int main() {
    function_a(1);        
    printf("%d",a);
    return (EXIT_SUCCESS);
}

After compiling it say:

main.c:26: error: `a' undeclared (first use in this function)
main.c:26: error: (Each undeclared identifier is reported only once
main.c:26: error: for each function it appears in.)

How to make a global declaration using enum in function located in header?
Thank you

Caslav
  • 487
  • 1
  • 5
  • 15
  • 1
    enums are just enumerated constants - what are you trying to achieve here exactly ? – Paul R Nov 22 '12 at 12:45
  • Regarding `enum`s you might like to read here: http://www.cprogramming.com/tutorial/enum.html – alk Nov 22 '12 at 12:48
  • 1
    So if you think this is strange then help me to make a declaration of xxx number of variables that have the same name but must be different in various parts of the program? – Caslav Nov 22 '12 at 12:56
  • 1
    If you want a global variable, you can define, say, "extern int global;" in a .h and give it a value in a .c file ("int global = 42") that includes the header. Then you can use the variable anywhere you include the header. But I'm not sure if this is what you want. – Mikkel K. Nov 22 '12 at 13:09
  • 2
    enums declared inside functions are not visible to other functions, not even if the function is in a header (which, BTW will give you link problems later if you don't declare them static). You also cannot reassign the value of an enum, even if you want to. They're not *variable*. – ams Nov 22 '12 at 13:20

5 Answers5

2

There's no use for an enum here, you just need 3 global variables and set those:

int a,b,c;

void function_a(int pr)
{
    if (pr == 1) {
      a = 5,
      b = 5,
      c = 5,
    } else {
      a = 2;
      b = 2;
      c = 2;
    }
}

int main() {
    function_a(1);        
    printf("%d",a);
    return (EXIT_SUCCESS);
}
nos
  • 223,662
  • 58
  • 417
  • 506
2

I have 2 suggestion for you:

1) using structure instead of enum

struct test_struct {
   int a;
   int b;
   int c;
} test;
void function_a(int pr)
{
    if (pr == 1)
    test = {
      .a = 5,
      .b = 5,
      .c = 5,
    };
    else
    test = {
      .a = 2,
      .b = 2,
      .c = 2,
    };
}

And program: (main.c)

int main() {
    function_a(1);        
    printf("%d",test.a);
    return (EXIT_SUCCESS);
}

2) Using simple variable

int a,b,c;

void function_a(int pr)
{
    if (pr == 1) {
      a = 5;
      b = 5;
      c = 5;
    }
    else {
      a = 2;
      b = 2;
      c = 2;
    }
}

And program: (main.c)

int main() {
    function_a(1);        
    printf("%d",a);
    return (EXIT_SUCCESS);
}
MOHAMED
  • 41,599
  • 58
  • 163
  • 268
1

If I understand you correctly you want a variable named x to have different values depending on the part of the program. A variable cannot refer to two different values at the same time but you can change the value of a variable through assignment at a certain point.

I think what you really need are functions. Calling a function (in the roughest sense) binds values to its parameters.

But seeing your awkward use of enums you might want to take a step back and pick up a good book about C. See the list here.

Community
  • 1
  • 1
pmr
  • 58,701
  • 10
  • 113
  • 156
  • This is a code handed down to me to make it "better". If I wrote the program I wouldn't use enums whatsoever but thank you for the literature list. Will be looking at it for sure. I got rusty around the edges in C... – Caslav Nov 22 '12 at 14:42
1

I would use this

void function_a(int pr)
{
    if (pr == 1)
    { //make them local
    enum{
      a = 5,
      b = 5,
      c = 5,
    };
     common_function(); // move common functionality inside a function and call it
    }
    else
    {  // make them local
      enum{
      a = 2,
      b = 2,
      c = 2,
    };
common_function(); // move common functionality inside a function and call it
}
}

Inside the common_function, you use the enum values

Anon
  • 2,608
  • 6
  • 26
  • 38
  • Broken in what sense? I agree this is not what I would write in production code but OP's want seems strange. – Anon Nov 22 '12 at 13:11
  • The function defines some local anonymous enums, and then does nothing with them. It calls `common_function`, but that can't see `a`, `b`, or `c` as they're not in scope. – ams Nov 22 '12 at 13:16
  • Ah, I thought it was obvious common_function would use enum values . (I even wrote it down). Therefore would take them as arguments. I will edit the answer anyway. Still new to SO and figuring out how to write good answers. – Anon Nov 22 '12 at 13:18
  • Yes, `common_function` could take them as parameters, but yours does not, and defining them as enum would be a waste of space, in such a simple function. – ams Nov 22 '12 at 13:21
  • Yes, it is waste of space. I think OP meant it for simple example. Otherwise it is clear one would use variables. – Anon Nov 22 '12 at 13:25
1

The compile time error you are getting because of compiler looking for a symbol a.

As the function call happens at run time so function_1(1) will be called at run time and then a comes into existence. But compiler needs to resolve the symbol or token a at compile time ,thus you are gettin this error .

Compiler error comes due to any syntax error. and here is the thing a is not declared.

NOTE: All symbols used in one translation unit should be well declared atcompile timeotherwise compiler will throw an error

You can solve this by using a macro call instead of function call. because macros are preprocessed. Or, you do #define the constants.

Omkant
  • 9,018
  • 8
  • 39
  • 59