Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

File Names

  • File names should follow capital camel case

Variables

  • Variables should be named in lower snake case. ie. `this_is_a_variable`.
  • Absolutely no exposing of global variables. All components should implement accessor methods, if applicable.
  • Variables in c files should be declared as static if they are outside the scope of any function. This ensures that the variable scope is contained within that file, and tells the developer that the variable isnt used anywher else.

...

Info
titleStatic variables

Note that in most cases, you're NOT going to want to declare a variable as static if inside a function. You only want to declare static inside c files with global variables.

Functions

  • Camel case on all functions. Eg. helloWorld()

...

  • If structs are to be exposed in an interface (in a .h file) typedef it
  • Raw struct names (in the initial declaration) should begin with an underscorebe the same as the typedef name
  • Field variables that are not meant to be accessed by other modules should also begin with an underscore
  • Naming for structs should be capital camel case

...

struct helloworld{
  int do_not_modify;
} HelloWorld;

//good
typedef struct _HelloWorld{
     int int16_t  _do_not_modify;
     uint32_t can_access_directly;
} HelloWorld;

Types

For code portability and easier unit testing, please use the types defined in <stdint.h> and <stdbool.h>. The table below summarizes the different possible types and their conversion to the standard types. As you can see, it also saves on a lot of typing.

C Standard TypeLength in Bytes (XC16)Portable Type
char1int8_t
unsigned char1uint8_t
int2int16_t
unsigned int2uint16_t
long int4int32_t
long unsigned int4uint32_t
long long int8int64_t
unsigned long long int8uint64_t
float4--
double48--
long double8--
--1bool
Info
titleBooleans

Note that with the inclusion of <stdbool.h>, you get the true and false types available to use

Info
titleDoubles vs Long Doubles

On most systems, a long double guarantees 8 bytes. Even though with XC16 they are the same, it is highly recommended to use long doubles over doubles.