* C code block: no return values
@ 2013-04-16 15:53 Roger Mason
2013-04-16 18:45 ` Eric Schulte
0 siblings, 1 reply; 3+ messages in thread
From: Roger Mason @ 2013-04-16 15:53 UTC (permalink / raw)
To: emacs-orgmode
Hello,
I'm working through examples in "A Multi-Language Computing Environment for
Literate Programming and Reproducible Research" by Shulte et al. J.
Stat. Software, 46/3, 2012.
This example compiles but results are not returned to the Org-mode buffer:
========================================================
#+name: cocktail.c
#+begin_src C :noweb yes :tangle cocktail.c
#include <stdio.h>
<<cocktail-sort>>
<<main>>
#+end_src
#+name: main
#+begin_src C
int main(int argc, char *argv[]) {
int lst[argc-1];
int i;
for(i=1;i<argc;i++)
lst[i-1] = atoi(argv[i]);
sort(lst, argc-1);
for(i=1;i<argc;i++)
printf("%d ", lst[i-1]);
printf("\n");
}
#+end_src
#+name: cocktail-sort
#+begin_src C :noweb yes
void sort(int *a, unsigned int l)
{
int swapped = 0;
int i;
do {
for(i=0; i < (l-1); i++) {
<<swap>>
}
if ( swapped == 0 ) break;
swapped = 0;
for(i= l - 2; i >= 0; i--) {
<<swap>>
}
} while(swapped > 0);
}
#+end_src
#+name: swap
#+begin_src C
if ( a[i] > a[i+1] ) {
int temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
swapped = 1;
}
#+end_src
#+call: cocktail.c[:cmdline 8 7 6 3 2 4 78]()
===========================================
Running C-c on the "call" line above produces:
===============================
#+RESULTS: cocktail.c[:cmdline 8 7 6 3 2 4 78]()
================================
The answers should be here. But they aren't.
Thanks for any help.
Roger
This electronic communication is governed by the terms and conditions at
http://www.mun.ca/cc/policies/electronic_communications_disclaimer_2012.php
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: C code block: no return values
2013-04-16 15:53 C code block: no return values Roger Mason
@ 2013-04-16 18:45 ` Eric Schulte
2013-04-16 18:57 ` Roger Mason
0 siblings, 1 reply; 3+ messages in thread
From: Eric Schulte @ 2013-04-16 18:45 UTC (permalink / raw)
To: Roger Mason; +Cc: emacs-orgmode
Roger Mason <rmason@mun.ca> writes:
> Hello,
>
> I'm working through examples in "A Multi-Language Computing Environment for
> Literate Programming and Reproducible Research" by Shulte et al. J.
> Stat. Software, 46/3, 2012.
>
> This example compiles but results are not returned to the Org-mode buffer:
> ========================================================
> #+name: cocktail.c
> #+begin_src C :noweb yes :tangle cocktail.c
> #include <stdio.h>
> <<cocktail-sort>>
> <<main>>
> #+end_src
>
> #+name: main
> #+begin_src C
> int main(int argc, char *argv[]) {
> int lst[argc-1];
> int i;
> for(i=1;i<argc;i++)
> lst[i-1] = atoi(argv[i]);
> sort(lst, argc-1);
> for(i=1;i<argc;i++)
> printf("%d ", lst[i-1]);
> printf("\n");
> }
> #+end_src
>
> #+name: cocktail-sort
> #+begin_src C :noweb yes
> void sort(int *a, unsigned int l)
> {
> int swapped = 0;
> int i;
> do {
> for(i=0; i < (l-1); i++) {
> <<swap>>
> }
> if ( swapped == 0 ) break;
> swapped = 0;
> for(i= l - 2; i >= 0; i--) {
> <<swap>>
> }
> } while(swapped > 0);
> }
> #+end_src
>
> #+name: swap
> #+begin_src C
> if ( a[i] > a[i+1] ) {
> int temp = a[i];
> a[i] = a[i+1];
> a[i+1] = temp;
> swapped = 1;
> }
> #+end_src
>
> #+call: cocktail.c[:cmdline 8 7 6 3 2 4 78]()
> ===========================================
>
> Running C-c on the "call" line above produces:
>
> ===============================
> #+RESULTS: cocktail.c[:cmdline 8 7 6 3 2 4 78]()
> ================================
>
> The answers should be here. But they aren't.
>
> Thanks for any help.
>
> Roger
>
Hi Roger,
Since the publication of that paper, the code block execution engine has
begun checking the return value of the invoked program to ensure it
exits with success before parsing the output. The C program in this
example actually returns the value of the final printf, which is
non-zero and looks like a return.
To get this example working with the latest version of Org-mode, one
needs to added a "return 0;" to the end of the last code block, yielding
the following.
#+name: main
#+begin_src C
int main(int argc, char *argv[]) {
int lst[argc-1];
int i;
for(i=1;i<argc;i++)
lst[i-1] = atoi(argv[i]);
sort(lst, argc-1);
for(i=1;i<argc;i++)
printf("%d ", lst[i-1]);
printf("\n");
return 0;
}
#+end_src
With this change the example works as expected on my system.
Org-mode should do a better job of alerting the user to the reason why
it is not returning a result.
Cheers,
--
Eric Schulte
http://cs.unm.edu/~eschulte
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: C code block: no return values
2013-04-16 18:45 ` Eric Schulte
@ 2013-04-16 18:57 ` Roger Mason
0 siblings, 0 replies; 3+ messages in thread
From: Roger Mason @ 2013-04-16 18:57 UTC (permalink / raw)
To: Eric Schulte; +Cc: emacs-orgmode
Hello Eric,
On 04/16/2013 04:15 PM, Eric Schulte wrote:
> Roger Mason <rmason@mun.ca> writes:
>
>> Hello,
>>
>> I'm working through examples in "A Multi-Language Computing Environment for
>> Literate Programming and Reproducible Research" by Shulte et al. J.
>> Stat. Software, 46/3, 2012.
>>
>> This example compiles but results are not returned to the Org-mode buffer:
>>
>> Hi Roger,
>>
>> Since the publication of that paper, the code block execution engine has
>> begun checking the return value of the invoked program to ensure it
>> exits with success before parsing the output. The C program in this
>> example actually returns the value of the final printf, which is
>> non-zero and looks like a return.
>>
>> To get this example working with the latest version of Org-mode, one
>> needs to added a "return 0;" to the end of the last code block, yielding
>> the following.
>>
>> #+name: main
>> #+begin_src C
>> int main(int argc, char *argv[]) {
>> int lst[argc-1];
>> int i;
>> for(i=1;i<argc;i++)
>> lst[i-1] = atoi(argv[i]);
>> sort(lst, argc-1);
>> for(i=1;i<argc;i++)
>> printf("%d ", lst[i-1]);
>> printf("\n");
>> return 0;
>> }
>> #+end_src
>>
>> With this change the example works as expected on my system.
>>
>> Org-mode should do a better job of alerting the user to the reason why
>> it is not returning a result.
>>
>> Cheers,
>>
Thank you. It works here too.
Best wishes,
Roger
This electronic communication is governed by the terms and conditions at
http://www.mun.ca/cc/policies/electronic_communications_disclaimer_2012.php
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2013-04-16 18:57 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-04-16 15:53 C code block: no return values Roger Mason
2013-04-16 18:45 ` Eric Schulte
2013-04-16 18:57 ` Roger Mason
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.