473,657 Members | 2,595 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Scope of #define

What is the scope of #define statement?
Say we have an inlude file as follows

---MyInclude1.h---
#ifndef MYINCLUDE1_H
#define MYINCLUDE1_H

#define EXAMPLE1 1000
#define EXAMPLE2 2000
#define EXAMPLE3 "Hello World"

#endif
---End of MyInclude1.h---

---MyInclude2.h---
#ifndef MYINCLUDE2_H
#define MYINCLUDE2_H

#define EXAMPLE1 "ABC"
#define EXAMPLE2 "DEF"
#define EXAMPLE3 3000

#endif
---End of MyInclude2.h---

If we include the MyInclude1.h and MyInclude2.h into my main program what
would be the value of EXAMPLE1,2,3 ?
Jul 22 '05 #1
2 5537
Kay wrote:
What is the scope of #define statement?
Don't you mean, what's the lifetime of the macro it defines? If so,
it's from the point of definition until the #undef for the same macro
or until the end of the translation unit preprocessing.
Say we have an inlude file as follows

---MyInclude1.h---
#ifndef MYINCLUDE1_H
#define MYINCLUDE1_H

#define EXAMPLE1 1000
#define EXAMPLE2 2000
#define EXAMPLE3 "Hello World"

#endif
---End of MyInclude1.h---

---MyInclude2.h---
#ifndef MYINCLUDE2_H
#define MYINCLUDE2_H

#define EXAMPLE1 "ABC"
#define EXAMPLE2 "DEF"
#define EXAMPLE3 3000

#endif
---End of MyInclude2.h---

If we include the MyInclude1.h and MyInclude2.h into my main program what
would be the value of EXAMPLE1,2,3 ?


What would be the value where? How do you include?

V
Jul 22 '05 #2
On Fri, 16 Jul 2004 12:29:33 -0700, Kay wrote:
What is the scope of #define statement?
It is a simple text replacement. There is no scope at all.
Say we have an inlude file as
follows

---MyInclude1.h---
#ifndef MYINCLUDE1_H
#define MYINCLUDE1_H

#define EXAMPLE1 1000
#define EXAMPLE2 2000
#define EXAMPLE3 "Hello World"

#endif
---End of MyInclude1.h---

---MyInclude2.h---
#ifndef MYINCLUDE2_H
#define MYINCLUDE2_H

#define EXAMPLE1 "ABC"
#define EXAMPLE2 "DEF"
#define EXAMPLE3 3000

#endif
---End of MyInclude2.h---

If we include the MyInclude1.h and MyInclude2.h into my main program
what would be the value of EXAMPLE1,2,3 ?


You would be redefining the macros.

By the way, your compiler must have an option to show you what the
preprocessed output looks like. With g++, it is -E:

g++ -E mycode.cppb

Try the following definitions instead of macros:

int const example1 = 1000;
int const example2 = 2000;
char const * const example3 = "Hello World";

Ali
Jul 22 '05 #3

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

Similar topics

5
27974
by: William | last post by:
In Peer.h, I have: class Peer { // ... }; In Overseer.h, I have: #include "Peer.h" #include <vector>
6
2268
by: Joe Molloy | last post by:
Hi, I'm wondering is there any way I can get a variable's value from within a class when the variable has been declared outside the class but in the same script as the class is contained in. For example, say I have the following script <?php constant myvar = "my original string";
5
2085
by: pembed2003 | last post by:
Hi all, I am reading the book "C How to Program" and in the chapter where it discuss scope rule, it says there are four scopes for a variable: function scope file scope block scope function-prototype scope I think(might be wrong):
23
1698
by: NotYetaNurd | last post by:
for(int i=0;i<6;i++) { // } wont i go out of scope after the for loop ...?
165
6827
by: Dieter | last post by:
Hi. In the snippet of code below, I'm trying to understand why when the struct dirent ** namelist is declared with "file" scope, I don't have a problem freeing the allocated memory. But when the struct is declared in main (block scope) it will segfault when passing namelist to freeFileNames().
39
3175
by: utab | last post by:
Dear all, Is there a clear distinction how to decide which functions to be members of a class and which not How is your attitude (Your general way from your experiences ...) "If the function changes the state of the object, it ought to be a member of that object." Reference Accelerated C++, A. Koenig, page 159.
1
25664
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause bad breath)? Scope describes the context in which a variable can be used. For example, if a variable's scope is a certain function, then that variable can only be used in that function. If you were to try to access that variable anywhere else in...
3
16542
by: psroga | last post by:
Can anyone look at this code and let me know why pthread_mutex_unlock and pthread_mutex_lock are giving me the "phtread_mutex_unlock" was not defined in this scope error. I am compiling the code on g++. # g++ -v Using built-in specs. Target: i386-redhat-linux Configured with: ../configure --prefix=/usr --mandir=/usr/share/man -- infodir=/usr/share/info --enable-shared --enable-threads=posix -- enable-checking=release --with-system-zlib...
5
6129
by: chromis | last post by:
Hi there, I've recently been updating a site to use locking on application level variables, and I am trying to use a commonly used method which copies the application struct into the request scope. Application variables are then accessed in this manner Request.App.<Var>. To begin with I had a simple functioning login system inside a subdirectory named admin, this subdirectory had it's own application.cfm, I wasn't sure whether to duplicate...
0
8397
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...
1
8503
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
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
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4158
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4315
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2731
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1957
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1620
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.