can't replace define to const

Discussion in 'C Programming' started by xianwei, Sep 10, 2007.

  1. xianwei

    xianwei Guest

    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?
     
    xianwei, Sep 10, 2007
    #1
    1. Advertising

  2. xianwei

    jacob navia Guest

    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.
     
    jacob navia, Sep 10, 2007
    #2
    1. Advertising

  3. xianwei

    Chris Dollin Guest

    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
     
    Chris Dollin, Sep 10, 2007
    #3
  4. xianwei

    xianwei Guest

    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 <> 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
     
    xianwei, Sep 10, 2007
    #4
  5. xianwei

    Chris Dollin Guest

    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
     
    Chris Dollin, Sep 10, 2007
    #5
  6. xianwei

    Default User Guest

    Re: can't replace define to const - TPA

    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>
     
    Default User, Sep 10, 2007
    #6
  7. 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/
     
    Bart van Ingen Schenau, Sep 10, 2007
    #7
  8. xianwei

    Jack Klein Guest

    On Mon, 10 Sep 2007 20:59:39 +0200, Bart van Ingen Schenau
    <> 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
     
    Jack Klein, Sep 11, 2007
    #8
  9. xianwei

    Jack Klein Guest

    On Mon, 10 Sep 2007 07:26:42 -0700, xianwei <>
    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
     
    Jack Klein, Sep 11, 2007
    #9
  10. xianwei

    xianwei Guest

    On 9 11 , 9 27 , Jack Klein <> wrote:
    > On Mon, 10 Sep 2007 07:26:42 -0700, xianwei <>
    > 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
     
    xianwei, Sep 11, 2007
    #10
  11. xianwei

    xianwei Guest

    Re: can't replace define to const - TPA

    On 9 11 , 12 42 , "Default User" <> wrote:
    > 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>


    oh, i'm sorry, i will try to fixed it.

    thank you point my fault.
     
    xianwei, Sep 11, 2007
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rayer
    Replies:
    4
    Views:
    512
    msalters
    Mar 22, 2005
  2. Replies:
    4
    Views:
    423
    Alf P. Steinbach
    May 5, 2006
  3. ThazKool
    Replies:
    1
    Views:
    457
  4. MoslyChang

    Replace #define with const???

    MoslyChang, Aug 9, 2007, in forum: C++
    Replies:
    7
    Views:
    665
  5. Javier
    Replies:
    2
    Views:
    593
    James Kanze
    Sep 4, 2007
Loading...

Share This Page