Mike -- No longer here
Mike -- No longer here

Reputation: 2092

Any way I can use a constant number in my program without storing it in memory?

This is my C code:

#include "stdio.h"
#define SIZE1 500;
int main(int argc,char* argv[]){
    unsigned long SIZE2=500;
    char astring2[SIZE2];
    char astring[SIZE1];
    return 0;
}

If I remove the line containing "define" and the line containing char astring[SIZE1]; then the program will work nicely, however at least 4 bytes in memory are required to store the number 500.

What I'd like to see work which does not work is me removing these two lines instead:

unsigned long SIZE2=500;
char astring2[SIZE2];

When I compile the complete code above, the compiler gives me these errors:

./teststring.c: In function 'main':
./teststring.c:6: error: expected ']' before ';' token

This suggests to me that it has a problem with SIZE1. I also tried putting quotes around the value of SIZE1 and I still receive the same error.

Is there a way I can do this or am I forced to store a number in memory to use it?

I don't want to constantly type the same number everywhere I need it in my program, so please don't suggest char astring[500] as an answer, but it would be nice if the compiler did that behind the scenes for me as it compiles the code to an executable format.

And my compiler is GCC version 4.1.2.

Upvotes: 2

Views: 84

Answers (1)

Almo
Almo

Reputation: 15861

You mean

#define SIZE1 500

In your code, whenever you use SIZE1 a semicolon is being inserted. Preprocessor commands like #define don't end with semicolons.

Upvotes: 11

Related Questions