473,569 Members | 2,601 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

behavior of define

I was read Linux kernel code [linux-2.6.19] in which I saw few define
macro defines in functions. The snap of the function is following and
file name "err_ev6.c" .

static int
ev6_parse_cbox( u64 c_addr, u64 c1_syn, u64 c2_syn,
u64 c_stat, u64 c_sts, int print)
{
char *sourcename[] = { "UNKNOWN", "UNKNOWN", "UNKNOWN",
"MEMORY", "BCACHE", "DCACHE",
"BCACHE PROBE", "BCACHE PROBE" };
char *streamname[] = { "D", "I" };
char *bitsname[] = { "SINGLE", "DOUBLE" };
int status = MCHK_DISPOSITIO N_REPORT;
int source = -1, stream = -1, bits = -1;

#define EV6__C_STAT__BC _PERR (0x01)
#define EV6__C_STAT__DC _PERR (0x02)
#define EV6__C_STAT__DS TREAM_MEM_ERR (0x03)
#define EV6__C_STAT__DS TREAM_BC_ERR (0x04)
..
..
..
}

I was tested following code.

#define AAA 90

void fun( )
{
#define _AAA 100
}

int main()
{
printf( "%d %d", AAA, _AAA );
return 0;
}

The result was "90 100". I am not able to understand, what is the
difference in use of define macro to defines in Global scoop or with in
Function scoop?

Regards,

-aims

Jan 9 '07 #1
4 2422

Mohammad Omer Nasir napsal:
I was read Linux kernel code [linux-2.6.19] in which I saw few define
macro defines in functions. The snap of the function is following and
file name "err_ev6.c" .

static int
ev6_parse_cbox( u64 c_addr, u64 c1_syn, u64 c2_syn,
u64 c_stat, u64 c_sts, int print)
{
char *sourcename[] = { "UNKNOWN", "UNKNOWN", "UNKNOWN",
"MEMORY", "BCACHE", "DCACHE",
"BCACHE PROBE", "BCACHE PROBE" };
char *streamname[] = { "D", "I" };
char *bitsname[] = { "SINGLE", "DOUBLE" };
int status = MCHK_DISPOSITIO N_REPORT;
int source = -1, stream = -1, bits = -1;

#define EV6__C_STAT__BC _PERR (0x01)
#define EV6__C_STAT__DC _PERR (0x02)
#define EV6__C_STAT__DS TREAM_MEM_ERR (0x03)
#define EV6__C_STAT__DS TREAM_BC_ERR (0x04)
.
.
.
}

I was tested following code.

#define AAA 90

void fun( )
{
#define _AAA 100
}

int main()
{
printf( "%d %d", AAA, _AAA );
return 0;
}

The result was "90 100". I am not able to understand, what is the
difference in use of define macro to defines in Global scoop or with in
Function scoop?

Regards,

-aims
It does not matter, where you #define your macro. When something is
defined inside of function, it is usually #undefined in the same
function (I do not know, ehther it is in your case too), because such
macro is meant only for usage in this function.

Jan 9 '07 #2

Mohammad Omer Nasir wrote:
I was read Linux kernel code [linux-2.6.19] in which I saw few define
macro defines in functions. The snap of the function is following and
file name "err_ev6.c" .

static int
ev6_parse_cbox( u64 c_addr, u64 c1_syn, u64 c2_syn,
u64 c_stat, u64 c_sts, int print)
{
<snip>
#define EV6__C_STAT__BC _PERR (0x01)
<snip>
}

I was tested following code.

#define AAA 90

void fun( )
{
#define _AAA 100
}

int main()
{
printf( "%d %d", AAA, _AAA );
return 0;
}

The result was "90 100". I am not able to understand, what is the
difference in use of define macro to defines in Global scoop or with in
Function scoop?
You mean scope, not scoop.

What is the difference? Nothing at all. Macro substitution (in your
case, replacing all[*] occurances of AAA with 90 and all[*] occurances
of _AAA with 100) is done as a simple textual replacement by the
preprocessor before the compiler gets to see the code.
[*] When I say all, I really mean all occurances of the symbol that
appear *after* the #define.

The preprocessor knows nothing about C++ scope rules. After it
encounters #define AAA 90, it will blindly replace all subsequent
occurances of AAA with 90, regardless of (amongst other things) the
scope of the #define or the symbol.

I'm only guessing, but one possible reason for the style of the code
you found is that, if the macro EV6__C_STAT__BC _PERR is only intended
to be used within the ev6_parse_cbox function, the author may have put
the macro definition inside the function to document that intent to a
human reader. But the preprocessor doesn't know that and, as you have
found, it will replace all subsequent occurances of
EV6__C_STAT__BC _PERR with (0x01) whether they are inside the definition
of ev6_parse_cbox or not.

Two things to note from a C++ point of view:
1. In C++, their is no reason to use a macro for a simple constant like
this. Use const int instead. Then the compiler will be able to respect
all the C++ rules (e.g. scope, type) that the preprocessor knows
nothing about.
2. Don't use names with a double underscore in your own code. They are
reserved for the implementation.

Gavin Deane

Jan 9 '07 #3
Mohammad Omer Nasir wrote:
{
#define _AAA 100
}
UNDEFINED. Underscore followed by uppercase letter is reserved
to the implementation.
>
int main()
{
printf( "%d %d", AAA, _AAA );
return 0;
}

The result was "90 100". I am not able to understand, what is the
difference in use of define macro to defines in Global scoop or with in
Function scoop?
#define has no scope. It's a text substitution that occurs early
on in the program compilation.

