KEIL C51 printf formatted output special usage

Publisher:SereneVoyageLatest update time:2017-01-19 Source: eefocusKeywords:KEIL  C51 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

/*******************************************
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);
}
}


Keywords:KEIL  C51 Reference address:KEIL C51 printf formatted output special usage

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

C51 programming requires pressing keys in a specified order
//There is a question that requires pressing four buttons in sequence. //If you do not press in the specified order, an error message will be displayed. //Requirements are as follows: //When SW1 is pressed for the first time, D1 is on, and when it is released, D1 is off (if the first press is not SW1, but SW2, 3, or
[Microcontroller]
C51 programming requires pressing keys in a specified order
Summary of methods for setting header file paths in keil4
Personal Records: I feel the relative path is more convenient. Keil looks for header files in the directory where the project file is located. The relative directory of the header files is relative to the .uvproj file. My current personal habit is Create: Doc, Listing, Output, Project, User folders Doc for doc
[Microcontroller]
Summary of methods for setting header file paths in keil4
C51 program for simulating RS232 serial communication with microcontroller IO port
This program has been used in one of my projects and is very stable. During the writing process, I referred to    the article http://www.51hei.com/mcu/1541.html on the 51hei website. Some subroutines are excerpted here. #include "reg52.h" #include "intrins.h"   #include "math.h"      #include "stdio.h" sbit BT_SND
[Microcontroller]
Design of Multi-gas Detection System Based on C51 Single Chip Microcomputer
  A gas sensor is a device that can convert information about the type of gas and its concentration into electrical signals. Based on the strength of these electrical signals, information about the presence of the gas to be tested in the environment can be obtained, so that detection, monitoring, and alarm can be perf
[Microcontroller]
Design of Multi-gas Detection System Based on C51 Single Chip Microcomputer
Introduction to Keil uVision4 basic data types for 51 MCU
Being familiar with basic data types is one of the basic prerequisites for using a C language compiler to write programs, and using Keil uVision4 to develop 51 MCU C programs is no exception. This article will systematically introduce the various basic data types of Keil uVision4, hoping to be helpful to readers.
[Microcontroller]
Proteus C51 simulation learning board 5 - serial port interrupt
In ancient times, the ways of transmitting information included beacon towers, pigeon carriers, and 800-mile express delivery. I even played with "megaphones" when I was a child. They all have their own transmission media: smoke signals, pigeons, horses, ropes. So is it possible to use one IO for communication in
[Microcontroller]
Proteus C51 simulation learning board 5 - serial port interrupt
604 LCD screen input and output C51 source program
#include #include "INC\LCD1604.H" sfr LCD_INTER = 0x80; //Data bus interface  sbit LCDBUSY=LCD_INTER^7; //Flag bit sfr LCD_CONTROL = 0xA0; //Data bus interface  sbit LCDRS = LCD_CONTROL^0; //Data, instruction selection  sbit LCDRW = LCD_CONTROL^1; //Read and write selection  sbit LCDE = LCD_CONTROL^2; //Enab
[Microcontroller]
16*16LED dot matrix display Chinese characters C51 program
The program hardware consists of two 164-control row and two 595-control column chips. //***************************Author: Undertaker QQ:1205946980*********************// #include reg52.h #include intrins.h #define uchar unsigned char #define uint unsigned int code uchar TAB ={ {0x00,0x80,0x00,0x80,0xFC,0x
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号