Introduction to C language basic data types

Keywords: C Back-end

  The above picture lists the keywords that define data types, and indicates the occupation and value range of different types in 32-bit machines. Next, let's briefly introduce these three data types.

1. Integer: there are two types of integers: signed and unsigned. Signed integers can be regarded as compatible with negative integers, while unsigned integers can only be compatible with positive integers.

① Signed integers can be divided into short int, long int, long int, and int. the row pair comparison is different, and their occupation length and value range in the computer are different. Among signed integers, the value range of short int is the smallest, and the value range of long int is the largest , when editing programs, integers (ints) are generally used as prefixes to define variables or constants without special circumstances. When the amount of calculation is large, long int can be used as the prefix to define data storage variables.

#Include < stdio. H > / / reference header file
int main()                 //Main function
{

short int num=1;          //Define a short integer constant num with a value of 1
long int num2=2;          //Define a long integer constant num2 with a value of 2
long long int num3=521;   //Define a long integer constant num3 with a value of 521
int iNUM=1314;            //Define an integer constant iNum with a value of 1314

return 0;                 //Return 0 after running
}

② Unsigned integers can be subdivided into unsigned short int, unsigned long int, unsigned long int and unsigned int. compared with signed integers, they can't take negative values and positive integers, except for the difference in value range, The placeholder bytes in a computer with the same number of bits correspond. In general, we use signed integers (ints) to define variables and constants.

#Include < stdio. H > / / reference header file
int main()                 //Main function
{

unsigned short int num=1;          //Define an unsigned short integer constant num with a value of 1
unsigned long int num2=2;          //Defines an unsigned long integer constant num2 with a value of 2
unsigned long long int num3=521;   //Define an unsigned long integer constant num3 with a value of 521
unsigned int iNUM=1314;            //Define an unsigned integer constant iNum with a value of 1314

return 0;                 //Return 0 after running
}

2. Real type: the real data type is also a floating-point type, which is mainly used to store high-precision data. Floating-point types are generally divided into single precision floating-point type (float) and double precision floating-point type (double float), both of which are signed.

① Single precision floating point type (float): the single precision floating point type occupies 4 bytes in 32-bit computers, and the value accuracy is six digits after the decimal point.

#Include < stdio. H > / / reference header file
int main()                 //Main function
{

float fNum=521.1314;        //Define a single precision floating-point constant fNum with a value of 521.1314

return 0;                 //Return 0 after running
}

② Double float: double float occupies 8 bytes in 32-bit computers, and the value precision is 15 digits after the decimal point.

#Include < stdio. H > / / reference header file
int main()                 //Main function
{

double float fNum=521.131412242004;        
//Define a single precision floating-point constant fNum with a value of 521.131412242004

return 0;                 //Return 0 after running
}

3. Character type: the character type is divided into signed character type and unsigned character type. Both types occupy one byte in a 32-bit machine. The difference between the two types is that one is signed and the other is unsigned. The value range of unsigned is larger than that of signed. The function of character type is to store characters. It can store symbols, numbers, letters, text ASCII....... Extension: in character type data storage, characters and strings are also subdivided. When the data to be defined is a string, the string shall be contained in a pair of double quotation marks. When the data to be defined is a character, the character shall be contained in a pair of single quotation marks

#Include < stdio. H > / / reference header file
int main()                 //Main function
{

//Define a string constant Array and assign a value: my name is jhx, 16years old
char Array="My name is jhx,16years old";
//Define an unsigned string constant, cararray, with the assignment: Today is 2021.10.30
unsigned char cArray="Today is 2021.10.30";
//Define A character constant temp and assign A value: A
cahr temp='A';
//Define an unsigned character constant cTemp and assign the value: b
unsigned char cTemp='b';

return 0;                 //Return 0 after running
}

4. Summary: in basic data types, we only use integer (int), single precision floating point (float) and character (char) when we knock code in normal training. Other types are not used frequently, but we should also remember their occupation and value range in the computer. The core function of data type is to define the type of constant or variable, Both integer and floating-point data are numbers, while character data are mainly characters, but in the Ascll table, the corresponding numbers correspond to the corresponding characters

#Include < stdio. H > / / reference header file
int main()                 //Main function
{

char array='A'; //Define A character constant array and assign A value: A

int num=array;  //Define an integer variable num and assign the character constant array to the integer variable num
return 0;                 //Return 0 after running
}

Like the above code, the decimal number corresponding to the character constant A in the Ascll table is 65, so when the value A stored in the character constant array is assigned to the integer variable num, the result is num=65.

The next chapter is a preview: C language input, output format control characters and escape characters.

Posted by daedlus on Sat, 30 Oct 2021 11:57:17 -0700