24. printf is cross-platform. The data type and output type must match.

Source: Internet
Author: User

1. Using printf should be said to be type insecure. Therefore, the stream input and output of C ++ is introduced.

For example:

# Include "stdint. H"

# Include "iostream"

Using namespace STD;

Int main ()

{

Int64_t A = 1;

Int B = 2;

Uint32_t uin = 1;

printf (" % P \ n ", & A, & B);

printf (" % LlU \ n ", uin);

cout

Printf ("% d \ n", a, B );

Return 0;

}

The output is:

0xbfd831e0 0xbfd831dc

13823853877176303617 // Error

1 2

1 0 // Error

As you can see,Uint32_tType. When LLD is used, an error occurs,Because printf reads data from the starting address offset byte Based on the type.

Using the stream in C ++ will not cause this problem. Attention should be paid to cross-platform applications.

2. type and number of bytes [3]

% Ld: Long int, 32-bit Platform 4 bytes

% LLD: long int, 32-bit platform 8 bytes

% Lf: double

**************

Typedef signed Char int8_t

Typedef short int int16_t;

Typedef int int32_t;

# If _ wordsize = 64

Typedef long int int64_t;

# Else

_ Extension __

Typedef long int int64_t;

# Endif

Refer:

[1]Http://www.vimer.cn/2011/08/printf-%E5%B0%8F%E4%BB%A3%E7%A0%81%EF%BC%8C%E5%A4%A7%E9%97% AE %E9%A2%98.html

[2]Http://www.cppblog.com/Solstice/archive/2010/04/06/111788.aspx

[3] type and number of bytes

Http://blog.sina.com.cn/s/blog_4b9eab320100sdex.html

[4] exploration of printf implementation

Http://www.cnblogs.com/hnrainll/archive/2011/08/05/2128496.html

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.