The size of the integer variable C in Turbo C, VC ++, and Linux

Source: Internet
Author: User

In C, the size of memory allocated for integer variables varies in different compiling environments. The ansi c standard does not specify the length of the identifier, but each c compilation system has its own rules.

Integer Variables in C: Short, Int, and long. The short type is not longer than int type, and the long type is not shorter than int type. In the TC environment, the short and INT types are allocated 2 bytes (Word), that is, 16 bits (BIT), and the long type is 4 bytes, that is, 32 bits. In other compiling environments, int may be allocated 4 bytes, which is the same as long type.

The memory space allocated for the three Integer Variables in each compilation environment is as follows:

In Turbo c2.0: int 16, short 16, long 32;

In VC ++ 6.0: int 32, short 16, long 32;

In Linux: int 32, short 16, long 32;

To verify the memory size allocated by the compiler for the three integers, you can write a test section in the current compiling environment.ProgramTo measure their actual size.

/*

Check the size of variable types (INT, short, long, etc.) in Turbo C. (Method 1)

*/

# Include "stdio. H "int main () {/* defines three integer variables, which are int and short's 16th-bit negative values 1, Long's 32nd-bit negative 1, other BITs are 0 */unsigned int n_int = 0x8000;/* n_int is an int variable */unsigned short n_short = 0x8000; /* n_int is the short variable */unsigned long n_long = 0x80000000;/* n_int is the long variable */unsigned size_int = 16, size_short = 16, size_long = 32; /* assume that the int and short types have a minimum of 16 bits, and the long type has a minimum of 32 bits */while (1)/* default endless loop */{n_int <= 1; /* n_int first shifts one digit to the left */If (n_int! = 0) size_int ++;/* n_int is not 0, indicating that the original 16-bit 1 has no overflow, and n_int has a higher position */else break;/* n_int is 0, indicates that the highest bit 1 overflows and the endless loop */} while (1)/* same as */{n_short <= 1; if (n_short! = 0) size_short ++; else break;} while (1)/* same as */{n_long <= 1; if (n_long! = 0) size_long ++; else break;}/* print the memory size occupied by three types of variables */printf ("in this suite: \ n size_int = % d \ n size_short = % d \ n size_long = % d \ n ", size_int, size_short, size_long);} running result in Turbo C: size_int = bytes = 16size_long = 32 in VC ++ _ 6.0, run result: size_int = bytes = 32size_long = 32 in linux_redhat, run result: size_int = 16size_short = 32size_long = 32

It can be seen that in different compiling environments, the number of bytes allocated for the three integer variables is different. Understanding this can prevent unexpected data overflow and cause errors. However, this error is usually not reported by the compiler, this leads to a transparent error, which does not exist but does exist.

There are many ways to test the data type size. You can also use the following method:

/*

Check the size of variable types (INT, short, long, etc.) in Turbo C. (Method 2)

*/

 
# Include "stdio. H "int main () {unsigned int n_int; unsigned short n_short; unsigned long n_long; printf (" in this suite: \ n num_int = % d \ n num_short = % d \ n num_long = % d \ n ", 8 * sizeof (n_int), 8 * sizeof (n_short ), 8 * sizeof (n_long); Return 0 ;}

This method is relatively simple. the sizeof function is used to check the size of the integer variable. However, note that the sizeof function returns the number of bytes, which must be multiplied by 8 (one byte occupies 8 digits ).

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.