Use in printf (C language)

Source: Internet
Author: User

Use in printf (C language)

# Include
 
  
#include

int main (int argc, const char * argv []) {

    // Shaping output
    printf (% d,% d, 3,4);
    printf (
);
    // The width of characters output by shaping,% md
    printf (% 4d,% 4d, 3,4);
    printf (
);

    // Long integer format output
    printf (% ld,% ld, (long) 3, (long) 4);
    printf (
);
    // Long shape can also set the output character width
    printf (% 8ld,% 8ld, (long) 3, (long) 4);
    printf (
);


    // Output% o in octal format
    int a = -1;
    printf (% d,% o, a, a);
    printf (
);
    printf (% d,% 13o, a, a); // Set the output width of characters
//-1 is stored in memory (exists in two's complement): eg: 111111111 (the first bit is the sign bit)
    printf (
);

    // X symbol output in hexadecimal
    int b = -1;
    printf (% x,% o,% d, b, b, b);
    
    printf (
);

    // u is unsigned output
    int c = -1;
    unsigned int d = 65535;
    printf (% x,% o,% d,% u, c, c, c, c);
    printf (
);
    printf (% x,% o,% d,% u, d, d, d, d);
    
    printf (
);

    // c format character, output one character
    char e = 'a';
    printf (% c,% d, e, e);
    
    printf (
);

    // s format character, used to output a string
// char f [] = china;
// prinf (% s, f);
#warning Problem with output string
    
    // f format character, used to output real numbers (including: single precision, double precision), output in decimal form
    // 1,% f format character, does not specify the width of the field, it is automatically specified by the system, so that all integer parts are output, and 6 decimal places are output. Note: Not all digits in the output numbers are significant digits, and the single-precision real number significant digits are generally 7 digits. The double-precision significant digit is 16 digits, giving 6 decimal places.
// float x, y;
// x = 11111111.111; y = 22222222,222;
// printf (% f
, x + y); // It can be seen from the result that only 7 bits are valid.
    double x, y;
    x = 11111111.111; y = 22222222.222;
    printf (% f
, x + y);
    // 2,% m.nf, specify the output data for occupying m columns, where n decimal places, if the value is greater than m, there will be no spaces on the left.
    // 3.% -M.nf is basically the same as% m.nf, except that the output value is leaning to the left and padding to the right.
    

    // symbol e, output as an exponent
    // 1,% e, do not specify the width occupied by the output data, and the number of decimal places in the number part. Some c compiler systems automatically indicate that the number given is 6 decimal places, and 5 digits of the exponent. eg: +002)
    printf (% e, 12389.454566); // and this system is the default stack of 4 bits
    printf (
);

    //%m.ne and% -m.ne, m, n,-are the same as before.
    double f = 123.456;
    printf (% e% 10e% 10.2e% .2e% -10.2e, f, f, f, f, f);
    printf (
);

    // The format character g is used to output real numbers. It automatically selects the f format or the e format according to the size of the number.
    double g = 123.456;
    printf (% f% e% g, g, g, g);
    printf (
);

    double h = 1234567123.456;
    printf (% f% e% g, h, h, h);

    
    // Note: A format character starts with% and ends with one of the above format characters. You can insert additional modifiers in the middle, which are actually ordinary characters.
// printf (c =% cf =% fs =% s, c, f, s); // In fact, f = is an ordinary character
    printf (
);

    // output% symbol
    printf (% f %%, 1.0 / 3);
    
    getchar ();
    return 0;
}  

The following figure shows the output of some corresponding format symbols:



 

The following is the Escape Character ""


 

 

 

 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.