C-Block Coding Basics

Based on many forum posts, the C-Block code stuff is confusing. If you’re a beginning C/C++ coder, there’s lots of difficult stuff in the templates. Even if you’re a great coder, there’s the mysterious Trunc() function.

Here’s what I understand (or think that I understand) on those topics. This is an early release on the dev branch of my GitHub page. See the CBlock_Doc folder.

I’m sticking my neck out here, pretending that I know stuff. Do correct me and we’ll eventually have a reliable document.



Hi Rob,

I have been reading your Cblock doc, which is fantastic.

However, do you have any comment on:

  1. the purpose of data stored in the struct template?
    In my guess, it seems to be used in conjuction with bzero to ensure that all those variable in the struct are cleared when the code is called for the first time.
  2. How does Trunc work? any clue on what these comments are supposed to mean?
    // limit the timestep to a tolerance if the circuit causes a change in struct sSTUDY2_X1
    // if(tmp != *inst) // implement a meaningful way to detect if the state has changed
    // *timestep = ttol;


Hi, Arief.

I’m not sure that I can briefly explain any better than in the “Per-Instance Data” and “Trunc()” sections of the lengthy PDF. An explanation of why and example of using the struct is in the first section. The Trunc() stuff is highly dependent on what you are trying to do.


Hi Robert,

Thanks for your answer.

  1. I still cant understand what you meant with Per-Instance Data. In one SCH file, I made two C-block and I dont see any interaction between the two C-block.

So, is it just for a good coding practice?

study3_x1.cpp (1.1 KB)
study3_x2.cpp (1.1 KB)

Sorry, I tried to just upload the .qsch but cant, so please refer to the picture above

  1. for Trunc(). still no clue…

If you are interested, we can connect via chat for better interaction, you may find my profile in LinkedIn (Arief Noor Rahman).


Hi, Arief.

Ok, your example has a single instance each of two components (the DLLs). Each DLL has it’s own copy of the sum and previous clock variables. No problem.

The problem is when you have multiple instances (schematic hierarchical blocks) that call a single component (DLL). For example, if X1 & X2 both call Study3_X1.dll you’d see the problem. Since the variables are in the DLL, both instances would be incrementing the same sum variable.

Did that clear it up?


Hi Robert,

Ah…its crystal clear now…

Thanks for your patience

No worries. I see it as valuable feedback for future document revisions. Thanks.


I supposed for most control or signal processing application, Trunc and data struct are completely unnecessary.

The only reasonable case for using Trunc is when we need to have multiple C-block accessing one .dll file. Something like multiple PWM comparator or multiple ADC (though I still think to use only one block with multiple channels will be easier).

Forcing the user to access the variables (as in the case of using Trunc) will make the code a bit longer and less readable.

Do you happened to have good suggestion on when to use this Trunc()?