What is the difference between Static Variable and Ordinary Variable?Which Variable is expensive to use in c++ programming?Give some resion?

3 Years
Discussion Span
Last Post by melissad

Why don't you Google 'example uses of static variables in C++ or C' ... or something like that ... you will learn, by the 'where' and 'why for' of usage ... much.

... glad to clarify, but if you are serious about learning to program, learning to use Google, or other search engines is key.

Hint, how long does a static variable persist in memory?

  1. Until you exit a program?
  2. Until you exit a function where it is declared?
  3. Until the cows come home?
  4. Until you pull the plug?

Also, Google ...
what gets stored on stack
what gets stored on heap
and perhaps study up on 'read only memory', while you are at it.
(mutable memory vs immutable)

Edited by David W


A static variable is a single memory location associated with the class.

A non-static variable (that is a member of a class) represents a different memory location for each instance of the class.

Static variables can be initialized only once and assign to 0 when an object is created.
