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

What's this in C?

static void (DEVICE_REQUEST)(void);

What does this line declare in C?
I am always meeting some codes like this,but I have never learnt that
before.
It looks like a declaration of a function,but why DEVICE_REQUEST has to
be quoted by ( ).
Anyone can explain that to me? thanks a lot!

Mar 7 '06 #1
17 2004
zh*********@gmail.com wrote:
static void (DEVICE_REQUEST)(void);

What does this line declare in C?
I am always meeting some codes like this,but I have never learnt that
before.
It looks like a declaration of a function,but why DEVICE_REQUEST has to
be quoted by ( ).
Anyone can explain that to me? thanks a lot!


Well, it would have helped a lot if you showed what DEVICE_REQUEST is.

One reason it might be in parentheses is if it's #defined as:

#define DEVICE_REQUEST *some_name

Parenthese then make sure your original line declares `some_name` as a
pointer to a function which takes no parameters and returns void
(nothing):

static void (*some_name)(void);

It is not a declaration of a function, but a pointer you can use to
call a function.

Is this the answer you were looking for? If not, let us know what
DEVICE_REQUEST is.

--
BR, Vladimir

Mar 7 '06 #2
zh*********@gmail.com wrote:
static void (DEVICE_REQUEST)(void);

What does this line declare in C?
I am always meeting some codes like this,but I have never learnt that
before.
It looks like a declaration of a function,but why DEVICE_REQUEST has to
be quoted by ( ).


It is. The parentheses are optional, but if a function-like macro named
DEVICE_REQUEST is defined, the parentheses prevent macro substitution.

--
Thad
Mar 7 '06 #3
zh*********@gmail.com said:
static void (DEVICE_REQUEST)(void);

What does this line declare in C?
A function called DEVICE_REQUEST, which takes no parameters and returns no
value, and which has internal linkage.
I am always meeting some codes like this,but I have never learnt that
before.
It looks like a declaration of a function,but why DEVICE_REQUEST has to
be quoted by ( ).


If some clown has done

#define DEVICE_REQUEST(x) someexpressioninvolving x

the parentheses stop the preprocessor from performing the usual
substitution. The definition and call would have to be similarly
parenthesised:

void (DEVICE_REQUEST)(void)
{
;
}

int main(void)
{
(DEVICE_REQUEST)();
return 0;
}

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Mar 7 '06 #4

<zh*********@gmail.com> wrote in message
news:11**********************@z34g2000cwc.googlegr oups.com...
static void (DEVICE_REQUEST)(void);

What does this line declare in C?
I am always meeting some codes like this,but I have never learnt that
before.
It looks like a declaration of a function,but why DEVICE_REQUEST has to
be quoted by ( ).
Anyone can explain that to me? thanks a lot!


Yes. It prevents DEVICE_REQUEST from being redefined by a macro. This is
frequently done in system libraries.

Rod Pemberton

Mar 8 '06 #5
"Rod Pemberton" <do*********@sorry.bitbucket.cmm> writes:
<zh*********@gmail.com> wrote in message
news:11**********************@z34g2000cwc.googlegr oups.com...
static void (DEVICE_REQUEST)(void);

What does this line declare in C?
I am always meeting some codes like this,but I have never learnt that
before.
It looks like a declaration of a function,but why DEVICE_REQUEST has to
be quoted by ( ).
Anyone can explain that to me? thanks a lot!


Yes. It prevents DEVICE_REQUEST from being redefined by a macro. This is
frequently done in system libraries.


It prevents any definition of DEVICE_REQUEST as a function-like macro
from being expanded at that point. If DEVICE_REQUEST is an
object-like macro, it will still be expanded. You're probably right
about the intent in this case, but it's impossible to tell without
seeing how and whether DEVICE_REQUEST is defined.

