can't replace define to const

X

xianwei

when i complier program include the following line, the program will
tell me
error: variable-size type declared outside of any function
const int TSIZE = 45;
const int FMAX = 5;

typedef struct {
char title[TSIZE];
int rating;
} film;

when i changed the
const int TSIZE = 45;
to
#define TSIZE 45
then program be well

Is there have anyone tell me why?
 
J

jacob navia

xianwei said:
when i complier program include the following line, the program will
tell me
error: variable-size type declared outside of any function
const int TSIZE = 45;
const int FMAX = 5;

typedef struct {
char title[TSIZE];
int rating;
} film;

when i changed the
const int TSIZE = 45;
to
#define TSIZE 45
then program be well

Is there have anyone tell me why?

Because in C, a declaration like
const int TSIZE=45;

is not considered a constant expression.
Some compilers, like lcc-win, will accept this in non conforming mode.
 
C

Chris Dollin

xianwei said:
when i complier program include the following line, the program will
tell me
error: variable-size type declared outside of any function
const int TSIZE = 45;
const int FMAX = 5;

typedef struct {
char title[TSIZE];
int rating;
} film;

when i changed the
const int TSIZE = 45;
to
#define TSIZE 45
then program be well

Is there have anyone tell me why?

In C, a variable declared `const` isn't a constant and so cannot appear
in a constant-expression.

`45`, however, /is/ a constant.
 
X

xianwei

thanks to Chris Dollin, jacob navia.
however, have methods to resolve this problem, use const statements
take the place of define statemetns?
because someone tell me use const better than define ~~~,

xianwei said:
when i complier program include the following line, the program will
tell me
error: variable-size type declared outside of any function
const int TSIZE = 45;
const int FMAX = 5;
typedef struct {
char title[TSIZE];
int rating;
} film;
when i changed the
const int TSIZE = 45;
to
#define TSIZE 45
then program be well
Is there have anyone tell me why?

In C, a variable declared `const` isn't a constant and so cannot appear
in a constant-expression.

`45`, however, /is/ a constant.

--
Chris "for fixed values of 10, anyway" Dollin

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN
 
C

Chris Dollin

xianwei said:
thanks to Chris Dollin, jacob navia.
however, have methods to resolve this problem, use const statements
take the place of define statemetns?
because someone tell me use const better than define ~~~,

For integer constants, I'd use an enum declaration (after making sure
that the base enum type can represent my numbers, which I can't do
Right Now as I have a train to catch -- but anything in range of an
int will be OK).
 
B

Bart van Ingen Schenau

xianwei said:
thanks to Chris Dollin, jacob navia.
however, have methods to resolve this problem, use const statements
take the place of define statemetns?
because someone tell me use const better than define ~~~,
Probably, that other person was talking about C++, which is a different
language.

In C, manifest constants must be introduced with a #define preprocessor
statement.
In C++, it is preferred to use const qualified variables for the same
purpose, although a #define is also possible.

Bart v Ingen Schenau
 
J

Jack Klein

Probably, that other person was talking about C++, which is a different
language.

In C, manifest constants must be introduced with a #define preprocessor
statement.

....or with an enumeration type declaration, if they are integer values
in the range of a signed int.
In C++, it is preferred to use const qualified variables for the same
purpose, although a #define is also possible.

Bart v Ingen Schenau

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
J

Jack Klein

when i complier program include the following line, the program will
tell me
error: variable-size type declared outside of any function
const int TSIZE = 45;
const int FMAX = 5;

typedef struct {
char title[TSIZE];
int rating;
} film;

when i changed the
const int TSIZE = 45;
to
#define TSIZE 45
then program be well

Is there have anyone tell me why?

As already said, because that is the way the C language is defined.

But what you could do is:

enum { TSIZE = 45, FMAX = 5 };

....and then use the symbols in constant expressions. Only works for
whole number values in the range that can fit in a signed int.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
X

xianwei

when i complier program include the following line, the program will
tell me
error: variable-size type declared outside of any function
const int TSIZE = 45;
const int FMAX = 5;
typedef struct {
char title[TSIZE];
int rating;
} film;
when i changed the
const int TSIZE = 45;
to
#define TSIZE 45
then program be well
Is there have anyone tell me why?

As already said, because that is the way the C language is defined.

But what you could do is:

enum { TSIZE = 45, FMAX = 5 };

...and then use the symbols in constant expressions. Only works for
whole number values in the range that can fit in a signed int.

--
Jack Klein
Home:http://JK-Technology.Com
FAQs for
comp.lang.chttp://c-faq.com/
comp.lang.c++http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html

very thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top