Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > can't replace define to const

Reply
Thread Tools

can't replace define to const

 
 
xianwei
Guest
Posts: n/a
 
      09-10-2007
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?

 
Reply With Quote
 
 
 
 
jacob navia
Guest
Posts: n/a
 
      09-10-2007
xianwei wrote:
> 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.
 
Reply With Quote
 
 
 
 
Chris Dollin
Guest
Posts: n/a
 
      09-10-2007
xianwei wrote:

> 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

 
Reply With Quote
 
xianwei
Guest
Posts: n/a
 
      09-10-2007
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 ~~~,

On Sep 10, 10:38 pm, Chris Dollin <(E-Mail Removed)> wrote:
> xianwei wrote:
> > 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



 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      09-10-2007
xianwei wrote:

> 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).

--
Chris "don't be silly, nurse, it's a diesel." Dollin

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

 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      09-10-2007
xianwei wrote:

> thanks to Chris Dollin, jacob navia.


Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or:
<http://www.caliburn.nl/topposting.html>
 
Reply With Quote
 
Bart van Ingen Schenau
Guest
Posts: n/a
 
      09-10-2007
xianwei wrote:

> 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
--
a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
c.l.c FAQ: http://www.eskimo.com/~scs/C-faq/top.html
c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      09-11-2007
On Mon, 10 Sep 2007 20:59:39 +0200, Bart van Ingen Schenau
<(E-Mail Removed)> wrote in comp.lang.c:

> xianwei wrote:
>
> > 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.


....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
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      09-11-2007
On Mon, 10 Sep 2007 07:26:42 -0700, xianwei <(E-Mail Removed)>
wrote in comp.lang.c:

> 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
 
Reply With Quote
 
xianwei
Guest
Posts: n/a
 
      09-11-2007
On 9 11 , 9 27 , Jack Klein <(E-Mail Removed)> wrote:
> On Mon, 10 Sep 2007 07:26:42 -0700, xianwei <(E-Mail Removed)>
> wrote in comp.lang.c:
>
>
>
> > 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Replace #define with const??? MoslyChang C++ 7 08-09-2007 09:25 PM
const int versus #define - does "const int" take up space in exe? ThazKool C++ 1 06-11-2006 04:55 AM
use a const variable as opposed to #define - Multiple const objects created yancheng.cheok@gmail.com C++ 4 05-05-2006 10:30 AM
Is it really necessery to replace #define from const? Rayer C++ 4 03-22-2005 10:48 AM



Advertisments