Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the integers in C language?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces you to the C language integers exist in what, the content is very detailed, interested friends can refer to, I hope to help you.

Integers are commonly used in programming, C language usually uses int to define integers (int is short for integer), which has been explained in detail in "C language variables and data types."

In modern operating systems, int typically occupies 4 bytes of memory, for a total of 32 bits. If you don't consider positive and negative numbers, when all the bits are 1, its value is the largest, 232-1=4,294,967,295≈ 4.3 billion, which is a large number, which is rarely used in actual development, while smaller numbers such as 1, 99, 12098, etc. are used more frequently.

Four bytes is more than enough to hold smaller integers, leaving two or three bytes free, which are wasted and no longer available for other data. Now personal computer memory is relatively large, low configuration also has 2G, waste some memory will not bring obvious loss; and in the early C language was invented, or in the microcontroller and embedded systems, memory is very scarce resources, all programs are trying to save memory.

On the other hand, 4.3 billion is a lot, but it's not enough to represent the world's population. You have to make integers take up more memory to represent larger values, such as 6 bytes or 8 bytes.

To make integers take up less memory, you can add short in front of int, and to make integers take up more memory, you can add long in front of int, for example:

shortinta=10;

shortintb,c=99;

longintm=102023;

longintn,p=562131;

Thus, a, b, and c only occupy 2 bytes of memory, while m, n, and p may occupy 8 bytes of memory.

You can also omit int and write only short and long, as follows:

shorta=10;

shortb,c=99;

longm=102023;

longn,p=562131;

This is a simpler way of writing and is often used in practical development.

int is the basic integer type, short and long are extensions of int, short saves memory, and long can accommodate larger values.

short, int, long are common integer types in C language, int is called integer, short is called short integer, long is called long integer.

Length of integer

The careful reader may notice that, in describing the length of short, int, and long types, we have used only positive terms for short, and uncertain terms such as "average" or "possible" for int and long. The implication of this description is that only the length of short is certain, which is two bytes, while the length of int and long is uncertain and behaves differently in different environments.

The number of bytes occupied by a data type is called the length of the data type. For example, short takes up 2 bytes of memory, so its length is 2.

This is also true. The C language does not strictly specify the length of short, int, and long, but only makes broad restrictions:

short takes at least 2 bytes.

int is recommended as a machine word length. The machine word length is 4 bytes in 32-bit environments and 8 bytes in 64-bit environments.

The length of short cannot be greater than int, and the length of long cannot be less than int.

To sum up, their length (number of bytes) relationship is:

2≤short≤int≤long

This means that short is not necessarily "short" and long is not necessarily "long," and they may take up the same number of bytes as int.

In a 16-bit environment, short is 2 bytes long, int is 2 bytes long, and long is 4 bytes long. The 16-bit environment is mostly used for single-chip computers and low-level embedded systems, and has not been seen on PCs and servers.

For 32-bit Windows, Linux, and MacOS, short is 2 bytes long, int is 4 bytes long, and long is 4 bytes long. The share of 32-bit systems on PCs and servers is also slowly declining, and embedded systems are increasingly using 32-bit.

In a 64-bit environment, different operating systems will have different results, as follows:

operating system short int long

Win64 (64-bit Windows) 2 4 4

Unix-like systems (including Unix, Linux, MacOS, BSD, Solaris, etc.) 2 4 8

At present, we use more PC systems for WinXP, Win7, Win8, Win10, MacOS, Linux, in these systems, the length of short and int are fixed, respectively 2 and 4, we can rest assured to use, only the length of long in Win64 and Unix-like systems will be different, pay attention to portability when using.

sizeof operator

The sizeof operator can be used to obtain the length of a data type, as follows:

#include

intmain()

{

shorta=10;

intb=100;

intshort_length=sizeofa;

intint_length=sizeof(b);

intlong_length=sizeof(long);

intchar_length=sizeof(char);

printf("short=%d,int=%d,long=%d,char=%d\n",short_length,int_length,long_length,char_length);

return0;

}

The results in 32-bit and Win64 environments are:

short=2,int=4,long=4,char=1

The results on 64-bit Linux and MacOS are:

short=2,int=4,long=8,char=1

sizeof is used to get the number of bytes occupied by a data type or variable. If it is followed by the variable name, then () can be omitted. If it is followed by a data type, it must be accompanied by ().

Note that sizeof is an operator in C, not a function,

About the C language integers exist in which it is shared here, I hope the above content can be of some help to everyone, you can learn more knowledge. If you think the article is good, you can share it so that more people can see it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report