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

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 5517
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
by: William | last post by:
In Peer.h, I have: class Peer { // ... }; In Overseer.h, I have: #include "Peer.h" #include <vector>
6
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. ...
5
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...
23
by: NotYetaNurd | last post by:
for(int i=0;i<6;i++) { // } wont i go out of scope after the for loop ...?
165
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...
39
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...
1
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...
3
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...
5
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...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.