nigelibrown
Posts: 8
Joined: Tue Feb 14, 2012 4:10 pm

My First Amazing Incredible C program

Wed May 23, 2012 2:27 pm

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

User avatar
SN
Posts: 1014
Joined: Mon Feb 13, 2012 8:06 pm
Location: Romiley, UK
Contact: Website

Re: My First Amazing Incredible C program

Wed May 23, 2012 3:16 pm

By the way, why are you fprintf'ing to stderr instead of doing a straight printf
Steve N – binatone mk4->intellivision->zx81->spectrum->cbm64->cpc6128->520stfm->pc->raspi ?

nigelibrown
Posts: 8
Joined: Tue Feb 14, 2012 4:10 pm

Re: My First Amazing Incredible C program

Wed May 23, 2012 3:43 pm

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?

robthebloke
Posts: 8
Joined: Fri Mar 09, 2012 1:30 pm

Re: My First Amazing Incredible C program

Wed May 23, 2012 4:03 pm

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;
}

Return to “C/C++”