Easy printing of variables

From Juneday education
Jump to: navigation, search

C

Print to stdout

In C, printing to stdout can be done using a so called printf statement,

  printf("count=%d\n", count);

or

  fprintf(stdout, "count=%d\n", count);

If you want to print the address of the count variable you use %p and &.

  fprintf(stdout, "count=%p\n", &count);

If you don't want to print only integer variables (using %d), here's a short list which of how to print other variables you might find enough:

  • %c to print a character
  • %d to print an integer
  • %ud to print an unsigned integer
  • %p to print a pointer
  • %s to print a string

Print to stderr

In C, printing to stderr can be done using a so called printf statement,

  fprintf(stderr, "count=%d\n", count);

Java

Print to stdout

In Java, printing to stdout can be done using a so called println statement,

  System.out.println("count=" + count);

Print to stderr

In Java, printing to stderr can be done using a so called println statement,

  System.err.println("count=" + count);