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

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_defined_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_defined_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_defined_var;
#else
extern int multible_defined_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 2313
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_defined_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_defined_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_defined_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_defined_var;
#else
extern int multible_defined_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
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...
11
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>...
15
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...
6
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
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? // //...
6
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...
6
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
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...
46
by: mattia | last post by:
I've see in some code: #ifdef __cplusplus extern "C" { #endif what does it mean? Thanks
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.