static Keyword
Static can be used to modify variables in C, and can also be used to modify functions.
static modifier variable
Variables can exist in three places in C: the global data area, the stack, and the heap. The stack and the heap are different.
int a ;
int main() {
int b ;
int c* = (int *)malloc(sizeof(int));
}
In the above code, a is a global variable, b is a stack variable, and c is a pointer to a heap variable.
Static modifier for global variables: restricts this variable to be referenced by this file only. Some programs are composed of multiple C files, which can reference variables from each other, but after adding static modifier, it can only be referenced by functions in this file.
Static modifies stack variables: The life cycle of stack variables is extended to the end of program execution. Generally speaking, the life cycle of stack variables is managed by the OS, and the life of stack variables ends during the stack unloading process. However, after adding the static modifier, the variable is no longer stored in the stack, but is stored with global variables. It is just scoped to the current function, and cannot be used after leaving the function that defines it, but when the function is called again, it can continue to be used, and the previous value is saved.
static modified function
The modification of static to functions is similar to the modification of global variables. It can only be called by functions in this file, and cannot be called by functions in other files of the same program.
static int i; //Only available in the current file
int j; // Available in the project
static void init() { //Only available in the current file
}
void callme() { // Available in the project
static int sum;
}
The global variable i and init() function above are only available in the current file. The scope of the global variable sum is only in the callme() function. The visibility of the variable j and the callme() function is the entire project. They can be called in another C file using the extern keyword. extern is used to tell the compiler that this variable or function has been defined in another file.
extern int j; //Call the file a
extern void callme(); //Call the file a
int main() {
callme();
}
Another use of extern is when C and C++ are mixed, if C++ calls a function or variable defined in a C source file, then extern should be added to tell the compiler to name the function in C way.
File A.cpp calls the variable i and function callme() defined in the previous file:
extern "C" //Call the variable in the c file in the c++ file
{
int j;
void callme();
}
int main() {
callme();
}
Rules of Use
Variables and methods that only need to be accessed in the current file can be defined as static types to reduce the risk of naming conflicts.
const, code keywords
Variables defined with const are stored in the RAM area (data, idata, xdata, etc.) along with their data. If you want to store them in the ROM area, you need to declare them with the code type, for example:
code char test[] = "This is a text string";
Variables declared with const far are stored in the HCONST memory area, which is usually in ROM.
STRING This compiler variable specifies where string constants are stored. This configuration allows you to place them in CODE, HCONST, or XCONST (the constant area in XDATA).
For any large lookup table, it should be declared as code, for example
code unsigned char default_base_fuel_PW_map[] = {0x08,0x08, ...} ;
reference
https://www.keil.com/support/man/docs/c51/c51_le_const.htm
Previous article:STC89C52 drives MAX7219LED dot matrix cascade, text scrolling effect
Next article:STC89C516 drives DS12C887 clock module
Recommended ReadingLatest update time:2024-11-23 02:03
- Popular Resources
- Popular amplifiers
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- STMicroelectronics discloses its 2027-2028 financial model and path to achieve its 2030 goals
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- 【GD32E231_DIY】-02: Development environment construction and creating a new project
- TI C64X DSP interrupt vector table configuration (hardware interrupt)
- How to create a hit product of 49 yuan NB-IOT locator?
- Taking a “family-first” approach to op amp design
- Who knows the name of the chip?
- How to realize 12864 display of 430f149 MCU AD sampling waveform
- What is FPGA?
- Pre-match training FAQ: Detailed explanation of ON Semiconductor's ultra-low power RSL10 Bluetooth SoC development board
- How to start with PCB layout and wiring to avoid noise caused by improper switching power supply layout
- Answer questions and get free samples: ON Semiconductor launches ultra-low power Bluetooth chip RSL10