Data Types in C

Chapter 2: Data Types in C

This chapter mainly describes what identifiers and variables are, introduces the basic data types of C language, and explains the conversion between basic data types in detail.

C Identifier

The name given to a variable or function during programming is an identifier, just like every child’s shoes in our network has a name, and the name is the identifier of the child’s shoes. The identifier of the C language is not free to name, and certain rules must be observed.

The C language stipulates that the identifier can be a string of letters (A to Z, a to z) , numbers (0 to 9) , and underscore _ , and the first character must be a letter or an underscore. There are also the following points to note when using identifiers:

(1) The length of the identifier should preferably not exceed 8 bits, because, in some versions of C, the first 8 bits of the identifier is valid, and when the first 8 bits of the two identifiers are the same, they are considered to be the same identifier.

(2) Identifiers are strictly case sensitive.

(3) It is best to choose meaningful English words to make “seeing the name”.

(4) The identifier cannot be a keyword in the C language. For more information on C language keywords, check out WIKI.


Multiple choice question:

Find out the legal identifiers in the following identifiers

A  $price

B  auto

C  For

D  3sport

Variables and assignments

Variables are quantities that can vary, and each variable has a name (identifier). Variables occupy a certain number of memory locations in memory. Variables must be defined before using variables. Distinguishing between variable names and variable values is two different concepts. It is like: the guest and the room number living in the room are two different concepts.

The general form of variable definition is: data type variable name;

Multiple variables of the same type: data type variable name, variable name, variable name…;

The naming conventions for variable names and identifiers are identical. %d will be explained in detail later.

Note: Continuous assignments are not allowed in the definition, such as int a=b=c=5; it is not legal.

There are two ways to assign a variable: 1. Declare and then assign   2. Declare the value at the same time, for example:

Basic data types

When we play games, the characters in the game are of a type, agile, power, spell, and so on. Similarly, the data in C language is also typed. In C language, data types can be divided into four types: basic data type, constructed data type, pointer type, and empty type.

Here we first explain the simplest and most commonly used integer, Float and character types of the basic data types

Integer data refers to numbers without decimals. There is a lot of information in life that is suitable for using integer data, such as the age of the person, the number of people in the class, the total number of pages in the book, and so on. Therefore, there are many types of integers:

Note: int, short int, long int are different depending on the compilation environment. The short int and long int are at least the range written in the table, but the int is in the table in the 16-bit compilation environment. In addition, the value of the c language int depends on the number of bytes it occupies. Different compilers have different regulations. The ANSI standard defines that int is 2 bytes, TC is in ANSI standard, and its int is 2 bytes. But in VC, an int is 4 bytes.

Floating point data refers to numbers with decimals. There is a lot of information in life that is suitable for using floating point data, such as human weight (unit: kilogram), commodity price, pi, and so on. Because of the difference in precision, it is divided into three types:

Note: There is no string variable in the C language, and the string can only exist in the character array, which will be discussed later.

Formatted output statement

The formatted output statement can also be said to be a placeholder output, which displays various types of data from the computer according to the formatted type and the specified position. The advantage of this is that it is beneficial for the computer to accurately give us the type of data we want.

The format is: printf (“output format”, output);

When the output statement contains ordinary characters, you can use the following format:

Printf(“normal character output formatter”, output item);


The output is: a=10

If you want to output multiple variables and specify the location of the variables, the format characters can also be used together, and the variables need to be separated by commas, such as:


The output is: integer: 10, decimal: 7.60000, character: c

Note: The number of formatters should correspond to the number of variables, constants, or expressions.

Multiple choice question

Float a = 1.7; select the corresponding output statement in the following options

A printf(“%d\n”, a);

B printf(a);

C printf(“%f\n”, a);

D printf(“%c\n”, a);

C Constant

The amount by which a value does not change during program execution is called a constant . C language constants can be divided into direct constants and symbolic constants .

Direct constants, also known as literals, are quantities that can be used directly, without explanation, such as:

  • Integer constant: 13, 0, -13;
  • Floating constants: 13.33, -24.4;
  • Character constants: ‘a’, ‘M’
  • String constant: “I love imooc!”

The following code prints these four direct constants separately:

In C, you can use an identifier to represent a constant, called a symbolic constant. Symbolic constants must be defined before they are used. The general form is:

 #define Identifier constant value

The identifiers of symbolic constants are generally used to use uppercase letters. The identifiers of variables are generally used to distinguish between lowercase letters. Here’s a small example of using symbolic constants:

Note: Constants are immutable

C Automatic type conversion

There is such a word called “the raccoon cat for the prince”, the data type of the variable exists in this case. There are automatic conversions for data types, such as:

Automatic conversion occurs automatically when compiling at different data type operations . The rules for automatic conversion are like the small box can be placed in the big box. The following figure shows the rules for automatic conversion of types.

Convert char type data to int type data and follow the corresponding value in ASCII code. For ASCII code, please check WIKI.

Note: Small bytes can be automatically converted to large bytes, but large bytes cannot be automatically converted to small bytes.

C Forced type conversion

Casting is done by defining a type conversion operation. Its general form is:

 (data type) (expression)

Its role is to cast the result of the expression to the type represented by the type specifier, for example:

Enter the result:

Be aware of the following issues when using casts:

1, data types and expressions must be bracketed, such as (int) (x / 2+ y) written as (int) x / 2+ y is converted x into int type and then divided by 2 and then y Added.

2. After conversion, the type and variable value of the original data will not be changed, and only the temporary conversion will be performed in this operation.

3. The calculation result after the forced conversion does not follow the rounding principle.

Leave a Reply

Your email address will not be published. Required fields are marked *