tags:

views:

163

answers:

4

When I let Eclipse create a new file (.c or .h file) in a C project the editor always auto creates a #define at the top of the file like this: If the file is named 'myCFile.c' there will be a #define at the start of the file like this

#ifndef MYCFILE_C_
#define MYCFILE_C_

I have seen other editors do this as well (Codewright and SlikEdit I think). The #defines don't seem to do anything for the editor as I can just delete them without any problem, and I can't think of a reason why I would want to use them. Does anyone know why they are there?

+5  A: 

It's to guard against multiple definitions.

eed3si9n
A: 

I think it's a throwback of C include issues, where multiple copies of the source would get included - unless you are meticulous with include chains (One file includes n others). Checking if a symbol is defined and including only if the symbol is defined - was a way out of this.

Gishu
+1  A: 

Sometimes people include a whole .c file in other .c files (or even .h files), so it has the exact same purpose of preventing an include file from getting included multiple times and the compiler spitting out multiple definition errors.

It is strange, though, that it would be the default behavior of an editor to put this in anything but a .h file. This would be a rarely needed feature.

Jim Buck
+1  A: 

A more modern version of this is to use:

#pragma once

It is quite unusual to see this in a .c file, normally it is in the header files only.

1800 INFORMATION