Site Search

Hello, I'm Duck.

 

Recently, I've been working hard on software training.

FPGAs are now equipped with CPUs, and the need for software has increased.

This time, we will solve the problems that occurred in the training of C language, which is often used for embedding.

initial value problem

Previously, Ume Onigiri-senpai's article mentioned that it is necessary to enter initial values in HDL.

In fact, even in C language, this initial value problem exists, and I have been annoyed many times.

In Ume Onigiri-senpai's article, the initial value problem was a problem during simulation.

However, in the case of the C language, it can become a big problem that affects the actual operation.

 

Now, in C language, what happens if the initial value is not set?

For example, consider a program that prints the numbers 0 through 9 to the console.

Let's display it using a loop statement.

Loop statements mainly include for statements and while statements.

I wrote the same program with for and while statements.

test1 code ( for )

test2 code ( while )

I thought I did it right and tried each one.

test1 output result

test2 output result

that?

Although the for statement is output properly, nothing is displayed in the while statement.

 

???

 

Where did I go wrong?

Cause investigation

Our program is very simple.

It seems that only the variable i is likely to be a problem in this.

For debugging, I added printf( ) as follows to see what the value of i is.

test3 code

test3 output result

I got some weird numbers.

that? I didn't remember entering such a number, but I noticed it when I thought.

You haven't assigned any value to i.

problem solving! ?

Therefore, we decided to assign 0 to i at the beginning.

test4 code

Let's run it.

test4 output result

Now the output is correct.

Apparently, the variable i had an undefined value because the initial value was not entered.

 

The condition of the while loop this time is i < 10, so if i is 10 or more at the beginning, the loop will not start in the first place.

Therefore, it seems that it ended without entering the while loop.

This doesn't show anything, does it? . .

It turns out that the input of the initial value is very important.

Summary

It is easy to forget to input the initial value in the for loop, which is difficult to forget in the while.

If you don't enter an initial value, you don't know what the variable will be.

I remembered that I had to be careful because it would cause bugs.

Duck C Language Related Articles

Easy-to-understand explanation of the basics of the C language, which is indispensable for embedded systems!

Initial value problem ver.C language
Forgetting initial values can cause trouble! ?

Actually the same person! ? arrays and pointers
Arrays and pointers, use these two that look similar but are different!

malloc ~challenge to the unknown~
Explains how to use memory, which can be said to be a feature of the C language!

Is it unreadable to humans? Binary file mystery
What is in the contents of that file that cannot be read as text! ?