# ISO/ANSI C Standard

### [1 Standard output functions (printf family)](https://www.rt-thread.org/document/site/#/rt-thread-version/rt-thread-standard/programming-manual/libc/iso-ansi-c?id=_1-%e6%a0%87%e5%87%86%e8%be%93%e5%87%ba%e5%87%bd%e6%95%b0-printf-%e5%ae%b6%e6%97%8f) <a href="#id-1-biao-zhun-shu-chu-han-shu-printf-jia-zu" id="id-1-biao-zhun-shu-chu-han-shu-printf-jia-zu"></a>

#### [1.1 printf function](https://www.rt-thread.org/document/site/#/rt-thread-version/rt-thread-standard/programming-manual/libc/iso-ansi-c?id=_11-printf-%e5%87%bd%e6%95%b0) <a href="#id-11printf-han-shu" id="id-11printf-han-shu"></a>

In Keil and IAR compilation platforms, users can directly use the printf function; in GCC, additional enabling `RT_USING_POSIX_FS`and `RT_USING_POSIX_STDIO`macros are required to use the printf function.

#### [1.2 Choosing between rt\_kprintf and printf](https://www.rt-thread.org/document/site/#/rt-thread-version/rt-thread-standard/programming-manual/libc/iso-ansi-c?id=_12-rt_kprintf-%e5%87%bd%e6%95%b0-%e4%b8%8e-printf-%e5%87%bd%e6%95%b0%e7%9a%84%e4%bd%bf%e7%94%a8%e9%80%89%e6%8b%a9) <a href="#id-12rtkprintf-han-shu-yu-printf-han-shu-de-shi-yong-xuan-ze" id="id-12rtkprintf-han-shu-yu-printf-han-shu-de-shi-yong-xuan-ze"></a>

If there is no special requirement, it is recommended to use the rt\_kprintf function, because printf is provided by the compiler platform, and its space occupation and memory usage are unknown. The printf function occupies much more ROM than the rt\_kprintf function.

Both the rt\_kprintf function and the printf function are non-thread functions. When used by multiple threads at the same time, cross-printing may occur. This is normal because according to the requirements of the C standard, the printf function is non-thread-safe.

The native rt\_kprintf function is optimized and occupies much less space than the printf function. However, the rt\_kprintf function does not support floating-point output. Therefore:

