1

I have searched the static usage on enums in c and then I am posting this topic here. I am a tester and I need to test the enum datatype for local,static ,global scope by writing a simple program.

A simple static variable can be tested easily, similarly I need to test static enum datatype.

I have written a simple code. Need your help guys.

#include<stdio.h>

enum day {sun=1,mon,tue,wed,thur,fri,sat};
static enum direction {UP,DOWN,RIGHT,LEFT};

int static_enum()
{
    static enum direction dir = UP;
    printf("dir = %d\n",dir);
    dir++;
}


int main()
{
    printf("\nsun = %d mon = %d tue = %d wed = %d thurs = %d fri = %d sat = %d\n",sun,mon,tue,wed,thur,fri,sat);
    enum day fri = 25;
    // now friday gets modified from 21 to 25 becasue we have re-assigned the value
    printf("\nfri = %d\n",fri);
    // now saturday will still have value 22
    printf("\nsat = %d\n",sat);
    printf("\n"); 
    static_enum();
    static_enum();
        return 0;
}

Let me know if the code tests the static functionality of enum data type. Thank you

Note : the topics I searched landed me in either C# or java.

vk41286
  • 113
  • 1
  • 1
  • 10
  • The `static enum` code in question doesn't look valid to me. See: http://stackoverflow.com/questions/4971436/c-what-does-static-enum-mean – John Zwinck Dec 17 '14 at 04:55
  • The code successfully compiles in gcc under linux and on codepad – vk41286 Dec 17 '14 at 05:06
  • 1
    There is plenty of code which is invalid yet still compiles. Anyway, let's say it's valid. Then, `static` has no effect. You can safely remove it. – John Zwinck Dec 17 '14 at 05:08
  • Thank you very much John Zwinck. I changed the code little bit and I could see the difference. Inside static enum() I removed the static keyword in static enum direction dir. The ouput I get is dir = 0, dir=0. But if I retain the keyword static then the output is dir=0 and dir = 1. I think this is sufficient for me to test the static functionality here. – vk41286 Dec 17 '14 at 05:41
  • What is your question? Do you not understand `static`? – David Heffernan Dec 17 '14 at 07:15
  • yes david, I do understand the keyword static. But my use case is to check the static enum functionality in a code. – vk41286 Dec 17 '14 at 07:46
  • you can ignore the printf statement in main functions.Just concentrate static_enum() functions. – vk41286 Dec 17 '14 at 07:50
  • @vk41286 Then you removed the wron `static`. Omit the `static` in `static enum direction {UP,DOWN,RIGHT,LEFT};` and keep it inside the function. – glglgl Dec 17 '14 at 09:49

1 Answers1

6

The static storage class is related to the linkage and storage of an object. It doesn't really have anything to do with the type of the object.

Below is an explanation of what (I think) your program is doing:


enum day {sun=1,mon,tue,wed,thur,fri,sat};

This declares an enumerated type with the tag name day, and also defines a number of enumeration constants and their values.

static enum direction {UP,DOWN,RIGHT,LEFT};

This declares an enumerated type with the tag name direction, and also defines a number of enumeration constants and their values.

The static storage-class is meaningless here, because you are not defining (allocating storage) for an object.

int static_enum()
{
    static enum direction dir = UP;
    printf("dir = %d\n",dir);
    dir++;
}

This defines a block-scope object named dir of type enum direction. The static storage-class means that the object will be allocated and initialized at program startup, and that it will retain its last stored value between function calls.

int main()
{
    printf("\nsun = %d mon = %d tue = %d wed = %d thurs = %d fri = %d sat = %d\n",sun,mon,tue,wed,thur,fri,sat);

This will output the values of the enumeration constants.

enum day fri = 25;

This defines a block-scope object named fri of type enum day, initialized with the value 25. The enumeration constant also named fri will not be visible anymore within this block (unless you re-declare enum day).

// now friday gets modified from 21 to 25 becasue we have re-assigned the value
printf("\nfri = %d\n",fri);

This outputs the value of the object fri, not the value of the enumeration constant fri. Nothing has been modified.

// now saturday will still have value 22
printf("\nsat = %d\n",sat);

This outputs the value of the enumeration constant sat, as expected.

printf("\n"); 
static_enum();
static_enum();
    return 0;
}

One might wonder why the language allows the static storage-class in a declaration that does not define (allocate storage for) an object. I think this is just a syntactical convenience due to the way that declarations and definitions share the same syntax. The C syntax allows you to pile any number of storage-class, type-specifier, type-qualifier, function-specifier and alignment-specifier in any order in a declaration. However, many combinations are disallowed or cause undefined behavior according to various semantic sections of the C standard. I don't think there is anything that prohibits the meaningless static keyword in the declaration of direction.

Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
  • I'm quite sure there isn't anything disallowing the useless storage-class specifier. `gcc -std=c99 -pedantic-errors` gives a warning, not an error (`-pedantic-errors` is at least intended to also catch compile-time UB). – mafso Dec 17 '14 at 11:09