The main reason why the C language requires mandatory definition of variables (define first, then use) is to facilitate the determination of types and allocation of space. In C language programming, variables must be defined first and then used. Undefined variables or definitions halfway may cause program errors.
In C language, all variables used are required to be mandatory defined, that is, "define first, use later".
In C language, the use of variables must first be defined. Describe its data type. The reasons may be as follows:
1. Different types of variables may have different encoding methods.
2. Different types of variables occupy different sizes of space. Space cannot be allocated in memory without prior explanation.
In C language programming, undefined variables or definitions halfway may cause program errors.
In fact, it is not necessary to define all variables at once in programming, because at the beginning of programming, not everyone can think of all the variables that need to be used at once. Generally, programming thinking is your own During the programming process, think of what variables and what types of variables you need to use, and then define them one by one before using them.
The purpose of the C language book is to emphasize the readability of the program, that is, to allow other people who read the program code to understand what the variable is, what the defined name is, how to assign the value, etc. etc., so it will be written that all variables must be defined at once. In fact, in the completed program, all variables need to be defined at once. It does not require us to define all variables at once during programming. As long as you need to use the variables, you can continue to add them after the few lines of code that define the variables. You don't have to deliberately come up with all the variable names and variable types first.
Related recommendations: c language tutorial video
The above is the detailed content of What is the main reason for requiring mandatory definition of variables in C language?. For more information, please follow other related articles on the PHP Chinese website!