C basic data types

There are three basic data types in C language:

   Silent character type (char)

   despiseInteger (int)

   get angry floating point (float)

The earliest basic data types of the C language are these three. (There are C90, C99, C11 just updated later, and I will talk about it later)

Today's focus is to find the memory overflow problem of the basic data types of C language based on the actual situation.

First, the C language defines the value ranges of several data types in the header file <limits.h>.

char

    int

short long long long
SCHAR_MAX

INT_MAX

SHRT_MAX LONG_MAX LLONG_MAX
SCHAR_MIN INT_MIN SHRT_MIN LONG_MIN LLONG_MIN
UCHAR_MAX UINT_MAX USHRT_MAX ULONG_MAX

ULLONG_MAX

CAHR_MAX        
CHAR_MIN        

        Before understanding the problem of memory overflow, let's supplement the basic knowledge of computer---digital system.

Computers are computing tools abstracted from nature, so we need to store data from nature on the computer before it can perform calculations. So people invented binary storage. (Don't ask me why I don't use decimal, because current is used to represent 0 and 1 in the machine. The high frequency is 1, and the low frequency is 0. It's really embarrassing. I am a scumbag liberal arts student. And the earliest computers were very large, It consumes a lot of power, and the decimal current is very unstable. Not to mention the technical problems, the electricity at that time really couldn't burn this decimal machine. If you are really interested, you can check the information.)

    Regarding the hexadecimal, it is assumed that you know and understand, and can perform simple operations.

    So how to store the data?

    These two we have to mention 10 kinds of people in this world, those who understand binary and those who don't.

    1, the original code: it is directly expressed in binary. (that is, the unsigend number)

        But the question is how to represent negative numbers in binary representation? (like -1, -2)

    Right is to use the highest bit of the four-bit 0000 to represent the sign (0 is positive, 1 is negative)

        For example: 0001(1) + 1001(-1) = 1010(-2) does not hold

                    0000 + 1000 = 1000 (-0) This is totally against natural logic

    However, when the original code represents a signed number, there is a negative zero, which is consistent with our original intention. How to do?


    2, that is the inverse code: regardless of the sign bit of the signed number, the other bits are inverted (0 becomes 1, 1 becomes 0), and the inverse code of the positive number is itself.

    Example: 0001 + 1110 = 1111 If you negate it again, it will become 1000. The result is correct, but negative zero. Don't you think there is something wrong?

    3. Then, the complement code is to add one to the inverse code. (The same positive numbers are not processed)

(Once again, I sighed how smart the boss is, and said that computers don’t need talent, so let’s figure it out for yourself! But we can’t be discouraged.)

Example: 0001 + 1111 = 11110 Processing: remove the highest digit, add 1, negate ————0000 is 0

Well, finally stuffed the elephant into the fridge. Time to close.

Here we only discuss the overflow problem of integer types.

#include <limits.h>
#include <stdio.h>
//Integer memory overflow


int main(void){
	printf(" INT_MAX = %d\n",INT_MAX);//INT_MAX = 2147483647
	printf(" INT_MIN = %d\n",INT_MIN);//INT_MIN = -2147483648
	printf(" INT_MAX + 1 = %d\n",INT_MAX + 1);//INT_MAX + 1 = -2147483648
	printf(" INT_MIN-1 = %d\n",INT_MIN-1);//INT_MIN-1 = 2147483647
	return 0;
}

You see, INT_MAX + 1 = INT_MIN.

You can use the clock to associate this overflow problem, and you will understand it at once.

Twelve o'clock plus one, the pointer will not point to thirteen but one.

Note: int is the default signed integer in C language. (for ease of typing)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326431988&siteId=291194637