当前位置:网站首页>Definition and usage of several special standards of C language
Definition and usage of several special standards of C language
2022-07-18 22:18:00 【strongerHuang】
Focus on + Star sign public Number , Don't miss the highlights

author | strongerHuang
WeChat official account | Embedded column
C Language has many special uses , If these special uses are used properly , Your code will become more robust , Easier to maintain .
For example, we are using STM32 Library assertion (assert), You will find that the official offer contains “__FILE__、__LINE__” Code for .
The standard peripheral library is located in stm32fxxx_conf.h file :
#define assert_param(expr) ((expr) ? (void)0 : assert_failed((uint8_t *)__FILE__, __LINE__))HAL Can make Assert:

In the code, you will see the following code :

If you add this assertion code , For developing large 、 Complex system , This sentence is actually very useful . Every time the program runs incorrectly , It will print the location specified by the program code , It is convenient for us to quickly find the wrong location in the huge program .
Our system will over time , Constantly update , That is, you need to submit many versions of executable files (hex、bin etc. ). however , The product is in later use , We upgraded some devices , Some devices may be ignored ( Forget to upgrade ), If there is a fault , How can we quickly find out which version of the software has failed ?
Our system will over time , Constantly update , That is, you need to submit many versions of executable files (hex、bin etc. ). however , The product is in later use , We upgraded some devices , Some devices may be ignored ( Forget to upgrade ), If there is a fault , How can we quickly find out which version of the software has failed ?
C Several special standard definitions
__FILE__ : Compiling file name
__LINE__ : Compiling line number of file
__DATE__: Date string at compile time Such as “Sep 22 2020”
__TIME__: Time string at compile time Such as ”10:00:00“
__STDC__: Judge whether the document is the standard C Program
1.__FILE__ Compile file name
File Chinese means document , The meaning here mainly refers to : The file being compiled corresponds to the path of the file being compiled and the name of the file .
Keil The path corresponding to the version is the path relative to the project file ,IAR The version path is relative Windows route .
Source code :
char BuildFile[] = __FILE__;
printf(" Compile file path :%s\n", BuildFile);Keil:
Compile file path :App\main.c
IAR:
Compile file path :C:\Users\strongerHuang\Desktop\ Project master directory \App\main.c
2.__LINE__ Compile file line number
The above is the compiled file name , Is a string , And here is the line number , It's an integer variable , This is the difference between the two ,
For example, add source code to the project :
char BuildLine = __LINE__;
printf(" The line where the code was compiled :%d\n", BuildLine);You can see strings that are not arrays , Print information :
The line where the code was compiled :44
In general ,__FILE__ Is and __LINE__ Use together , Used to print our code information , Convenient and fast location of code .
3.__DATE__ Compile date
__DATE__ date , It should be noted that : This date is when you compile Windows The date of the system , If the corresponding part of the code has been compiled before , There is no compilation later , This date is still the previous date , Instead of the later compilation date . therefore , If this is used to finalize the version , You need to recompile the project completely when finalizing the version , It will be updated to the date of your last compilation .
Code :
char BuildDate[] = __DATE__;
printf(" Compile date :%s\n", BuildDate);Output results :
Compile date :Sep 22 2017
4.__TIME__ Compile time
This and __DATE__ The same principle , Compile time , Also a string .
Once again remind : Used to finalize the version : Recompile required , This is the last compilation time .
Code :
char BuildTime[] = __TIME__;
printf(" Compile time :%s\n", BuildTime);Output results
Compile time :10:00:00
5.__STDC__ standard C Code
This standard is rarely used in our MCU and embedded programming , When the procedure is required to be strictly followed ANSIC The identifier is assigned as 1, It is mainly to judge whether our procedure documents are standards C Program .
Code :
#ifdef __STDC__
printf(" standard C Code file \n");
#else
printf(" Nonstandard C Code file \n");
#endif------------ END ------------

● special column 《 Embedded tools 》
● special column 《 Embedded development 》
● special column 《Keil course 》
● Embedded column selection tutorial
Pay attention to the reply of the official account “ Add group ” Join the technical exchange group according to the rules , reply “1024” See more .


Click on “ Read the original ” See more sharing .
边栏推荐
- 面试官:建造者模式是什么?
- Chapter 9.1 program design of MATLAB
- WTL第一个窗口
- Flutter is stuck in running gradle task 'assemblydebug' Solution of
- Graduation season -- common interview questions in database
- Omnivore, a non picky AI model, focuses on images, videos and 3D data!
- 动态规划之4种背包问题
- J-dict-select-tag drop-down box failure solution
- [UCOS III source code analysis] - time slice rotation
- SFF1602-MHCHXM超快恢复二极管SFF1602
猜你喜欢

Linux solves the problem of oracle:ora-12537: tns:connection closed

Omnivore, a non picky AI model, focuses on images, videos and 3D data!

国产之光!高分时空表征学习模型 UniFormer

Leetcode 1309. 解码字母到整数映射(可以,一次过)

C language - array

今天去 OPPO 面试,被问麻了

In depth analysis of multiple knapsack problem (Part 2)

How do top enterprises such as Starbucks, Coca Cola and apple carry out brand marketing

Arduino窗口乱码问题

A New Optimizer Using Particle Swarm Theory
随机推荐
Spark Streaming 编程指南
操作dom逆序,面试题
JS handwritten sort
Towhee 每日模型周报
How does MySQL execute SQL statements
What did the new operator do? Interview
[UCOS III source code analysis] - time slice rotation
RPA ecosystem revealed, supporting the life source of RPA enterprises' billions of valuation
今天去 OPPO 面试,被问麻了
js 华为od日志时间排序
Leetcode 1309. 解码字母到整数映射(可以,一次过)
迪奥疑似抄袭中国马面裙,国内官网已下架该产品
SCI paper submission process
C语言-数组
Shuan Q, Dachang was forced to graduate and recited eight part essay for a month without a window. Fortunately, he got an offer
Ucos-iii learning notes - Software Timer
C language - array
Leetcode 1331. Array sequence number conversion
Linux solves the problem of oracle:ora-12537: tns:connection closed
Linux 解决 Oracle :ORA-12537: TNS:connection closed(连接关闭)问题