Embedded C Language: The Intricacies of Variable Initialization

I am Lao Wen, an embedded engineer who loves learning.Follow me to become even better together!

When writing code, we assign an initial value to variables to preventcompiler issues that may lead to uncertain initial values for variables.

For numeric type variables, they are often initialized to 0, but how should other types of variables, such ascharacter and pointer types, be initialized?

Numeric Variable Initialization

Integer and floating-point variables can be initialized at the time of definition, generally initialized to<span><span>0</span></span>.

int    inum  = 0;
float  fnum = 0.00f;
double dnum = 0.00;

Character Variable Initialization

Character variables can also be initialized at the time of definition, generally initialized to<span><span>'\0'</span></span>.

char ch = '\0'; 

String Initialization

There are many methods for string initialization; I will briefly introduce three. Since a string is essentially an array of characters, the ultimate goal of its initialization is to initialize each character in the character array to<span><span>'\0'</span></span>.

Method One: Use an empty string<span><span>""</span></span>.

char str[10]="";

Method Two: Use<span><span>memset</span></span>.

char str[10];
memset(str,0,sizeof(str));

Method Three: Write a loop.

char str[10];
for(int i =0; i <10; i++)
{
    str[i]='\0';
}

The second method of initialization is recommended, which is to use<span><span>memset</span></span><span><span> for initialization.</span></span><blockquote><span><span>Many people have a partial understanding of</span></span><code><span><span>memset</span></span>; they know it can initialize many data types but do not understand its principles. Here is a brief explanation:

int num;
memset(&num,0,sizeof(int));
printf("step1=%d\n", num);
memset(&num,1,sizeof(int));
printf("step2=%d\n", num);

Before discussing, let’s look at the output results.

chenyc@DESKTOP-IU8FEL6:~/src$ gcc -o memset memset.c -g
chenyc@DESKTOP-IU8FEL6:~/src$ ./memset
step1 = 0
step2 = 16843009
chenyc@DESKTOP-IU8FEL6:~/src$

Seeing this output, is it different from what you expected?<span><span>step1 = 0</span></span> is understandable, but <span><span>step2 = 16843009</span></span> is confusing for many. According to common sense, shouldn’t it be <span><span>= 1</span></span>? This is what I want to explain: memset fills by bytes. We know that <span><span>int</span></span> is 4 bytes (each byte has 8 bits), so in binary it should be:

00000000 00000000 00000000 00000000

According to the byte filling principle, the result of step1 is that all 4 bytes are filled with 0, so the result remains 0:

00000000 00000000 00000000 00000000

In contrast, step2 fills each byte with 1 (note that it is each byte, not each bit), so the corresponding result should be:

00000001 00000001 00000001 00000001

You can convert the above binary number to decimal and see if it equals<span><span>16843009</span></span>. So strictly speaking, the memset function does not have initialization functionality; it is merely a byte-filling function that has been extended to serve as an initializer in practice.

There is a small trick in string initialization. We know that a string is essentially a character array, so it has two characteristics:

  • The string is contiguous in memory,
  • The string ends with<span><span>'\0'</span></span>. Therefore, when initializing, we always prefer to allocate memory that is one character longer than the string itself.
char year[4+1];
memset(year,0,sizeof(year));
strcpy(year,"2018");

Pointer Initialization

Generally, pointers are initialized to<span><span>NULL</span></span>.

int*pnum =NULL;
int num =0;
pnum =&num;

