Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 732431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:08:28+00:00 2026-05-14T07:08:28+00:00

I’ve read that static variables are used inside function when one doesn’t want the

  • 0

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.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T07:08:29+00:00Added an answer on May 14, 2026 at 7:08 am
    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 452k
  • Answers 452k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Someone emailed me this very question yesterday, and I answered… May 15, 2026 at 9:04 pm
  • Editorial Team
    Editorial Team added an answer The Django ORM does everything I want it to do.… May 15, 2026 at 9:04 pm
  • Editorial Team
    Editorial Team added an answer Each database object has a Triggers property, which you can… May 15, 2026 at 9:04 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.