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. 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.
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
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.
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
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.
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. |