How to use a 64-bit integer in C?

Source: Internet
Author: User

First, the ANSI c99 standard does not contain 64-bit integers. Secondly, many practical compilers support 64-bit integer types.
For specific analysis, see: http://blog.csdn.net/lychee007/archive/2010/04/04/5449237.aspx
The key is the table:

Variable definition Output Mode GCC (mingw32) G ++ (mingw32) GCC (Linux i386) G ++ (Linux i386) Microsoftvisual C ++ 6.0
Long long "% LLD" Error Error Correct Correct Unable to compile
Long long "% I64d" Correct Correct Error Error Unable to compile
_ Int64 "LLD" Error Error Unable to compile Unable to compile Error
_ Int64 "% I64d" Correct Correct Unable to compile Unable to compile Correct
Long long Cout Non-C ++ Correct Non-C ++ Correct Unable to compile
_ Int64 Cout Non-C ++ Correct Non-C ++ Unable to compile Unable to compile
Long long Printint64 () Correct Correct Correct Correct Unable to compile

We noticed that the support for 64-bit integers in VC ++ 6.0 is the most "that.

To publish a new bookCode: 2.4.3 64-bit integer

Question:Enter a positive integer to count the number of positive factors. N <= (12 power of 10)

Solution to the Standard C language:
Compiler: TCC (basically the international standard of c99, it's amazing !)

 0001 /* 0002 2.4.3, 64-bit integer 0003 Example 0004 Enter a positive integer n to count the number of positive factors. 0005 N <= (12 power of 10) 0006      0007 Solution: 0008 The exhaustion method starts from 1, and every possible factor X is lifted. If the condition (N % x = 0) is met, the counter count increments by 1. 0009 TIPS: 0010 You only need to enumerate the range of [1 .. SQRT (n; 0011 If X is a factor of N, (n/X) is also a factor of N. 0012 0013       0014 */ 0015 # Include "stdio. H" 0016 # Include "math. H" 0017 Void main (){ 0018 Long long n, 0019 X,/* hypothetical factor */ 0020 Count = 0;/* counter */ 0021  0022 Scanf ("% i64d", & N ); 0023 For (x = 1; x <= (long) SQRT (n); X = x + 1 ){ 0024 If (N % x = 0 ){ 0025 Count = count + 2;/* The reason for + 2 is that X and (n/x) are both factors of N */ 0026 /* Printf ("% i64d \ n", x );*/ 0027 If (N/x = x) Count = count-1; 0028 } 0029 } 0030 Printf ("number of factors: % i64d", count ); 0031  0032 Return; 0033 }

The above code is written in itself, but it will be told during compilation in VC ++ 6.0:

...... Test. C (4): Error c2632: 'long' followed by 'long' is illegal

This is the proof that VC ++ 6.0 cannot recognize long!

So,How does VC ++ 6.0 complete the 64-bit integer required in the above Code?

See the following code: (the only secret is to change long to _ int64. Note:There are two underscores on the left of int64!)

0001# Include "stdio. H"0002# Include "math. H"0003Void main (){0004_ Int64 N,0005X,/* hypothetical factor */0006Count = 0;/* counter */00070008Scanf ("% i64d", & N );0009Printf ("% i64d", N );0010For (x = 1; x <= (_ int64) SQRT (n); X = x + 1 ){0011If (N % x = 0 ){0012Count = count + 2;/* The reason for + 2 is that X and (n/x) are both factors of N */0013/* Printf ("% i64d \ n", x );*/0014If (N/x = x) Count = count-1;0015}0016}0017Printf ("number of factors: % i64d", count );0018Return;0019}

Please note that the above Code can correctly process the data input of an extremely large integer within the power of 12 of 10.

Believe it? Try it!

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.