Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > how the macro works

Reply
Thread Tools

how the macro works

 
 
George2
Guest
Posts: n/a
 
      12-12-2007
Hello everyone,


I can not understand how the following code works and assign 100 to
counter variable?

Code:
void Foo (int* input)
{
	*input = 100;

	return;
}

#define GETFOO  (Foo( &counter ), counter)

int counter;

int main()
{
	GETFOO;
	return 0;
}

thanks in advance,
George
 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      12-12-2007
George2 said:

> Hello everyone,
>
>
> I can not understand how the following code works and assign 100 to
> counter variable?
>
> void Foo (int* input)
> {
> *input = 100;
>
> return;
> }
>
> #define GETFOO (Foo( &counter ), counter)
>
> int counter;
>
> int main()
> {
> GETFOO;
> return 0;
> }


After macro replacements have occurred, your program becomes:

void Foo (int* input)
{
*input = 100;

return;
}


int counter;

int main()
{
(Foo( &counter ), counter);
return 0;
}

Now that you see it like that, does it strike you as being a bit weird?


--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
 
 
 
Toms hilidhe
Guest
Posts: n/a
 
      12-12-2007
George2 <(E-Mail Removed)> wrote in news:c0f8e56c-9fb3-40d6-a504-
http://www.velocityreviews.com/forums/(E-Mail Removed):

> I can not understand how the following code works and assign 100 to
> counter variable?



My guess is you don't know how the comma operator works.

It takes two operands, a left one and a right one.

It evaluates the left operand, then evaluates the right operand, and the
resulting expression is equal to the right operand. (I can't remember if
you can use it as an L-value, but my guess would be no).

If you have:

y = Func(), 5;


then it's as if you wrote:

Func();

y = 5;

That is: "Func()" gets evaluated, then "5" gets evaluated, and the
resultant expression is "5".


--
Toms hilidhe
 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      12-12-2007
Toms hilidhe wrote:

(Those are funny characters in the name.)

> George2 <(E-Mail Removed)> wrote in news:c0f8e56c-9fb3-40d6-a504-
> (E-Mail Removed):
>
>> I can not understand how the following code works and assign 100 to
>> counter variable?

>
> My guess is you don't know how the comma operator works.
>
> It takes two operands, a left one and a right one.
>
> It evaluates the left operand, then evaluates the right operand, and the
> resulting expression is equal to the right operand. (I can't remember if
> you can use it as an L-value, but my guess would be no).


So far, so good, but:

> If you have:
>
> y = Func(), 5;
>
> then it's as if you wrote:
>
> Func();
>
> y = 5;


No; it's as though you'd written

y = Func(); 5;

Comma is the least binding operator (when it's an operator at all).

Example code:

#include <stdio.h>

int func(void)
{ return 17; }

int main(void)
{
int y;
y = func(), 5;
fprintf( stderr, "y == %d\n", y );
return 0;
}

Output:

y == 17

--
Chris "an operator of unspecified precedence" Dollin

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

 
Reply With Quote
 
Toms hilidhe
Guest
Posts: n/a
 
      12-12-2007
Chris Dollin <(E-Mail Removed)> wrote in
news:fjomsl$1tk$(E-Mail Removed):

> No; it's as though you'd written
>
> y = Func(); 5;
>
> Comma is the least binding operator (when it's an operator at all).



Wups a daisy, I should laminate an operator precedence table and blue-tack
it to my laptop .

--
Toms hilidhe
 
Reply With Quote
 
Mark Bluemel
Guest
Posts: n/a
 
      12-12-2007
Toms hilidhe wrote:
> Chris Dollin <(E-Mail Removed)> wrote in
> news:fjomsl$1tk$(E-Mail Removed):
>
>> No; it's as though you'd written
>>
>> y = Func(); 5;
>>
>> Comma is the least binding operator (when it's an operator at all).

>
>
> Wups a daisy, I should laminate an operator precedence table and blue-tack
> it to my laptop .
>

Or test your assertions with a program...
 
Reply With Quote
 
hakuna
Guest
Posts: n/a
 
      12-12-2007
> Example code:
>
> #include <stdio.h>
>
> int func(void)
> { return 17; }
>
> int main(void)
> {
> int y;
> y = func(), 5;
> fprintf( stderr, "y == %d\n", y );
> return 0;
> }
>
> Output:
>
> y == 17


get the right operand like this:
y = (func(), 5);

the Output is: y==5
 
Reply With Quote
 
Philip Potter
Guest
Posts: n/a
 
      12-12-2007
Chris Dollin wrote:
> Tom�s � h�ilidhe wrote:
>
> (Those are funny characters in the name.)


Not as funny as they appear to me:
"Tom��������������� ���������������� " wrote:
 
Reply With Quote
 
Toms hilidhe
Guest
Posts: n/a
 
      12-12-2007
Philip Potter <(E-Mail Removed)> wrote in news:fjp0c2$v2j$(E-Mail Removed):

> Chris Dollin wrote:
>> Tom�s � h�ilidhe wrote:
>>
>> (Those are funny characters in the name.)

>
> Not as funny as they appear to me:
> "Tom��������������� ������
> ���������" wrote:




Do you not use UTF-8 in your newsreader?

--
Toms hilidhe
 
Reply With Quote
 
Philip Potter
Guest
Posts: n/a
 
      12-12-2007
"Tom��������������� ���������������� " wrote:
> Philip Potter <(E-Mail Removed)> wrote in news:fjp0c2$v2j$(E-Mail Removed):
>
>> Chris Dollin wrote:
>>> Tom�s � h�ilidhe wrote:
>>>
>>> (Those are funny characters in the name.)

>> Not as funny as they appear to me:
>> "Tomï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿ ½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ ������ï
>> ¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿ ½ï¿½" wrote:

>
> Do you not use UTF-8 in your newsreader?


The important thing is that you do not set a character encoding in your
headers. For example:

Content-Type: text/plain; charset=utf-8

If you don't provide this information, my newsreader cannot magically
know which character set you're using; and you have no reason to expect
it to default to UTF-8 because I might meet someone else who expects it
to default to ISO-8859-1.

As a matter of fact I do use UTF-8 as default. If I change it to
ISO-8859-1 then I get "Tomás Ó hÉilidhe wrote:" which looks more likely.
So it seems you aren't using UTF-8 at all, but one of the ISO-8859 family.

Unfortunately, I don't have any advice for you on how to rectify the
situation because I use a different newsreader. Nevertheless, I wish you
the best of luck fixing it.

Phil
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
When I turn on my PC, it works, works, works. Problem! Fogar Computer Information 1 01-17-2006 12:57 AM
#define macro to enclose an older macro with strings Dead RAM C++ 20 07-14-2004 10:58 AM
After rebooting my PC works, works, works! Antivirus problem? Adriano Computer Information 1 12-15-2003 05:30 AM
macro name from macro? D Senthil Kumar C Programming 1 09-21-2003 07:02 PM



Advertisments