(If DEVICE_REQUEST *isn't* a macro, naming it in all-caps is probably
poor style.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 8 '06 #6
"Rod Pemberton" <do*********@sorry.bitbucket.cmm> writes:
<zh*********@gmail.com> wrote in message
static void (DEVICE_REQUEST)(void);


It prevents DEVICE_REQUEST from being redefined by a macro.


This could be better phrased. In fact, it doesn't prevent any
such definition, it just prevents the function-like macro named
DEVICE_REQUEST, if any, from being expanded at that point in the
code. (If DEVICE_REQUEST is the name of a macro that does not
take arguments, it will still be expanded.)
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Mar 8 '06 #7

"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
"Rod Pemberton" <do*********@sorry.bitbucket.cmm> writes:
<zh*********@gmail.com> wrote in message
news:11**********************@z34g2000cwc.googlegr oups.com...
static void (DEVICE_REQUEST)(void);

What does this line declare in C?
I am always meeting some codes like this,but I have never learnt that
before.
It looks like a declaration of a function,but why DEVICE_REQUEST has to
be quoted by ( ).
Anyone can explain that to me? thanks a lot!


Yes. It prevents DEVICE_REQUEST from being redefined by a macro. This is frequently done in system libraries.


It prevents any definition of DEVICE_REQUEST as a function-like macro
from being expanded at that point. If DEVICE_REQUEST is an
object-like macro, it will still be expanded. You're probably right
about the intent in this case, but it's impossible to tell without
seeing how and whether DEVICE_REQUEST is defined.


You, Ben and Thad, make a distinction between a "function-like macro" and an
"object-like macro."

P.J. Plauger's exact wording on how this works (or is supposed to work):
"In the definition of a visible library function, the function name is
surrounded by parenthesis. ... Any such function can have its declaration
masked by a macro definition in its corresponding header. The parenthesis
prevent the translator from recognizing the macro and expanding it."

He makes no distinction between the two as far as I can tell. So, where
exactly do you get this?
Rod Pemberton
Mar 8 '06 #8
"Rod Pemberton" <do*********@sorry.bitbucket.cmm> writes:
[...]
You, Ben and Thad, make a distinction between a "function-like macro" and an
"object-like macro."

P.J. Plauger's exact wording on how this works (or is supposed to work):
"In the definition of a visible library function, the function name is
surrounded by parenthesis. ... Any such function can have its declaration
masked by a macro definition in its corresponding header. The parenthesis
prevent the translator from recognizing the macro and expanding it."

He makes no distinction between the two as far as I can tell. So, where
exactly do you get this?


C99 6.10.3.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 8 '06 #9
Thanks everyone!
I also found these codes:
#define DEVICE_REQUEST do_hd_request

and do_hd_request is a function like this:
void do_hd_request(void)
{
...........
}

Mar 8 '06 #10
Thanks everyone!
I also found these codes:
#define DEVICE_REQUEST do_hd_request

and do_hd_request is a function like this:
void do_hd_request(void)
{
...........
}

Mar 8 '06 #11
"zh*********@gmail.com" <zh*********@gmail.com> writes:
Thanks everyone!
I also found these codes:
#define DEVICE_REQUEST do_hd_request

and do_hd_request is a function like this:
void do_hd_request(void)
{
..........
}


Please read <http://cfaj.freeshell.org/google/>. Tell your friends.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 8 '06 #12
On Tue, 7 Mar 2006 20:46:27 -0500, "Rod Pemberton"
<do*********@sorry.bitbucket.cmm> wrote in comp.lang.c:

"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
"Rod Pemberton" <do*********@sorry.bitbucket.cmm> writes:
<zh*********@gmail.com> wrote in message
news:11**********************@z34g2000cwc.googlegr oups.com...
> static void (DEVICE_REQUEST)(void);
>
> What does this line declare in C?
> I am always meeting some codes like this,but I have never learnt that
> before.
> It looks like a declaration of a function,but why DEVICE_REQUEST has to
> be quoted by ( ).
> Anyone can explain that to me? thanks a lot!

Yes. It prevents DEVICE_REQUEST from being redefined by a macro. This is frequently done in system libraries.


It prevents any definition of DEVICE_REQUEST as a function-like macro
from being expanded at that point. If DEVICE_REQUEST is an
object-like macro, it will still be expanded. You're probably right
about the intent in this case, but it's impossible to tell without
seeing how and whether DEVICE_REQUEST is defined.


You, Ben and Thad, make a distinction between a "function-like macro" and an
"object-like macro."

P.J. Plauger's exact wording on how this works (or is supposed to work):
"In the definition of a visible library function, the function name is
surrounded by parenthesis. ... Any such function can have its declaration
masked by a macro definition in its corresponding header. The parenthesis
prevent the translator from recognizing the macro and expanding it."

He makes no distinction between the two as far as I can tell. So, where
exactly do you get this?


Doesn't it seem clear to you that the statement you quoted above is
specifically about function names?

Somebody else cited a reference from C99, but it has to do with the
distinction between "ordinary" and "function-like" macros.

Ordinary macros are simple text replacement where the macro itself is
replaced by the specified text. This happens everywhere except in a
few specific contexts, such as inside a comment or quoted string, when
used with the "stringizing" preprocessor operator, or when it appears
recursively in its own expansion. Nothing other than the macro
identifier itself it replaced.

In function-like macros, the macro identifier itself is likely to be
replaced, but also parenthesized arguments that follow it. In other
words, the macro actually defines multiple identifiers that are
replaced on expansion.

Example:

#define SHOW_ERROR(err) fprintf(stderr, "Error = %d\n", (err))

Here the SHOW_ERROR macro not only generates replacement text for
itself when expanded, it also replaces the parameter 'err' with the
macro argument.

The C grammar for defining this type of macro requires that the
opening '(' be the first preprocessor token after the macro
identifier, meaning there can be nothing but white space characters in
between.

The requirement for the preprocessor to expand this macro is similar,
namely that the '(' preprocessor token immediately follow the macro
identifier. Putting the macro identifier in parentheses places a ')'
preprocessor token between the identifier and the '('.

Try it and see.

Also try:

#define X_PLUS_3 x + 3

#include <stdio.h>

int main(void)
{
int x = 0;
printf("%d\n", X_PLUS_3);
x = 10;
printf("%d\n", (X_PLUS_3));
return 0;
}

....and see what the parentheses do there.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Mar 8 '06 #13
"Rod Pemberton" <do*********@sorry.bitbucket.cmm> writes:
P.J. Plauger's exact wording on how this works (or is supposed to work):
"In the definition of a visible library function, the function name is
surrounded by parenthesis. ... Any such function can have its declaration
masked by a macro definition in its corresponding header. The parenthesis
prevent the translator from recognizing the macro and expanding it."

He makes no distinction between the two as far as I can tell. So, where
exactly do you get this?


The standard actually says something similar, in C99 7.1.4:

Any macro definition of a function can be suppressed locally
by enclosing the name of the function in parentheses,
because the name is then not followed by the left
parenthesis that indicates expansion of a macro function
name.

Clearly, an object-like macro definition can't be suppressed the
same way.
--
Go not to Usenet for counsel, for they will say both no and yes.
Mar 8 '06 #14
Ben Pfaff <bl*@cs.stanford.edu> writes:
"Rod Pemberton" <do*********@sorry.bitbucket.cmm> writes:
P.J. Plauger's exact wording on how this works (or is supposed to work):
"In the definition of a visible library function, the function name is
surrounded by parenthesis. ... Any such function can have its declaration
masked by a macro definition in its corresponding header. The parenthesis
prevent the translator from recognizing the macro and expanding it."

He makes no distinction between the two as far as I can tell. So, where
exactly do you get this?


The standard actually says something similar, in C99 7.1.4:

Any macro definition of a function can be suppressed locally
by enclosing the name of the function in parentheses,
because the name is then not followed by the left
parenthesis that indicates expansion of a macro function
name.

Clearly, an object-like macro definition can't be suppressed the
same way.


Yes, but the context makes it clear that it refers only to the
function-like macro definitions that are allowed by the standard:

Any function declared in a header may be additionally implemented
as a function-like macro defined in the header, so if a library
function is declared explicitly when its header is included, one
of the techniques shown below can be used to ensure the
declaration is not affected by such a macro. Any macro definition
of a function can be suppressed locally by enclosing the name of
the function in parentheses, because the name is then not followed
by the left parenthesis that indicates expansion of a macro
function name. For the same syntactic reason, it is permitted to
take the address of a library function even if it is also defined
as a macro.

I don't think an implementation is allowed to provide an object-like
macro for a library function (at least not in a way that makes any
difference).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 8 '06 #15
Keith Thompson <ks***@mib.org> writes:
Ben Pfaff <bl*@cs.stanford.edu> writes:
"Rod Pemberton" <do*********@sorry.bitbucket.cmm> writes:
P.J. Plauger's exact wording on how this works (or is supposed to work):
"In the definition of a visible library function, the function name is
surrounded by parenthesis. ... Any such function can have its declaration
masked by a macro definition in its corresponding header. The parenthesis
prevent the translator from recognizing the macro and expanding it."

He makes no distinction between the two as far as I can tell. So, where
exactly do you get this?
The standard actually says something similar, in C99 7.1.4:

Any macro definition of a function can be suppressed locally
by enclosing the name of the function in parentheses,
because the name is then not followed by the left
parenthesis that indicates expansion of a macro function
name.
[quoting rearranged to suit the way I meant my text to be
interpreted, see below]
Yes, but the context makes it clear that it refers only to the
function-like macro definitions that are allowed by the standard:


I am certain that text elsewhere in the standard adds up to what
the sentence quoted states. In other words, if that sentence
wasn't in the standard, or if it appeared only in a footnote, it
would still be true.

Thus, I don't see how the context matters.
Clearly, an object-like macro definition can't be suppressed the
same way.


I don't think an implementation is allowed to provide an object-like
macro for a library function (at least not in a way that makes any
difference).


Agreed.
--
Bite me! said C.
Mar 8 '06 #16

"Jack Klein" <ja*******@spamcop.net> wrote in message
news:66********************************@4ax.com...
On Tue, 7 Mar 2006 20:46:27 -0500, "Rod Pemberton"
<do*********@sorry.bitbucket.cmm> wrote in comp.lang.c:

"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
"Rod Pemberton" <do*********@sorry.bitbucket.cmm> writes:
> <zh*********@gmail.com> wrote in message
> news:11**********************@z34g2000cwc.googlegr oups.com...
>> static void (DEVICE_REQUEST)(void);
>>
>> What does this line declare in C?
>> I am always meeting some codes like this,but I have never learnt that >> before.
>> It looks like a declaration of a function,but why DEVICE_REQUEST has to >> be quoted by ( ).
>> Anyone can explain that to me? thanks a lot!
>
> Yes. It prevents DEVICE_REQUEST from being redefined by a macro.
This is
> frequently done in system libraries.

It prevents any definition of DEVICE_REQUEST as a function-like macro
from being expanded at that point. If DEVICE_REQUEST is an
object-like macro, it will still be expanded. You're probably right
about the intent in this case, but it's impossible to tell without
seeing how and whether DEVICE_REQUEST is defined.


You, Ben and Thad, make a distinction between a "function-like macro" and an "object-like macro."

P.J. Plauger's exact wording on how this works (or is supposed to work):
"In the definition of a visible library function, the function name is
surrounded by parenthesis. ... Any such function can have its declaration masked by a macro definition in its corresponding header. The parenthesis prevent the translator from recognizing the macro and expanding it."

He makes no distinction between the two as far as I can tell. So, where
exactly do you get this?


Doesn't it seem clear to you that the statement you quoted above is
specifically about function names?


Plauger's statement doesn't say the same thing (to me) as C99 7.1.4. Given
situation A) and B) below, my understanding, from Plauger, was that
"DEVICE_REQUEST" wouldn't be expanded or replaced as "do_hd_request" in
either case. C99 7.1.4 makes it clear that only case A) is unexpanded.

