Use printf () with caution in Keil ()

Source: Internet
Author: User

The commonly used debugging methods in single-chip microcomputer debugging include printf and simulation. printf is flexible but not easy to use.ProgramWill die badly;

A simple example:

Main Program Main. c

The key initialization is called in Main. C, and the actual operations are commented out, leaving an empty shell,

 

It is set as software simulation in the project. TheoreticallyCodeThere is no problem, and there is actually no problem

 

The compilation result is shown in the first figure. There is no warning and no error. During simulation, the program should run an endless loop:

 

Of course, the above is a normal situation, but the actual simulation effect is:

Point programs cannot run, let alone a single step. It's no wonder:

. S Startup File-> main. C-> call keyinit ()-> return-> endless loop

 

This logic does not have any problems. Why cannot it be run?

 

Analysis:

Key. c

Key. h

 

 

 

From the above, stdio is referenced. h printf, while printf () needs to call putchar (). This module needs to be implemented externally, but it is not implemented in the program and there is no alarm during compilation, resulting in an implicit call.

Therefore, the program cannot run. We recommend that you implement it yourself when using printf. If problems do not occur, set the debugging switch before.

Solution:

1. Implement putchar ();

2. Implement printf ();

3. Do not use printf ();

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.