Why is volatile needed in C?

前端 未结 18 2194
攒了一身酷
攒了一身酷 2020-11-22 04:07

Why is volatile needed in C? What is it used for? What will it do?

18条回答
  •  清酒与你
    2020-11-22 04:27

    There are two uses. These are specially used more often in embedded development.

    1. Compiler will not optimise the functions that uses variables that are defined with volatile keyword

    2. Volatile is used to access exact memory locations in RAM, ROM, etc... This is used more often to control memory-mapped devices, access CPU registers and locate specific memory locations.

    See examples with assembly listing. Re: Usage of C "volatile" Keyword in Embedded Development

提交回复
热议问题