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

Macrozzz

Hi,
While reading about MACRO's and C preprocessor,
I came across something.

You need to write a simple macro CHAR(x) which takes a character x and
converts it into ASCII value of x.

printf("%c",CHAR(a)) ==a
printf("%c",CHAR(X)) ==X

No, this simple definition doesn't work:
#define CHAR(x) 'x'


Cheers,

Sandeep.

Dec 5 '06 #1
7 1578
sandy said:
Hi,
While reading about MACRO's and C preprocessor,
I came across something.

You need to write a simple macro CHAR(x) which takes a character x and
converts it into ASCII value of x.
If you're on an ASCII system, this is easy:

#define CHAR(x) x

If you're on an EBCDIC system, this is still easy:

#define CHAR(x) convert_ebcdic_to_ascii(x)

but writing convert_ebcdic_to_ascii might slow you down a little.

And if you're on some other system foo, s/ebcdic/foo/ in the previous
sentence.

For portability, you might want to write yourself a
convert_native_to_ascii() function, which on ASCII systems would be a NOP,
but which would do the necessary on other systems. The macro then becomes

#define CHAR(x) convert_native_to_ascii(x)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 5 '06 #2
Le 05-12-2006, sandy <sa***********@gmail.coma écrit*:
You need to write a simple macro CHAR(x) which takes a character x and
converts it into ASCII value of x.
First, ASCII is not the only possible encoding.
Second, 'x' is a numerical value.
printf("%c",CHAR(a)) ==a
printf("%c",CHAR(X)) ==X

No, this simple definition doesn't work:
#define CHAR(x) 'x'
#include <assert.h>
#define CHAR(X) (#X)[0]

int main(){
assert( CHAR(x) == 'x' );
}

How does it works ? #X ask the preprocesseur to transform
#foo into "foo". And then, (#foo)[0] --"foo"[0] == 'f'

Marc Boyer
Dec 5 '06 #3

Marc Boyer wrote:
Le 05-12-2006, sandy <sa***********@gmail.coma écrit :
You need to write a simple macro CHAR(x) which takes a character x and
converts it into ASCII value of x.

First, ASCII is not the only possible encoding.
Second, 'x' is a numerical value.
printf("%c",CHAR(a)) ==a
printf("%c",CHAR(X)) ==X

No, this simple definition doesn't work:
#define CHAR(x) 'x'

#include <assert.h>
#define CHAR(X) (#X)[0]

int main(){
assert( CHAR(x) == 'x' );
}

How does it works ? #X ask the preprocesseur to transform
#foo into "foo". And then, (#foo)[0] --"foo"[0] == 'f'
This assumes that ASCII is the character encoding for the system. The
OP gave no such assurance.

Dec 5 '06 #4
Le 05-12-2006, santosh <sa*********@gmail.coma écrit*:
>
Marc Boyer wrote:
>Le 05-12-2006, sandy <sa***********@gmail.coma écrit :
You need to write a simple macro CHAR(x) which takes a character x and
converts it into ASCII value of x.

First, ASCII is not the only possible encoding.
Second, 'x' is a numerical value.
printf("%c",CHAR(a)) ==a
printf("%c",CHAR(X)) ==X

No, this simple definition doesn't work:
#define CHAR(x) 'x'

#include <assert.h>
#define CHAR(X) (#X)[0]

int main(){
assert( CHAR(x) == 'x' );
}

How does it works ? #X ask the preprocesseur to transform
#foo into "foo". And then, (#foo)[0] --"foo"[0] == 'f'

This assumes that ASCII is the character encoding for the system. The
OP gave no such assurance.
Yes, I wrote 'First, ASCII is not the only possible encoding.'
But I was assuming (given its example) that the real need of
the OP is to transform x into 'x', not to get an ASCII value.

Marc Boyer
Dec 5 '06 #5
sandy wrote:
Hi,
While reading about MACRO's and C preprocessor,
I came across something.

You need to write a simple macro CHAR(x) which takes a character x and
converts it into ASCII value of x.

printf("%c",CHAR(a)) ==a
printf("%c",CHAR(X)) ==X

No, this simple definition doesn't work:
#define CHAR(x) 'x'
If your system is an ASCII based one then this is as easy as:
#define CHAR2ASCII(ch) ((int)(ch))

But if not, then some of conversion is necessary. There's no one
'catch-all' macro that can do the job.

Dec 5 '06 #6
Le 05-12-2006, santosh <sa*********@gmail.coma écrit*:
sandy wrote:
>Hi,
While reading about MACRO's and C preprocessor,
I came across something.

You need to write a simple macro CHAR(x) which takes a character x and
converts it into ASCII value of x.

printf("%c",CHAR(a)) ==a
printf("%c",CHAR(X)) ==X

No, this simple definition doesn't work:
#define CHAR(x) 'x'

If your system is an ASCII based one then this is as easy as:
#define CHAR2ASCII(ch) ((int)(ch))
Why did you do such cast ?

Marc Boyer
Dec 5 '06 #7
On 4 Dec 2006 23:38:18 -0800, "sandy" <sa***********@gmail.comwrote:
>Hi,
While reading about MACRO's and C preprocessor,
I came across something.

You need to write a simple macro CHAR(x) which takes a character x and
converts it into ASCII value of x.

printf("%c",CHAR(a)) ==a
printf("%c",CHAR(X)) ==X

No, this simple definition doesn't work:
#define CHAR(x) 'x'
Of course it doesn't. There is no printable character whose ASCII
value can be expressed in a single character. Therefore, using %c is
doomed to failure, whether you use a macro or not.

Change your printf to %d or %x and it works fine.
Remove del for email
Dec 6 '06 #8

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

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.