const variables, enums, etc... have scope and should be preferred.
Jan 9 '07 #4
Thanks, now all of the points are clear..

regards,

-aims

On Jan 9, 3:28 pm, "Gavin Deane" <deane_ga...@ho tmail.comwrote:
Mohammad Omer Nasir wrote:
I was read Linux kernel code [linux-2.6.19] in which I saw few define
macro defines in functions. The snap of the function is following and
file name "err_ev6.c" .
static int
ev6_parse_cbox( u64 c_addr, u64 c1_syn, u64 c2_syn,
u64 c_stat, u64 c_sts, int print)
{<snip>
#define EV6__C_STAT__BC _PERR (0x01)<snip>


}
I was tested following code.
#define AAA 90
void fun( )
{
#define _AAA 100
}
int main()
{
printf( "%d %d", AAA, _AAA );
return 0;
}
The result was "90 100". I am not able to understand, what is the
difference in use of define macro to defines in Global scoop or with in
Function scoop?You mean scope, not scoop.

What is the difference? Nothing at all. Macro substitution (in your
case, replacing all[*] occurances of AAA with 90 and all[*] occurances
of _AAA with 100) is done as a simple textual replacement by the
preprocessor before the compiler gets to see the code.
[*] When I say all, I really mean all occurances of the symbol that
appear *after* the #define.

The preprocessor knows nothing about C++ scope rules. After it
encounters #define AAA 90, it will blindly replace all subsequent
occurances of AAA with 90, regardless of (amongst other things) the
scope of the #define or the symbol.

I'm only guessing, but one possible reason for the style of the code
you found is that, if the macro EV6__C_STAT__BC _PERR is only intended
to be used within the ev6_parse_cbox function, the author may have put
the macro definition inside the function to document that intent to a
human reader. But the preprocessor doesn't know that and, as you have
found, it will replace all subsequent occurances of
EV6__C_STAT__BC _PERR with (0x01) whether they are inside the definition
of ev6_parse_cbox or not.

Two things to note from a C++ point of view:
1. In C++, their is no reason to use a macro for a simple constant like
this. Use const int instead. Then the compiler will be able to respect
all the C++ rules (e.g. scope, type) that the preprocessor knows
nothing about.
2. Don't use names with a double underscore in your own code. They are
reserved for the implementation.

Gavin Deane
Jan 10 '07 #5

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

Similar topics

3
1635
by: Andrew | last post by:
Hi all , i am supposed to create a program that performs a specific operation to a 2-D matrix whose elements are positive (or zero ). This operation is repeated T times on the matrix and only the final matix is needed . The operation to be perormed is V(t,x,y)=1/2*(sqrt(V(t-1,x-1,y))+sqrt(V(t-1,x-1,y))) . The problem iis that i have (or i...
19
2560
by: E. Robert Tisdale | last post by:
In the context of the comp.lang.c newsgroup, the term "undefined behavior" actually refers to behavior not defined by the ANSI/ISO C 9 standard. Specifically, it is *not* true that "anything can happen" if your C code invokes "undefined behavior". Behavior not defined by the ANSI/ISO C 9 standard may be defined by some other standard (i.e....
66
3014
by: Mantorok Redgormor | last post by:
#include <stdio.h> struct foo { int example; struct bar *ptr; }; int main(void) { struct foo baz; baz.ptr = NULL; /* Undefined behavior? */ return 0;
23
3156
by: Ken Turkowski | last post by:
The construct (void*)(((long)ptr + 3) & ~3) worked well until now to enforce alignment of the pointer to long boundaries. However, now VC++ warns about it, undoubtedly to help things work on 64 bit machines, i.e. with 64 bit pointers. In the early days of C, where there were problems with the size of int being 16 or 32 bits, the response...
6
5955
by: Samuel M. Smith | last post by:
I have been playing around with a subclass of dict wrt a recipe for setting dict items using attribute syntax. The dict class has some read only attributes that generate an exception if I try to assign a value to them. I wanted to trap for this exception in a subclass using super but it doesn't happen. I have read Guido's tutorial on new...
12
6503
by: Laurent Deniau | last post by:
I was playing a bit with the preprocessor of gcc (4.1.1). The following macros expand to: #define A(...) __VA_ARGS__ #define B(x,...) __VA_ARGS__ A() -nothing, *no warning* A(x) -x B() -nothing, *warning ISO C99 requires rest arguments to be used*
3
1711
by: Mike Cain | last post by:
I have an odd situation I'm trying to understand..... I'm using MS VS 7.0 C++ with the MFC CBuffer class. If I do this: #define NUM_ELEMENTS 2 CBuffer<char, 512 x; CBuffer<char, 512 y; So as you can see I have two variables which will hold strings, each of
28
2514
by: v4vijayakumar | last post by:
#include <string> #include <iostream> using namespace std; int main() { string str; str.resize(5); str = 't';
2
2157
by: Chris Thomasson | last post by:
I was wondering if the 'SLINK_*' and 'SLIST_*' macros, which implement a simple singly-linked list, will produce _any_ possible undefined behavior: ____________________________ #include <stdio.h> #include <stdlib.h> #include <assert.h>
4
1609
by: er | last post by:
hi, the code below generates the following behavior. cld someone please help understand it? 1) clean project + build project generates build errors (see bottom) 2) build a second time, errors disappears, run: ok 3) uncomment and comment , no problem 4) uncomment and , also no problem #ifndef A_IMPL_H_
0
7701
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...
0
7615
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...
0
7924
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. ...
0
8130
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...
1
7677
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...
0
7979
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...
0
5219
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...
0
3653
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...
1
1223
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.