Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #66185

Floating point to string, and back via `%a'...

From "Chris M. Thomasson" <nospam@nospam.nospam>
Newsgroups comp.lang.c
Subject Floating point to string, and back via `%a'...
Date 2015-07-22 12:46 -0700
Organization Aioe.org NNTP Server
Message-ID <moorva$su7$1@speranza.aioe.org> (permalink)

Show all headers | View raw


How in the heck can I use the `%a' format specifier in 
conjunction with sprintf/sscanf so that I can convert a 
floating point to a string, and back? I have some code, 
but it does not work with `%a'. Damn!

Also, why does it seem to work for a %f %lf sprintf to sscanf pair?
_______________________________________________________________
#include <stdio.h>


int 
main(void)
{
    char buf[256] = { '\0' };
    double no = 1.0123456789;

    printf("no = %.10f\n\n", no);


    /* try to use default precision `%a' with sprintf/sscanf */
    {
        sprintf(buf, "%a", no);

        double nc = 0.0;
        int status = sscanf(buf, "%a", &nc);

        if (! status || status == EOF)
        {
            printf("sscanf `%%a' error!\n");
        }

        printf("(%%s):buf = %s\n", buf);
        printf("(%%a):nc = %f\n\n", nc);
    }


    /* try to use custom precision `%.10a' with sprintf/sscanf */
    {
        sprintf(buf, "%.10a", no);

        double nc = 0.0;
        int status = sscanf(buf, "%.10a", &nc);

        if (! status || status == EOF)
        {
            printf("sscanf `%%.10a' error!\n");
        }

        printf("(%%s):buf = %s\n", buf);
        printf("(%%.10a):nc = %.10f\n\n", nc);
    }


    /* try to use default precision `%f' with sprintf/sscanf */
    {
        sprintf(buf, "%f", no);

        double nc = 0.0;
        int status = sscanf(buf, "%lf", &nc);

        if (! status || status == EOF)
        {
            printf("sscanf %%lf error!\n");
        }

        printf("(%%s):buf = %s\n", buf);
        printf("(%%f):nc = %f\n\n", nc);
    }


    /* try to use custom precision `%.10f' with sprintf/sscanf */
    {
        sprintf(buf, "%.10f", no);

        double nc = 0.0;
        int status = sscanf(buf, "%lf", &nc);

        if (! status || status == EOF)
        {
            printf("sscanf %%lf error!\n");
        }

        printf("(%%s):buf = %s\n", buf);
        printf("(%%.10f):nc = %.10f\n\n", nc);
    }


    return 0;
}

_______________________________________________________________


What am I doing wrong? How do I use `%a' properly!

;^O

Back to comp.lang.c | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

Floating point to string, and back via `%a'... "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-07-22 12:46 -0700
  Re: Floating point to string, and back via `%a'... James Kuyper <jameskuyper@verizon.net> - 2015-07-22 16:19 -0400
    Re: Floating point to string, and back via `%a'... "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-07-22 13:49 -0700
      Re: Floating point to string, and back via `%a'... Keith Thompson <kst-u@mib.org> - 2015-07-22 14:01 -0700
      Re: Floating point to string, and back via `%a'... James Kuyper <jameskuyper@verizon.net> - 2015-07-22 17:18 -0400
        Re: Floating point to string, and back via `%a'... "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-07-22 14:22 -0700
  Re: Floating point to string, and back via `%a'... Keith Thompson <kst-u@mib.org> - 2015-07-22 13:40 -0700
    Re: Floating point to string, and back via `%a'... "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-07-22 14:01 -0700
      Re: Floating point to string, and back via `%a'... Keith Thompson <kst-u@mib.org> - 2015-07-22 14:12 -0700
        Re: Floating point to string, and back via `%a'... "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-07-22 14:20 -0700
          Re: Floating point to string, and back via `%a'... "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-07-22 14:24 -0700
          Re: Floating point to string, and back via `%a'... glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-07-22 21:30 +0000
            Re: Floating point to string, and back via `%a'... "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-07-22 14:39 -0700
          Re: Floating point to string, and back via `%a'... Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2015-07-22 19:49 -0600
  Re: Floating point to string, and back via `%a'... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-07-22 22:28 +0100
    Re: Floating point to string, and back via `%a'... "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-07-22 14:34 -0700
      Re: Floating point to string, and back via `%a'... Keith Thompson <kst-u@mib.org> - 2015-07-22 14:58 -0700
        Re: Floating point to string, and back via `%a'... "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-07-22 15:43 -0700
        Re: Floating point to string, and back via `%a'... "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-07-22 15:45 -0700
        Re: Floating point to string, and back via `%a'... "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-07-22 15:51 -0700
          Re: Floating point to string, and back via `%a'... Robert Wessel <robertwessel2@yahoo.com> - 2015-07-22 18:21 -0500
            Re: Floating point to string, and back via `%a'... Keith Thompson <kst-u@mib.org> - 2015-07-22 16:58 -0700
        Re: Floating point to string, and back via `%a'... Keith Thompson <kst-u@mib.org> - 2015-07-22 16:52 -0700
        Re: Floating point to string, and back via `%a'... Keith Thompson <kst-u@mib.org> - 2015-07-22 17:08 -0700
      Re: Floating point to string, and back via `%a'... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-07-23 00:01 +0100
        Re: Floating point to string, and back via `%a'... "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-07-22 19:40 -0700
          Re: Floating point to string, and back via `%a'... Keith Thompson <kst-u@mib.org> - 2015-07-22 22:32 -0700
          Re: Floating point to string, and back via `%a'... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-07-23 11:07 +0100
            Re: Floating point to string, and back via `%a'... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-07-23 11:25 +0100
              Re: Floating point to string, and back via `%a'... "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-07-24 11:44 -0700
            Re: Floating point to string, and back via `%a'... "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-07-24 11:47 -0700

csiph-web