1

Basically, printf doesn't produce output if I put it in main().

This works:

#+begin_src C
printf("Hello World!\n");
#+end_src

But this

#+begin_src C
void main() {
printf("Hello World!\n");
}
#+end_src

results in

Code block produced no output

I have tried adding :results output to the begin_src line. I have tried including stdio.h. When I look in *Messages*, I see that when I wrap the printf in main, I get the error

**Error reading results: (beginning-of-buffer)**

What is wrong here?

itsjeyd
  • 5,070
  • 2
  • 30
  • 49
William Everett
  • 751
  • 1
  • 8
  • 18

1 Answers1

2

Try this:

#+name: main
#+begin_src C
int main(int argc, char *argv[]) {
printf("Hello World!\n");

return 0;
}
#+end_src
kikiz
  • 40
  • 7
  • That worked, even without the parameters being fed into main or the `#+name: main` (and I had to put a semi-colon after `return 0`). Apparently it just needed to be an int with a return? – William Everett Jun 13 '14 at 23:20
  • It's always depends on used ANSI C standard. If it's C89/90 you can write without int, if not int is necessary. And yes you should add semicolon after return 0 it was mine mistake, thx. – kikiz Jun 14 '14 at 00:09
  • Nope. As I play further, it doesn't matter what standard I use. Adding `:flags -std=c99` allows me to use c99 features, but doesn't allow me to get output from a void function. – William Everett Jun 14 '14 at 00:36
  • 1
    void main() {...} isn't ANSI C standard, it's allowed by some C compilers but it's wrong. In ANSI C C89/C90 you can write just main() {...} cause int is implied. – kikiz Jun 14 '14 at 00:39