Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use #include directive correctly?

Tags:

c++

c

Is there any material about how to use #include correctly? I didn't find any C/C++ text book that explains this usage in detail. In formal project, I always get confused in dealing with it.

like image 241
MainID Avatar asked Jan 21 '09 09:01

MainID


People also ask

When should a semicolon be used examples?

A semicolon may be used between independent clauses joined by a connector, such as and, but, or, nor, etc., when one or more commas appear in the first clause. Example: When I finish here, and I will soon, I'll be glad to help you; and that is a promise I will keep.

What is a semicolon example?

When to Use a Semicolon. A semicolon (;) is a punctuation mark that has two main functions: Semicolons separate items in a complex list. For example, The Council is comprised of ten members: three from Sydney, Australia; four from Auckland, New Zealand; two from Suva, Fiji; and one from Honiara, Solomon Islands.


1 Answers

The big one that always tripped me up was this:

This searches in the header path:

#include <stdio.h>

This searches in your local directory:

#include "myfile.h"

Second thing you should do with EVERY header is this:

myfilename.h:

#ifndef MYFILENAME_H
#define MYFILENAME_H
//put code here
#endif

This pattern means that you cannot fall over on redefining the headers in your compilation (Cheers to orsogufo for pointing out to me this is called an "include guard"). Do some reading on how the C compiler actually compiles the files (before linking) because that will make the world of #define and #include make a whole lot of sense to you, the C compiler when it comes to parsing text isn't very intelligent. (The C compiler itself however is another matter)

like image 167
Spence Avatar answered Sep 20 '22 04:09

Spence