static cons v/s cons

Discussion in 'C Programming' started by c.lang.myself@gmail.com, Dec 7, 2008.

  1. Guest

    What is difference between following two-

    const int constant 23;

    v/s

    static const int constant 23;
     
    , Dec 7, 2008
    #1
    1. Advertising

  2. Zero Guest

    Hi there,

    can you provide your code,
    the answer denpends whether you use the variables in the scope of a
    function or of a module.

    Zeh Mau
     
    Zero, Dec 7, 2008
    #2
    1. Advertising

  3. Flash Gordon Guest

    wrote, On 07/12/08 16:12:
    > What is difference between following two-
    >
    > const int constant 23;


    This is a syntax error without using the static keyword.

    > v/s
    >
    > static const int constant 23;


    This is a syntax error using the static keyword.

    Please make sure when asking a question you have compiled the actual
    code you are posting. Otherwise it can be hard to know what you are
    asking about.

    I'm guessing that the actual answer to your question would be the
    definition of the keyword static. What static means depends on whether
    it is a file scope or block scope definition. At file scope it means the
    variable cannot be accessed by name from any other translation unit (C
    source file + all the files it includes directly or indirectly), at
    block scope (i.e. declared within a function) it means that the variable
    exists for the lifetime of the program. The keyword const has nothing to
    do with the difference.
    --
    Flash Gordon
    If spamming me sent it to
    If emailing me use my reply-to address
    See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/
     
    Flash Gordon, Dec 7, 2008
    #3
  4. Guest

    On Dec 7, 4:12 pm, "" <>
    wrote:
    > What is difference between following two-
    >
    > const int constant 23;
    >
    >                v/s
    >
    > static const int constant 23;


    this isn't C code
     
    , Dec 7, 2008
    #4
  5. litchie Guest

    On Dec 7, 8:12 am, "" <>
    wrote:
    > What is difference between following two-
    >
    > const int constant 23;
    >
    >                v/s
    >
    > static const int constant 23;


    With "static", you can't reference the variable "constant" in another
    source file.
     
    litchie, Dec 8, 2008
    #5
  6. James Kuyper Guest

    litchie wrote:
    > On Dec 7, 8:12 am, "" <>
    > wrote:
    >> What is difference between following two-
    >>
    >> const int constant 23;
    >>
    >> v/s
    >>
    >> static const int constant 23;

    >
    > With "static", you can't reference the variable "constant" in another
    > source file.


    More precisely, you can't refer to it by name from another translation
    unit. You can refer to it using a pointer:

    get_static.h:
    ===================================
    #ifndef GET_STATIC_H
    #define GET_STATIC_H
    extern const int* get_static(void);
    #endif

    main.c:
    ===================================
    #include "get_static.h"

    int main(void)
    {
    printf("%d\n", *get_static());
    return 0;
    }

    get_static.c:
    ===============================================
    #include "get_static.h"
    static const in constant = 23;
    const int *get_value(void) { return &constant;}


    This is obviously less useful for objects defined as 'const' than it
    would be for objects whose values could be changed through the pointer.
     
    James Kuyper, Dec 8, 2008
    #6
    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. Kumar Vijay Mishra

    PSL pros and cons

    Kumar Vijay Mishra, Sep 29, 2004, in forum: VHDL
    Replies:
    2
    Views:
    2,651
    vhdlcohen
    Oct 2, 2004
  2. Benny
    Replies:
    1
    Views:
    455
    Paul Wistrand
    Mar 1, 2004
  3. Lucas Tam
    Replies:
    3
    Views:
    2,045
    Reza Alirezaei
    Aug 9, 2005
  4. Axehelm

    Static Method Pros and Cons

    Axehelm, May 26, 2004, in forum: Java
    Replies:
    8
    Views:
    8,265
    Roedy Green
    May 28, 2004
  5. andy

    static cons and static

    andy, Nov 5, 2004, in forum: C Programming
    Replies:
    10
    Views:
    550
    Thomas Stegen
    Nov 8, 2004
Loading...

Share This Page