当前位置:网站首页>C language | printf output function

C language | printf output function

2022-06-24 17:25:00 Kobayashi C language

C The concept of language output

The output is based on the computer host , The output of data from a computer to an output device is called output ,C The language itself does not contain output statements , If you don't add headers , The following code will report an error .

//#include<stdio.h>// The header file  
int main()// The main function   Program entry  
{
   printf("Hello xiaolin!\n");// \n It means new line  
   return 0;
}

Compilation result :

D:\ Yan Xiaolin \ Handout materials \1-100\ test .cpp In function 'int main()':
D:\ Yan Xiaolin \ Handout materials \1-100\ test .cpp [Error] 'printf' was not declared in this scope

C Language printf usage

1、 General format

printf( Format control , Output table columns )

  • Format control

Format control is a string enclosed in double quotation marks , call “ Conversion control string ”, abbreviation “ Format string ”, contain :

  • Format statement : from % And format characters , Such as %d、%f.
  • Ordinary character : That is, the characters that need to be output as they are .

  • Output table columns
    • The output table column is some data that the program needs to output , It can be a constant 、 Variable or expression .

C Language format characters

1、d Format symbol

Used to output a signed decimal integer .

2、c Format symbol

Used to output a character

3、s Format symbol

Used to output a string

4、f Format symbol

Used to output real numbers , Output in decimal form , There are two uses :

① Basic type , use %f, Do not specify the length of the output data , According to the actual situation of the data, the system determines the number of columns occupied by the data .

② Specify the data width and scale , use %m.nf,m The data representing the output occupies m That's ok ,n It means that it contains n Decimal place .

③ The output data is aligned to the left , use %-m.nf, Function and ② almost , That's the data to the left , Fill in the space at the right end .

5、e Format symbol

Used to specify that real numbers are output exponentially .

C Language format characters

significance

d

Output a decimal integer by symbol

c

Output a character

s

Output a string

f

Output real numbers in decimal form

%m.nf

m Specify the data width ,n Specify the number of decimal places

e

Output real numbers in exponential form

l

And d The format is the same , Used to d Format symbol

o

Output as octal integer

x

Output as a hexadecimal number

u

Output unsigned decimal integer data

g

Output floating point number , The system selects automatically f or e Format symbol

above , If you read it and think it helps you , Just give Xiao Lin a compliment , Share it with the people around you , In this way, Kobayashi has the power to renew itself , Thank you on your knees ~

C Language learning route     C Language development tools

VC6.0、Devc++、VS2019 Use the tutorial

100 Please go to the official account for the source code cases :C From language introduction to mastery

原网站

版权声明
本文为[Kobayashi C language]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/03/20210319105855544w.html