473,407 Members | 2,320 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,407 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 2014
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...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
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,...
0
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,...
0
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...
0
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...

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.