C language | summary of data types extracted from three books

preface 👦

The conclusion of this chapter will be summarized in combination with three C language books, describing the basic types of data, the use and scope of data types, when to use which data type is more appropriate.

1, What are the data types of C? What is it? 🥴

C language has only 4 data types:

  • Integer;
  • Floating point type;
  • Pointer;
  • Aggregation type;
  • Other types: all derived from the above four types.

1.1 data type keywords


View size


int main(){
	printf("int:%d bytes\n", sizeof(int));
	printf("char:%d byte\n", sizeof(char));	/* char The number of bytes of type is 1 */
	printf("short:%d bytes\n", sizeof(short));	/* short The number of bytes of type is 2 */
	printf("long:%d bytes\n", sizeof(long));	/* long The number of bytes of type is 4 */
	printf("float:%d bytes\n", sizeof(float));	/* float The number of bytes of type is 4 */
	printf("double:%d bytes\n", sizeof(double));	/* double The number of bytes of type is 8 */
	printf("long double:%d bytes\n", sizeof(long double));	/* long double Type has 8 or 10 or 12 bytes */

	return 0;

Then let's expand according to the above types... [Note: there are many contents of pointer and aggregation, which will be summarized separately later]

2, Introduction to data types 😲

1.1 integer data type

Integers include characters, short integers, integers, and long integers. [some students may wonder why characters belong to integer type???] because computers use digital coding. Commonly used is ASCII encoding, using special integers to represent specific characters. For example, as you will learn later, all the characters read by the file reading function are basically of type int, marked with EOF(-1).

1.1.1 int type


It must be declared before use;

int i;
int k,j;
int m=10;

View integer size

  • int type length is related not only to the specific CPU architecture, but also to the compiler.
  • There are three criteria for determining the int length:
    • ① C/C + + specifies that the int word length is the same as the machine word length;
    • ② The word length of the operating system may not be the same as that of the machine; (the former is less than or equal to the latter)
    • ③ The compiler defines the int word length according to the word length of the operating system;
int main(){
	printf("minimum(Signed)Integer:%d\n", INT_MIN);
	printf("maximum(Signed)Integer:%d\n", INT_MAX);
	printf("maximum(Unsigned)Integer:%d\n", UINT_MAX);


Understand what words are

  • Word is a natural storage unit given when designing a computer;
  • It is a term used to describe computer data unit or storage unit together with bit and byte;
  • 1 is only 8 bits long, and 64 bits have appeared today;
  • The longer the word, the faster the computer data transmission and the more memory allowed to be accessed.

1.1.2 short type

The storage space occupied may be less than that of int type. It is suitable for occasions with small values to save space;

View short integer size


int main(){
	printf("minimum(Signed)Short value:%d\n", SHRT_MIN);
	printf("maximum(Signed)Short value:%d\n", SHRT_MAX);
	printf("maximum(Unsigned)Short value:%d\n", USHRT_MAX);


1.1.3 long type

It may occupy more storage space than int, which is suitable for large numerical occasions;

View long integer size


int main(){
	// Long integer
	printf("minimum(Signed)Long integer:%d\n", LONG_MIN);
	printf("maximum(Signed)Long integer:%d\n", LONG_MAX);
	printf("maximum(Unsigned)Long integer:%d\n", ULONG_MAX);

	// long long 
	printf("minimum(Signed)Long integer:%d\n", LLONG_MIN);
	printf("maximum(Signed)Long integer:%d\n", LLONG_MAX);
	printf("maximum(Unsigned)Long integer:%d\n", ULLONG_MAX);


With the long type, why is there the long type?

At present, computers are generally 64 bit processors, which is introduced in order to store 64 bit integers;
More common: long long(64 bits), long(32 bits), short(16 bits), int(16 bits / 32 bits);

11.4 signed and unsigned keywords

Signed: [optional] you can add a declaration before any signed keyword for emphasis, including negative numbers.
Unsigned: [required] to indicate unsigned, you need to add before the keyword, only 0 and positive numbers;

1.1.5 char type

  • Storing letters or punctuation marks to accommodate string type values;
  • A storage unit stored in 8 bits.

int main(){
	char a = 'AXDF';		// Storage unit stored in 8 bits
	printf("ret: %c\n", a);

// Since the storage unit is 8 bits, only the last 8 bits are valid

View character size


int main(){
	printf("Number of digits of one character:%d\n", CHAR_BIT);
	printf("minimum(Signed)Character type:%d\n", SCHAR_MIN);
	printf("maximum(Signed)Character type:%d\n", SCHAR_MAX);
	printf("maximum(Unsigned)Character type:%d\n", UCHAR_MAX);


1.1.6 _Bool type

It is usually used for logical judgment, that is, true and false, accounting for only 1 bit of storage space;

1.1.7 _Complex type

Plural: float_Complex,double_Complex,long double_Complex;

1.1.8 _ Imaging type

Represents an imaginary number: float_Imaginary,double_Imaginary,long double_Imaginary;

1.1.9 precautions:

  • It cannot be understood literally. Long integers must be longer than short integers, which are not specified in the standard, but cannot be shorter than short integers;
  • Long integers should be at least as long as integers, and integers should be at least as long as short integers;
  • Try not to use unsigned types in code, even if the data represented does not have negative values;
  • Generally, unsigned numbers can be used only when bit segments and binary masks are used;

1.2 floating point data types

1.2.1 float type

  • Floating point type, usually an approximation of the actual value. If 10.0 may store bit 9.99999;
  • Generally, it can be accurate to at least 6 significant digits, and the range is generally 10-37 ~ 10 + 37;

Counting method

  • Number: 12.123;
  • Scientific counting method: 1.2x107;
  • Index counting method: 1.2e7;

int main(){
	float f = 112.231;
	printf("%f\n", f);
	printf("%e\n", f);

View float range

#include <float.h>

int main(){
	printf("minimum float Value:%e\n", FLT_MIN);
	printf("maximum float Value:%e\n", FLT_MAX);
	return 0;

1.2.2 double type

  • It is double precision, accounting for 64 bits, which improves the precision;
  • At least 10 significant digits;
  • There is also a long double to meet the needs of higher precision;

View double range

#include <float.h>

int main(){
	printf("minimum double Value:%e\n", DBL_MIN);
	printf("maximum double Value:%e\n", DBL_MAX);
	return 0;

3, Portability issues in use

In order to unify, some types of functions are the same in each system. In 1999, ANSI introduced C99 and added header files stdint.h and inttypes.h.

Therefore, in the development process, in order to avoid problems in the migration process, try to use the two header files to define variables;

3.1 introduction to header file stdint.h

typedef defines the typeUnsigned typedescribe
intmax_tuintmax_tInteger type of maximum width
int8_tuint8_tAn integer type with a width of 8 bits. For signed types, negative values are represented by the complement of 2
int16_tuint16_tAn integer type with a width of 16 bits. For signed types, negative values are represented by the complement of 2
int32_tuint32_tAn integer type with a width of 32 bits. For signed types, negative values are represented by the complement of 2
int64_tuint64_tAn integer type with a width of 64 bits. For signed types, negative values are represented by the complement of 2
int_least8_tuint_least8_tMinimum 8-bit integer type
int_least16_tuint_least16_tMinimum 16 bit integer type
int_least32_tuint_least32_tMinimum 32-bit integer type
int_least64_tuint_least64_tMinimum 64 bit integer type
int_fast8_tuint_fast8_tMinimum 8-bit integer type
int_fast16_tuint_fast16_tMinimum 16 bit integer type
int_fast32_tuint_fast32_tMinimum 32-bit integer type
int_fast64_tuint_fast64_tMinimum 64 bit integer type
intptr_tuintptr_tInteger type, which can save the value converted from void pointer, and then convert it back to the type of value equal to the original pointer

3.2 portable code shall comply with the following standards

  • Use only identified characteristics;
  • Do not break through any limitations implemented by the compiler;
  • Does not produce any output that depends on compiler defined or undefined or undefined properties.

Tags: C

Posted on Thu, 14 Oct 2021 17:19:45 -0400 by devxtec