开发者

Error if i declare constant with #define or const in my C program

开发者 https://www.devze.com 2023-04-07 10:33 出处:网络
I use gcc version 4.3.2 (Debian 4.3.2-1.1). I wrote a simple program in C to implement and test a integer stack. Stack is implemented by the STACK structure. I used a constant named STACKSIZE to defin

I use gcc version 4.3.2 (Debian 4.3.2-1.1). I wrote a simple program in C to implement and test a integer stack. Stack is implemented by the STACK structure. I used a constant named STACKSIZE to define the STACK's size. My program code looks like this:

#include<stdio.h>
#include<stdlib.h>

#define开发者_如何学JAVA STACKSIZE 10;

typedef struct {
    int size;
    int items[STACKSIZE];
} STACK;

void push(STACK *ps, int x)
{
    if (ps->size == STACKSIZE) {
        fputs("Error: stack overflow\n", stderr);
        abort();
    } else
        ps->items[ps->size++] = x;
}

int pop(STACK *ps)
{
    if (ps->size == 0){
        fputs("Error: stack underflow\n", stderr);
        abort();
    } else
    return ps->items[--ps->size];
}

int main() {
    STACK st;
    st.size = 0;
    int i;
    for(i=0; i < STACKSIZE + 1; i++) {
        push(&st, i);
    }
    while(st.size != 0)
        printf("%d\n", pop(&st));
    printf("%d\n", pop(&st));
    return 0;
}

when i used #define STACKSIZE 10; gcc would return following errors:

ex_stack1.c:8: error: expected ‘]’ before ‘;’ token
ex_stack1.c:9: warning: no semicolon at end of struct or union
ex_stack1.c: In function ‘push’:
ex_stack1.c:13: error: expected ‘)’ before ‘;’ token
ex_stack1.c:17: error: ‘STACK’ has no member named ‘items’
ex_stack1.c: In function ‘pop’:
ex_stack1.c:26: error: ‘STACK’ has no member named ‘items’
ex_stack1.c: In function ‘main’:
ex_stack1.c:33: error: expected ‘)’ before ‘;’ token

when i used

const int STACKSIZE=10;

gcc would return following error:

ex_stack1.c:8: error: variably modified ‘items’ at file scope

when i used

enum {STACKSIZE=10};

gcc would compile my program successfully.

What happenned? How should i modify my program to use

#define STACKSIZE 10;

or

const int STACKSIZE=10;


Drop the semicolon, it's wrong

#define STACKSIZE 10;
                    ^

If you keep it the preprocessor will translate int items[STACKSIZE]; into the obviously wrong int items[10;];.

For the const bit, there is a C FAQ.

The value of a const-qualified object is not a constant expression in the full sense of the term, and cannot be used for array dimensions, case labels, and the like.


For future reference, you can view the results of the preprocessor by making use of the -E option to gcc. That is,

gcc -E ex_stack1.c -o ex_stack1.i 

Examination of the resulting ex_stack1.i file would have made the problem more obvious.


#define does textual replacement. Since you have:

#define STACKSIZE 10;

then

typedef struct {
    int size;
    int items[STACKSIZE];
} STACK;

becomes:

typedef struct {
    int size;
    int items[10;];
} STACK;

In C, const is used to declare variables that can't (easily) be modified. They are not compile-time constants.

enum, however, does define a compile-time constant. In general, you should prefer enum over const int over #define where possible. (See Advantage and disadvantages of #define vs. constants? ).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号