static, const, code keywords in C51 development

Publisher:千变万化Latest update time:2022-08-08 Source: csdn Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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

Reference address:static, const, code keywords in C51 development

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

The role of data, idata, xdata, pdata, and code in C51
In terms of data storage type, the 8051 series has on-chip and off-chip program memory, on-chip and off-chip data memory. The on-chip program memory is also divided into direct addressing area and indirect addressing area. Data: Store the data in the 128B RAM space from 0x00 to 0x7f. You can use acc to read and
[Microcontroller]
Detailed explanation of the usage of const and volatile in embedded C language
       The main feature of const is read-only, with constants and constant pointers. If you don't carefully analyze the writing format of C language statements, coupled with the use of pointers, it is particularly easy to make mistakes.        The volatile keyword is a type modifier. The type variable declared with it
[Microcontroller]
MCU data/idata/xdata/pdata/bit/code
DATA: Fixedly refers to the first 128 RAM from 0x00 to 0x7f, which can be directly read and written using acc, with the fastest speed and the smallest generated code. DATA is a data memory address between 0 and 127, or a special function register (SFR) address in the range of 128 .. 255. The two are accessed in diff
[Microcontroller]
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号