473,698 Members | 2,440 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Understanding How To Use #ifdef Constant #define Constant Sequence In Multible Files

Hello,

How is this possible. If I have two separate files that include the
same dot h file as described below why am I allowed to access the same
value for a constant. I compiled this program using a makefile. (Sorry,
I can't post the long makefile or program.)

What the code is suppose to do is: main () includes the file and the
variable multible_define d_var actually gets defined. file2.c will get the
external definition only because now the constant TESTC is defined.

If I fool around with the makefile I can remove the error saying variable
multible_define d_var is multiply defined and the program works fine.

The reason I have to know is if I create another test program and define
just a constant without any other pre-compiler directives involved in just
main() it's undefined in other program files.

file1.c
-------

#include "test.h"

main ()
{
....

t2 ();
}
test.h
------
#ifndef TESTC
#define TESTC
int multible_define d_var;
#else
extern int multible_define d_var;
#endif
file2.c
-------
#include "test.h"
void t2 ()
{
....
}
Thank you,
Christopher Lusardi

P.S.: I apologize if there is a typo in the code I posted.
Jul 22 '05 #1
1 2337
Ian
Christopher M. Lusardi wrote:
Hello,

How is this possible. If I have two separate files that include the
same dot h file as described below why am I allowed to access the same
value for a constant. I compiled this program using a makefile. (Sorry,
I can't post the long makefile or program.)

What the code is suppose to do is: main () includes the file and the
variable multible_define d_var actually gets defined. file2.c will get the
external definition only because now the constant TESTC is defined.

If I fool around with the makefile I can remove the error saying variable
multible_define d_var is multiply defined and the program works fine.

The reason I have to know is if I create another test program and define
just a constant without any other pre-compiler directives involved in just
main() it's undefined in other program files.
put the line

int multible_define d_var;

in one of the c files, not the header!

Ian
file1.c
-------

#include "test.h"

main ()
{
...

t2 ();
}
test.h
------
#ifndef TESTC
#define TESTC
int multible_define d_var;
#else
extern int multible_define d_var;
#endif
file2.c
-------
#include "test.h"
void t2 ()
{
...
}
Thank you,
Christopher Lusardi

P.S.: I apologize if there is a typo in the code I posted.

Jul 22 '05 #2

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

Similar topics

6
2007
by: Jani Yusef | last post by:
I have a HW problem stated as shown at the top of the solution. The thing is is that I am not 100% sure wtf constant memory means. Well, I think I do but I am confused. Does my solution use contant memory in terms of the length of the list i? If so why not and how could I change it to be so? I am sure the solution is O(n) since the list must only iterated once and the dictionary is O(1), correct? Thanks for the help!! #You are given a...
11
5290
by: lovecreatesbeauty | last post by:
Hello experts, Is const_cast only applied to pointers or references? If I have a constant object, then how can I remove constant attribute from it? #include <vector> #include <string> using namespace std;
15
699
by: Scott | last post by:
Hi All, I have the following C code in a header file, outside of any functions: const float X = 50; const float Y = 100 * X; But, when compiling, I get an error: initializer element is not constant
6
74347
by: Michael B Allen | last post by:
Which is the preferred method for preprocessor tests and why? #ifdef XYZ or #if XYZ or #if defined(XYZ) and
1
3216
by: Michael Sgier | last post by:
Hi I get the error: No case-independent string comparison (stricmp, strcasecmp) with the code below. Why...where should stricmp be defined? And how do i get rid of the error on Linux? // // Function portability // #ifndef HAVE_STRICMP
6
26442
by: canoewhiteh2o | last post by:
I am converting a couple of C header files to C#. It is mainly just a bunch C structs but I am not sure how to handle the #ifdef and #ifndef in C#. For example: #ifndef DATE_TIME #define DATE_TIME unsigned long #endif // NOT DATE_TIME The #define DATE_TIME I am handling with: public const UInt32 DATE_TIME;
6
27846
by: anirbid.banerjee | last post by:
Hi, I need to write a macro which would have a lot many conditional #ifdef ... #endif blocks in it. #define _xx_macro (x) { ... \ ... \ /* some code (); */ #ifdef _SOME_STMT \ ... \ ... \
2
1817
by: William.Joseph.Batson | last post by:
Hi, I found the following c++ code, but I need help in understand what it is trying to do: if the caller does this DPRINTF(D_MINI_FS, ("CHXMiniFileSystem()\n")); what will happen according to the following macro?
46
17227
by: mattia | last post by:
I've see in some code: #ifdef __cplusplus extern "C" { #endif what does it mean? Thanks
0
8676
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8608
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9161
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9029
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8867
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7732
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6522
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2006
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.