how the macro works

G

George2

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
 
R

Richard Heathfield

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?
 
T

Tomás Ó hÉilidhe

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".
 
C

Chris Dollin

Tomás Ó hÉilidhe wrote:

(Those are funny characters in the name.)
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
 
T

Tomás Ó hÉilidhe

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 :-D.
 
M

Mark Bluemel

Tomás Ó hÉilidhe said:
Wups a daisy, I should laminate an operator precedence table and blue-tack
it to my laptop :-D.
Or test your assertions with a program...
 
H

hakuna

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
 
T

Tomás Ó hÉilidhe

Philip Potter said:
Not as funny as they appear to me:
"Tom����������������������ï
¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½" wrote:



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

Philip Potter

Tom�������������������������������� said:
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
 
T

Tomás Ó hÉilidhe

Philip Potter said:
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.


I started out using a Linux newsreader called Pan, but it turned out to be
crap.

At the moment I'm using a Windows newsreader called XNews, which is
supposedly the best newsreader ever (or so say the people who recommended
it to me). I wouldn't have thought that XNews got it wrong when it came to
character encoding. . . ?
 
B

Ben Bacarisse

I started out using a Linux newsreader called Pan, but it turned out to be
crap.

At the moment I'm using a Windows newsreader called XNews, which is
supposedly the best newsreader ever (or so say the people who recommended
it to me). I wouldn't have thought that XNews got it wrong when it came to
character encoding. . . ?

I would not have thought so either, but your postings arrive here with
no encoding specified and characters outside the safe 7-bit range.
There is probably an XNews group or mailing list where you can ask
about fixing this.
 
R

Richard Bos

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

The most important thing is that, Welshmen and Friesians
notwithstanding, Usenet is still a 7-bit medium, and those who assume
otherwise do so at their own peril, and at the peril of their names'
spelling.

Richard
 
P

Philip Potter

Tom�������������������������������� said:
I started out using a Linux newsreader called Pan, but it turned out to be
crap.

At the moment I'm using a Windows newsreader called XNews, which is
supposedly the best newsreader ever (or so say the people who recommended
it to me). I wouldn't have thought that XNews got it wrong when it came to
character encoding. . . ?

It seems that they did. Appealing to reputation can be useful, but it
doesn't trump the evidence of your postings. Have a look at the full
headers yourself - notice that most people here will declare a charset
in their headers, especially if they want to use non-ASCII characters
like ᩲ‡. A quick google for "xnews charset" turned up this:

http://tinyurl.com/ypfygj

which seems to show that XNews' creator considers the software to be
aimed at himself only, and isn't interested in catering to things he
isn't interested in. It seems character sets is one of those things.

There is also a link on that page to a tool which claims to fix this
problem in XNews.

Phil
 
T

Tomás Ó hÉilidhe

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

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


I manually went into my XNews settings and added "custom headers".

How does this post look?
 
T

Tomás Ó hÉilidhe

I manually went into my XNews settings and added "custom headers".

How does this post look?


Damn, looks like I didn't enable the new settings.


How does this post look?
 
P

Philip Potter

Tom�������������������������������� said:
I manually went into my XNews settings and added "custom headers".

How does this post look?

Can't you see for yourself? Find the option in XNews to display all headers.

In any case, I think the above attribution line speaks for itself.

Incidentally, your sig comes out as:
Tomás Ó hÉilidhe
which is fine. It seems your message body is UTF-8 but your header is
ISO-whatever-it-is. This is not a good situation.
 
D

David Thompson

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

Content-Type: text/plain; charset=utf-8
Or 8859 or whatever. But that applies to the (single-part) BODY.
The correct way to use non-7-bit-ASCII IN HEADERS, in particular the
'From:' header, is =?cset?Q?qp?= or =?cset?B?b64?= per RFC 2047.

It's probably possible to configure this by hand if your program can't
(or won't) be fixed; alternatively the OP might consider just using
the (ASCII) address and an ASCII-compatible nickname, with the real
name in the sig -- which at protocol level is in the body, as above.
Or if there is an acceptable ASCII transliteration of the name.

- formerly david.thompson1 || achar(64) || worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top