473,399 Members | 2,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

novice question

I'm looking at some codes I wrote five years ago when taking a C++
course. Having not touched C++ since, I have forgotten most of it.
Now I can't even comprehend my own codes.

I have a question:

What does these two lines in the attached header file do? I
vaguely remember there is something special about the underscores.

#ifndef __RECORD__
#define __RECORD__

///////////////////////////// Record.h
////////////////////////////////////
// Declare a Record struct to be used in file.h
////////////////////////////////////////////////////////////////////////////
#include "utildll.h"
////////////////////////////////////////////////////////////////////////////
//
// Record struct - contains 4 fields: Name, SSN, Birthdate, State
//
////////////////////////////////////////////////////////////////////////////
#ifndef __RECORD__
#define __RECORD__

struct Record
{
char cName [21],
cSSN [12],
cBirthdate [9],
cState [3];

}; // end of Record structure

#endif
Oct 6 '05 #1
9 1773
John Smith wrote:
I'm looking at some codes I wrote five years ago when taking a C++
course. Having not touched C++ since, I have forgotten most of it.
Now I can't even comprehend my own codes.

I have a question:

What does these two lines in the attached header file do? I
vaguely remember there is something special about the underscores.

#ifndef __RECORD__
#define __RECORD__


a) these two lines are inclusion guards. Everything up to the matching
#endif is included only once.

b) double underscores in identifiers are reserved for the compiler and the
standard library that comes with it. You must not use those in macros of
your own.
Best

Kai-Uwe Bux

Oct 6 '05 #2
* John Smith:

What does these two lines in the attached header file do?

#ifndef __RECORD__
#define __RECORD__
Those are preprocessor directives. Combined, they ensure that the text in
this file is only included once in each compilation unit. #ifndef tells the
preprocessor to pass on subsequent text to the compiler proper only if the
specified symbol isn't yet defined (this effect lasts until the corresponding
#endif), and #define defines the symbol so that next time around, if this file
is included again somewhere later in this compilation unit, the text is
guaranteed to be ignored, not passed on to the compiler proper.

I vaguely remember there is something special about the underscores.


Yes, names with two underscores are reserved for the implementation. So that
name is a bad one. It may happen to be defined by some standard header.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 6 '05 #3
John Smith wrote:
I'm looking at some codes I wrote five years ago when taking a C++
course. Having not touched C++ since, I have forgotten most of it.
Now I can't even comprehend my own codes.

I have a question:

What does these two lines in the attached header file do?
They prevent multiple inclusions of the file, because only the first time
through is __RECORD__ not defined. The #ifndef tests whether it's not
defined, and the #define defines it so the test fails if the file is
included again.
I
vaguely remember there is something special about the underscores.
Yes, you aren't supposed to use leading underscores in names. They are
reserved for the compiler and its supplied libraries' own names.

#ifndef __RECORD__
#define __RECORD__

///////////////////////////// Record.h
////////////////////////////////////
// Declare a Record struct to be used in file.h
//////////////////////////////////////////////////////////////////////////// #include "utildll.h"
//////////////////////////////////////////////////////////////////////////// //
// Record struct - contains 4 fields: Name, SSN, Birthdate, State
//
//////////////////////////////////////////////////////////////////////////// #ifndef __RECORD__
#define __RECORD__
Normally these would be at the very top (without the leading underscores).
Otherwise you might be unnecessarily, or wrongly, including anything that's
above them more than once (in this case the #include "utildll.h").

struct Record
{
char cName [21],
cSSN [12],
cBirthdate [9],
cState [3];

}; // end of Record structure

#endif


DW
Oct 6 '05 #4
John Smith wrote:
I'm looking at some codes I wrote five years ago when taking a C++
course. Having not touched C++ since, I have forgotten most of it.
Now I can't even comprehend my own codes.

I have a question:

What does these two lines in the attached header file do? I
vaguely remember there is something special about the underscores.

#ifndef __RECORD__
#define __RECORD__

#endif


.........The #ifndef directive is mainly used to prevent multiple
definitions in a header file. For eg. consider a header file (with a
function defined in it) included in two other header files or two
source files. This will lead to multiple inclusions of same code (as
seen by compiler).

To avoid this, we use above directives....for the first inclusion the
#ifndef returns true but for second inclusion the #ifndef returns
false.

AFAIK, there is nothing special about underscores, they are used only
to make sure the name used is unique. You can also try MYRECORD.
HTH,
Hemanth

Oct 6 '05 #5
Kev
Kai-Uwe Bux <jk********@gmx.net> wrote in
news:di**********@murdoch.acc.Virginia.EDU:
b) double underscores in identifiers are reserved for the compiler and
the standard library that comes with it. You must not use those in
macros of your own.


I didnt know this. I always had the underscores because thats what I
generally saw often. Usually just the name of the file... minus the dot.
What kind of problem could this potentially cause?
Oct 6 '05 #6
Kev wrote:
Kai-Uwe Bux <jk********@gmx.net> wrote in
news:di**********@murdoch.acc.Virginia.EDU:

b) double underscores in identifiers are reserved for the compiler and
the standard library that comes with it. You must not use those in
macros of your own.

I didnt know this. I always had the underscores because thats what I
generally saw often. Usually just the name of the file... minus the dot.
What kind of problem could this potentially cause?


Double underscores are reserved for the compiler so it can define it's
own macros. If the compiler had defined a macro __RECORD__ that would
interfere with the macro you had defined yourself.

Unfortunately there is a lot of bad C++ code around and a lot of people
copying code. It's better to understand C++ that just copy it, this is a
good place for gaining understanding.

John
Oct 6 '05 #7
"Kai-Uwe Bux" <jk********@gmx.net> wrote in message
news:di**********@murdoch.acc.Virginia.EDU...
b) double underscores in identifiers are reserved for the compiler and the
standard library that comes with it. You must not use those in macros of
your own.


As I understand the standard, a single underscore in the OP's case would
also be out:
17.4.3.1.2
1 Certain sets of names and function signatures are always reserved to the
implementation:
- Each name that contains a double underscore (_ _) or begins with an
underscore followed by an uppercase letter (2.11) is reserved to the
implementation for any use.

DW

Oct 6 '05 #8
Kev
John Harrison <jo*************@hotmail.com> wrote in news:2o41f.10702
$h******@newsfe2-gui.ntli.net:

Unfortunately there is a lot of bad C++ code around and a lot of people
copying code. It's better to understand C++ that just copy it, this is a
good place for gaining understanding.


True. But then we all learn by seeing what others have done. At least in
the beginning. Great artists are no different until they develop their own
skills. In fact there is a controversial theory, with some controversial
'evidence', that the great masters traced much of their work by using
mirrors and projections which were invented around the same time.
Supposedly xplaining why the proportions in their paintings are so perfect.
But I digress ;o)

I suppose it also explains the other examples I have seen that include very
long strings of random number-letter combinations, and why VC++ always does
it for pregenerated headers. As far I can tell from my limited experience
anyways. I have as of yet had no such problems in my wee little programs.
But I shall change them.

Thnx
Oct 6 '05 #9

"Kev" <di**************@pht.zzz> wrote in message
news:Xn**********************@216.168.3.44...
John Harrison <jo*************@hotmail.com> wrote in news:2o41f.10702
$h******@newsfe2-gui.ntli.net:

Unfortunately there is a lot of bad C++ code around and a lot of people
copying code. It's better to understand C++ that just copy it, this is a
good place for gaining understanding.
True. But then we all learn by seeing what others have done.


But we can be misled if we not sure exactly what it is
that those others have done, and in this case exactly
who has done it. You were most likely looking at what
an implementor did. There are rules in place which govern
what an implementor or a program author can do. Some of
these rules reserve certain forms of tokens for the implementor
so that they can be assured that there are no name clashes
(if the program author also adheres to the rules).

At least in
the beginning. Great artists are no different until they develop their own
skills. In fact there is a controversial theory, with some controversial
'evidence', that the great masters traced much of their work by using
mirrors and projections which were invented around the same time.
Supposedly xplaining why the proportions in their paintings are so
perfect.
But I digress ;o)

I suppose it also explains the other examples I have seen that include
very
long strings of random number-letter combinations, and why VC++ always
does
it for pregenerated headers. As far I can tell from my limited experience
anyways. I have as of yet had no such problems in my wee little programs.
But I shall change them.


Such problems don't tend to occur until a critical time
(such as when the program gets demonstrated or delivered
to clients) :-)

-Mike
Oct 6 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Christopher Richards | last post by:
It is possible to be able to receive email alerts (say, from Google News) and publish them to a web page automatically? I am a novice as far as PHP goes, but I can open and write to a file and...
5
by: Marian | last post by:
Hi, I am totaly novice in .NET and I am studying a book about this. There was mentioned "assembly". I did not understand, how function does it has . I would like to know the exact run of code...
2
by: George | last post by:
Is this group for a novice C++ programmers?
21
by: AES/newspost | last post by:
My understanding -- I'm not an expert -- is that on (some? many? all?) standard Internet servers a URL can point to a subdirectory name followed by a backslash, and that links to this URL will...
6
by: ronwer | last post by:
Hello, The title doesn't completely cover the question I have, but it's a bit more complicated problem we have. We are using a database, based on Acces, but developed by a third party...
3
by: herrcho | last post by:
Here is the code.. #define NAME "MEGATHINK, INC" #define ADDRESS "10 Megabuck Plaza" #define PLACE "Megapolis, CA 94904" int main() { starbar(); printf("%s\n",NAME);
2
by: Dmitry Sazonov | last post by:
I'm novice here and I'm sorry for stupid question. We are trying to understand web services architecture, is it better than TIBCO.Randevouz and does webservices fit our needs. I understand, I...
1
by: Christo | last post by:
I have a question please this is just theory at the moment I am a novice java programmer Is it possible for me to write information to a hashmap and then once it has been written to extract a...
1
by: TwistedSpanner | last post by:
Hello all, For the record I am a complete java novice. I have to write a program to generate/output to screen 10 simple maths question and output a final score . The question is as follows ...
9
by: Kelii | last post by:
I've been trying to get this piece to work for a few hours, but have given up. I hope someone out there can help, I think the issue is relatively straightforward, but being a novice, I'm stumped....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.