Pointers can be both loved and hated. Generally, integers, strings, etc., can be used directly after initialization, but if a pointer is initialized to<span><span>NULL</span></span><code><span><span>, and memory is not reallocated for that pointer, it can lead to unpredictable errors (the most common being segmentation faults caused by dereferencing a null pointer).</span></span><span><span> In dynamic memory management, since the variable's memory is allocated on the heap, functions like</span></span><code><span><span>malloc</span></span><span><span>, </span></span><code><span><span>calloc</span></span><span><span>, etc., are used to request dynamic memory. After use, it is necessary to release it promptly, and it is also essential to set the pointer to NULL after freeing dynamic memory, which is often overlooked.</span></span><pre><code class="language-c">char*p =NULL;
p=(char*)malloc(100);
if(NULL== p)
{
printf("Memory Allocated at: %x\n",p);
}
else
{
printf("Not Enough Memory!\n");
}
free(p);
p =NULL;// This line is essential to set the pointer to NULL; otherwise, subsequent operations on this wild pointer may lead to serious issues.
A common mistake many people make is that when a pointer is passed as an argument, it degenerates into an array, so many think to use<span><span>memset</span></span><span><span> to initialize that pointer:</span></span><pre><code class="language-c">void fun(char*pstr)
{
memset(pstr,0,sizeof(pstr));
...
}
This usage is incorrect. Regardless of whether pointers can be initialized with<span><span>memset</span></span><span>, the pointer first holds a 4-byte address, so</span><code><span><span>sizeof(pstr)</span></span><span><span> will always equal </span></span><code><span><span>4</span></span><span><span>, making this initialization meaningless.</span></span><h1><span><span>Structure Initialization</span></span></h1><span><span>Structure initialization is relatively simple; it is generally done using</span></span><code><span><span>memset</span></span><span><span>.</span></span><pre><code class="language-c">typedef struct student
{
int id;
char name[20];
char sex;
}STU;
STU stu1;
memset((char*)&stu1,0,sizeof(stu1));
Regarding the length issue of initializing structures, that is, the third parameter of<span><span>memset</span></span><span><span>, generally, passing the data type and variable name has the same effect. In the example above, the following writing is equivalent:</span></span><pre><code class="language-c">memset((char*)&stu1,0,sizeof(STU));
However, for initializing arrays of structures, the length needs to be noted, as illustrated in the example above:

STU stus[10];
memset((char*)&stus,0,sizeof(stus));// Correct, the array itself is contiguous in memory, and sizeof returns the byte length of the array.
memset((char*)&stus,0,sizeof(STU));// Incorrect, this only initializes the first STU structure; the remaining 9 STU elements are not initialized.
memset((char*)&stus,0,sizeof(STU)*10);// Correct, the effect is the same as the first.

Some people habitually write the second parameter of<span><span>memset</span></span><span><span> as follows:</span></span><pre><code class="language-c">memset((char*)&stu1,0x00,sizeof(stu1));
As long as you understand that<span><span>memset</span></span><span><span> fills by bytes, you will know that this writing is also correct and has no issues.</span></span><p><em><span><span>Source: Internet, copyright belongs to the original author. If there is any infringement, please contact for deletion.</span></span></em></p><p><strong><strong><span><span>-END-</span></span></strong></strong></p><strong><strong><span><span>Previous Recommendations: Click the image to jump to read</span></span></strong></strong><span><span><span><img alt="Embedded C Language: The Intricacies of Variable Initialization" src="https://boardor.com/wp-content/uploads/2025/11/1f596fb7-cd42-4879-a828-2da0e6fb9598.jpeg"/></span></span></span><p><span>For embedded programmers, how difficult and complex is it to switch to hardware?</span></p><span><span><span><img alt="Embedded C Language: The Intricacies of Variable Initialization" src="https://boardor.com/wp-content/uploads/2025/11/d3936cda-7ea0-4018-9f2e-0c11b0653ecf.jpeg"/></span></span></span><p><span>In embedded C programming, how to properly handle fatal or non-fatal errors during program execution?</span></p><span><span><span><img alt="Embedded C Language: The Intricacies of Variable Initialization" src="https://boardor.com/wp-content/uploads/2025/11/174811c9-8704-4d8c-addf-d00553a3a5b0.jpeg"/></span></span></span><p><span>What are the key points of restructuring embedded software?</span></p><span><span>I am Lao Wen, an embedded engineer who loves learning.</span></span><strong><span><span>Follow me</span></span></strong><span><span> to become even better together!</span></span>

Leave a Comment