The second day of learning C language: C language data types and variables (Part 1)

Table of contents:

        1. Introduction to data types

        2.Introduction to signed and unsigned

        3. Value range of data type

1. Introduction to data types

    C language provides rich data types to describe various data in life. Use the integer type to describe integers, the character type to describe characters, and the floating point type to describe decimals. The so-called "type" refers to the common characteristics shared by similar data. Only when the compiler knows the type of data can it know how to operate the data.

    1.1.Character;

1.char
2.[signed] char
3.unsigned char

    1.2. Plastic surgery;

1.Short integer
2.short [int]
3.[signed] short [int]
4.unsigned short [int]

5. Integer type
6.int
7.[signed] int
8.unsigned int

9. Long integer
10.long [int]
11.[signed] long [int]
12.unsigned long [int]

13. Longer integer type, introduced in C99
14.long long [int]
15.[signed] long long [int]
16. unsigned long long [int]

    1.3. Floating point type;

1.float
2.double
3.long double

   1.4.Boolean type;

1. The Boolean type was also introduced in C99, which specifically represents true and false.
2._Bool

3. The use of Boolean type must include the header file <stdbool.h> 

4. The value of a Boolean type variable is: true or false.

5.#define bool _Bool

6.#define false 0

7.#define true 1

// 代码演示
_Bool flag = true;
if (flag)
    printf("I Like C.");

2.Introduction to signed and unsigned

    C language uses [signed] and [unsigned] keywords to modify character types and integer types.

    [signed] keyword: indicates a type with a sign, including negative values.

    [unsigned] keyword: indicates that this type does not have a negative sign and can only represent zero and positive integers.

    For int, it is signed by default, so [int] is also equivalent to [signed int].

    Because it is by default, the keyword [signed] is not written by default, and it is okay if it is written.

    The advantage of declaring an integer variable as unsigned is that the maximum integer value that can be represented by the same length of memory is doubled. For example, the value range of 16-bit [signed short int] is: [-32768~32767], the maximum value is [32767]; and the value range of [unsigned short int] is: [0~65535], the maximum value increases It’s as big as [65,535]. The value range of 32-bit [signedint] can be found in the definition given in limits.h.

    It is recommended to use the Everthing tool here to find it quickly. Official website link: voidtools

     

 3. Value range of data type

     In fact, each data type has its own value range, which is the range of the maximum and minimum values ​​of the stored values. With a rich variety of types, we can choose the appropriate type in the appropriate scenario. If you want to check the limit values ​​of different data types on the current system: the [limits.h] file explains the value range of the integer type; the [float.h] header file explains the value range of the floating-point type.

    For the sake of code portability, when you need to know the limit value of a certain integer type, you should try to use these constants.

  • SCHAR_MIN, SCHAR_MAX: minimum and maximum values ​​of signed char.

  • SHRT_MIN, SHRT_MAX: the minimum and maximum values ​​of short.

  • INT_MIN, INT_MAX: the minimum and maximum value of int.

  • LONG_MIN, LONG_MAX: The minimum and maximum values ​​of long.

  • LLONG_MIN, LLONG_MAX: The minimum and maximum values ​​of long long.

  • UCHAR_MAX: The maximum value of unsigned char.

  • USHRT_MAX: The maximum value of unsigned short.

  • UINT_MAX: The maximum value of unsigned int.

  • ULONG_MAX: The maximum value of unsigned long.

  • ULLONG_MAX: The maximum value of unsigned long long.

This article introduces the data types and variables of C language. Because there are too many knowledge points, it is divided into two articles for our friends who are studying to browse or for those who have forgotten it to remember it. Anyway, it is all It’s dry stuff, and learning is the source. 

Supongo que te gusta

Origin blog.csdn.net/m0_58724783/article/details/131941470
Recomendado
Clasificación