How to determine the number of digits of the operating system without sizeof

Source: Internet
Author: User

 

How to determine the number of digits of the operating system without sizeof

As shown in a post on the Forum

Http://topic.csdn.net/u/20110926/14/3cf3149d-6715-471d-9105-933d55e9c9ad.html

There is a solution:

 

 
# Include <stdio. h> # include <string. h> char Buf [17]; int main () {int * P = (int *)-1; sprintf (BUF, "% x", P ); printf ("system is % d bit. \ n ", strlen (BUF) * 4 );}

 

However, it is said that the 64-bit system does not work. No 64-bit system is used for verification ..

Or

 
Int x = 32767 + 1 ;//0x7fffIf (x <0) {printf ("16 \ n");} else {printf ("32 \ n ");}

And

 
You should know the addressing capability. Print an address at will, int * P, and then printf ("% d", P). For example, if the output is 7a3e, it is 16 bits, if it is ipv7a3e, It is a 32-bit

This statement

 

Someone posted the content on msdn.

In 64-bit windows, int and long are 32-bit values. ForProgramDo not assign the pointer to a 32-bit variable. On the 64-bit platform, the pointer is 64-bit. if the pointer is assigned to a 32-bit variable, the pointer value should be truncated. In 64-bit windows, size_t, time_t, and ptrdiff_t are 64-bit values. In Visual C ++ versions earlier than visual C ++ 2005 on a 32-bit Windows operating system, time_t is a 32-bit value. In Visual C ++ 2005 and later versions, time_t is a 64-bit integer by default. For more information, see time management.

So

 
# Define OS _bits (INT) (ptrdiff_t *) 0 + 1) <3)

This approach should also be feasible

In fact, it is difficult to have fixed answers to such questions, because sometimes we do not know whether the result is the number of digits of the compiler or the number of digits of the system, in addition, the results of different compilers may be different in systems with different digits.

We should just consider the various ideas.

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.