C Tutorial/Data Type/Integer Family

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Float family (real numbers with decimal points)

  1. Float data type
  2. Double data type

2.7.Integer Family 2.7.1. <A href="/Tutorial/C/0040__Data-Type/Integerfamily.htm">Integer family</a> 2.7.2. Float family (real numbers with decimal points) 2.7.3. <A href="/Tutorial/C/0040__Data-Type/Integerdatatypestorageallocations.htm">Integer data type storage allocations</a> 2.7.4. <A href="/Tutorial/C/0040__Data-Type/Usingtheintegerconversionspecifiers.htm">Using the integer conversion specifiers</a> 2.7.5. <A href="/Tutorial/C/0040__Data-Type/Printinganunsignedintegerinbits.htm">Printing an unsigned integer in bits</a>

Integer data type storage allocations

Data Type Allocation Range signed char 1 byte -2^7 to 2^7-1(-128 to 127) Unsigned char 1 byte 0 to 2^8-1(0 to 255) short 2 bytes -2^15 to 2^15 -1 (-32768 to 32767) Unsigned short 2 bytes 0 to 216 -1 (0 to 65535) long int 4 bytes 2^31 to 2^31-1 (2,147,483,648 to 2,147,483,647) int 2 or 4 bytes depending on implementation

Integer family

Integer data types are used for storing whole numbers and characters.

Integer family has

  1. char data type
  2. int data type
  3. short int data type
  4. long int data type
  1. They have the different amount of storage space.
  2. Each type has two variants, signed and unsigned.

2.7.Integer Family 2.7.1. Integer family 2.7.2. <A href="/Tutorial/C/0040__Data-Type/Floatfamilyrealnumberswithdecimalpoints.htm">Float family (real numbers with decimal points)</a> 2.7.3. <A href="/Tutorial/C/0040__Data-Type/Integerdatatypestorageallocations.htm">Integer data type storage allocations</a> 2.7.4. <A href="/Tutorial/C/0040__Data-Type/Usingtheintegerconversionspecifiers.htm">Using the integer conversion specifiers</a> 2.7.5. <A href="/Tutorial/C/0040__Data-Type/Printinganunsignedintegerinbits.htm">Printing an unsigned integer in bits</a>

Printing an unsigned integer in bits

#include <stdio.h>
int main() { 
   unsigned value = 7;
   unsigned c; 
   unsigned displayMask = 1 << 31; 
   printf( "%10u = ", value );
   for ( c = 1; c <= 32; c++ ) { 
      putchar( value & displayMask ? "1" : "0" );
      value <<= 1;
      if ( c % 8 == 0 ) {
         putchar( " " );
      }
   }
   return 0;
}
7 = 00000000 00000000 00000000 00000111

Using the integer conversion specifiers

#include <stdio.h>
int main()
{ 
   printf( "%d\n", 455 );
   printf( "%i\n", 455 );  /* i same as d in printf */
   printf( "%d\n", +455 );
   printf( "%d\n", -455 );
   printf( "%hd\n", 32000 );
   printf( "%ld\n", 2000000000 );
   printf( "%o\n", 455 );
   printf( "%u\n", 455 );
   printf( "%u\n", -455 );
   printf( "%x\n", 455 );
   printf( "%X\n", 455 );
 
   return 0;
}
455
455
455
-455
32000
2000000000
707
455
4294966841
1c7
1C7