Embedded C coding standard

Embedded C Coding Standard was developed to minimize bugs in firmware by fixating on practical rules that keep bugs out–while additionally ameliorating the maintainability and portability of embedded software. A C coding standard can avail keep bugs out of embedded software by leveraging prevalent language features and development tools.

Embedded C coding standard

K&R C

The C was developed by Dennis Ritchie between 1969 and 1973 at Bell Labs. It was widely accepted by almost all the professional programmers, and they started making their own additions to it. After a few years, there were a lot of C variants available for a programmer to choose from. The C Programming Language’, published in 1978 by Brian Kernighan and Dennis Ritchie; hence, the name K&R after Kernighan and Ritchie.

That time K&R C acted as an informal standards specification for C but also added language features like the new data types long int and unsigned int and the compound assignment operator.

The K&R C proposed a standardized I/O library. Since the most popular C standard followed in India was still ANSI C, the main differences between K&R C and ANSI C. There are 32 keywords in C.

According to K&R C, there are only 28 keywords in C. One keyword was called entry, which was neither implemented at that point in time by any of the compilers nor accepted into the list of keywords in ANSI C.

The other major difference is the definition of the function. A function definition in K&R C has the parameter types declared on separate lines. Consider the following lines of code showing a function definition in K&R C:

float fun( a, b )

int a;

float b;

{

float c;

c = a * b;

return c;

}

The function fun( ) accepts an integer and a floating-point variable, and returns the product. This is not the style for function definition in ANSI C.

The first line of the function definition in ANSI C will be

float fun( int a, float b ).

The gcc does not have an option to specify compilation in the K&R C standard. But programs written in K&R C will also be compiled without any errors because gcc compiler is backward compatible with K&R C.

ANSI C

The  K&R C was accepted by many programmers a standard of C, but no one was ready to accept it as the official standard of C.

The American National Standards Institute (ANSI) addressed this issue in 1983 by forming a committee, and the final draft of the standards it formulated was released in 1989. This is the reason why ANSI C is also called C89.

ANSI C is depends on the POSIX standard. POSIX is the Portable Operating System Interface, a family of standards specified by the IEEE Computer Society for maintaining compatibility between operating systems. The same standard proposed by ANSI was adopted by ISO officially as ISO/IEC 9899:1990 in 1990. This is the reason why ANSI C is also called as ISO C and C90. for one standard and  the four different names were given.

The five keywords const, enum, signed, void and volatile were added, and the keyword entry was dropped in ANSI C. The option -ansi from gcc compiler will compile programs in the ANSI C standard.

The options -std=c89 and -std=c90 will also compile programs in ANSI C. Since gcc is a backward compatible compiler, the above given options will result in successful compilation of programs with K&R C features. Almost all the popular C compilers support ANSI C features. These include compilers like gccfrom GCC, Portable C Compiler (PCC), Clang from LLVM, etc.

C99 Standard

C99 is the informal name given to the ISO/IEC 9899:1999 standards specification for C that was adopted in 1999. The C99 standard added five more keywords to ANSI C, and the total number of keywords became 37.

The keywords added in C99 are _Bool, _Complex, _Imaginary, inline and restrict.

  • The keyword _Bool is used to declare a new integer type capable of storing 0 and 1.
  • The keywords _Complex and _Imaginary are used to declare complex and imaginary floating point type variables to handle complex numbers.
  • The keyword inline is used to declare inline functions in C, similar to C++ inline functions.
  • The keyword restrict is used to tell the compiler that for the lifetime of the pointer, only the pointer itself or a value directly derived from it will be used to access the object to which it points.

New header files like <stdbool.h>, <complex.h>, <tgmath.h>, <inttypes.h>, etc, were also added in C99. A new integer data type called long long int with a minimum size of 8 bytes was added in C99.

In gcc compiler long long int usually takes 8 bytes. The C99 program named factorial.c given below to finds the factorial of 20. A program using the data type long int with the usual minimum size of 4 bytes will given the answer as -2102132736 due to overflow.

#include<stdio.h>

int main( )

{

long long int f=1;

int a=20,i=1;

for(i=1; i<=a; i++)

{

f = f * i;

}

printf(“the factorial of %d is %lld\n”,a, f);

return 0;

}

The program can be compiled by executing the command gcc -std=c99 fact.c or the command gcc fact.c on the terminal.The second command works due to ,by default, gcc compiler supports long long int.

The Features like variable length arrays, better support for IEEE floating point standard, macros with variable number of arguments, support for C++ style one line comments (//) many more, were  added in C99.

The official documentation of gcc: ‘ISO C99. This standard is substantially completely supported’. The legal term ‘substantially complete’ is slightly confusing but I believe it means that the gcc compiler supports almost all the features proposed by the standards documentation of C99.

C Language

The current and latest standard of the C programming language is C11 and, as the name indicates, this standard was adopted in 2011. The formal document describing the C11 standard is called ISO/IEC 9899:2011.

To C11, seven more keywords were added to the C programming language, thereby making the total number of keywords, 44. The seven keywords added to C99 are _Alignas, _Alignof, _Atomic, _Generic, _Noreturn, _Static_assert and _Thread_local. Consider the C11 program noreturn.c shown below, which uses the keyword _Noreturn.

#include<stdio.h>

_Noreturn fun( )

{

printf(“\nI TOO WILL BE PRINTED 🙂 \n\n”);

}

int main( )

{

printf(“\nI WILL BE PRINTED 🙂 \n”);

fun( );

printf(“\nI WILL NOT BE PRINTED 🙁 WHY? \n”);

}

There are a few warnings because the main( ) has one more line of code after the completion of the function fun( ). he last printf( ) in main( ) not is printed This is due to the difference between a function returning void and a function with the _Noreturn attribute. The keyword void only means that the function does not return any values to the called function, but when the called function terminates the program, the program counter register makes sure that the execution continues with the called function.

But in case of a function with the _Noreturn attribute, the whole program terminates after the completion of the called function. This is the reason why the statements after the function call of fun( ) didn’t get executed.

The function gets( ) caused a lot of mayhem and so was deprecated in C99, and completely removed in C11. Header files like <stdnoreturn.h> are also added in C11. Support for concurrent programming is another important feature added by the C11 standard.

Anonymous structures and unions are also supported in C11. But unlike C99, where the implementation of variable length arrays was mandatory, in C11, this is an optional feature. So even C11-compatible compilers may not have this feature.

The official documentation of gcc again says that ‘ISO C11, the 2011 revision of the ISO C standard. This standard is substantially completely supported’. the gcc compiler supports most of the features proposed by the C11 standard documentation. The option –std=c11 of gcc will compile programs in C11 standard.

Embedded C

The embedded C standard is different from all other c standards that is from K&R to C11, based on user requirements the programming languages are changed.

The standard known as Embedded C is slightly different from all the others. C from K&R C to C11 depicts the changes of a programming language over time, based on user requirements.

Embedded C is a standard that is being developed in parallel with c standards. A lot of non-standard features were used in C programs written for embedded systems. in 2008, the C Standards committee came up with a standardisation document called embedded C.

Embedded C  has the syntax and semantics of normal C with additional features like , named address spaces,fixed-point arithmetic, and basic I/O hardware addressing.

The keywords _Fract and _Accum are used in Embedded C.

New header files like <iohw.h> have also been proposed by the Embedded C standards committee. To support fixed-point arithmetic two groups of fixed-point data types called the fract types and the accum types were added to the C language. The main advantage of the Embedded C standard is that it is simpler and easier to learn than traditional C.