"Learning note", "C language" type specifier

Source: Internet
Author: User
Tags integer numbers

1. Short and long

1> Short and long can provide integer numbers of different lengths, that is, the range of values that can be changed for integer numbers. In the 64bit compiler environment, int occupies 4 bytes (32bit), the value range is -231~231-1;short occupies 2 bytes (16bit), the value range is -215~215-1;long occupies 8 bytes (64bit), the value range is -263~ 263-1

2> summarizes: In a 64-bit compiler environment, short accounts for 2 bytes (16 bits), int accounts for 4 bytes (32 bits), and Long is 8 bytes (64 bits). Therefore, if you use an integer that is not very large, you can use short instead of int, which saves memory overhead.

3> in the world of compilers, different compiler environment, int, short, long, the range of values and occupy the length is not the same. For example, in a 16bit compiler environment, a long takes only 4 bytes. Fortunately, ANSI \ ISO has the following rules in place:

Short and int are at least 16 bits (2 bytes)

Long is at least 32 bits (4 bytes)

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

Char must be 8 bits (1 bytes), after all, char is the smallest data type we can program

4> can use 2 consecutive long, that is, a long long. In general, the range of long long is not less than long, for example, in a 32bit compiler environment, a long long takes up 8 bytes, and a long occupies 4 bytes. However, in the 64bit compiler environment, a long long is the same as long, which occupies 8 bytes.

5> another point to make clear is that short int is equivalent to Short,long int equivalent to long,long long int equivalent to long long

2. Signed and unsigned

1> first to be clear: Signed int is equivalent to signed,unsigned int equivalent to unsigned

The difference between 2> signed and unsigned is whether their highest bit is to be used as a sign bit, and does not change the length of the data, that is, the number of bytes, as short and long do.

Signed: Represents a symbol, which means that the highest bit is used as the sign bit, so it includes positive, negative, and 0. In fact, the highest bit of int is the sign bit, already includes the positive and negative number and 0, so signed and int are the same, signed equivalent to signed int, also equivalent to int. The value range of signed is 231 ~ 231-1

Unsigned: represents unsigned, which means that the highest bit is not used as a sign bit, so it does not include negative numbers. Under the 64bit compiler environment, int occupies 4 bytes (32bit), so the value range of unsigned is: 0000 0000 0000 0000 0000 0000 0000 0000 ~ 1111 1111 1111 1111 1111 1111 11 11 1111, i.e. 0 ~ 232–1

3. Code

1#include <stdio.h>2 /*3 int 4 bytes%d4 Short 2 byte%d5 a long 8 bytes%ld6 A long long 8 bytes%lld7  8  9 signedTen unsigned%u One  */ A intMain () - { -     //0000 0000 0000 0000 0000 0000 0000 0000 the     //2 of 31 square-1 -     //2 of 32 square-1 -     intnum; -      +     /* - the difference between signed and unsigned: + signed highest bit to be used as sign bit A unsigned highest bit not as sign bit at     */ -     //signed = = signed int -     //signed signed: positive, 0, negative -SignedintA =Ten; -Signed A2 =Ten; -      in     //unsigned int = = Unsigned -     //unsigned unsigned: 0, positive toUnsignedintb =Ten; +unsigned b2 =Ten; -      the     LongUnsignedintc =34343; *     Longunsigned C2 =423432; $     Panax Notoginseng      ShortUnsignedintD =4343; -      Shortunsigned d2 =43243; the      +      ShortSignedintE =54354; A      ShortSigned e2 =434; the      +      -      $      $     return 0; - } -  the voidLongandshort () - {Wuyi     //long = = Long int the     Long intA =100645654654645645l; -     LongA2 =100645654654645645l; Wu      -     //long long int = = Long long About     Long Long intc =100645654654645645ll; $     Long LongC2 =4535435435435ll; -      -     //Short = = Short int -      Short intD =5454; A      ShortD2 =43434; +      the     //printf ("%lld\n", c); -      $     ints =sizeof(Long Long int); theprintf"%d\n", s); the}

"Learning note", "C language" type specifier

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.