/*******************************************
KEIL has expanded the b, h, and l to set the input byte width:
(1) b is 8 bits
(2) h is 16 bits (default)
(3) l is 32 bits
In Keil C51, when using printf to output a single-byte variable, you need to use %bd, such as
unsigned char counter;
printf("Current count: %bd\n", counter); // Output 8-bit "decimal signed integer"
printf("Current count: %bx\n", counter); // Output 8-bit "unsigned hexadecimal integer"
In standard C language, %d is used:
printf("Current count: %d\n", counter);
1. Format specifiers The
format specifiers provided by Turbo C2.0 are as follows:
━━━━━━━━━━━━━━━━━━━━━━━━━━━Symbol
Function────────────────────────────
%
d Decimal signed integer
%u Decimal unsigned integer
%f Floating-point number
%s Character string
%c Single character
%p Pointer value
%e Exponential floating-point number
%x, %X Unsigned hexadecimal integer
%0 Unsigned octal integer
%g Automatically select the appropriate representation━━━━━━━━━━━━━━━━━━━━━━━━━━━━Description
:
(
1). You can insert a number between "%" and a letter to indicate the maximum field width.
For example: %3d means outputting a 3-digit integer. If it is less than 3 digits, it will be right-aligned.
%9.2f means outputting a floating point number with a field width of 9, where the decimal place is 2 and the integer place is 6.
The decimal point occupies one place, and if it is less than 9 places, it
will be right-aligned. %8s means outputting a string of 8 characters, and if it is less than 8 characters, it will be right-aligned.
If the length of the string or the number of integer digits exceeds the specified field width, it will be output according to its actual length.
However, for floating point numbers, if the number of digits in the integer part exceeds the specified integer digit width, it will be output according to the actual integer digits;
if the number of digits in the decimal part exceeds the specified decimal digit width, it will be output according to the specified width and rounded.
In addition, if you want to add some zeros before the output value, you should add a zero before the field width item
. For example: d means that when outputting a value less than 4 digits, it will add zeros in front to make the total width 4 digits.
If a floating point number is used to represent the output format of a character or integer quantity, the number after the decimal point represents the maximum width, and the
number before the decimal point represents the minimum width.
For example: %6.9s means displaying a string with a length not less than 6 and not more than 9. If it is greater than 9,
the content after the 9th character will be deleted.
(2). You can add a lowercase letter l between "%" and a letter to indicate that the output is a long number.
For example: %ld indicates the output of a long integer
%lf indicates the output of a double floating point number
(3). You can control the output to be left-aligned or right-aligned, that is, adding a "-" sign between "%" and a letter
indicates that the output is left-aligned, otherwise it is right-aligned.
For example: %-7d means outputting a 7-bit integer left-aligned %
-10s means outputting 10 characters left-aligned
2. Some special characters━━━━━━━━━━━━━━━━━━━━━━━━━Character
Function──────────────────────────────
\ n Line feed \f Clear screen and feed page \r Carriage return \t Tab character \xhh means an ASCII code represented in hexadecimal, where hh is 1 to 2 hexadecimal digits━━━━━━━━━━━━━━━━━━━━━━━━━ *******************************************/
#include
#include
void delay(unsigned int time)
{
unsigned int i,j;
for(i=0;i<1000;i++)
for(j=0;j}
void main(void)
{
SCON = 0×50; //Serial port mode 1, allow receiving
TMOD = 0×20; //Timer 1 timing mode 2
TCON = 0×40; //Set timer 1 to start counting
TH1 = 0xFD; //11.0592MHz 9600 baud rate
TI = 1;
TR1 = 1; //Start timer
while(1)
{
printf("Current count: %lx\n",0×12345678); //32-bit hexadecimal number
delay(100);
}
}
Previous article:Talk about the memory allocation and optimization of C51 in Keil
Next article:Keil C51's printf
Recommended ReadingLatest update time:2024-11-16 14:43
- Popular Resources
- Popular amplifiers
- 西门子S7-12001500 PLC SCL语言编程从入门到精通 (北岛李工)
- Siemens Motion Control Technology and Engineering Applications (Tongxue, edited by Wu Xiaojun)
- How to read electrical control circuit diagrams (Classic best-selling books on electronics and electrical engineering) (Zheng Fengyi)
- MCU C language programming and Proteus simulation technology (Xu Aijun)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- EEWORLD University Hall----Application of TI? Interface Chips in Automotive Products
- EEWORLD University ---- IoT Moment: Protocol Standards Driving the Development of IoT
- A simple pull-up and pull-down circuit causes the control chip to heat up!
- I would like some advice on how to import .brd files into DXP
- [RT-Thread reading notes] Part 2: Implementing multithreading
- VGA Display Circle - Simple Design and Application FPGA
- [NXP Rapid IoT Review] Rapid IoT Studio Simple Programming Step 2 Counter plus Bluetooth
- What does this PCB package marking mean?
- Problems found during PCB rule checking
- Study and use of DSP6678 chip