/* case A) parameterized or function-like macro */
#define DEVICE_REQUEST(x) do_hd_request(x)
void (DEVICE_REQUEST)(void);

/* case B) non-parameterized or object-like macro */
#define DEVICE_REQUEST do_hd_request
void (DEVICE_REQUEST)(void);

Rod Pemberton
Mar 8 '06 #17
On Tue, 07 Mar 2006 21:47:46 -0600, Jack Klein <ja*******@spamcop.net>
wrote:
<snip>
In function-like macros, the macro identifier itself is likely to be
replaced, but also parenthesized arguments that follow it. In other
words, the macro actually defines multiple identifiers that are
replaced on expansion.

Example:

#define SHOW_ERROR(err) fprintf(stderr, "Error = %d\n", (err))

Here the SHOW_ERROR macro not only generates replacement text for
itself when expanded, it also replaces the parameter 'err' with the
macro argument.

The C grammar for defining this type of macro requires that the
opening '(' be the first preprocessor token after the macro
identifier, meaning there can be nothing but white space characters in
between.
No whitespace. #define FOO (bar) xyz
is an object-like macro that expands to (bar) xyz .
The requirement for the preprocessor to expand this macro is similar,
namely that the '(' preprocessor token immediately follow the macro
identifier. Putting the macro identifier in parentheses places a ')'
preprocessor token between the identifier and the '('.
Right.
Try it and see.

