LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-18-2005, 08:02 PM   #1
KissDaFeetOfSean
LQ Newbie
 
Registered: Jun 2005
Posts: 27

Rep: Reputation: 15
#ifndef


Hi,
I know this is probably a stupid question, but can someone please explain in plain English what the purpose of "#ifndef" is? I.e., say we have a C header file like this, named "header.h":

#ifndef MY_HEADER_FILE
#define MY_HEADER_FILE

/* some function declarations */

#endif

Is this merely to avoid duplication of including header files? And why the capital letters for the MY_HEADER_FILE? Thanks.

Sean
 
Old 07-18-2005, 08:34 PM   #2
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
Re: #ifndef

Quote:
Originally posted by KissDaFeetOfSean
Hi,
I know this is probably a stupid question, but can someone please explain in plain English what the purpose of "#ifndef" is? I.e., say we have a C header file like this, named "header.h":

#ifndef MY_HEADER_FILE
#define MY_HEADER_FILE

/* some function declarations */

#endif

Is this merely to avoid duplication of including header files? And why the capital letters for the MY_HEADER_FILE? Thanks.

Sean
Yeah. This means if MY_HEADER_FILE is not defined then continue else ommit the part between
ifndef ......endif

In the case you have posted, it is to avoid including the same header files multiple times.
I'm don't think capital letters are needed, but this is a notation used for many years, so keep it this way. In the procompiler commands it's consider good style to use UPPERCASE

Last edited by perfect_circle; 07-18-2005 at 08:38 PM.
 
Old 07-19-2005, 11:18 AM   #3
amos
Member
 
Registered: Dec 2002
Location: Bolton, UK
Distribution: Kubuntu
Posts: 224

Rep: Reputation: 30
Plus if the file is called header.h then it is good practice to do

#ifndef HEADER_H

that way you don't accidently have the same capitalised name in two header files.

Cheers
Amos
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
what does #ifndef and #endif means ??? indian Programming 4 03-06-2005 09:51 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration