473,396 Members | 1,722 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,396 software developers and data experts.

validating problem

Hi all,
I have got a problem where i have to execute
a portion of code only when a header file is
included somewhere in the source file. Can some
body explain how to do it.
for example
if included placebo.h{
execute this part of code
}
Thanks
Divya

Nov 15 '05 #1
11 1264
foodic wrote:
Hi all,
I have got a problem where i have to execute
a portion of code only when a header file is
included somewhere in the source file. Can some
body explain how to do it.
for example
if included placebo.h{
execute this part of code
}
Thanks
Divya


You could make this:

File placebo.h:

#define PLACEBO_H
.....

File whatever.c

....
#ifdef PLACEBO_H

code to execute

#endif
....

Note that this will only work if the header file is included _before_
this construct. This should be no great problem since all your includes
should be at the very start of the file...

Nov 15 '05 #2

foodic wrote:
Hi all,
I have got a problem where i have to execute
a portion of code only when a header file is
included somewhere in the source file. Can some
body explain how to do it.
for example
if included placebo.h{
execute this part of code
}
Thanks
Divya

No,you can't do this directly.However,most of the head files define a
macro to identify itself.For example:
#ifndef _BACKWARD_ALLOC_H
#define _BACKWARD_ALLOC_H 1

Others are like this.
So you can use:
#ifdef _HEAD_FILE_NAME_H
/*do something*/
#endif

Nov 15 '05 #3
Cong Wang wrote on 29/07/05 :
No,you can't do this directly.However,most of the head files define a
macro to identify itself.For example:
#ifndef _BACKWARD_ALLOC_H


Please keep in mind that identifiers beginning with _[_A-Z] belong to
the implementation name space.

#ifndef H_BACKWARD_ALLOC

is fine for a user.
--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Mal nommer les choses c'est ajouter du malheur au
monde." -- Albert Camus.
Nov 15 '05 #4
Cong Wang wrote:
For example:
#ifndef _BACKWARD_ALLOC_H
#define _BACKWARD_ALLOC_H 1


<quote>
The C standard reserves quite a few identifiers, meaning that you must
not create variables, functions, or macros with those names. If you
don't obey the restriction, you can have nasty subtle bugs in your
program. This page lists the reserved identifiers and discusses the
pitfalls.
</quote>

http://web.archive.org/web/200312032...h/c-predef.htm
Nov 15 '05 #5
foodic wrote:
Hi all,
I have got a problem where i have to execute
a portion of code only when a header file is
included somewhere in the source file. Can some
body explain how to do it.
for example
if included placebo.h{
execute this part of code
}


If placebo.h is written well, it will have guards against multiple
inclusion. The header will be wrapped in something like
#if !defined(PLACEBO_H_GUARD)
#define PLACEBO_H_GUARD

/* the header contents go here */
#endif

Look in the header and find the name of the guarding macro. Use it in
your own code:

#if defined(PLACEBO_H_GUARD)
/* code to be used when placebo.h is included */
#else
/* code to be used otherwise */
#endif
Nov 15 '05 #6
Cong Wang wrote:
No,you can't do this directly.However,most of the head files define a
macro to identify itself.For example:
#ifndef _BACKWARD_ALLOC_H

^^
This underscore should not be here unless the header is part of the
implementation.
Nov 15 '05 #7
Martin Ambuhl <ma*****@earthlink.net> writes:
foodic wrote:
I have got a problem where i have to execute
a portion of code only when a header file is
included somewhere in the source file. Can some
body explain how to do it.
for example
if included placebo.h{
execute this part of code
}


If placebo.h is written well, it will have guards against multiple
inclusion. The header will be wrapped in something like
#if !defined(PLACEBO_H_GUARD)
#define PLACEBO_H_GUARD

/* the header contents go here */
#endif

Look in the header and find the name of the guarding macro. Use it in
your own code:

#if defined(PLACEBO_H_GUARD)
/* code to be used when placebo.h is included */
#else
/* code to be used otherwise */
#endif


Yes, that can work, but personally I'd hesitate to use a guarding
macro outside the header that defines it. If I were maintaining the
header itself, I'd feel free to change the name of the guarding macro;
it probably wouldn't occur to me that someone else might be using it.
I don't think of the name of the macro as part of the interface.

It's usually better to use something that *is* part of the interface.
For example, <stdbool.h> provides "__bool_true_false_are_defined".

If the header doesn't provide a testable macro, perhaps you can
prevail on the author to add one (which should be easy if you're the
author). If that's not feasible, you can replace each occurrence of
#include "placebo.h"
with
#include "placebo.h"
#define PLACEBO_INCLUDED

