Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use keywords in C language

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to use keywords in C language". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Keyword change history

On December 16, 1999, ISO launched the C99 standard, which added five C language keywords:

Inline restrict _ Bool _ Complex _ Imaginary (note that bool has never been a C language keyword)

On December 8, 2011, ISO released a new standard for C language, C11, which added seven C language keywords:

_ Alignas _ Alignof _ Atomic _ Static_assert _ Noreturn _ Thread_local _ Generic

two。 Keyword list auto: declare automatic variable break: jump out of the current loop case: switch statement branch char: declare character variable or function return value type const: declare read-only variable continue: end the current loop Start the next round of loop default: switch statement "other" branch do: loop body of loop statement double: declare double-precision floating point variable or function return type else: conditional statement negative branch (used with if) enum: declare enumerated type extern: declare variable or function to define float in another file or elsewhere in this file: declare floating point variable or function return type for: A loop statement goto: unconditional jump statement if: conditional statement int: declare integer variable or function long: declare long integer variable or function return value type register: declare register variable return: subroutine return statement (can take parameters Short: declare short integer variable or function signed: declare signed type variable or function sizeof: calculate data type or variable length (that is, bytes occupied) static: declare static variable struct: declare structure type switch: used for switching statement typedef: used to alias data type unsigned: declare unsigned type variable or function union: declare common type void: declare function has no return value or argument Declare untyped pointer volatile: indicates that variables can be implicitly changed during program execution while: loop conditions for loop statements 3.c language keyword advantages (c99, not c11 standard)

Simple and compact, flexible and convenient

ANSI C has only 32 keywords and 9 kinds of control statements. The program is written freely and is case-sensitive. Combine the basic structure and statements of the high-level language with the practicality of the low-level language. C language can operate on bits, bytes and addresses like assembly language, which are the most basic work units of a computer.

Operator rich

The operators of the C language cover a wide range of 34 operators. The C language treats parentheses, assignments, cast, and so on as operators. As a result, the operation types of C language are extremely rich and the expression types are diversified. Flexible use of various operators can achieve operations that are difficult to achieve in other high-level languages.

Rich data types

The data types of C language are: integer type, real type, character type, array type, pointer type, structure type, common type and so on. It can be used to realize the operation of various complex data structures. The concept of pointer is introduced to make the program more efficient. In addition, C language has a powerful graphics function, supporting a variety of monitors and drivers. And the calculation function and logic judgment function are powerful.

At the same time, there are a variety of powerful extension functions for different compilers.

In addition,

C language is so rich in data types and powerful pointer functions that it has a strong ability to control the hardware, so many operating system kernels and MCU chip program development prefer hardware.

4. You can mention the volatile keyword here.

Volatile is always related to optimization. The compiler has a technology called data flow analysis, which analyzes where the variables in the program are assigned, where they are used, and where they fail. The analysis results can be used for constant merging, constant propagation and other optimization, and some code can be further eliminated. But sometimes these optimizations are not needed by the program, and you can disable them with the volatile keyword.

Volatile is meant to be "volatile" because accessing registers is much faster than accessing memory cells, so compilers generally optimize to reduce access memory, but may read dirty data. When required to declare the value of a variable using volatile, the system always re-reads data from its memory, even if its previous instructions have just read data from there. To be precise, when you encounter a variable declared by this keyword, the compiler no longer optimizes the code that accesses the variable, providing stable access to a particular address; if you do not use valatile, the compiler will optimize the declared statement. (to put it succinctly: the volatile keyword affects the compilation result of the compiler. Use the variable declared by volatile to indicate that the variable may change at any time. The operation related to this variable should not be optimized to avoid errors.)

Look at two examples:

1 > tell compiler not to do any optimization

For example, to send two instructions to an address: int * ip =.; / / device address * ip = 1; / / the first instruction * ip = 2; / / the second instruction above the program compiler may be optimized: int * ip =.; * ip = 2; as a result, the first instruction is lost. If you use volatile, compiler does not allow any optimization, thus ensuring the original intention of the program: volatile int * ip =.; * ip = 1 / 2; even if you want compiler to optimize, it will not turn the two payment statements into one. It can only do other optimizations.

2 > variables defined with volatile are changed outside the program and must be read from memory each time, and backups placed in cache or registers cannot be reused.

For example:

Volatile char a; astat0; while (! a) {/ / do some things;} doother (); if there is no volatiledoother () will not be executed "how to use keywords in C language" content here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report