iOS Learning get started-c-_3

Source: Internet
Author: User

Do not know the example of the previous article has friends to try, the friend should be very confused, why will the error?

Let's compile a few files to see how the header files are imported.

A.c

void Hellow () {printf ("hellow");   }

A.h

" B.h "; void hellow ();

B.c

void Word () {printf ("word");   }

D..

void Word ();

TEST.c

#inculde <stdio.h>"a.h"int  main () {    hellow ();    Word ();       }

Next, compile the test.c as a precompiled file. GCC-E Test.c-o

Use Notepad to open the file, the preceding code regardless of pull to the bottom

You can see the code we wrote. What is the preceding code? The previous big lump is the library file <stdio.h> code, so the include import file can be understood as a copy file come in and look down

# 1 "testA.h" 1 so he copied all the code in TESTA.H:

But the copy of the time and encountered TestA.h inside the # 1 "testB.h" 1 so the TestB.h all the code also came in the following sentence

# 1 "testB.h" 1
void Word ();

At this point, Testa has not finished copying the second line of Testa.

void Hellow (); When the include is copied, the main function starts to compile.

In the eye of a question A.h contains B.h,b.h and contains A.h. What happens when you think about precompilation?

Pre-compiled to A.h found a line of # include "B.h" so to copy B.h, and found that B.h has a line # include "A.h" and to copy A.h, resulting in an infinite loop ...

So how do we solve this problem?

#ifndef Name

#define Name

Header file Contents

#endif

Ifndef is a shorthand for "if not defined" , a macro definition that can be branched based on whether a variable has already been defined.

Define defines a macro endif is the terminating if

Here are some of the personal understanding and test results for the header file

1. When the head file refers to itself or to each other, a dead loop prompts the loop too deep
2. When a and B are included in C compile can pass but the precompiled state can see the C file appears two copies of the program will not be able to run, but slowed the load of the program
3. If a method with the same name in A/b and a compile error (without invoking the compilation pass) is not explicitly defined in the main function call
4.C The program is precompiled to find the target file (*.O) based on the header file, so the target file recompilation does not affect the files it depends on
5. Resolve header file import recurrence or looping issues
#ifndef _test_h_
#define _test_h_
Header file Contents
#endif

--------------------------------------------------------------------------------------------------------------- ------------------

This is where the head file comes in. Previously mentioned macro definition what is a macro definition?

#define A=3; It's a definition of a macro. There's a lot of people who think this is defining a global constant, but he's similar to a constant, and he's going to replace all a in the code with 3, but this is done in the preprocessing phase,

For example, you write int sum=2+a in the code; Then the preprocessing becomes the int sum=2+3;

The constant const a=3 is defined, and the space is handled and allocated by the compiler

Here, by the way, the macro definition function, let's write an example.

#include <stdio.h>#define SUM (A, B) (a*b)int  main () {    int a=5 ;     int b=6;     int result=SUM (a+b,b+a);  
printf ("%d\n", result);}

Dare to guess what the output is?

Copy the code once and the result is not as different as imagined?

If you're confused, compile it into a precompiled file.

Look for it carefully.  Does the macro definition function disappear? Yes, he replaced all the functions that called it with a block of code. This is a fun place to define a macro function.

Now that you know the reason, the solution is a glance.

#define SUM (A, B) ((a) * (b))
So the value of the compiled output is what you expect.

iOS Learning get started-c-_3

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.