473,320 Members | 1,862 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.

Preprocessing

Hi,

I am a bit confuse. If I have the header file hdr.h with the
following:

#ifndef HDR_H
#define HDR_H
#define a 10
#define b 20
#endif

In file1.c:
#include "hdr.h"
/* and other codes to access constant a and b after this */

In file2.c:
#include "hdr.h"
/* and other codes to access constant a and b after this */

If these 2 C files to be compiled in the same project, would it still
legal? Can I get access to both constants in both files?

Thanks.

Jun 29 '06 #1
1 1468


ta*********@yahoo.com wrote On 06/29/06 12:35,:
Hi,

I am a bit confuse. If I have the header file hdr.h with the
following:

#ifndef HDR_H
#define HDR_H
#define a 10
#define b 20
#endif

In file1.c:
#include "hdr.h"
/* and other codes to access constant a and b after this */

In file2.c:
#include "hdr.h"
/* and other codes to access constant a and b after this */

If these 2 C files to be compiled in the same project, would it still
legal? Can I get access to both constants in both files?


Yes and yes. However, I think you do not understand
the nature of a and b. They are not "constants," but
"macros." After the #defines, each appearance of a or b
is recognized as a reference to a macro, and the reference
is replaced by the macro's definition. In this case, the
definitions are numeric constants, but other macros could
be defined differently. HDR_H, for example, is a macro
whose definition is empty: if you were to use HDR_H in the
code, the compiler would recognize it as a macro and replace
it with its definition, namely, nothing at all.

You need to re-read your C textbook until you understand
the difference between a macro and a variable.

--
Er*********@sun.com

Jun 29 '06 #2

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

Similar topics

2
by: Ron | last post by:
The C++ standard ISO/IEC 14882 specifies pp-numbers as follow: pp-number: digit .digit pp-number digit pp-number nondigit pp-number e sign pp-number E sign pp-number .
3
by: Ron | last post by:
Please, consider the following code: const int a = 1; #if a == 1 #define VAR 200 #else #define VAR 100 #endif int main() {
1
by: Alex Sedow | last post by:
Where to get/buy tool for partial preprocessing? 1. What I mean by partial macro preprocessing. This is mode in which almost sources (>99%) is not preprocessed. But some macros (setted by...
12
by: Francois Grieu | last post by:
Can #include safely use a preprocessing token, as in #define HEADERFILE "stdio.h" #include HEADERFILE int main(void) {return printf("Hello, world\n")*0;} TIA, François Grieu
1
by: Xiangliang Meng | last post by:
Hi, all. Recently, I find there is a way in our project to maintain a global set in many files by using preprocessing directives. I'm wondering if we could find a better method for this. Many...
3
by: Frodo Baggins | last post by:
Hi All, I have a piece of code (not written by me) that is failing on compile with the error: pasting "xdr_ndmp_connect_open_request" and "," does not give a valid preprocessing token The...
5
by: cody | last post by:
the following leads to an error (note that TEST is not defined): #if TEST string s = @" #"; // <-- the error is "preprocessing directive expected" #endif also, here we get the same error: ...
4
by: Henrik Goldman | last post by:
I have an application which compiles on a number of different platforms. Now I'm trying to incorporate an external library which is only available to a subset of the platforms that my application...
5
by: Francois Grieu | last post by:
One of the C compiler that I use <OT>(Keil's CX51)</OTbarks at #define a(b) b int main(void){return a( #if 0 #endif 0);} More generally, this compiler seems confused by any preprocessing...
40
by: Bill Cunningham | last post by:
I have been thinking about hiding headers from my compiler's preprocessor and allowing others to be shown. I want to use the most used and add others is necessary. Would this be how it is properly...
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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
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...
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.