Dev-C ++'s experiment on the long type

Source: Internet
Author: User

Kingwei 2005.3.10

Tutorial environment: Dev-C ++ 4.9.6.0 (GCC/mingw32), use the-wall compilation Option

# Include <stdio. h>

Int main ()
{
Signed long int v_signed_long_long_int;
Unsigned long int v_unsigned_long_long_int;

/* Part1: Use % i64d and % i64u */
 
/* [-2 ^ 63, 2 ^ 63-1] ==> [-9223372036854775808,922 3372042554775807] */
Scanf ("% i64d", & v_signed_long_long_int );
Printf ("% i64d/N", v_signed_long_long_int );

/* [0, 2 ^ 64-1] ==> [0, 18446744073709551615] */
Scanf ("% i64u", & v_unsigned_long_long_int );
Printf ("% i64u/N", v_unsigned_long_long_int );

/* Part2: Use % LLD and % LlU */
 
/* [-2 ^ 63, 2 ^ 63-1] ==> [-9223372036854775808,922 3372042554775807] */
Scanf ("% lld", & v_signed_long_long_int );
Printf ("% lld/n", v_signed_long_long_int );

/* [0, 2 ^ 64-1] ==> [0, 18446744073709551615] */
Scanf ("% llu", & v_unsigned_long_long_int );
Printf ("% llu/n", v_unsigned_long_long_int );
 
Return 0;
}

This program is compiled in Dev-C ++ with a series of Warnings:

D:/Dev-C ++ experiment on the long type/llint_format.cpp [Warning] In function 'int main ()':
14 D:/Dev-C ++ long type Experiment/llint_format.cpp [Warning] unknown conversion type character 'I' in format
14 D:/Dev-C ++ experiment on the long type/llint_format.cpp [Warning] too using arguments for format
15 D:/Dev-C ++ long type Experiment/llint_format.cpp [Warning] unknown conversion type character 'I' in format
15 D:/Dev-C ++ long-type Experiment/llint_format.cpp [Warning] too arguments for format
18 D:/Dev-C ++ long type Experiment/llint_format.cpp [Warning] unknown conversion type character 'I' in format
18 D:/Dev-C ++ experiment on the long type/llint_format.cpp [Warning] too using arguments for format
19 D:/Dev-C ++ long type Experiment/llint_format.cpp [Warning] unknown conversion type character 'I' in format
19 D:/Dev-C ++ long-type Experiment/llint_format.cpp [Warning] too arguments for format

As you can see, Warning is generated because the format character % I is used in PART1. PART2 is normal, and the compiler does not give any warning information.

But -- see the test results:

----- Test case #1: Lower Bound -----

-9223372036854775808
0
-9223372036854775808
0

Output:

-9223372036854775808
0
0
0

----- Test case #2: Upper Bound -----

9223372036854775807
18446744073709551615
9223372036854775807
18446744073709551615

Output:

9223372036854775807
18446744073709551615
-1
4294967295

----- Test case #3: underflow -----

-9223372036854775809
-1
-9223372036854775809
-1

Output:

9223372036854775807
18446744073709551615
-1
4294967295

----- Test case #4: overflow -----

9223372036854775808
18446744073709551616
9223372036854775808
18446744073709551616

Output:

-9223372036854775808
0
0
0

On the contrary, Part1 works completely normally, but Part2 produces problems.
Why? I guess like this:

"% LLD" and "% LlU" are the format characters used by GCC/g ++ for long int type (64 bits) Input and Output in Linux.

While "% i64d" and "% i64u" are the format descriptions used in the Microsoft Vc ++ library for input and output _ int64 type.

The problem lies in: Dev-C ++ uses the compiler is mingw32, mingw32 is one of the x86-win32 GCC sub-projects, the core of the compiler is GCC in Linux.

During the compilation phase, the GCC compiler checks this code. Obviously, it does not recognize "% i64d ",
Therefore, the warning "unknown conversion type character 'I' in format" is given, which is not considered as a format specifier.
Next, GCC finds that there is no valid specifier in the entire format string, but the data parameters passed to printf/scanf are not only the format string,
So it gave the second warning "Too parse arguments for format ".

GCC naturally accepts "% LLD" and "% LlU. The tragedy laid the groundwork.

Mingw32 uses the GCC Rule Check syntax during compilation, but uses the Microsoft library for connection and runtime.
The printf and scanf functions in this library certainly do not know "% LLD" and "% LlU" under Linux GCC, for "% i64d" and "% i64u ", it is acceptable.

-- The above result is displayed.

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.