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


Groups > comp.lang.c > #109716

Re: solved Re: obtaining a c value from a perl script

From Manfred <invalid@invalid.add>
Newsgroups comp.lang.c
Subject Re: solved Re: obtaining a c value from a perl script
Date 2017-05-10 23:21 +0200
Organization Aioe.org NNTP Server
Message-ID <of0084$1fp2$1@gioia.aioe.org> (permalink)
References (6 earlier) <01776996-90c9-4721-ab10-ab6149d828fa@googlegroups.com> <enat1jFqnvhU1@mid.individual.net> <32cc7dc7-4419-44a3-b0f5-50d1a7f0d400@googlegroups.com> <endhaeFbvqgU1@mid.individual.net> <874lwuazve.fsf@bsb.me.uk>

Show all headers | View raw


On 05/09/2017 04:06 PM, Ben Bacarisse wrote:
> fred flintstone <fred@server.invalid> writes:
> 
> <snip>
>> This program was called with "./jup".
>> Value from perl : 2457987.259259
>> Can't use an undefined value as a SCALAR reference at 3.neptune.pl line 102.
>> $
>>
>> #include <stdio.h>
>> #include <stdlib.h>
>>
>> int main (int argc, char *argv[])
>> {
>>    double d;
>>    printf ("This program was called with \"%s\".\n",argv[0]);
>>    if (argc == 2)
>>      {
>>         d = atof(argv[1]);
>>         printf("Value from perl : %f\n", d );
>>         char array[20] = "Hello from C";
>>         return ( array );
> 
> You compiler should have told you that this was, at best, dodgy.  main
> is an int valued function and 'array' is not an int.
> 
> Just as you are choosing to pass the number to your program as a string,
> you need to get the results back as a string.  To do that you *print*
> the result:
> 
>    #include <stdio.h>
>    #include <stdlib.h>
> 
>    int main (int argc, char *argv[])
>    {
>         if (argc == 2)
>              printf("%f\n", atof(argv[1]) / 2);
>         else fprintf(stderr,  "Error: 1 argument expected.\n");
>         return 0;
>    }
> 
> and in Perl you collect this output for later use:
> 
>    $ perl -e 'print "C program returns ", `./jup 22`;'
>    C program returns 11.000000
> 
> <snip>
> 

It may be worth noting (if not, please forgive my redundancy) that there 
are in fact two outputs from the C program here:
1) the return value from main() - this (int) value is returned to the OS 
as exit status of the process. Being constrained as an int value (a 
single machine word in most (all?) architectures) its use is often 
limited to little more than a success/failure code.

2) the output stream (stdout, implicitly used by printf) - this is 
redirected by the calling process (the Perl interpreter in this case) 
into some receiver/container (the script context in this case, thanks to 
the syntax that Ben gave you: `./jup 22`). Being a stream, this can 
accommodate for (virtually) any kind of data. Here a stream of chars 
containing a textual representation of the double value atof(argv[1]) / 2.

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


Thread

