In the past, when I was in school, the teacher taught us not to forget to include header files when writing C language. Here, the extension is. H files, which have never understood this mechanism before, always think that this is not customizable and can only contain library header files. But today, I was a bit confused. I wanted to try my guess. So I got up and wrote a few small words in the morning.CodeI want to verify whether my thoughts are correct.
Let's take a look at my experiment code:
The first is the masterProgramSource File Name: Main. c. It is stored in any directory in any system. Because it is tested on WINXP and the compiling environment is wintc, therefore, ensure that the length of each level of directory name (Folder name) in the compilation path is no longer than 8 characters, and ensure that the path cannot contain UNICODE character encoding (I'm not sure about this, because there are no tests for Japanese, Korean, and so on, this is only the case in the Chinese environment), that is, the directory name cannot contain Chinese characters.
Exp1:
/***************************
Include learning and testing code containing commands
***************************/
# Include <stdio. h>
# Inlcude "C: \ define. H"
Int main (INT argc, char * argv [])
{
Printf ("% lD", uintmax );
Printf ("\ nthe size of int in this system is: % d", intlen );
Getch ();
Return 0; // This place usually has a big impact on the environment script, but it does not seem to have a big impact on its own programs. It is estimated that the compiler does not have strict requirements on this, otherwise, the compiler will automatically add this statement,
}
The following is my define. h file, which is stored in the C root directory:
# Define size sizeof (INT)
# If 4 = size
# Define uintmax 4294967295
# Define intlen 4
# Else
# Define uintmax 65535
# Define intlen 2
# Endif
After the program is compiled and run, the results are as follows)
65535
The size of the int in this systemis: 2
As I guess, the include command can contain the header file defined by the programmer, not just the header file included in the compiling environment.
So I guess: If I use define. h file. whether the c file is successful, the result is also successful, and the running result is the same as the previous one; in this case, I will go into a step to guess, I will change the file name to .txt to test, the result is also successful.
Through the above experiment, I came to the conclusion that the file contained in the include command has little to do with the file extension, as long as the file content is described according to the C language specification, at the same time, the content encoding of the stored file is based on the text file encoding.
Next we will discuss the preprocessing mechanism:
1. include command
First, an Include statement is not a C statement. But if include is used in C source code, why isn't it a C statement? In the C Specification, C statements are clearly defined as semicolons (;) and end with C statements.
I personally think that include provides a file search function for programmers. This search process is carried out under the path specified by programmers, when a specified file exists, the programmer copies the content of the file to the source code and compiles it.
2. Differences between include <> and include ""
<> Indicates that the search path is searched in the environment variable set in the compiling environment.
"" Indicates to search in the path set by the environment variable. If not, search in the User-Defined directory.
3. Let's talk about the above Code.
# If
# Else
# Endif
The use of this preprocessing command is similar to that of if... else... the statement is used in the same way, but the format is a little different. At the same time, this statement can be used in the main function. I didn't know it when I went to school (I learned automation, not a computer Major, the teacher did not teach and used the textbooks prepared by his teacher. The recommended reference book is the second edition of Tan haoqiang, which is not used in this example)
And we found that:
# If
# Else
# Endif's judgment condition does not need to be enclosed by (). I have tested it. If it is included, it can also be included with if... else... the statement is the biggest difference, and all others are the same.