You are reading the article Storage Classes In C: Auto, Extern, Static, Register (Examples) updated in December 2023 on the website Hatcungthantuong.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested January 2024 Storage Classes In C: Auto, Extern, Static, Register (Examples)
What is Storage Class in C?
A storage class represents the visibility and a location of a variable. It tells from what part of code we can access a variable. A storage class in C is used to describe the following things:
The variable scope.
The location where the variable will be stored.
The initialized value of a variable.
A lifetime of a variable.
Who can access a variable?
Thus a storage class is used to represent the information about a variable.
NOTE: A variable is not only associated with a data type, its value but also a storage class.
What are the Types of Storage Classes in C?There are total four types of standard storage classes. The table below represents the storage classes in C.
Storage class Purpose
auto It is a default storage class.
extern It is a global variable.
static It is a local variable which is capable of returning a value even when control is transferred to the function call.
register It is a variable which is stored inside a Register.
In this C tutorial, you will learn different types of storage classes in C with examples-
Auto Storage Class in C
The variables defined using auto storage class are called as local variables. Auto stands for automatic storage class. A variable is in auto storage class by default if it is not explicitly specified.
The scope of an auto variable is limited with the particular block only. Once the control goes out of the block, the access is destroyed. This means only the block in which the auto variable is declared can access it.
A keyword auto is used to define an auto storage class. By default, an auto variable contains a garbage value.
Example, auto int age;The program below defines a function with has two local variables
int add(void) { int a=13; auto int b=48; return a+b;}We take another program which shows the scope level “visibility level” for auto variables in each block code which are independently to each other:
int main( ) { auto int j = 1; { auto int j= 2; { auto int j = 3; printf ( ” %d “, j); } printf ( “t %d “,j); } printf( “%dn”, j);}
OUTPUT:
3 2 1Extern Storage Class in C
Extern stands for external storage class. Extern storage class is used when we have global functions or variables which are shared between two or more files.
Keyword extern is used to declaring a global variable or function in another file to provide the reference of variable or function which have been already defined in the original file.
The variables defined using an extern keyword are called as global variables. These variables are accessible throughout the program. Notice that the extern variable cannot be initialized it has already been defined in the original file.
Example, extern void display(); First File: main.cextern i; main() { printf(“value of the external integer is = %dn”, i); return 0;}
Second File: original.ci=48;
Result:
value of the external integer is = 48In order to compile and run the above code, follow the below steps
In order to compile and run the above code, follow the below steps
Step 1) Create new project,
Select Console Application
Step 6) Put the main code as shown in the previous program in the main.c file and save it
Step 8) Put and save the C code of the original.c file shown in the previous example without the main() function.
Step 9) Build and run your project. The result is shown in the next figure
Static Storage Class in C
The static variables are used within function/ file as local static variables. They can also be used as a global variable
Static local variable is a local variable that retains and stores its value between function calls or block and remains visible only to the function or block in which it is defined.
Static global variables are global variables visible only to the file in which it is declared.
Example: static int count = 10;Keep in mind that static variable has a default initial value zero and is initialized only once in its lifetime.
void next(void); static int counter = 7; /* global variable */ main() { while(counter<10) { next(); counter++; } return 0;} void next( void ) { /* function definition */ static int iteration = 13; /* local static variable */ iteration ++; printf(“iteration=%d and counter= %dn”, iteration, counter);}
Result:
iteration=14 and counter= 7 iteration=15 and counter= 8 iteration=16 and counter= 9Global variables are accessible throughout the file whereas static variables are accessible only to the particular part of a code.
The lifespan of a static variable is in the entire program code. A variable which is declared or initialized using static keyword always contains zero as a default value.
Register Storage Class in C
You can use the register storage class when you want to store local variables within functions or blocks in CPU registers instead of RAM to have quick access to these variables. For example, “counters” are a good candidate to be stored in the register.
Example: register int age;The keyword register is used to declare a register storage class. The variables declared using register storage class has lifespan throughout the program.
It is similar to the auto storage class. The variable is limited to the particular block. The only difference is that the variables declared using register storage class are stored inside CPU registers instead of a memory. Register has faster access than that of the main memory.
The variables declared using register storage class has no default value. These variables are often declared at the beginning of a program.
main() { {register int weight; int *ptr=&weight ;/*it produces an error when the compilation occurs ,we cannot get a memory location when dealing with CPU register*/} }
OUTPUT:
error: address of register variable 'weight' requestedThe next table summarizes the principal features of each storage class which are commonly used in C programming
Storage Class Declaration Storage Default Initial Value Scope Lifetime
auto Inside a function/block Memory Unpredictable Within the function/block Within the function/block
register Inside a function/block CPU Registers Garbage Within the function/block Within the function/block
extern Outside all functions Memory Zero Entire the file and other files where the variable is declared as extern program runtime
Static (local) Inside a function/block Memory Zero Within the function/block program runtime
Static (global) Outside all functions Memory Zero Global program runtime
SummaryIn this tutorial we have discussed storage classes in C, to sum up:
A storage class in C is used to represent additional information about a variable.
Storage class represents the scope and lifespan of a variable.
It also tells who can access a variable and from where?
Auto, extern, register, static are the four different storage classes in a C program.
A storage class specifier in C language is used to define variables, functions, and parameters.
auto is used for a local variable defined within a block or function
register is used to store the variable in CPU registers rather memory location for quick access.
Static is used for both global and local variables. Each one has its use case within a C program.
Extern is used for data sharing between C project files.
You're reading Storage Classes In C: Auto, Extern, Static, Register (Examples)
Interfaces In C++ (Abstract Classes)
Interfaces in C++ (Abstract Classes)
An interface describes the behavior or capabilities of a C++ class without committing to a particular implementation of that class.
The C++ interfaces are implemented using abstract classes and these abstract classes should not be confused with data abstraction which is a concept of keeping implementation details separate from associated data.
A class is made abstract by declaring at least one of its functions as pure virtual function. A pure virtual function is specified by placing “= 0” in its declaration as follows −
class Box { public: virtual double getVolume() = 0; private: double length; double breadth; double height; };The purpose of an abstract class (often referred to as an ABC) is to provide an appropriate base class from which other classes can inherit. Abstract classes cannot be used to instantiate objects and serves only as an interface. Attempting to instantiate an object of an abstract class causes a compilation error.
Thus, if a subclass of an ABC needs to be instantiated, it has to implement each of the virtual functions, which means that it supports the interface declared by the ABC. Failure to override a pure virtual function in a derived class, then attempting to instantiate objects of that class, is a compilation error.
Classes that can be used to instantiate objects are called concrete classes.
Abstract Class ExampleConsider the following example where parent class provides an interface to the base class to implement a function called getArea() −
using namespace std; class Shape { public: virtual int getArea() = 0; void setWidth(int w) { width = w; } void setHeight(int h) { height = h; } protected: int width; int height; }; class Rectangle: public Shape { public: int getArea() { return (width * height); } }; class Triangle: public Shape { public: int getArea() { return (width * height)/2; } }; int main(void) { Rectangle Rect; Triangle Tri; Rect.setWidth(5); Rect.setHeight(7); cout << "Total Rectangle area: " << Rect.getArea() << endl; Tri.setWidth(5); Tri.setHeight(7); cout << "Total Triangle area: " << Tri.getArea() << endl; return 0; }When the above code is compiled and executed, it produces the following result −
Total Rectangle area: 35 Total Triangle area: 17You can see how an abstract class defined an interface in terms of getArea() and two other classes implemented same function but with different algorithm to calculate the area specific to the shape.
Designing StrategyAn object-oriented system might use an abstract base class to provide a common and standardized interface appropriate for all the external applications. Then, through inheritance from that abstract base class, derived classes are formed that operate similarly.
The capabilities (i.e., the public functions) offered by the external applications are provided as pure virtual functions in the abstract base class. The implementations of these pure virtual functions are provided in the derived classes that correspond to the specific types of the application.
This architecture also allows new applications to be added to a system easily, even after the system has been defined.
Advertisements
Static Members Of A C++ Class
Static Members of a C++ Class
We can define class members static using static keyword. When we declare a member of a class as static it means no matter how many objects of the class are created, there is only one copy of the static member.
A static member is shared by all objects of the class. All static data is initialized to zero when the first object is created, if no other initialization is present. We can’t put it in the class definition but it can be initialized outside the class as done in the following example by redeclaring the static variable, using the scope resolution operator :: to identify which class it belongs to.
Let us try the following example to understand the concept of static data members −
using namespace std; class Box { public: static int objectCount; Box(double l = 2.0, double b = 2.0, double h = 2.0) { cout <<"Constructor called." << endl; length = l; breadth = b; height = h; objectCount++; } double Volume() { return length * breadth * height; } private: double length; double breadth; double height; }; int Box::objectCount = 0; int main(void) { Box Box1(3.3, 1.2, 1.5); Box Box2(8.5, 6.0, 2.0); cout << "Total objects: " << Box::objectCount << endl; return 0; }When the above code is compiled and executed, it produces the following result −
Constructor called. Constructor called. Total objects: 2 Static Function MembersBy declaring a function member as static, you make it independent of any particular object of the class. A static member function can be called even if no objects of the class exist and the static functions are accessed using only the class name and the scope resolution operator ::.
A static member function can only access static data member, other static member functions and any other functions from outside the class.
Static member functions have a class scope and they do not have access to the this pointer of the class. You could use a static member function to determine whether some objects of the class have been created or not.
Let us try the following example to understand the concept of static function members −
using namespace std; class Box { public: static int objectCount; Box(double l = 2.0, double b = 2.0, double h = 2.0) { cout <<"Constructor called." << endl; length = l; breadth = b; height = h; objectCount++; } double Volume() { return length * breadth * height; } static int getCount() { return objectCount; } private: double length; double breadth; double height; }; int Box::objectCount = 0; int main(void) { cout << "Inital Stage Count: " << Box::getCount() << endl; Box Box1(3.3, 1.2, 1.5); Box Box2(8.5, 6.0, 2.0); cout << "Final Stage Count: " << Box::getCount() << endl; return 0; }When the above code is compiled and executed, it produces the following result −
Inital Stage Count: 0 Constructor called. Constructor called. Final Stage Count: 2cpp_classes_objects.htm
Advertisements
Datetime Getdatetimeformats() Method In C
The DateTime.GetDateTimeFormats() method in C# is used to convert the value of this instance to all the string representations supported by the standard date and time format specifiers.
SyntaxFollowing is the syntax −
public string[] GetDateTimeFormats () public string[] GetDateTimeFormats (char ch);Above, ch is a standard date and time format string.
ExampleLet us now see an example to implement the DateTime.GetDateTimeFormats() method −
using System; public class Demo { public static void Main() { DateTime d = new DateTime(2023, 11, 10, 7, 20, 45); string[] res = d.GetDateTimeFormats(); foreach(string s in res) Console.WriteLine(s); } } OutputThis will produce the following output −
11/10/2023 11/10/19 11/10/19 11/10/2023 19/11/10 2023-11-10 10-Nov-19 Sunday, November 10, 2023 November 10, 2023 Sunday, 10 November, 2023 10 November, 2023 Sunday, November 10, 2023 7:20 AM Sunday, November 10, 2023 07:20 AM Sunday, November 10, 2023 7:20 Sunday, November 10, 2023 07:20 November 10, 2023 7:20 AM November 10, 2023 07:20 AM November 10, 2023 7:20 November 10, 2023 07:20 Sunday, 10 November, 2023 7:20 AM Sunday, 10 November, 2023 07:20 AM Sunday, 10 November, 2023 7:20 Sunday, 10 November, 2023 07:20 10 November, 2023 7:20 AM 10 November, 2023 07:20 AM 10 November, 2023 7:20 10 November, 2023 07:20 Sunday, November 10, 2023 7:20:45 AM Sunday, November 10, 2023 07:20:45 AM Sunday, November 10, 2023 7:20:45 Sunday, November 10, 2023 07:20:45 November 10, 2023 7:20:45 AM November 10, 2023 07:20:45 AM November 10, 2023 7:20:45 November 10, 2023 07:20:45 Sunday, 10 November, 2023 7:20:45 AM Sunday, 10 November, 2023 07:20:45 AM Sunday, 10 November, 2023 7:20:45 Sunday, 10 November, 2023 07:20:45 10 November, 2023 7:20:45 AM 10 November, 2023 07:20:45 AM 10 November, 2023 7:20:45 10 November, 2023 07:20:45 11/10/2023 7:20 AM 11/10/2023 07:20 AM 11/10/2023 7:20 11/10/2023 07:20 11/10/19 7:20 AM 11/10/19 07:20 AM 11/10/19 7:20 11/10/19 07:20 11/10/19 7:20 AM 11/10/19 07:20 AM 11/10/19 7:20 11/10/19 07:20 11/10/2023 7:20 AM 11/10/2023 07:20 AM 11/10/2023 7:20 11/10/2023 07:20 19/11/10 7:20 AM 19/11/10 07:20 AM 19/11/10 7:20 19/11/10 07:20 2023-11-10 7:20 AM 2023-11-10 07:20 AM 2023-11-10 7:20 2023-11-10 07:20 10-Nov-19 7:20 AM 10-Nov-19 07:20 AM 10-Nov-19 7:20 10-Nov-19 07:20 11/10/2023 7:20:45 AM 11/10/2023 07:20:45 AM 11/10/2023 7:20:45 11/10/2023 07:20:45 11/10/19 7:20:45 AM 11/10/19 07:20:45 AM 11/10/19 7:20:45 11/10/19 07:20:45 11/10/19 7:20:45 AM 11/10/19 07:20:45 AM 11/10/19 7:20:45 11/10/19 07:20:45 11/10/2023 7:20:45 AM 11/10/2023 07:20:45 AM 11/10/2023 7:20:45 11/10/2023 07:20:45 19/11/10 7:20:45 AM 19/11/10 07:20:45 AM 19/11/10 7:20:45 19/11/10 07:20:45 2023-11-10 7:20:45 AM 2023-11-10 07:20:45 AM 2023-11-10 7:20:45 2023-11-10 07:20:45 10-Nov-19 7:20:45 AM 10-Nov-19 07:20:45 AM 10-Nov-19 7:20:45 10-Nov-19 07:20:45 November 10 November 10 2023-11-10T07:20:45.0000000 2023-11-10T07:20:45.0000000 2023-11-10T07:20:45 7:20 AM 07:20 AM 7:20 07:20 7:20:45 AM 07:20:45 AM 7:20:45 07:20:45 2023-11-10 07:20:45Z Sunday, November 10, 2023 7:20:45 AM Sunday, November 10, 2023 07:20:45 AM Sunday, November 10, 2023 7:20:45 Sunday, November 10, 2023 07:20:45 November 10, 2023 7:20:45 AM November 10, 2023 07:20:45 AM November 10, 2023 7:20:45 November 10, 2023 07:20:45 Sunday, 10 November, 2023 7:20:45 AM Sunday, 10 November, 2023 07:20:45 AM Sunday, 10 November, 2023 7:20:45 Sunday, 10 November, 2023 07:20:45 10 November, 2023 7:20:45 AM 10 November, 2023 07:20:45 AM 10 November, 2023 7:20:45 10 November, 2023 07:20:45 November 2023 November 2023 ExampleLet us now see another example to implement the DateTime.GetDateTimeFormats() method. For char format, we use format specifiers like “d” for Short date pattern, “D” for Long date pattern, “F” for Full date/time pattern (long time), etc,
using System; public class Demo { public static void Main() { DateTime d = new DateTime(2023, 11, 10, 7, 20, 45); string[] res = d.GetDateTimeFormats('F'); foreach(string s in res) Console.WriteLine(s); } } OutputThis will produce the following output −
Sunday, November 10, 2023 7:20:45 AM Sunday, November 10, 2023 07:20:45 AM Sunday, November 10, 2023 7:20:45 Sunday, November 10, 2023 07:20:45 November 10, 2023 7:20:45 AM November 10, 2023 07:20:45 AM November 10, 2023 7:20:45 November 10, 2023 07:20:45 Sunday, 10 November, 2023 7:20:45 AM Sunday, 10 November, 2023 07:20:45 AM Sunday, 10 November, 2023 7:20:45 Sunday, 10 November, 2023 07:20:45 10 November, 2023 7:20:45 AM 10 November, 2023 07:20:45 AM 10 November, 2023 7:20:45 10 November, 2023 07:20:45Static Vs Dynamic Web Page
Difference Between Static vs Dynamic Web Page
Web development, programming languages, Software testing & others
Dynamic web pages are written in languages like C++, Python, ASP. Net, AJAX, etc. Information like the weather forecast, social media posts, news channel, live updates of any tournament, which keeps on changing after every few seconds, comes under the dynamic web page because every time the user sends the request, the content to be displayed is changed on the web page.
The web page is simply a document that is available on the WWW (World Wide Web) to be accessible as a web resource. In order to access these web pages, a web browser (which acts as a client) sends the request to the Webserver (acting as a server) through HTTP protocol. The server then responds to the client by displaying the web page on a web browser. Static web pages are simple pages written in languages like HTML, CSS, and JavaScript. When the client sends the request to the Webserver, the server responds to the request by displaying the web page to the web browser without performing any additional actions. The server acts as a simple storage location for the web pages, which are displayed to the client upon request.
Head to Head Comparison between Static vs Dynamic Web Page (Infographics)Below are the top 7 comparisons between Static vs Dynamic Web Page:
Key Differences Between Static vs Dynamic Web PageLet us discuss some key differences between Static vs Dynamic Web Page in the following points:
2. Static web pages are not interactive, i.e. they display the same results every time a page is requested, whereas dynamic web pages take inputs from the user, processing them and displaying results accordingly
3. Static web pages are not scalable, i.e. if the user wishes to display various types of data to the client, a different web page is required for each type, whereas this can happen in a single dynamic web page by displaying the data according to the user requirements.
4. Real-time information through APIs and the data from the databases can be easily accessed in an organized and structured way through dynamic web pages, which is not possible in the case of static web pages that display the data statically and no interaction with the database and other APIs is possible.
6. Talking about the cost of creating both static and dynamic web pages, static web pages costs only at the starting of creation but ultimately, dynamic web pages cost much higher as the cost of functionality, database, and platform for writing the code is added.
7. In terms of simplicity, static web pages are very simple to create as newbies can easily create them in development, whereas strong server-side scripting knowledge, database knowledge and interaction between the two are needed in the case of dynamic web pages.
Static vs Dynamic Web Page Comparison TableThe table below summarizes the comparisons between Static vs Dynamic Web Page:
Static web pages Dynamic web pages
A static web page’s content remains the same and does not change on any specific factor until the programmer/user changes it manually. The content of the dynamic web page keeps on changing depending on various factors.
Web programming languages like HTML, CSS, JavaScript, etc., are used to create static web pages. Web programming languages like AJAX, Python, chúng tôi etc., are used to create dynamic web pages.
The page loading time of a static web page is very less than that of a dynamic web page until very heavy images, videos, graphics are attached to the page. The page loading time of a dynamic web page is comparatively higher than that static web page as every time the processing written in the program is performed before displaying the web page.
Good programming knowledge is needed to create the logic of the dynamic web page.
No interaction with databases or other websites/servers is done in static web pages. Interaction with APIs, databases, other web servers are created in the case of dynamic web pages.
Content change rarely happens in the case of a static web page until the programmer changes the file manually. Content change frequently happens in the case of the dynamic web page as the server generates the new content every time the request is sent.
Used where only the informational part needs to be displayed to the client. Used when the interactive, changing and functional content needs to be displayed to the client.
ConclusionThe above description clearly explains the difference between static and dynamic web pages. There is always confusion for the newbies in development between the two. Before programming or creating web pages, it is important to have in-depth knowledge of each terminology, like web pages, websites, web server, and client-server architecture, to understand things better. An important point to keep in mind regarding the static and dynamic web page is that both the pages’ response is the HTML content in the web browser through the HTTP protocol.
Recommended ArticlesThis is a guide to the top difference between Static vs Dynamic Web Page. Here we also discuss the Static vs Dynamic Web Page key differences with Infographics and Comparison table. You may also have a look at the following articles to learn more –
In Ios 12, Imessage Images Are Auto
iOS 12 auto-saves any iMessage attachments snapped with the Messages camera to Photos.
TAKEAWAYS:
iOS 12 auto-saves images taken with Messages to the Photos app.
This happens as soon as you attach the image to a thread.
Previously, Messages didn’t save your photos to the camera roll.
Images snapped with the Messages camera are saved as JPGs, not HEIC files.
To protect your privacy, location continues to be stripped from iMessage photos.
These changes were first spotted on iOS 12, which released in September 2023.
Messages camera gets smarterTaking an image with iOS 11’s Messages camera and attaching it to an iMessage wouldn’t automatically save the photo to the camera roll. On iOS 12 and up, any photos taken using the camera built into the Message app itself is simultaneously saved to Photos.
This is similar to other apps, like WhatsApp.
Tapping the blue arrow sends the photo and simultaneously saves it in Photos
Upon attaching your image to an iMessage, the Messages app creates a copy in Photos
There are many, many benefits to having all your images in the Photos. For starters, such a workflow lets you use the powerful new search capabilities on iOS 12 to find the image you need using keywords like people names, dates, the recognized object and scenes and more.
Two things to keep in mind.
JPEGs without location dataFirstly, the Messages camera saves the images to Photos in the ubiquitous JPEG format rather than in the space-saving HEIC file. As a result, these snaps will take up more storage than the equivalent photo taken with the Camera app.
The version saved to Photos is a JPEG file with location stripped from metadata
And secondly, the version saved to your camera roll is still location-free. Messages has always done that in order to protect your privacy, meaning such photos will be harder to surface in the future with the built-in Photos search feature.
TUTORIAL: How to use Photos iMessage app
Messages cannot be set to automatically add all the received photos and videos to the camera roll, but that doesn’t mean you can’t bulk-save iMessage attachments to Photos.
Begin by tapping the recipient’s name at the top of the thread, then choose Info.
Messages lets you save all iMessage image attachments to Photos with a few taps
Scroll down to browse the images from the thread. Tap and hold one of the photos and choose More from the bubble menu to select the media you’d like to keep, then tap Save Image(s).
Power users could even use Hazel for Mac to automate this task.
Other Photos and Messages perks on iOS 12Photos on iOS 12 delivers other improvements, starting with an all-new For You tab where you’ll find sharing and filter suggestions, shared iCloud albums, featured photos and so forth.
TUTORIAL: Using For You tab and other new features in iOS 12 Photos
As for Messages, you’ll notice the redesigned App Strip at the bottom with larger icons and a more streamlined interface overall, as evidenced by the screenshot right below.
The clunky Messages camera with its super tiny viewfinder and no support for taking images using the volume button has been replaced with a beautiful fullscreen camera with effects such as stickers, filters, text, shapes and more.
In iOS 12, attaching media to Messages is handled by the new Photos iMessage app
Even the process for attaching items from your library has been vastly improved thanks to a new Photos iMessage app (see that Photos icon in the App Strip at the bottom?).
TUTORIAL: How to use iMessage effects like stickers, filters and shapes
With it, not only can you easily attach one or more photos and videos from your library to an iMessage but also receive Siri-powered photo suggestions based on who you’re messaging with, what you’re talking about and where you’ve taken photos together.
How do you like these improvements for Photos and Messages on iOS 12?
Update the detailed information about Storage Classes In C: Auto, Extern, Static, Register (Examples) on the Hatcungthantuong.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!