Skip to content

Embedded C coding style

Shiva Karthick edited this page Oct 21, 2023 · 5 revisions

Guiding questions

  • What is C coding style and do we really need one?
  • When there are many coding styles, which do we follow?
  • What are some of the things which we need to take note, when working with embedded systems?

C-Coding styles

Embedded C-Concepts

  1. Macros & the preprocessor, pointers & memory management, concurrency, bit manipulation, when to use volatile and const, signed vs unsigned types, a bit of inline assembly
  2. bitwise operations, pointers, memory management, structs, unions (unions and structs make dealing with saving to flash/eeprom a lot easier), make sure you know the size of the types for the particular mcu/cpu, static, const, volatile, extern, linking, familiarize yourself with the manuals/datasheets for everything you're using, finite state machine or a RTOS depending on resource availability, and what you need

Bitwise operations and manipulations in C

  1. https://systems-encyclopedia.cs.illinois.edu/articles/bitwise-operations/
  2. https://www.youtube.com/watch?v=7Iru_LM3qY0

Dynamic memory allocation

  1. https://www.embedded-software-engineering.de/dynamic-memory-allocation-justifiably-taboo-a-632951/

Interview questions

  1. I keep a list of questions I've been asked during interviews. Most of these were from one coding review right out of school. https://www.reddit.com/r/embedded/comments/ubm8ut/comment/i658o31/?utm_source=share&utm_medium=web2x&context=3

    • What happens to a bit that is 'shifted out'? *0b00000001 >> 1 - where does the 1 go?
    • When are parens required in shifting/casting? can you make an example of an operation that doesn't do what you want? (with casting, shifting, math, etc)
    • what happens when you dereference a NULL pointer?
    • What do you like about c++ (they asked right after saying they only use C...)
    • Read these variable declarations (complex declarations)
    • when should you use 'const' in function parameters?
    • What is the difference if you have the const before or after the _ in : void foo(int _ const bar)" vs "void foo(const int * bar)
    • what will happen: if (x + 5 > y / 4)
    • whats the difference between an array and a pointer
    • what are function callbacks, their uses, and their dangers
    • how to optimize a running average function?
    • how much work should you do in interrupts, what happens when you call a function that generates another interrupt within an interrupt?
    • Read this linker file and explain it
    • edit: u/Dark_Tranquility had some good thoughts I totally missed:
    • pointers, pointer arithmetic
    • uint8_t * foo = 0xab; foo++; foo is what?
    • uint8_t _ foo = 0xab; (uint32_t_)foo++; what is foo?
    • What does volatile do? When should you use volatile?

Resources

  1. Quantum Leap, OOP in C uses structs and pointers magic to create OOP like structures
  2. Jacob Sorber

Miscellaneous stuff (Good readings)