obtaining a c value from a perl script fred flintstone <fred@server.invalid> - 2017-04-30 04:36 -0700
  Re: obtaining a c value from a perl script Noob <root@127.0.0.1> - 2017-04-30 14:48 +0200
    Re: obtaining a c value from a perl script fred flintstone <fred@server.invalid> - 2017-05-01 02:25 -0700
      Re: obtaining a c value from a perl script Keith Thompson <kst-u@mib.org> - 2017-05-01 08:46 -0700
        Re: obtaining a c value from a perl script fred flintstone <fred@server.invalid> - 2017-05-03 02:12 -0700
          Re: obtaining a c value from a perl script Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-05-03 11:48 +0100
  Re: obtaining a c value from a perl script Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-04-30 16:16 +0100
    Re: obtaining a c value from a perl script fred flintstone <fred@server.invalid> - 2017-05-01 04:18 -0700
      Re: obtaining a c value from a perl script Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-05-01 13:41 +0100
        Re: obtaining a c value from a perl script Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2017-05-03 05:10 -0700
          Re: obtaining a c value from a perl script Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-05-03 15:00 +0100
          Re: obtaining a c value from a perl script Keith Thompson <kst-u@mib.org> - 2017-05-03 08:55 -0700
            Re: obtaining a c value from a perl script Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2017-05-03 09:02 -0700
              Re: obtaining a c value from a perl script Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-05-03 17:22 +0100
                Re: obtaining a c value from a perl script fred flintstone <fred@server.invalid> - 2017-05-08 01:43 -0700
                Re: obtaining a c value from a perl script Keith Thompson <kst-u@mib.org> - 2017-05-08 09:06 -0700
                Re: obtaining a c value from a perl script fred flintstone <fred@server.invalid> - 2017-05-09 00:35 -0700
                Re: obtaining a c value from a perl script David Brown <david.brown@hesbynett.no> - 2017-05-09 13:59 +0200
              Re: obtaining a c value from a perl script fred flintstone <fred@server.invalid> - 2017-05-08 02:45 -0700
                Re: obtaining a c value from a perl script Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2017-05-08 04:39 -0700
                solved Re: obtaining a c value from a perl script fred flintstone <fred@server.invalid> - 2017-05-09 02:43 -0700
                Re: solved Re: obtaining a c value from a perl script Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-05-09 15:06 +0100
                Re: solved Re: obtaining a c value from a perl script fred flintstone <fred@server.invalid> - 2017-05-10 01:03 -0700
                Re: solved Re: obtaining a c value from a perl script Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-05-10 11:18 +0100
                Re: solved Re: obtaining a c value from a perl script fred flintstone <fred@server.invalid> - 2017-05-12 02:26 -0700
                Re: solved Re: obtaining a c value from a perl script Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-05-12 11:51 +0100
                Re: solved Re: obtaining a c value from a perl script fred flintstone <fred@server.invalid> - 2017-05-12 03:49 -0700
                Re: solved Re: obtaining a c value from a perl script Manfred <invalid@invalid.add> - 2017-05-10 23:21 +0200
                Re: solved Re: obtaining a c value from a perl script Keith Thompson <kst-u@mib.org> - 2017-05-10 16:23 -0700
                Re: solved Re: obtaining a c value from a perl script Robert Wessel <robertwessel2@yahoo.com> - 2017-05-11 15:57 -0500
                Re: solved Re: obtaining a c value from a perl script Jerry Stuckle <jstucklex@attglobal.net> - 2017-05-11 18:57 -0400
                Re: solved Re: obtaining a c value from a perl script Robert Wessel <robertwessel2@yahoo.com> - 2017-05-11 18:38 -0500
                Re: solved Re: obtaining a c value from a perl script Robert Wessel <robertwessel2@yahoo.com> - 2017-05-11 18:46 -0500
                Re: solved Re: obtaining a c value from a perl script Jerry Stuckle <jstucklex@attglobal.net> - 2017-05-11 21:11 -0400
                Re: solved Re: obtaining a c value from a perl script Robert Wessel <robertwessel2@yahoo.com> - 2017-05-12 01:51 -0500
                Re: solved Re: obtaining a c value from a perl script Jerry Stuckle <jstucklex@attglobal.net> - 2017-05-14 23:49 -0400
                Re: obtaining a c value from a perl script Tim Rentsch <txr@alumni.caltech.edu> - 2017-05-09 07:46 -0700
            Re: obtaining a c value from a perl script mark.bluemel@gmail.com - 2017-05-03 09:06 -0700
              Re: obtaining a c value from a perl script gazelle@shell.xmission.com (Kenny McCormack) - 2017-05-03 17:02 +0000
              Re: obtaining a c value from a perl script fred flintstone <fred@server.invalid> - 2017-05-08 01:51 -0700
      Re: obtaining a c value from a perl script Manfred <noname@invalid.add> - 2017-05-04 18:21 +0200
        Re: obtaining a c value from a perl script fred flintstone <fred@server.invalid> - 2017-05-08 03:52 -0700
          Re: obtaining a c value from a perl script Jerry Stuckle <jstucklex@attglobal.net> - 2017-05-08 08:38 -0400
          Re: obtaining a c value from a perl script Keith Thompson <kst-u@mib.org> - 2017-05-08 09:14 -0700
          Re: obtaining a c value from a perl script "James R. Kuyper" <jameskuyper@verizon.net> - 2017-05-08 12:17 -0400
            Re: obtaining a c value from a perl script Keith Thompson <kst-u@mib.org> - 2017-05-08 09:26 -0700
              Re: obtaining a c value from a perl script jameskuyper@verizon.net - 2017-05-08 11:02 -0700
                Re: obtaining a c value from a perl script Keith Thompson <kst-u@mib.org> - 2017-05-08 11:39 -0700
              Re: obtaining a c value from a perl script Tim Rentsch <txr@alumni.caltech.edu> - 2017-05-09 07:16 -0700
                Re: obtaining a c value from a perl script Keith Thompson <kst-u@mib.org> - 2017-05-09 08:18 -0700
                Re: obtaining a c value from a perl script fred flintstone <fred@server.invalid> - 2017-05-10 00:29 -0700
                Re: obtaining a c value from a perl script Tim Rentsch <txr@alumni.caltech.edu> - 2017-05-10 05:26 -0700
            Re: obtaining a c value from a perl script Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-05-08 18:20 +0100

csiph-web