jesuashok has asked for the wisdom of the Perl Monks concerning the following question:

dear monks,

# include <stdio.h> main () { printf ( "%d",'c' ); }
The above C language code prints the ascii value of the character 'c'.
I would like to have the same behaviour in perl.
#!/usr/bin/perl use strict; use warnings; printf "%d\n",'c'; Output :- Argument "c" isn't numeric in printf at l.pl line 5. 0
I would like to know why the conversion does not occur ? am I missing something there in the perl code ?

Replies are listed 'Best First'.
Re: Getting the ascii number through perl's printf
by jettero (Monsignor) on Jan 10, 2007 at 01:37 UTC

    Short answer: no. Use ord instead.

    The reason that doesn't work in perl is a culture problem. In C, all your "strings" are actually character arrays, and all your characters are really single byte integers. It's just not like that in perl. But print "lol \%d=", ord 'a', "\n" will work!

    -Paul

Re: Getting the ascii number through perl's printf
by GrandFather (Saint) on Jan 10, 2007 at 01:48 UTC

    In C a char is an int. A char constant is really and truely an int, while a char is an 8 bit quantity that behaves like an int (and may be signed or not depending on compiler and options).

    In Perl a char is a string containing a single character. Using a string in a numerical context 'numifies' the string - gets it interpreted as a number. The string constant 'c' turns into 0 when numified. Not what you want. What you need is to use the function ord which produces the ordinal value for the first character in the string passed to it.


    DWIM is Perl's answer to Gödel
Re: Getting the ascii number through perl's printf
by ikegami (Patriarch) on Jan 10, 2007 at 04:01 UTC

    The number returned ord may not be an ASCII code.

    • When passed a string of characters, ord will return the UNICODE character number of the first character in the string. (For the 128 ASCII characters, the UNICODE character number and the ASCII character number are the same.)

    • When passed a string of encoded characters, ord may return a useless value.

    • When passed a string of packed bytes, ord will return the value of the first byte.

    The same pretty much applies to C's printf too.

Re: Getting the ascii number through perl's printf
by virtualsue (Vicar) on Jan 10, 2007 at 04:45 UTC

    As jettero says, you must use ord if you want Perl to output an ASCII value. The C idiom you are using doesn't make sense in Perl.

    #!/usr/bin/perl use warnings; use strict; print ord('c'), "\n";
    The 'c' in your C snippet looks just like the 'c' in your Perl snippet, but they're not the same. In C, as you already know, 'c' is a char, a character constant which can be readily exchanged for its corresponding ASCII value. In Perl, 'c' is a scalar value that is also referred to as a string literal. There is no such thing as a char in Perl, though a string literal can certainly contain only one character. I encourage you to read (and refer back to) perldata. With that out of the way, why did your program spit out a warning and convert your string literal 'c' to 0? Grandfather said that it was because Perl "numified" 'c', and that this is Perl slang which means 'what happens when a scalar is used in is a numeric context'. The following snippet may shed a bit more light, try running it.
    #!/usr/bin/perl use warnings; use strict; printf "%d\n", '1234'; # The entire string is an integer printf "%d\n", '12c3'; # printf stops the format conversion at the fi +rst non-integer printf "%d\n", 'c123'; # No characters converted, 0 is returned printf "%d\n", '0c123'; # Conversion stops after first character, 0 is + returned