Page 1 of 1

My First Amazing Incredible C program

Posted: Wed May 23, 2012 2:27 pm
by nigelibrown

Code: Select all

sudo apt-get install cmake
cd ~/Desktop
mkdir HelloW
cd HelloW
pico CMakeFiles.txt
project(rpi)
add_executable(rpi rpi.c)
^O
RETURN
^X
pico rpi.c
#include <stdio.h>
int main(int argc,char *argv[]) {
fprintf(stderr, “Hello World”);
}
^O
RETURN
^X
cmake –G “Unix Makefiles”
make
./rpi

Re: My First Amazing Incredible C program

Posted: Wed May 23, 2012 3:16 pm
by SN
By the way, why are you fprintf'ing to stderr instead of doing a straight printf

Re: My First Amazing Incredible C program

Posted: Wed May 23, 2012 3:43 pm
by nigelibrown
Habit. I use fprintf() usually as means of storing debug logs, so instead of using stderr I would open a FILE* and redirect the output there. Would be nice to know if anyone has actually tried?

Re: My First Amazing Incredible C program

Posted: Wed May 23, 2012 4:03 pm
by robthebloke
fprinting to stdout is syntactical sugar for printf. Printing program output to the error log (stderr) however, is a little bit of a no no. As for printing to a file, it's not that difficult....

Code: Select all

#include <stdio.h>

int main(int argc, char* argv[]) 
{
  FILE* fp = fopen("output.txt", "w");
  if(fp)
  {
    fprintf(fp, “Hello World!\n”);
    fclose(fp);
  }
  return 0;
}