[Post] C header file (. h) function of C header file (. h)

Source: Internet
Author: User
Function of C header file (. h)

I have not yet found a thorough description of the usage of. H files in C language. In actual application, you can only draw images from the gourd tree, but you do not know why. It is very depressing! If you have nothing to worry about, sort out the network-related content to deepen your understanding.

Theoretical Overview:
In. H, the declaration of variables, arrays, and functions defined in the. c file of the same name is usually put. The declaration must be used externally by. C.

1) role of the H file:

1. Convenient development: contains the common constants, structures, types, functions, and variable declarations required by some files;

2. Enable the function scope to start from the position of the function declaration, rather than the position defined by the function (practice summary)

3. Interfaces: an interface (for example, stdio. h) that can be provided to the outside world for a software package ).

2) what should be included in the H file: constants, structures, Type Definitions, functions, and variable declarations.

3) The hfile should not have any: variable definition, function definition.

4) extern problem:
1. extern is required for variables;

2. For a function, the default state of the function is extern. If a function is to be changed to be visible only in the file, add static.

5) include: Although declarations and type definitions can be repeated, Conditional compilation is recommended.
# Ifndef _ filename_h,
# DEFINE _ filename_h
......

# Endif

Summary:
First look at the simplestProgram: Hello World

1 /*Test1.c*/
2Main ()
3{
4Printf ("Hello world! \ N");
5}

Note that there is no. h file in test1, and the compilation can be successful. Modify the program, as shown below:

1 /*Test2.c*/
2Prtstr ()
3{
4Printf ("Hello world! \ N");
5}
6Main ()
7{
8Prtstr ();
9}

There is still no. h file in test2.c, and the compilation can still be successful. Next, modify the program:

1 /*Test3.c*/
2Main ()
3{
4Prtstr ();
5}
6
7Prtstr ()
8{
9Printf ("Hello world! \ N");
10}

There is still no. h file in test3.c. Compilation failed → _ →. Does the function position affect the compilation process? Now let's get familiar with the concept in C language: scope.

Here we will only talk about. the top-level scope related to hware. The top-level scope is extended from the declaration point to the end of the source program text. For the prtstr () function, there is no separate declaration, only definition, it starts from the defined row to the end of the file. That is to say, it is already in its scope at the reference point of main () function of test2.c. The reference point of main () function in test3.c is not its scope, so compilation will fail. What should I do in this case? There are two methods. One is to let us go back to test2.c. The order is nothing for us. Who should be different first? As long as the program can be compiled and run, let main () always put the file to the end. Let's look at another routine. Let's see if this method works at any time.

1 /*Test4.c*/ 
2Play2 ()
3{
4Play1 ();
5}
6
7Play1 ()
8{
9Play2 ();
10}
11
12Main ()
13{
14Play1 ();
15}

This is a frequently usedAlgorithm, Function nesting. Which of the following functions is play1 and play2 in front? In this case, we need to use the second method and the Declaration.

1 /*Test5.c*/ 
2Play1 ();
3Play2 ();
4
5Play2 ()
6{
7Play1 ();
8}
9Play1 ()
10{
11Play2 ();
12}
13Main ()
14{
15Play1 ();
16}

A large software project may have thousands or tens of thousands of plays, not just play1 and play2, so there may be n play1 (); play2 (); in this statement, we need to find a way to manage play1 (); play2 (); instead of putting it in. c file, so. h file appears.

 1   /*  Test. h  */ 
2 Play1 ();
3 Play2 ();
4 /* Test6.c */
5 # Include "test. h"
6 Play2 ()
7 {
8 Play1 ();
9 }
10 Play1 ();
11 {
12 Play2 ();
13 }
14 Main ()
15 {
16 Play1 ();
17 }

The above is the most basic function of the. h file.

Original post address: http://www.cnblogs.com/polestar/archive/2012/02/24/2366724.html

I have not yet found a thorough description of the usage of. H files in C language. In actual application, you can only draw images from the gourd tree, but you do not know why. It is very depressing! If you have nothing to worry about, sort out the network-related content to deepen your understanding.

Theoretical Overview:
In. H, the declaration of variables, arrays, and functions defined in the. c file of the same name is usually put. The declaration must be used externally by. C.

1) role of the H file:

1. Convenient development: contains the common constants, structures, types, functions, and variable declarations required by some files;

2. Enable the function scope to start from the position of the function declaration, rather than the position defined by the function (practice summary)

3. Interfaces: an interface (for example, stdio. h) that can be provided to the outside world for a software package ).

2) what should be included in the H file: constants, structures, Type Definitions, functions, and variable declarations.

3) The hfile should not have any: variable definition, function definition.

4) extern problem:
1. extern is required for variables;

2. For a function, the default state of the function is extern. If a function is to be changed to be visible only in the file, add static.

5) include: Although declarations and type definitions can be repeated, Conditional compilation is recommended.
# Ifndef _ filename_h,
# DEFINE _ filename_h
......

# Endif

Summary:
First look at the simplest program: Hello World

 
1 /*Test1.c*/
2Main ()
3{
4Printf ("Hello world! \ N");
5}

Note that there is no. h file in test1, and the compilation can be successful. Modify the program, as shown below:

1 /*Test2.c*/
2Prtstr ()
3{
4Printf ("Hello world! \ N");
5}
6Main ()
7{
8Prtstr ();
9}

There is still no. h file in test2.c, and the compilation can still be successful. Next, modify the program:

1 /*Test3.c*/
2Main ()
3{
4Prtstr ();
5}
6
7Prtstr ()
8{
9Printf ("Hello world! \ N");
10}

There is still no. h file in test3.c. Compilation failed → _ →. Does the function position affect the compilation process? Now let's get familiar with the concept in C language: scope.

Here we will only talk about. the top-level scope related to hware. The top-level scope is extended from the declaration point to the end of the source program text. For the prtstr () function, there is no separate declaration, only definition, it starts from the defined row to the end of the file. That is to say, it is already in its scope at the reference point of main () function of test2.c. The reference point of main () function in test3.c is not its scope, so compilation will fail. What should I do in this case? There are two methods. One is to let us go back to test2.c. The order is nothing for us. Who should be different first? As long as the program can be compiled and run, let main () always put the file to the end. Let's look at another routine. Let's see if this method works at any time.

1 /*Test4.c*/ 
2Play2 ()
3{
4Play1 ();
5}
6
7Play1 ()
8{
9Play2 ();
10}
11
12Main ()
13{
14Play1 ();
15}

This is a frequently used algorithm, nested functions. Which of the following functions is play1 and play2 in front? In this case, we need to use the second method and the Declaration.

1 /*Test5.c*/ 
2Play1 ();
3Play2 ();
4
5Play2 ()
6{
7Play1 ();
8}
9Play1 ()
10{
11Play2 ();
12}
13Main ()
14{
15Play1 ();
16}

A large software project may have thousands or tens of thousands of plays, not just play1 and play2, so there may be n play1 (); play2 (); in this statement, we need to find a way to manage play1 (); play2 (); instead of putting it in. c file, so. h file appears.

 1   /*  Test. h  */ 
2 Play1 ();
3 Play2 ();
4 /* Test6.c */
5 # Include "test. h"
6 Play2 ()
7 {
8 Play1 ();
9 }
10 Play1 ();
11 {
12 Play2 ();
13 }
14 Main ()
15 {
16 Play1 ();
17 }

The above is the most basic function of the. h file.

Original post address: http://www.cnblogs.com/polestar/archive/2012/02/24/2366724.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.