如何理解C编程中的格式说明符[关闭]

问题描述 投票:-2回答:1

很多人在使用C格式说明符时遇到很多问题。他们大多数都不知道:

  1. 如何使用它们?
  2. 何时可以使用它们?
  3. 不同数据类型的范围是什么?
c format-specifiers
1个回答
0
投票

我想用表格显示这些,并通过说明为每个格式说明符提供一个示例。

字符

Format specifier    Description         Type                range                   Uses
    %c              Character           char                -128 to 128             Use to output single character
    %c              Character           unsigned char          0 to 255             Use to output single character
    %s              String              char *                  -                   Use to output any range of strings

示例

#include <stdio.h>

int main()
{
    char letter1 = 'a';
    unsigned char letter2 = 'a';

    char *words1 = "Hello World!";
    char words2[] = "Hello Guys!";

    printf("letter 1 = %c", letter1); //Output only one character
    printf("\nletter 2 = %c\n", letter2);

    printf("\n%s", words1); //Output any range of strings
    printf("\n%s", words2);

    letter1 = 255;
    letter2 = 255;

    printf("\n\nchar          = %d\n", letter1); //Diferrence between char and unsigned char
    printf("Unsigned char = %d\n", letter2);

    return 0;
}

输出

letter 1 = a
letter 2 = a

Hello World!
Hello Guys!

char          = -1
Unsigned char = 255


整数(短)

Format specifier    Description         Type                range(Uses) 
    %hd             Integer             short int           -32,768 to 32,767
    %hu                 "               unsigned short int  0 to 65,535

示例

#include <stdio.h>

int main()
{
    short int min1 = -32768;
    short int max1 = 32767;

    unsigned short int min2 = 0;
    unsigned short int max2 = 65535;

    printf("Minimum value of short int = %hd", min1); 
    printf("\nMaximum value of short int = %hd", max1); 

    printf("\n\nMinimum value of unsigned short int = %hu", min2); 
    printf("\nMaximum value of unsigned short int = %hu", max2); 

    return 0;
}

输出

Minimum value of short int = -32768
Maximum value of short int = 32767

Minimum value of unsigned short int = 0
Maximum value of unsigned short int = 65535


整数

Format specifier    Description     Type                    range                       Uses
    %d              Integer         int             -2,147,483,648 to 2,147,483,647     Can take integers as decimals
    %u                  "           unsigned int                 0 to 4,294,967,295     Output only positive numbers
    %i                  "           unsigned int    -2,147,483,648 to 2,147,483,647     Can take integers as decimals, 
                                                                                        hexadecimals and octals type.

示例

#include <stdio.h>

int main()
{
    int min1 = -2147483648;
    int max1 = 2147483647;

    unsigned int min2 = 0;
    unsigned int max2 = 4294967295;

    unsigned int min3 = -2147483648;
    unsigned int max3 = 2147483647;

    unsigned int decimal = 12;
    unsigned int octal = 012;
    unsigned int hexadecimal = 0X12;


    printf("Minimum value of int = %d", min1); 
    printf("\nMaximum value of int = %d", max1);

    printf("\n\nMinimum value of unsigned int(u) = %u", min2); 
    printf("\nMaximum value of unsigned int(u) = %u", max2);

    printf("\n\nMinimum value of unsigned int(i) = %i", min3); 
    printf("\nMaximum value of unsigned int(i) = %i", max3);

    printf("\n\n12 in decimal format       = %i", decimal); 
    printf("\n012 in octal format        = %i", octal);
    printf("\n0X12 in hexadecimal format = %i", hexadecimal);

    return 0;
}

输出

Minimum value of int = -2147483648
Maximum value of int = 2147483647

Minimum value of unsigned int(u) = 0
Maximum value of unsigned int(u) = 4294967295

Minimum value of unsigned int(i) = -2147483648
Maximum value of unsigned int(i) = 2147483647

12 in decimal format       = 12
012 in octal format        = 10
0X12 in hexadecimal format = 18


Integer(long and long long)

Format specifier    Description     Type                                            range(Uses)
    %ld or %li      Integer         long int                            -2,147,483,648 to 2,147,483,647
    %lu                 "           unsigned long int                                0 to 4,294,967,295
    %lld or %lli        "           long long int           -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807(-(2^63) to (2^63)-1)
    %llu                "           unsigned long long int                           0 to 18,446,744,073,709,551,615 

示例

#include <stdio.h>

