The "static" keyword in C++ is used to declare a variable or a function with static duration. It has several implications, which can affect the behavior and scope of a variable or function. In this article, we'll discuss the differences between a static variable and a non-static variable in C++.
Storage Duration: A static variable is stored in the data segment of a program, while a non-static variable is stored on the stack. The data segment is a portion of memory that is reserved for storing data that persists throughout the life of a program, while the stack is used for temporary data storage.
Initialization: A static variable is automatically initialized to zero when the program starts, while a non-static variable needs to be explicitly initialized.
Scope: A static variable has file scope, meaning it can be accessed from anywhere within the same file. A non-static variable has block scope, meaning it can only be accessed within the block in which it is defined.
Lifetime: The lifetime of a static variable is the entire duration of the program, while the lifetime of a non-static variable is limited to the scope in which it is defined.
Accessibility: A static function can only be accessed within the same file, while a non-static function can be accessed from anywhere in the program.
Australia
UK
UAE
Singapore
Canada
New
Zealand
Malaysia
USA
India
South
Africa
Ireland
Saudi
Arab
Qatar
Kuwait
Hongkong
Copyright 2016-2023 www.programmingshark.com - All Rights Reserved.
Disclaimer : Any type of help and guidance service given by us is just for reference purpose. We never ask any of our clients to submit our solution guide as it is, anywhere.