2

I tried to run a sample from C Primer Plus:

Listing 2.1 The first.c Program
#+BEGIN_SRC C :results output
#include <stdio.h>
int main(void)                /* a simple program             */
{
    int num;                  /* define a variable called num */
    num = 1;                  /* assign a value to num        */

    printf("I am a simple "); /* use the printf() function    */
    printf("computer.n");
    printf("My favorite number is %d because it is first.n",num);

    return 0;
}
#+END_SRC

It report mysterious errors as:

/tmp/babel-xEtnj6/C-src-mefAEj.c:9:15: error: stray ‘\302’ in program
    9 | int main(void)                /* a simple program             */
      |               ^
/tmp/babel-xEtnj6/C-src-mefAEj.c:9:16: error: stray ‘\240’ in program
    9 | int main(void)                /* a simple program             */
      |                ^
/tmp/babel-xEtnj6/C-src-mefAEj.c:9:17: error: stray ‘\302’ in program
    9 | int main(void)                /* a simple program             */

If main() was removed, it works:

#+BEGIN_SRC C
printf("Literature Programming");
#+END_SRC

#+RESULTS:
: Literature Programming

Unfortunately, most C code is encapsulated in 'main`.

How could I get the first example working?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
AbstProcDo
  • 19,953
  • 19
  • 81
  • 138
  • It isn't mysterious. This is a ***very*** common error when copying code from web pages, [PDF](https://en.wikipedia.org/wiki/Portable_Document_Format) documents, through chat (e.g. [Skype Chat](https://en.wikipedia.org/wiki/Features_of_Skype#Skype_chat) or [Facebook Messenger](https://en.wikipedia.org/wiki/Facebook_Messenger)), etc. The canonical question is *[Compilation error: stray ‘\302’ in program, etc.](https://stackoverflow.com/questions/19198332)*. – Peter Mortensen Apr 25 '23 at 16:53
  • cont' - 302 is a signature for the start of (some of) the UTF-8 sequences. In this case, it is likely 302 240 (octal) → 0xC2 0xA0 (hexadecimal) → UTF-8 sequence for Unicode code point U+00A0 ([NO-BREAK SPACE](https://www.charset.org/utf-8)). – Peter Mortensen Apr 25 '23 at 16:59
  • It can be searched for (and replaced) by the regular expression `\x{00A0}`. Indeed, there are 101 instances of NO-BREAK SPACE in this code... (and none of the other common ones) – Peter Mortensen Apr 25 '23 at 17:00
  • What is *"org babel"*? Is it related to [Emacs](https://en.wikipedia.org/wiki/Emacs)' [Org-mode](https://en.wikipedia.org/wiki/Org-mode)? Or is it something else? – Peter Mortensen Apr 25 '23 at 17:45
  • OK, *"org-babel is an extension of GNU [Emacs](https://en.wikipedia.org/wiki/Emacs) [Org-mode](https://en.wikipedia.org/wiki/Org-mode)"* – Peter Mortensen May 23 '23 at 19:42
  • Re *"Literature Programming"*: Do you mean *"[literate programming](https://en.wikipedia.org/wiki/Literate_programming)"* (of [Donald Knuth](https://en.wikipedia.org/wiki/Donald_Knuth))? That would explain the [CWEB](https://en.wikipedia.org/wiki/Web_(programming_system)#CWEB)-like code (the `#+`). – Peter Mortensen May 23 '23 at 19:42
  • Should this be tagged differently? – Peter Mortensen May 23 '23 at 19:44
  • Is the first line actually part of the code? – Peter Mortensen May 23 '23 at 19:47
  • The real problem is there are ***101 no-break spaces*** (Unicode code point U+00A0 (NO-BREAK SPACE)) in the code as posted, instead of regular space... This is known to happen if code is copied through [Skype Chat](https://en.wikipedia.org/wiki/Features_of_Skype#Skype_chat). – Peter Mortensen May 23 '23 at 19:54
  • This is a ***very*** common error when copying code from web pages, [PDF](https://en.wikipedia.org/wiki/Portable_Document_Format) documents, through chat (e.g. [Skype Chat](https://en.wikipedia.org/wiki/Features_of_Skype#Skype_chat) or [Facebook Messenger](https://en.wikipedia.org/wiki/Facebook_Messenger)), etc. The canonical question is *[Compilation error: stray ‘\302’ in program, etc.](https://stackoverflow.com/questions/19198332)*. – Peter Mortensen May 23 '23 at 19:55

1 Answers1

3

You can try by adding :main no to your code block

#+BEGIN_SRC C :results output :main no
#include <stdio.h>

int main(void)                /* a simple program             */
{
    int num;                  /* define a variable called num */
    num = 1;                  /* assign a value to num        */

    printf("I am a simple "); /* use the printf() function    */
    printf("computer.n");
    printf("My favorite number is %d because it is first.n",num);

    return 0;
}
#+END_SRC

Also note that there are other useful modifiers like :flags, :lib, :cmdline... See Header Arguments for C, C++, D Source Code Blocks for further details.

Picaud Vincent
  • 10,518
  • 5
  • 31
  • 70