Also try:

#define X_PLUS_3 x + 3

#include <stdio.h>

int main(void)
{
int x = 0;
printf("%d\n", X_PLUS_3);
x = 10;
printf("%d\n", (X_PLUS_3));
return 0;
}

...and see what the parentheses do there.


Huh? In this case, they don't do anything.

- David.Thompson1 at worldnet.att.net
Mar 20 '06 #18

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

Similar topics

2
by: thecrow | last post by:
Alright, what the hell is going on here? In the following code, I expect the printed result to be: DEBUG: frank's last name is burns. Instead, what I get is: DEBUG: frank's last name is...
220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
92
by: Reed L. O'Brien | last post by:
I see rotor was removed for 2.4 and the docs say use an AES module provided separately... Is there a standard module that works alike or an AES module that works alike but with better encryption?...
137
by: Philippe C. Martin | last post by:
I apologize in advance for launching this post but I might get enlightment somehow (PS: I am _very_ agnostic ;-). - 1) I do not consider my intelligence/education above average - 2) I am very...
12
by: Dario | last post by:
The following simple program behaves differently in Windows and Linux . #include <stdexcept> #include <iostream> #include <string> using namespace std; class LogicError : public logic_error {...
125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
47
by: Neal | last post by:
Patrick Griffiths weighs in on the CSS vs table layout debate in his blog entry "Tables my ass" - http://www.htmldog.com/ptg/archives/000049.php . A quite good article.
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
8
by: Midnight Java Junkie | last post by:
Dear Colleagues: I feel that the dumbest questions are those that are never asked. I have been given the opportunity to get into .NET. Our organization has a subscription with Microsoft that...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
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...

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.