4

I'm learning C right now and there is a conversion specifier %a which writes a number in p-notation as opposed to %e which writes something in e-notation (exponential notation).

What is p-notation?

3
  • Show us some code. Is this really printf?
    – bmargulies
    Commented Feb 7, 2010 at 1:54
  • I don't know of p-notation, and there is no %a specifier. Where are you reading this, and what's the output look like? Commented Feb 7, 2010 at 1:54
  • 1
    Here's some code: float x = 1.2 * 4.78; printf("%a", x); Here's the output: 0x1.6f1aap+2 @BlueRaja I'm reading it in C Primer by Stephen Prata. I appreciate your help and interest.
    – Corey
    Commented Feb 7, 2010 at 23:23

5 Answers 5

8

You use %a to get a hexadecimal representation of a floating-point number. This might be useful if you are a student learning floating-point representations, or if you want to be able to read and write an exact floating-point number with no rounding error (but not very human-readable).

This format specificier, along with many others, was added as part of the C99 standard. Dinkumware have an excellent C99 library reference free online; it's PJ Plauger's company, and he had a lot to do with both C89 and C99 standard libraries. Link above is to printing functions; the general library reference is http://www.dinkumware.com/manuals/default.aspx

0
5

Here is an extract from the c99 standard, section 7.19.6.1 (7) which shows the details for %a or %A (similar to the mac details given by dmckee above):

A double argument representing a floating-point number is converted in the style [−]0xh.hhhhp±d, where there is one hexadecimal digit (which is nonzero if the argument is a normalized floating-point number and is otherwise unspecified) before the decimal-point character and the number of hexadecimal digits after it is equal to the precision; if the precision is missing and FLT_RADIX is a power of 2, then the precision is sufficient for an exact representation of the value; if the precision is missing and FLT_RADIX is not a power of 2, then the precision is sufficient to distinguish248) values of type double, except that trailing zeros may be omitted; if the precision is zero and the # flag is not specified, no decimal- point character appears. The letters abcdef are used for a conversion and the letters ABCDEF for A conversion. The A conversion specifier produces a number with X and P instead of x and p. The exponent always contains at least one digit, and only as many more digits as necessary to represent the decimal exponent of 2. If the value is zero, the exponent is zero.

0
3

From the printf(3) man page on my Mac OS X box (therefore the BSD c standard library implementation):

aA
The double argument is rounded and converted to hexadecimal nota- tion in the style [-]0xh.hhhp[+-]d, where the number of digits after the hexadecimal-point character is equal to the precision specification. If the precision is missing, it is taken as enough to represent the floating-point number exactly, and no rounding occurs. If the precision is zero, no hexadecimal-point character appears. The p is a literal character p', and the exponent consists of a positive or negative sign followed by a decimal number representing an exponent of 2. The A conversion uses the prefix ``0X'' (rather than ``0x''), the letters ``ABCDEF'' (rather than ``abcdef'') to represent the hex digits, and the letterP' (rather than `p') to separate the mantissa and exponent.

The 'p' (or 'P') serves to separate the (hexadecimal) mantissa from the (hexadecimal) exponent.

These specifiers are not in my K&R, and the man page is not specific about what standard (if any) specifies them.

I just checked my Debian 5.0 box (using glibc 2.7) which also has it; that man page says that it is c99 related (again, no reference to any particular standard).

5
  • trust the MAc to be different! ;) Commented Feb 7, 2010 at 2:18
  • @Mitch: You said it. I hadn't heard of it until the OP asked. In any case, it is sufficiently rare that is should be avoided in anything that might every become production code... Commented Feb 7, 2010 at 2:25
  • 1
    It's present in some Linux printf's. I think it's a C99 thing. See here: swoolley.org/man.cgi/3/printf
    – mipadi
    Commented Feb 7, 2010 at 2:32
  • Going to have to modify my "Don't use" advice to "Wait a while before using this. You don't want your code to be hard to port because the target compiler's libc isn't up to the newest spec." Commented Feb 7, 2010 at 2:35
  • 1
    It's definitely C99. This link from IBM seems to confirm: publib.boulder.ibm.com/infocenter/zos/v1r9/index.jsp?topic=/…
    – Jon Purdy
    Commented Feb 7, 2010 at 4:12
2

This might be useful: http://www.cppreference.com/wiki/c/io/printf

Specifically, here are the format specifiers you can use in printf (w/o modifiers like .02 etc):

Code    Format
%c  character
%d  signed integers
%i  signed integers
%I64d   long long (8B integer), MS-specific
%I64u   unsigned long long (8B integer), MS-specific
%e  scientific notation, with a lowercase “e”
%E  scientific notation, with a uppercase “E”
%f  floating point
%g  use %e or %f, whichever is shorter
%G  use %E or %f, whichever is shorter
%o  octal
%s  a string of characters
%u  unsigned integer
%x  unsigned hexadecimal, with lowercase letters
%X  unsigned hexadecimal, with uppercase letters
%p  a pointer
%n  the argument shall be a pointer to an integer into which is placed the number of characters written so far
2

There is no %a format specifier (as as I'm aware, and certainly not in any of the common implementations).

There is a %p format specifier which prints a pointer address.

Ref.

UPDATE: please see other posts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.