1. If you need the rt\_kprintf function to support floating-point output, you can install [the rt\_vsnprintf\_full package](https://github.com/mysterywolf/rt_vsnprintf_full) .
2. If you need the rt\_kprintf function to support thread-safe output, you can install [the rt\_kprintf\_threadsafe package](https://github.com/mysterywolf/rt_kprintf_threadsafe) .
3. The above two packages can be installed and used at the same time to enable rt\_kprintf to support thread safety and floating point type output capabilities.

#### [1.3 Other string formatting output functions (such as snprintf, etc.)](https://www.rt-thread.org/document/site/#/rt-thread-version/rt-thread-standard/programming-manual/libc/iso-ansi-c?id=_13-%e5%85%b6%e4%bb%96%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%a0%bc%e5%bc%8f%e5%8c%96%e8%be%93%e5%87%ba%e5%87%bd%e6%95%b0-%e4%be%8b%e5%a6%82-snprintf-%e7%ad%89) <a href="#id-13-qi-ta-zi-fu-chuan-ge-shi-hua-shu-chu-han-shu-li-ru-snprintf-deng" id="id-13-qi-ta-zi-fu-chuan-ge-shi-hua-shu-chu-han-shu-li-ru-snprintf-deng"></a>

Other string formatting output functions (such as snprintf, etc.) can be used directly.

It is strongly recommended that users use rt\_snprintf and other RT-Thread functions instead of snprintf to reduce resource consumption. Especially under the GCC compilation chain, the snprintf function provided by Newlib (the default C library inside the GCC tool chain) is not thread-safe, and may cause a crash when multiple threads output floating-point numbers without protection (it is abnormal for the snprintf function to be non-thread-safe).

Similarly, native rt\_snprintf and other functions do not support floating-point output. Users can install [the rt\_vsnprintf\_full package](https://github.com/mysterywolf/rt_vsnprintf_full) to support floating-point output.

### [2 Memory related functions](https://www.rt-thread.org/document/site/#/rt-thread-version/rt-thread-standard/programming-manual/libc/iso-ansi-c?id=_2-%e5%86%85%e5%ad%98%e7%9b%b8%e5%85%b3%e5%87%bd%e6%95%b0) <a href="#id-2-nei-cun-xiang-guan-han-shu" id="id-2-nei-cun-xiang-guan-han-shu"></a>

The malloc, realloc, calloc, and free memory allocation functions can be used directly. They have been taken over by the rt\_malloc, rt\_calloc, rt\_realloc, and rt\_free functions. There is no difference between the two.

memcpy, memset and other memory operation related functions. It should be noted that it is recommended to use rt\_memcpy and rt\_memset instead of memcpy and memset. Although the rt\_memxxx function is slower than the memory operation function of the C library, it is safer; unless you are very concerned about the copy speed, it is recommended to use the rt\_memxxx function. If you are using a Cortex-M MCU, you can install the [rt\_memcpy\_cm](https://github.com/mysterywolf/rt_memcpy_cm) software package to accelerate the assembly of the rt\_memcpy function. This software package is safe and efficient.

### [3 String related functions](https://www.rt-thread.org/document/site/#/rt-thread-version/rt-thread-standard/programming-manual/libc/iso-ansi-c?id=_3-%e5%ad%97%e7%ac%a6%e4%b8%b2%e7%9b%b8%e5%85%b3%e5%87%bd%e6%95%b0) <a href="#id-3-zi-fu-chuan-xiang-guan-han-shu" id="id-3-zi-fu-chuan-xiang-guan-han-shu"></a>

String-related functions such as strlen are built-in functions of the compiler C library and can be used directly.

### [4 System functions](https://www.rt-thread.org/document/site/#/rt-thread-version/rt-thread-standard/programming-manual/libc/iso-ansi-c?id=_4-%e7%b3%bb%e7%bb%9f%e5%87%bd%e6%95%b0) <a href="#id-4-xi-tong-han-shu" id="id-4-xi-tong-han-shu"></a>

exit and abort functions, calling them is equivalent to deleting the current thread.

The system function can be used to execute some FinSH commands built into a C program.

### [5 Time function](https://www.rt-thread.org/document/site/#/rt-thread-version/rt-thread-standard/programming-manual/libc/iso-ansi-c?id=_5-%e6%97%b6%e9%97%b4%e5%87%bd%e6%95%b0) <a href="#id-5-shi-jian-han-shu" id="id-5-shi-jian-han-shu"></a>

All time functions specified by ANSI/ISO-C can be used, such as time, ctime, etc. Note: Some functions involving obtaining the current time require the RTC device to be enabled to work properly. If these functions are called without the RTC device enabled, a warning will be given in the serial terminal.

Supports time zone function. `RT_LIBC_DEFAULT_TIMEZONE`The default time zone can be set through macro definition. The default time zone is UTC+8 Beijing time. During operation, you can also use tz\_set(), tz\_get(), tz\_is\_dst() functions to dynamically set or obtain time zone related information (need to include \<sys/time.h> header file).

***Notice:***

1. Time zones are for people to see, not for machines to see. Therefore, the underlying driver should not use time with time zones, but should use Greenwich Mean Time, i.e., UTC+0. When and only when the time needs to be displayed to people, it is necessary to temporarily use a function with a time zone conversion function (such as ctime) to display the time. Otherwise, it is easy to cause time confusion.

   Note the difference between the following two groups of functions, which have the same functionality, the only difference being whether or not they take time zone issues into account:

   |         | Consider time zones | No time zone consideration |
   | ------- | ------------------- | -------------------------- |
   | Group 1 | localtime           | gmtime                     |
   | Group 2 | mktime              | timegm                     |
2. Currently, time-related functions do not support automatic conversion to daylight saving time. Daylight saving time is not used in China, but mainly in Europe and the United States.

### [6 kservice.c](https://www.rt-thread.org/document/site/#/rt-thread-version/rt-thread-standard/programming-manual/libc/iso-ansi-c?id=_6-kservicec) <a href="#id-6-kservicec" id="id-6-kservicec"></a>

There is a file called kservice.c in the RT-Thread kernel. This file provides C library functions implemented by RT-Thread itself, such as rt\_strlen, rt\_strcpy, etc. The purpose of these functions is to make the RT-Thread kernel self-sustaining, that is, it can still run normally without relying on any C library. In addition, as mentioned above, some compiler built-in C library functions have some defects. The functions provided in the kservice.c file are safer and controllable. If you think that the functions provided in the kservice.c file are duplicated with the compiler built-in C library functions, you can remap most of the functions in kservice.c to the compiler built-in C library functions through configuration. For example: after turning on the option shown in the figure below, calling the rt\_strlen function is actually equivalent to directly calling the strlen function of the compiler built-in C library.

![kservice](https://www.rt-thread.org/document/site/rt-thread-version/rt-thread-standard/programming-manual/libc/figures/kservice.png)

### [7 Standard library C header files](https://www.rt-thread.org/document/site/#/rt-thread-version/rt-thread-standard/programming-manual/libc/iso-ansi-c?id=_7-%e6%a0%87%e5%87%86%e5%ba%93c%e5%a4%b4%e6%96%87%e4%bb%b6) <a href="#id-7-biao-zhun-kuctou-wen-jian" id="id-7-biao-zhun-kuctou-wen-jian"></a>

#### [7.1 Compatibility of different compilation platforms](https://www.rt-thread.org/document/site/#/rt-thread-version/rt-thread-standard/programming-manual/libc/iso-ansi-c?id=_71-%e4%b8%8d%e5%90%8c%e7%bc%96%e8%af%91%e5%b9%b3%e5%8f%b0%e7%9a%84%e5%85%bc%e5%ae%b9) <a href="#id-71-bu-tong-bian-yi-ping-tai-de-jian-rong" id="id-71-bu-tong-bian-yi-ping-tai-de-jian-rong"></a>

To ensure compatibility across different compilers and tool chains, it is recommended that users apply the following code:

* Use `sys/time.h`instead`time.h`
* Use `sys/errno.h`instead`errno.h`
* Use `sys/signal.h`instead`signal.h`

#### [7.2 POSIX extension function declarations in standard library C header files](https://www.rt-thread.org/document/site/#/rt-thread-version/rt-thread-standard/programming-manual/libc/iso-ansi-c?id=_72-%e6%a0%87%e5%87%86%e5%ba%93c%e5%a4%b4%e6%96%87%e4%bb%b6%e7%9a%84posix%e6%89%a9%e5%b1%95%e5%87%bd%e6%95%b0%e5%a3%b0%e6%98%8e) <a href="#id-72-biao-zhun-kuctou-wen-jian-de-posix-kuo-zhan-han-shu-sheng-ming" id="id-72-biao-zhun-kuctou-wen-jian-de-posix-kuo-zhan-han-shu-sheng-ming"></a>

POSIX extends some function declarations based on the standard library C header file. For example, `stdio.h`there is no `getline`function in the standard C header file , which appears in `stdio.h`the file in the POSIX.1-2008 standard. Therefore, if you use POSIX extended C library functions similar to the above (such as `getline`), you need to reference `posix/stdio.h`the header file instead of `stdio.h`the header file. The same applies to other C standard library header files, such as `ctype.h`, `stdlib.h`etc.

This is because the standard header files in the built-in C libraries of Keil, IAR, and WIN32 compilers only support the ISO-ANSI C standard and do not support POSIX extended function declarations. Therefore, the compiler balancing layer of RT-Thread uses this method to supplement the above three compilers, so that the upper-level code can run easily across compiler platforms.

<br>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.aic-eec.com/recommended_by_aic/rt-thread-university-program/components/iso-ansi-c-standard.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