int main()
{
    long int min1 = -2147483648;
    long int max1 = 2147483647;

    unsigned long int min2 = 0;
    unsigned long int max2 = 4294967295;

    long long int min3 = -9223372036854775808; // 2^63
    long long int max3 = 9223372036854775807; // 2^63 - 1

    unsigned long long int min4 = 0;
    unsigned long long int max4 = 18446744073709551615;


    printf("Minimum value of long int(ld) = %ld", min1); 
    printf("\nMaximum value of long int(ld) = %ld", max1);

    printf("\n\nMinimum value of long int(li) = %li", min1); 
    printf("\nMaximum value of long int(li) = %li", max1);

    printf("\n\nMinimum value of unsigned long int(lu) = %lu", min2); 
    printf("\nMaximum value of unsigned long int(lu) = %lu", max2);

    printf("\n\nMinimum value of long long int(lld) = %lld", min3); 
    printf("\nMaximum value of long long int(lld) = %lld", max3);

    printf("\n\nMinimum value of long long int(lli) = %lli", min3); 
    printf("\nMaximum value of long long int(lli) = %lli", max3);

    printf("\n\nMinimum value of unsigned long long int(llu) = %llu", min4); 
    printf("\nMaximum value of unsigned long long int(llu) = %llu", max4);

    return 0;
}

输出

Minimum value of long int(ld) = -2147483648
Maximum value of long int(ld) = 2147483647

Minimum value of long int(li) = -2147483648
Maximum value of long int(li) = 2147483647

Minimum value of unsigned long int(lu) = 0
Maximum value of unsigned long int(lu) = 4294967295

Minimum value of long long int(lld) = -9223372036854775808
Maximum value of long long int(lld) = 9223372036854775807

Minimum value of long long int(lli) = -9223372036854775808
Maximum value of long long int(lli) = 9223372036854775807

Minimum value of unsigned long long int(llu) = 0
Maximum value of unsigned long long int(llu) = 18446744073709551615


浮动,双精度和长双精度

Format specifier    Description     Type            Range                   Uses
    %f              Float           float           1.2E-38 to 3.4E+38      Use when number has 6 decimal places
    %lf             Double          double          2.3E-308 to 1.7E+308    Use when number has 15 decimal places
    %Lf             Long Double     long Double     3.4E-4932 to 1.1E+4932  Use when number has 19 decimal places
    %e or %E        Float, Double   float, double                           Scientific notation of float values
    %g or %G        Float, Double   float, double                           Scientific notation of float values
                                                                            Accept integers and float double also

示例

#include <stdio.h>

int main()
{
    float number1 = 5.12;
    double number2 = 5;
    long double number3 = 5;

    printf("Float number is(f)        = %f", number1);
    printf("\nDouble number is(lf)      = %lf", number2);
    printf("\nLong Double number is(Lf) = %Lf", number3);

    printf("\n\nFloat number is(e)       = %e", number1);
    printf("\nDouble number is(e)      = %e", number2);

    printf("\n\nFloat number is(E)       = %E", number1);
    printf("\nDouble number is(E)      = %E", number2);

    printf("\n\nFloat number is(g)       = %g", number1); //Support for both integers and floats, doubles
    printf("\nDouble number is(g)      = %g", number2);

    printf("\n\nFloat number is(G)       = %G", number1); //Support for both integers and floats, doubles
    printf("\nDouble number is(G)      = %G", number2);

    return 0;
}

输出

Float number is(f)        = 5.120000
Double number is(lf)      = 5.000000
Long Double number is(Lf) = 5.000000

Float number is(e)       = 5.120000e+00
Double number is(e)      = 5.000000e+00

Float number is(E)       = 5.120000E+00
Double number is(E)      = 5.000000E+00

Float number is(g)       = 5.12
Double number is(g)      = 5

Float number is(G)       = 5.12
Double number is(G)      = 5


其他说明符

Format specifier    Description     Type                            Uses
    %o              Integer         short int, unsigned short int   Octal representation of Integer.
                                    int, unsigned int, long int

    %x or %X        Integer         short int, unsigned short int   Hexadecimal representation of an Integer.
                                    int, unsigned int, long int

    %p              Void *          void *                          Use to get address of pointer or any other variable
                                                                    Address of pointer to void void *

    %n              -               -                               Prints nothing.It cause printf() to load the variable 
                                                                    pointed by corresponding argument. The loading is done with
                                                                    a value which is equal to the number of characters printed 
                                                                    by printf() before the occurrence of %n.

    %%              -               -                               Prints % character.

示例

#include <stdio.h>

int main()
{
    int num1 = 65;
    int num2 = 67;
    int num3 = 15;

    int* ptr =&num3;

    int num4;
    int num5 = 20;

    printf("Octal representation of 65 = %o", num1);
    printf("\nOctal representation of 67 = %o", num2);

    printf("\n\nHexadecimal representation of 15(x) = %x", num3);
    printf("\nHexadecimal representation of 15(X) = %X", num3);

    printf("\n\nAddress of pointer = %p",ptr);

    printf("\n\nThe value of %nnum4 and %nnum5 is =  ", &num4, &num5); //Uses of %n
    printf("%d   %d", num4, num5); 

    printf("\n\nI got 50%% discount when I was shopping"); //Uses of %%

    return 0;
}

输出

Octal representation of 65 = 101
Octal representation of 67 = 103

Hexadecimal representation of 15(x) = f
Hexadecimal representation of 15(X) = F

Address of pointer = 0x7ffe402adda4 

The value of num4 and num5 is =  15   24

I got 50% discount when I was shopping


I got more details about %n from

© www.soinside.com 2019 - 2024. All rights reserved.