constant

String constant

  • character

For example: ‘f’,’ I ‘,’z’,’a’ The compiler allocates space for each character.

‘f’ ‘i’ ‘z’ ‘a’
  • String Example:"hello"The compiler allocates space for each character in a string'\ 0'The end.
‘h’ ‘e’ ‘l’ ‘l’ ‘o’ ‘\ 0’

Basic types of

  • Integer:short int.int.long int.long long int(c99 added). The size of bytes is written in ascending order.
  • Floating point types:float.double.long double
  • Character types:char
  • Boolean type:_Bool
  • Enumeration types:enum

The sizeof operator

The user gets the length of the data type or expression.

  • sizeof(object); // sizeof(object);
  • sizeof(type_name); // sizeof(type);
  • sizeof object; // sizeof objects;

The following data types are for reference only, depending on the compiler

#include <stdio.h>

int main(a){
  int i;
  char j;
  float k;

  i=123;
  j='c';
  k=3.123;

  printf("size of int is %d \n".sizeof(int));
  printf("size of i is %d \n".sizeof i );
  printf("size of char is %d \n".sizeof(char));
  printf("size of j is %d \n".sizeof j );
  printf("size of flaot is %d \n".sizeof(float));
  printf("size of k is %d \n".sizeof k );

  return 0;
}
Copy the code
size of int is 4
size of i is 4
size of char is 1
size of j is 1
size of flaot is 4
size of k is 4
Copy the code
#include<stdio.h>

int main(a){

printf("int sizeof is %d\n".sizeof(int));
printf("short int size is %d\n".sizeof(short));
printf("long int size is %d\n".sizeof(long));
printf("long long int size is %d\n".sizeof(long long));
printf("char size is %d\n".sizeof(char));
printf("_Bool size is %d\n".sizeof(_Bool));
printf("float size is %d\n".sizeof(float));
printf("double size is %d\n".sizeof(double));
printf("long double size is %d\n".sizeof(long double));

return 0;
}

Copy the code
int sizeof is 4
short int size is 2
long int size is 8
long long int size is 8
char size is 1
_Bool size is 1
float size is 4
double size is 8
long double size is 16
Copy the code

Signed and unsigned

Signed: represents the signed bit

Unsigned: indicates that the unsigned bit is ≥0

#include<stdio.h>
int main(a){
short i;
unsigned short j;
i = - 1;
j = - 1;
printf("i is %d\n",i);
printf("j is %u\n",j);
return 0;
}
Copy the code
i is -1
j is 65535
Copy the code

Computer data unit

The smallest unit that a cup can read: bit.

Bit => B

1/0

Byte => B

1 1 1 1 1 1 1 1

Relationship: 1B = 8b

How many numbers can a byte represent? Decimal: 255 hexadecimal: FF

Maximum value: 2 to the n -1

binary The decimal system hexadecimal
0 0 0
1 1 1
10 2 2
11 3 3
100 4 4
101 5 5
110 6 6
111 7 7
1000 8 8
1001 9 9
1010 10 A
1011 11 B
1100 12 C
1101 13 D
1110 14 E
1111 15 F
10000 16 10
10001 17 11
. . .
11111111 255 FF
  • To calculate

We know that int is 4 bytes.

#include<stdio.h>
#include<math.h>
int main(a){
  int result = pow(2.32)- 1;
  printf("result is %d\n",result);
  return 0;
}
Copy the code
test6.c: In function'the main: test6. C: 4-2: warning: overflowinImplicit constant conversion [-woverflow] int result = pow(2,32)-1; ^ result is 2147483647Copy the code

The run reported a warning, out of the defined range. Why is that?

Since ints are defined with a signed type before them by default, the first bit on the left is used to represent the sign bit. If 0 is positive, if 1 is negative. So there are only 7 bits in int result, and its maximum value is 2^(4*8-1) -1:2,147,483,647.

Modified as follows:

#include<stdio.h>
#include<math.h>
int main(a){
  unsigned int result = pow(2.32) - 1; // Prefixes int with unsigned
  printf("result is %u\n",result); // change %d to %u
  return 0;
}
Copy the code
[root@localhost day1]$ gcc  test6.c  && ./a.out
result is 4294967295
Copy the code

Normally, the result is 2^8-1:4294967295.

How does a computer store values?

It is stored in the form of complement.

  • Complement of a positive number: The binary form of the number.
  • The complement of negative numbers:
  1. So let’s take the binary of the absolute value of the number, which is the binary of the positive number.
  2. Reverse the value of the first step bitwise.
  3. Add the value of step 2 to 1.

Such as:

7:

0 0 0 0 0 1 1 1

– 7:

  1. Get the binary of 7
0 0 0 0 0 1 1 1
  1. According to the not
1 1 1 1 1 0 0 0
  1. + 1
1 1 1 1 1 0 0 1

conclusion

When the first digit on the left is 0, the more 1s that follow, the higher the value.

When the first digit on the left is 1, the more zeros that follow, the higher the value.

  • Value range of basic data types

Signed 1 byte => -2^(1×8-1) to 2^(1×8-1)-1

Unsigned 1 byte => 0 ~ 2^(1×8)-1

Signed 4 bytes => -2^(4×8-1) to 2^(4×8-1)-1

Unsigned 4 bytes => 0 to 2^(4×8)-1

And so on

Why complement?

Check out the link:

Baidu Encyclopedia: complement code

The complement of two