A study on the C language of single-chip microcomputer--Why variables should be assigned initial value

Source: Internet
Author: User

There are many books which say that variables are best to assign initial values. But why should the initial value be? What kind of accident might happen if we don't assign an initial value? The following is my analysis of the implementation of the 51 single-chip microcomputer for the compiler to see the Mcu,keil.
It is well known that variables are stored in RAM, lost after power-down, and are all 0 by default after power-up. Then the value of the variable with no initial values is 0, which should be taken for granted.
However, when the microcontroller reset (including the hardware reset is the reset button, watchdog reset, and other software program reset), the microcontroller just re-jump back to the main function to start execution, and did not empty ram! Therefore, variables that are simply defined without assigning an initial value (especially global variables) will still use the values left before the reset!
For example, when I debug serial port to parse the serial port of GPS information, when I downloaded the program containing the decoder program, the information stored in a character array. When I commented out the extent of the decoding, I downloaded it again using the power-off download, and found that there was still information displayed. Pressing the reset button several times is useless. Finally try to power off again, the residual information is no longer present.
PS: I use the PL2303 download line when the discovery, disconnect VCC power supply, its serial interface still has a certain level, you can also see the Power LED light light. This level is not enough to maintain the microcontroller work, but can keep the data in RAM is not lost! So the power outage mentioned above is to unplug the download line from the computer USB instead of the self-locking switch on the development Board.
In summary, if a variable does not have an initialization assignment before the reference, its data will refer to the pre-reset data after the reset, which can have unintended consequences! Therefore, it is best to assign an initial value when defining a variable (that is, 0). The pointer variable is more so, or you may not be able to tell where the pointer is pointing!

A study on the C language of single-chip microcomputer--Why variables should be assigned initial value

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.