{"id":4197,"date":"2021-10-22T16:28:35","date_gmt":"2021-10-22T14:28:35","guid":{"rendered":"https:\/\/msalamon.pl\/?p=4197"},"modified":"2025-12-27T15:59:29","modified_gmt":"2025-12-27T14:59:29","slug":"how-is-c-for-microcontrollers-different-from-c-for-a-pc","status":"publish","type":"post","link":"https:\/\/msalamon.pl\/en\/how-is-c-for-microcontrollers-different-from-c-for-a-pc\/","title":{"rendered":"How is C for microcontrollers different from C for a PC?"},"content":{"rendered":"\n<p>You can often come across big banners, slogans, or even course and book titles: \u201cC for AVR\u201d, \u201cC course for STM32\u201d. This can give the impression that the C language is different for each microcontroller. Even more so for a computer, right?<\/p>\n\n\n\n<p>So let\u2019s think about how C for microcontrollers differs from C for PCs.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>Division into PC and MCU \u2013 does it make sense?<\/strong><\/h1>\n\n\n\n<p>First, it would be worth considering whether such a division even exists at all.<\/p>\n\n\n\n<p>In my opinion, this division isn\u2019t entirely appropriate. <strong>C is C.<\/strong> Period. It contains several dozen keywords describing various statements, data types, specifiers, etc. <strong>We use exactly the same ones when writing programs for a PC and the same ones when writing programs for microcontrollers.<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>So where is the difference?<\/strong><\/h2>\n\n\n\n<p>The differences we should be talking about are <strong>at the level of how we use the language.<\/strong> We\u2019ll write programs a bit differently for an MCU, and a bit differently for a PC. We have the same statements available, but we\u2019ll use some more often and others less often. That\u2019s what this is about.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Bitwise operations are everyday life<\/strong><\/h2>\n\n\n\n<p><strong>Bitwise operations are the norm<\/strong> when you write code for a microcontroller. Seriously.<\/p>\n\n\n\n<p>At university, when learning C programming, we studied on PCs. I don\u2019t remember needing bitwise operations while writing various sorting algorithms, tree searches, Dijkstra, etc. Everything was done on \u201cwhole\u201d bytes.<\/p>\n\n\n\n<p>It\u2019s different on a microcontroller. <strong>When accessing registers<\/strong>, if you want to set a parameter, then <strong>bit-level access is mandatory.<\/strong> Why? Because within one register, each bit can correspond to a different thing. In integrated circuits, silicon is saved, so everything is tightly packed.<\/p>\n\n\n\n<p>So if <strong>you want to change something in such tightly packed data, you have to perform several bitwise operations.<\/strong> For example, <strong>mask<\/strong> other values you\u2019re not interested in so you don\u2019t accidentally touch them.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Pointers, pointers everywhere<\/strong><\/h2>\n\n\n\n<p>In a microcontroller, pointers are everywhere. If you ever program directly on registers, then writing to a register is precisely writing to a specific address in the microcontroller\u2019s memory.<\/p>\n\n\n\n<p>Naturally, you have to treat such a register write as a write through a pointer, or more precisely, writing a value to the place the pointer points to.<\/p>\n\n\n\n<p>Going further: there is a statement that <strong>if you want to write efficient C code, you must avoid copying data from place to place.<\/strong> This is exactly what pointers help with. <strong>You pass the address of the place<\/strong> where the data you care about is located and\u2026 <strong>you work on the original<\/strong> without the need to copy.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Standard library<\/strong><\/h2>\n\n\n\n<p>Not everyone realizes this, but <strong>the C standard library for PCs differs from the one for microcontrollers.<\/strong> Not at the level of whether a function exists or not. Although I once was surprised that I was missing the <em>itoa()<\/em> function in avr-gcc\u2026<\/p>\n\n\n\n<p>The point is that <strong>the implementation of standard libraries is different.<\/strong> After all, you have to use different CPU instructions to achieve the same effect. Some topics need to be approached differently.<\/p>\n\n\n\n<p>Unfortunately, implementations for microcontrollers are not great. By including such a <em>printf<\/em>, you also include a bunch of other libraries. That starts to weigh a lot. The code simply bloats and takes up a lot of space in the microcontroller\u2019s memory.<\/p>\n\n\n\n<p>Another example is <strong>dynamic memory allocation<\/strong> using, for example, the malloc function. The standard, and especially the implementation, is not clearly defined. <strong>We cannot rely on it because it is unpredictable.<\/strong> On a microcontroller, we can\u2019t afford such a large uncertainty. Especially if we are building systems related to human safety.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Where to learn C for microcontrollers?<\/h2>\n\n\n\n<p>I created a course dedicated to microcontrollers. <strong>In it, I teach C from the ground up<\/strong> (the course content is conducted in Polish).<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/cdlamikrokontrolerow.pl\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"500\" height=\"99\" src=\"https:\/\/msalamon.pl\/wp-content\/uploads\/2021\/10\/Kurs-C-dla-embedded-logo-500px.jpg\" alt=\"\" class=\"wp-image-1967\" srcset=\"https:\/\/msalamon.pl\/wp-content\/uploads\/2021\/10\/Kurs-C-dla-embedded-logo-500px.jpg 500w, https:\/\/msalamon.pl\/wp-content\/uploads\/2021\/10\/Kurs-C-dla-embedded-logo-500px-300x59.jpg 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/a><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p><strong>I gathered my experience from several years of embedded programming<\/strong> and I want to pass on the best possible knowledge to you. I participated in various projects: on my own, a start-up, a medium-sized company, and a huge corporation.<\/p>\n\n\n\n<p>In addition to the basics and syntax, <strong>I share a ton of good practices.<\/strong> I weave this in between explaining subsequent aspects of the C language.<\/p>\n\n\n\n<p>An additional advantage is also that I show <strong>how to run a project well.<\/strong> I\u2019ll show you how to deal with building abstraction layers. We\u2019ll use <strong>structures, pointers, and callbacks<\/strong> for that. And of course, splitting into files. That helps a lot.<\/p>\n\n\n\n<p>Such separated layers are much easier to \u043f\u0435\u0440\u0435\u043d\u043e\u0441 between projects, and even between different microcontroller families.<\/p>\n\n\n\n<p><strong>How to join the course? See the full offer at <a href=\"https:\/\/cdlamikrokontrolerow.pl\" target=\"_blank\" rel=\"noopener\">https:\/\/cdlamikrokontrolerow.pl<\/a><\/strong> (the course content is conducted in Polish).<\/p>\n\n\n\n<p>Don\u2019t wait, because the offer is valid until Friday, October 29, 2021, until 20:00.<\/p>\n\n\n\n<p><strong>I hope we\u2019ll see each other in the course!<\/strong><\/p>\n\n\n\n<p>P.S. Let me know in the comments whether this lighter topic on the blog is interesting!<\/p>\n\n\n<div class=\"kk-star-ratings kksr-auto kksr-align-left kksr-valign-bottom\"\n    data-payload='{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;4197&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;bottom&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;0&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;0&quot;,&quot;starsonly&quot;:&quot;&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;0&quot;,&quot;greet&quot;:&quot;&quot;,&quot;legend&quot;:&quot;0\\\/5 - (0 votes)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;title&quot;:&quot;How is C for microcontrollers different from C for a PC?&quot;,&quot;width&quot;:&quot;0&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}'>\n            \n<div class=\"kksr-stars\">\n    \n<div class=\"kksr-stars-inactive\">\n            <div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 0px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 0px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 0px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 0px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 0px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n    <\/div>\n    \n<div class=\"kksr-stars-active\" style=\"width: 0px;\">\n            <div class=\"kksr-star\" style=\"padding-right: 0px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 0px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 0px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 0px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 0px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n    <\/div>\n<\/div>\n                \n\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\">\n            <span class=\"kksr-muted\"><\/span>\n    <\/div>\n    <\/div>\n","protected":false},"excerpt":{"rendered":"<p>You can often come across big banners, slogans, or even course and book titles: \u201cC for AVR\u201d, \u201cC course for STM32\u201d. This can give the impression that the C language is different for each microcontroller. Even more so for a computer, right? So let\u2019s think about how C for microcontrollers [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3654,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[160],"tags":[176,174],"class_list":["post-4197","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-stm32","tag-programming","tag-stm32"],"_links":{"self":[{"href":"https:\/\/msalamon.pl\/en\/wp-json\/wp\/v2\/posts\/4197","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/msalamon.pl\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/msalamon.pl\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/msalamon.pl\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/msalamon.pl\/en\/wp-json\/wp\/v2\/comments?post=4197"}],"version-history":[{"count":3,"href":"https:\/\/msalamon.pl\/en\/wp-json\/wp\/v2\/posts\/4197\/revisions"}],"predecessor-version":[{"id":4200,"href":"https:\/\/msalamon.pl\/en\/wp-json\/wp\/v2\/posts\/4197\/revisions\/4200"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/msalamon.pl\/en\/wp-json\/wp\/v2\/media\/3654"}],"wp:attachment":[{"href":"https:\/\/msalamon.pl\/en\/wp-json\/wp\/v2\/media?parent=4197"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/msalamon.pl\/en\/wp-json\/wp\/v2\/categories?post=4197"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/msalamon.pl\/en\/wp-json\/wp\/v2\/tags?post=4197"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}