Don’t Overlook the C Language Standards in Embedded Programming!

Recently, during a code review, I noticed that many colleagues’ coding practices were teetering on the edge of risk, with one of the most glaring issues being the arbitrary placement of local variable definitions.

For seasoned C programmers, most have developed the habit of “defining variables at the beginning of a statement block”. I vaguely remember my teachers emphasizing in class that local variables in C should always be placed at the start of a function.

void Function(void)
{
    int Var1 = 0;
    int Var2 = 0;
    //do something......
}

When I first started learning C, I felt there was nothing much to explain; I just followed the “rules” from the books and my teachers. Later, when I learned C++, I found that C++ is relatively flexible in this regard. You can define variables anytime as long as you pay attention to their scope, without needing to adhere to the rule of placing variable definitions at the start of a function.

void Function(void)
{
    int Var1 = 0;
    //do something......
    int Var2 = 0;
    //do something......
}

It wasn’t until a long time ago that I saw someone else’s C program defining variables as flexibly as in C++ that I realized there are different standards for C, and that’s when I began to understand the C language standards.

As the saying goes, “Without rules, there is no square circle”; different compilers introduce relevant “rules” based on different standards to constrain and guide programmers in their design.

As C is the primary programming language in the embedded field, it should continuously be corrected and optimized, which requires dedicated individuals to handle these tasks — the C Language Standardization Committee. Currently, the main standards referenced by compilers are:

C89 (C90) Approved in 1989, released in 1990 (ISO/IEC 9899: 1990)
C99 Released in 1999 (ISO/IEC 9899: 1999)
C11 Released in 2011 (ISO/IEC 9899: 2011)

Of course, I understand that the current standards have been updated to C17, but which compilers support it is another story.

Compilers, to meet the needs of different developers and ensure compatibility with old and new code projects, will expose options related to the C language standards in their compilation options. Developers can choose according to their needs when compiling their source code. For example:

In the IAR compiler:

Don't Overlook the C Language Standards in Embedded Programming!

In the GCC compiler:GCC provides some extended options for compiling C programs, using -std to set them. For example, when compiling, you can choose -std=gnu99, which corresponds to some extensions of C99 by GNU.Especially when it comes to code portability, it’s essential to be aware of the C standard that the current tools adhere to; otherwise, a plethora of compilation errors can be quite troublesome.As I mentioned earlier, although the C language standards are continuously updated, compilers do not necessarily support them synchronously. Most embedded compilers still primarily support up to the C99 standard, and even then, they may not fully support it.I believe the main reason is that implementing the relevant standards can be quite cumbersome, and their usage frequency in related fields is too low, so they haven’t received comprehensive support. However, some compilers do add their own special extensions for a better customer experience.Here, I will briefly list some commonly added standard items in C99 compared to C89:1. Preprocessing, support for line comments “//”;2. The inline keyword, support for inline functions;3. Variable declarations do not have to be at the beginning of a statement block, which is also mentioned earlier. A common way to use this feature in a for loop is: for(int i=0;i<100;i++);4. Enhanced arrays, support for variable-length arrays;5. Introduction of the __func__ predefined identifier;6. No longer supports implicit returns; there must be a return value, such as return;7. Modifications to integer promotion, any integer type lower than int or unsigned int can be replaced with int or unsigned int.There are many more, but I can’t list them all here. Interested friends can refer to the C99 standard documentation.

Finally

That’s all for today. If you found this helpful, please remember to give a like~

Don't Overlook the C Language Standards in Embedded Programming!

END

Author: bug菌

Source: The Last Bug

Copyright belongs to the original author. If there is any infringement, please contact for deletion.Recommended ReadingWho is the king language that conquers everything?To learn digital electronics, the junior sister actually made a 555 chip herselfLet the newcomer modify a piece of C code, and almost died of anger after seeing the result!→ Follow for more updates ←

Leave a Comment