Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > static cons v/s cons

Reply
Thread Tools

static cons v/s cons

 
 
c.lang.myself@gmail.com
Guest
Posts: n/a
 
      12-07-2008
What is difference between following two-

const int constant 23;

v/s

static const int constant 23;
 
Reply With Quote
 
 
 
 
Zero
Guest
Posts: n/a
 
      12-07-2008
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
 
Reply With Quote
 
 
 
 
Flash Gordon
Guest
Posts: n/a
 
      12-07-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) 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 (E-Mail Removed)
If emailing me use my reply-to address
See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/
 
Reply With Quote
 
nick_keighley_nospam@hotmail.com
Guest
Posts: n/a
 
      12-07-2008
On Dec 7, 4:12*pm, "(E-Mail Removed)" <(E-Mail Removed)>
wrote:
> What is difference between following two-
>
> const int constant 23;
>
> * * * * * * * *v/s
>
> static const int constant 23;


this isn't C code
 
Reply With Quote
 
litchie
Guest
Posts: n/a
 
      12-08-2008
On Dec 7, 8:12*am, "(E-Mail Removed)" <(E-Mail Removed)>
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.
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      12-08-2008
litchie wrote:
> On Dec 7, 8:12 am, "(E-Mail Removed)" <(E-Mail Removed)>
> 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.
 
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
Web Services vs. Remoting Hosted In IIS - Pros/Cons Whats the difference? Lucas Tam ASP .Net 3 08-09-2005 06:07 PM
static cons and static andy C Programming 10 11-08-2004 10:18 AM
PSL pros and cons Kumar Vijay Mishra VHDL 2 10-02-2004 12:34 AM
Static Method Pros and Cons Axehelm Java 8 05-28-2004 01:57 AM
Pros and Cons about automatic increase in MS SQL 2000 Benny ASP .Net 1 03-01-2004 10:21 PM



Advertisments