Detailed analysis of printf function

Knowledge point 1: Use formula of printf function

          1) printf("XXX placeholder 1 XXX placeholder 2 XXX placeholder 3", replace 1, replace 2, replace 3);

example:

#include <stdio.h>
int main()
{
int a = 1;
float b = 2.345;
char c = 'a';
printf("整型a为%d 浮点b为%f 字符c为%c 字符c对应的ASCII码为%d", a, b, c, c);
return 0;
}

Knowledge point 2: usage of printf function

          1) printf is a variable parameter function. (The number and type of parameters are undefined);

                a) Its number of parameters is uncertain, and the parameter types are also uncertain;

printf("%d", 1); // 两个参数
printf("%d %f", 1, 2.3); // 三个参数
printf("%d %f %c", 1, 2.3, 'H'); // 四个参数

        2) The first parameter must be a string;

        3) The first parameter contains the characters that need to be output and the placeholders that need to be replaced;

        4) The second and subsequent parameters will replace the placeholders in turn;

        5) The type and number of placeholders need to correspond to the type and number of subsequent parameters;

Knowledge point 3: Placeholders of integer types

          1) Type promotion of signed integers. (For the integer type char with signed bit, short, when the variable parameter of printf is passed in, it will be promoted to int. Integer types higher than int will not change);

               a) Lower level than int will be promoted to intl;

               b) When processing char, short, int, you can use %d to occupy the space;

               c) Use %ld to occupy space for long. For more advanced long long, you need to use %lld to occupy the space;

Conclusion: Use %d for char, short, and int. long uses %ld. long long uses %lld.

          2) Type promotion of unsigned integer types;

                a) For unsigned bit integers unsigned char and unsigned short, when the variable parameters of printf are passed in, they will be promoted to unsigned int;

                b) Integer types higher than unsigned int will not change;

                c) For unsigned integers, d needs to be replaced by u to indicate that the highest bit is not regarded as a sign bit, but as a data bit;

Conclusion: Use %u for unsigned char, unsigned short, and unsigned int. Use %lu for unsigned long. unsigned long long uses %llu.

          3) Type promotion of floating point types;

               a) float will be promoted to double, and double will not change;

Conclusion: Both float and double use %f.

Knowledge point 4: Conversion specifications

          1) Conversion operation d;

#include <stdio.h>
int main()
{
char c1 = 127;
short s1 = 32767;
int n1 = 2147483647;
char c2 = -128;
short s2 = -32768;
int n2 = -2147483648;
printf("%d\n", c1);
printf("%d\n", s1);
printf("%d\n", n1);
printf("%d\n", c2);
printf("%d\n", s2);
printf("%d\n", n2);
return 0;
}

      Variables c1, s1, c2, and s2 are all lower-level integer types than int. They are converted to int when entering printf. Therefore, use the conversion operation d to take the sizeof(int) byte binary data, convert them into characters according to the signed integer type, and print them on the console;

            2) Conversion operation u;

#include <stdio.h>
int main()
{
unsigned char c1 = 0;
unsigned short s1 = 0;
unsigned int n1 = 0;
unsigned char c2 = 255;
unsigned short s2 = 65535;
unsigned int n2 = 4294967295;
printf("%u\n", c1);
printf("%u\n", s1);
printf("%u\n", n1);
printf("%u\n", c2);
printf("%u\n", s2);
printf("%u\n", n2);
return 0;
}

      Variables c1, s1, c2, and s2 are all lower-level integer types than unsigned int. They are converted to unsigned int when entering printf. Therefore, use the conversion operation u to take the sizeof (unsigned int) byte binary data, convert them into characters according to the unsigned integer type, and print them on the console;

         3) Misuse of conversion operations d and u. Since the value ranges of signed int and unsigned int are inconsistent, mismatching of data types and conversion operations is likely to result in incorrect conversion results;

         4) Conversion operation c;

#include <stdio.h>
int main()
{
char c = 65;
short s = 66;
int n = 67;
printf("c=%d\n", c);
printf("s=%d\n", s);
printf("n=%d\n", n);
printf("c=%c\n", c);
printf("s=%c\n", s);
printf("n=%c\n", n);
return 0;
}

       Variables c, s, n are integer types lower than or equal to int. They are converted to int when entering printf. Therefore, use the conversion operation c to take the sizeof(int) byte binary data, convert the value into the ASCII character corresponding to the value and print it on the console.

         5) Conversion operations f, e, E;

#include <stdio.h>
int main()
{
float f = 1.234;
double df = 1.234567;
printf("%f\n", f);
printf("%f\n", df);
return 0;
}

     Variables f and df are integer types lower than or equal to double. They are converted to double when entering printf. Therefore, using conversion operation f, take sizeof(double) bytes of binary data, convert them into characters according to double-precision floating point type and print them on the console.

#include <stdio.h>
int main()
{
float f = 1.234;
double df = 1.234567;
printf("%e\n", f);
printf("%e\n", df);
printf("%E\n", f);
printf("%E\n", df);
return 0;
}

       The conversion operation e, E is similar to f, but uses e counting. The difference between e and E is that one uses a lowercase e and the other uses an uppercase E.

         6) Conversion operations o, x, X;

#include <stdio.h>
int main()
{
unsigned int n = 123456;
printf("%u\n", n); // 十进制
printf("%o\n", n); // 八进制
printf("%x\n", n); // 十六进制,小写字母
printf("%X\n", n); // 十六进制,大写字母
return 0;
}

        a) o, x, X conversion operation will obtain sizeof (unsigned int) byte binary data;

        b) oConvert the data into octal characters according to unsigned integer type and print it on the console;

        c) x, X converts the data into hexadecimal characters according to the unsigned integer type and prints it on the console;

        d) The difference between x and X is that one uses lowercase letters and the other uses uppercase letters;

    7) Conversion operations;

#include <stdio.h>
int main()
{
printf("%s", "Hello World\n");
return 0;
}

      The s conversion operation will obtain sizeof(char *) byte binary data and regard this data as the first address of the string. Starting from the first address, the string will be output. 

Guess you like

Origin blog.csdn.net/m0_73463638/article/details/127042922