static cons v/s cons

C

c.lang.myself

What is difference between following two-

const int constant 23;

v/s

static const int constant 23;
 
Z

Zero

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
 
F

Flash Gordon

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

litchie

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

James Kuyper

litchie said:
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.
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top