[c language] data type (including Boolean type) and sizeof keyword

Why does the C language set so many data types?

        We can think backwards from the results of the use of data types.

        The use of data types results in the definition of variables and constants . Variables and constants are stored in memory cells. In order to run the program faster and fully use the memory space, the memory is divided into units, and the size of each unit is one byte . Data is stored in these memory cells. The C language subdivides a variety of data types, and stipulates that each data type occupies a few bytes of memory space , which saves space resources and the types are more in line with people's customary cognition, making it easier for us to remember. Also note that integers and decimals are stored differently in memory, so the C language has integer and floating point types.

        The data types of C language are divided into self-defined data types and built-in data types .

        The design of self-defined data types gives C language more flexibility and scalability. Custom data types include structures, enumerations, and unions.

Introduction to built-in data types >>>

        There are eight types: char character type occupies 1 byte of memory space, short short integer type 2 byte, int integer type 4 byte, long long integer type (under the VS compiler) 4 byte, long long longer integer type 8 byte. float Single-precision floating-point type 4 bytes. double Double-precision floating-point type 8 bytes. There is also a Boolean type. When using it, you need to carefully consider the possible variation range of your data to choose the appropriate data type.

        Because the C language clearly stipulates that the value of 0 is false and the value of non-zero is true , so generally everyone directly uses numbers to indicate true and false. But a more space-efficient and readable way to write it is to use the Boolean type . The specific usage method is as follows.

        The Boolean type is named  _Bool  and its size is 1 byte. Assign any non-zero value to the _Bool type, it will be converted to 1 first, and then the assignment is performed, and it means true. Assigning a value of zero to the _Bool type results in 0, which means false.

        You can use the sizeof operator to calculate the size of data types and variables, and the unit of the return value is bytes.

         Note that the value of true in the figure ends up being 1.

 sizeof operator >>>

        About you may see that some codes can run without parentheses after sizeof, this is because sizeof is an operator, not a library function.

        The use of operators does not require parentheses, such as + - *, but if the operand of sizeof is a data type, parentheses must be added to use it , otherwise the compilation will not pass. The use of functions must have () The parentheses here are also called function call operators.

 

 

The most important words >>>

           If you don’t understand, remember to private message me ^ ^.

        (You can also leave a message directly in the comment area^ ^)

Guess you like

Origin blog.csdn.net/zoe23333/article/details/129709981