C++ Storage ClassesStorage class is used to define the lifetime and visibility of a variable and/or function within a C++ program. Lifetime refers to the period during which the variable remains active and visibility refers to the module of a program in which the variable is accessible. There are five types of storage classes, which can be used in a C++ program
Automatic Storage ClassIt is the default storage class for all local variables. The auto keyword is applied to all local variables automatically. The above example defines two variables with a same storage class, auto can only be used within functions. Register Storage ClassThe register variable allocates memory in register than RAM. Its size is same of register size. It has a faster access than other variables. It is recommended to use register variable only for quick access such as in counter. Note: We can't get the address of register variable. Static Storage ClassThe static variable is initialized only once and exists till the end of a program. It retains its value between multiple functions call. The static variable has the default value 0 which is provided by compiler. Output: i= 1 and j= 1 i= 2 and j= 1 i= 3 and j= 1 External Storage ClassThe extern variable is visible to all the programs. It is used if two or more files are sharing same variable or function.
Next TopicC++ Arrays
|
JavaTpoint offers too many high quality services. Mail us on [email protected], to get more information about given services.
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week