Discussion:
switch statements
(too old to reply)
Konwen Kuan
2004-02-04 21:24:11 UTC
Permalink
Can I use switch statements with the varument of VarValue type?

Or, is there any way I can check the output of the array variable. Like in
printf command, what would i put after the %? Thanks
Alexander Smith
2004-02-05 04:01:04 UTC
Permalink
Yes you can use switch statements with enumerated types. You can do
something like this:

switch (my_var) {
case ZERO:
/* Do something. */
break;
case ONE:
/* Do something. */
break;
case X:
/* Do something. */
break;
default:
/* If you get here, it's probably an error. */
printf("error: my_var has an invalid value.\n");
exit(1);
}

If you want to print out the values, remember that in C enumerated types
can be treated like integers. (This is not actually true in C++, but
that's ok because you're not writing C++ programs.) So, you can do this:
printf("my_var is %d\n", my_var);

Look at the definition of the enumeration:

typedef enum { ZERO = 0, ONE, X } VarValue;

The way enumerated types work, ZERO will have the value 0, ONE will have
the value 1, and X will have the value 2. So, if the above printf()
statement prints out the value 2, for example, it means my_var has the
value X.

Alexander

P.S. While we're taking about the VarValue enumerated type, a word of
warning: in checkAssignment, you have to read an assignment vector which
is passed in as a string (character array), and use it to assign values to
the variables in the SAT solver. The variables are of type VarValue; the
character array is full of chars. They are not the same. So, if you're
directly assigning one to the other, then you're doing something wrong.
Post by Konwen Kuan
Can I use switch statements with the varument of VarValue type?
Or, is there any way I can check the output of the array variable. Like in
printf command, what would i put after the %? Thanks
Loading...