Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static, define, and const in C

I've read that static variables are used inside function when one doesn't want the variable value to change/initialize each time the function is called. But what about defining a variable static in the main program before "main" e.g.

#include <stdio.h>

static double m = 30000;

int main(void)
{
value = m * 2 + 3;
}

Here the variable m has a constant value that won't get modified later in the main program. In the same line of thought what difference does it make to have these instead of using the static definition:

const double m = 30000;

or

#define m 30000  //m or M  

and then making sure here to use double operations in the main code so as to convert m to the right data type.

like image 321
yCalleecharan Avatar asked Apr 09 '10 21:04

yCalleecharan


People also ask

What is static and const in C?

const means that you're not changing the value after it has been initialised. static inside a function means the variable will exist before and after the function has executed. static outside of a function means that the scope of the symbol marked static is limited to that . c file and cannot be seen outside of it.

What is static and const?

A static keyword is been used to declare a variable or a method as static. A const keyword is been used to assign a constant or a fixed value to a variable. In JavaScript, the static keyword is used with methods and classes too. In JavaScript, the const keyword is used with arrays and objects too.

What is difference between #define and const?

#define is a preprocessor directive. Constants are used to make variables constant such that never change during execution once defined. 2. Constants are also called literals.

When to use const vs define C?

const and #define both are used for handle constants in source code, but they few differences. #define is used to define some values with a name (string), this defined string is known as Macro definition in C, C++ while const is a keyword or used to make the value of an identifier (that is constant) constant.


1 Answers

static double m = 30000; 

double foo(double x, double y) {
    return x/m + y;
}

This doesn't win you anything. A copy of m has to be made to do the computation. Also if you do:

double bar( double x, double y) {
     m += x + y;
     return  m;
}

Then all calls to bar will change m. Static variables outside of functions (or classes) are really global variables with file scope. Other files can't get at them by extern

Static variables inside a function are still like global variables, except that even other functions in the same file can't see them directly.

const double m = 30000;

This is better and in many cases best. If the compiler sees this global const and then sees a reference to m then it knows that rather than generate code to load the value from where ever it is (which likely requires loading a literal address into a register first) to a register or stack position to do computations it can just make a register be 30000 or sometimes generate an instruction with 30000 encoded right in there.

The down side to this is that the compiler has to assume that other souce files will want to read m and has to actually store a copy as a variable (but a constant variable) in the object file.

I'm not sure if it is standard but you can sometimes do extern const double m = 30000; and the compiler will use 30000 to optimize and assume that another file actually has a copy of m that will be stored in the executable. You can also do static const double m = 30000; and the compiler can assume that no one else will expect that a copy of m is stored in the object code generated from this source file.

Doing

#define m 30000

is more risky. You will not get a warning or error if previously there was another m declared as a variable, constant, or function. Also, for preprocessor macros like this it is easy to mess up. For example:

#define BASE_ADDRESS 48
#define MY_OFFSET  9
#define MY_ADDRESS  BASE_ADDRESS+MY_OFFSET
...
  return MY_ADDRESS*4;

Yes, this is a stupid example, but what this looks like after the preprocessor gets done with it is

...
  return 48+9*4;

Which is

 return 48+(9*4);

And that's not what you probably wanted.

Another place where macros are bad is when you have large constants, such as strings. Strings require that they be addressable by pointer and are more difficult to optimize away than integers and floating point literal or constant numbers. You could easily make a very large program if you had lots of stuff like:

#define JIM "Jim"
#define JOHN "John"

and then used JIM and JOHN all over your programs because the compiler might not be able to see that you really only needed the strings "Jom" and "John" once in the program.

That being said, it is not uncommon to see constants being declared like that, and often they are properly done that way by people who know what they are doing.

like image 171
nategoose Avatar answered Sep 23 '22 18:09

nategoose