C Language data type (i)

Source: Internet
Author: User

(Strong data type)

1. Constants

Constants are immutable quantities in a program

10 is constant

  

Two types of constants

#define Defining Macro Constants

Const

  

#对于 a constant of # define type, the habit of C is to capitalize the constant name, and for ordinary const constants and variables, it is generally lowercase combined with uppercase

#include <stdio.h>int  main () {    printf ("%d") ;     return 0 ;}
#include <stdio.h>#define// define a macro constant value of tenint  main () {    printf ("%d", MAX);     return 0 ;}
 #include <stdio.h> #define  MAX //   defines a macro constant value of  int   main () { // max = $;  //  equals must be a variable on the left, cannot be a constant, the value of a constant cannot be modified  printf ( " %d   " , MAX);  return  0  ;}  
#include <stdio.h>int  main () {        constint;    printf ("%d", a);     return 0 ;}

  

The above is an integer constant

  

String constants

  

#include <stdio.h>#define STRING "Hello world\n"; int Main () {        constchar'hello C';}

2. Bits byte word

A bit can only represent 0 12 status bit 1 bit 1bit

One byte for eight binary 8 bits byte 8 bit a byte

500GB b is a byte 10Mb bit

1 words 2 bytes short Word

2 characters for double word Zword

 

08 Binary 0x 16 binary

  

Original code anti-code complement

Integer accounted for 4 bytes below only 1 bytes are written example

    

sizeof keyword

is the C keyword, which is a function of specifying the number of bytes occupied by the specified data type.

#include <stdio.h>int  main () {        printf ("%d\n"sizeof (ten));     return 0 ;}

32-bit (bit) operating system returns the number of the 4 largest stored number 2 (32-1)

  size_t Type

  

int type

INT constant variable

int is a 32-bit binary integer that occupies 4 bytes in memory '

%d, output a signed 10 binary integer

%u, representing the output of an unsigned 10-binary integer

int Main () {        printf ("%x\n"0x10);   output Ten    printf ("%x\n");  The output decimal 10 corresponds to 16 bits a    printf ("%x\n"0xa);   output a    return0;}

#include <stdio.h>intMain () {printf ("%x\n",0x10);//Output Tenprintf"%x\n",Ten);//output Decimal 10 corresponds to 16 bits aprintf"%x\n",0xa);//output aprintf"%x", -7); return 0;}/*printf ("%x",-7); Output FFFFFFF9 (This is-7 of the complement converted to 16 binary) source 10000000 00000000 000000000 00000111 Anti-code 11111111 11111111 111111111 11111000 complement 11111111 1111 1111 111111111 11111001*/

/*
-7
1000 0111 with a signed eye-7 unsigned Vision 135
*/

%x represents output hexadecimal number (hexadecimal does not express negative numbers)

%x output 16 binary number in uppercase

%o Output octal number (2 binary and octal do not express negative numbers)

Short, long, int, long long, unsigned, signed (signed number)

Short is a shorter integer in 32-bit operating system accounting for 2 bytes

Long represents the length of an integer, under the 32-bit system under 4 bytes under a 64-bit system, Windows 4 bytes Unix is eight bytes (different operating systems not the same)

Int (32-bit system or above) is 4 bytes in any case

Long Long is a 64-bit, eight-byte integer for 32-bit operating system CPU register bit 32 bits, the calculation of the Longlong type of data, the efficiency is very low

Unsigned unsigned number, can be added after any integer type

#include <stdio.h>int  main () {        int;   4 bytes short    ;    printf ("%d\n"sizeof(b)); // 2    return 0 ;}
#include <stdio.h>int  main () {        int;   4 bytes short    ;     Long Long Ten ;    printf ("%d\n"sizeof(c)); // 8    return 0 ;}
#include <stdio.h>intMain () {intA =Ten;//4 bytes     Shortb =Ten; Long Longc =Ten; unsignedintD =Ten; printf ("%d\n",sizeof(d));//4    return 0;}

Integer overflow

When an integer is calculated that exceeds the maximum unit that the integer can hold, the integer overflows, and the result of the overflow is a high discard.

When a small integer is copied to a large integer, the sign bit is not lost and will be integrated

     Short 0xFFFF ;     1 ;    printf ("%d\n", ABC); // 0    return 0;
  
#include <stdio.h>intMain () {intA =Ten;//4 bytes     Shortb =Ten; Long Longc =Ten; unsignedintD =Ten; printf ("%d\n",sizeof(d));//4unsigned ShortABC =0xFFFF; //ABC = ABC + 1; //printf ("%d\n", ABC);//0ABC= ABC + -; printf ("%d\n", ABC);// AboutABC=2; ABC= ABC-5; printf ("%u\n", ABC);//65533    intI1 =0x12345678; ABC=I1; printf ("%x\n", ABC);//5678     ShortABC1 =-2; //0000 0000 0010//1111 1111 1111 1101//1111 1111 1111 1110 ( -2)//1111 1111 1111 1111 1111 1111 1111 1110I1=ABC1; printf ("%x\n", I1);//Fffffffeunsigned ShortABC2 =0; ABC2= ABC2-1; //00000000 00000000//10000000 00000001//11111111 11111110//11111111 11111111 (65535)printf"%d\n", ABC2);//65535//00000000 00000000 00001100//10000000 00000000 00000000 00001010//11111111 11111111 11111111 11110110    return 0;}

The memory is in bytes.

1 integers, 4 bytes

Big-endian alignment and small-end alignment

For a complex instruction CPU with this x86 architecture, integers are placed backwards in memory, low address arm,intel, high address low, small end aligned

But for the CPU of the Uniux server, more is the big-endian-aligned way to store integers

  

C Language data type (i)

Related Article

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.