The OP's original question seemed to imply that he wants to do the
test at run time. Since the question of whether the header is
included is determined during compilation, it makes more sense to use
a preprocessor test (#if or #ifdef rather than if(...)).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #8
On 29 Jul 2005 03:12:29 -0700, "foodic"
<di******************@yahoo.co.in> wrote:
Hi all,
I have got a problem where i have to execute
a portion of code only when a header file is
included somewhere in the source file. Can some
body explain how to do it.
for example
if included placebo.h{
execute this part of code
}


You do realize that a header is included when your program is
compiled, not when it is executed?

If you want the code to be compiled only if the header is included,
you can define a macro in the header and use #ifdef to control whether
it is compiled or not.

If you want the code to be present but only executed if the header was
included, I'm not sure. Most of the regulars here recommend against
having a header file define an object or contain executable code.
<<Remove the del for email>>
Nov 15 '05 #9
Martin Ambuhl wrote:

If placebo.h is written well, it will have guards against multiple
inclusion. The header will be wrapped in something like
#if !defined(PLACEBO_H_GUARD)
#define PLACEBO_H_GUARD

/* the header contents go here */
#endif


This is fine for placebo.h, but if there's a header called effect.h,
then a guard name like EFFECT_H_GUARD would clash with reserved E*
from <errno.h>. A better convention would be H_XXXXX.

--
Peter

Nov 15 '05 #10
Peter Nilsson wrote:
Martin Ambuhl wrote:
If placebo.h is written well, it will have guards against multiple
inclusion. The header will be wrapped in something like
#if !defined(PLACEBO_H_GUARD)
#define PLACEBO_H_GUARD

/* the header contents go here */
#endif

This is fine for placebo.h, but if there's a header called effect.h,
then a guard name like EFFECT_H_GUARD would clash with reserved E*
from <errno.h>. A better convention would be H_XXXXX.


I did not propose a "convention" to use in naming a header guard. I
proposed using the header guard that already exists in a well-written
header. If you had read my post, you would know that. You would also
know that the effect.h header guard you don't like could not be in a
well-written header, so is already excluded from consideration. Schmuck.

Nov 15 '05 #11
Martin Ambuhl wrote:
Peter Nilsson wrote:
Martin Ambuhl wrote:
If placebo.h is written well, it will have guards against multiple
inclusion. The header will be wrapped in something like
#if !defined(PLACEBO_H_GUARD)
#define PLACEBO_H_GUARD

/* the header contents go here */
#endif
This is fine for placebo.h, but if there's a header called effect.h,
then a guard name like EFFECT_H_GUARD would clash with reserved E*
from <errno.h>. A better convention would be H_XXXXX.


I did not propose a "convention" to use in naming a header guard.


But you did give a sample consistent with the popular HEADER_H[_...]
convention.
I proposed using the header guard that already exists in a well-
written header.
Yes, and that answered the OP's question. What I was commenting on
was a side issue, an issue that you yourself picked up on in the
OP's original post, namely that certain identifiers are not
appropriate as header guard names.
If you had read my post, you would know that.
Did you read my post and what it said?
You would also know that the effect.h header guard you don't like
could not be in a well-written header,
I know that and you know that, but not all newbies to C reading clc
will know that. Hence my post (and yours.)
so is already excluded from consideration.
By you, not by me.
Schmuck.


<sigh> All I did was to provide further examples of reserved
identifiers which are inapropriate for use as ordinary header
guards. If there was anything wrong with my post from an ISO C
perspective, please supply a chapter and verse correction
thereof.

--
Peter

Nov 15 '05 #12

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

Similar topics

6
by: Alex Bink | last post by:
Hi, I have a validating event on a textbox in which I want to prevent the user to leave the textbox without entering the right data. Only if he clicks on another specific control he is allowed...
4
by: Eric | last post by:
Is there a way to cancel the validating event on the closing event? I have 2 textboxes that I use the validating event to check for numeric data. If I try to close the form without putting a...
0
by: Joe | last post by:
Hi For a while now I have been finding postings of problems with the validating event not firing on controls properly. I too had this problem. The event would fire when clicking on another...
0
by: Matthew | last post by:
All, I have searched google and the newsgroups but can't find anything the same as what I am experiencing (though I may have missed something). I have controls (textboxes) within UserControls...
21
by: Darin | last post by:
I have a form w/ a textbox and Cancel button on it. I have a routine to handle textbox.validating, and I have the form setup so the Cancel button is the Cancel button. WHen the user clicks on...
16
by: Al Santino | last post by:
Hi, It appears displaying a messagebox in a validating event will cancel the subsequent event. In the program below, button 2's click event doesn't fire if you open a dialog box in button 1's...
3
by: TheSteph | last post by:
Hi Experts ! I have a Winform Program in C# / .NET 2.0 I would like to ensure that a value in a TextBox is a valid Int32 when user get out of it (TextBox loose focus)
1
by: =?Utf-8?B?bGpsZXZlbmQy?= | last post by:
I've noticed that controls do not raise a Validating event if they are contained in a ToolStripDropDown via a ToolStripControlHost item. Please run the following sample and follow the instructions...
8
by: Peted | last post by:
I have an amazing problem which i think i have no hope of solving Im working with a c# dot net module that is hosted by and runs under a delphi form envrioment. Dont ask me how this insanity has...
2
by: Peted | last post by:
Hi if i derive a reference to a control on a winform (ie Control activeControl = somecontrol on the form) how can i test if that control has a validating or validated event and more importantly...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.