Printing
2002-01-05 19:09:08
Category: c:general
Description: Example of printing text to the screen
Author: mind
Viewed: 3483
Rating: (9 votes)


/* print.c by mindsport [mind@infract.net]
 *
 * short simple example on how to print different ways..
 *
 * NOTE: in all examples \n is used to break a line
 *
 * 01/05/02
 *
 */
 
#include <stdio.h>
 
int main() {
  char text[100]; /* defining text as a string with 100 byte size */
  char string[]="test\n"; /* defining string as a string with text 'test' */
 
  /* note arg's are strings like the above examples, integers, arrays, etc.. */
 
  /* printf("text", args); */
 
  printf("Hello, World!\n");
 
  /* this example prints the arg 'string' using %s */
 
  printf("test message: %s", string);
 
  /* you can also define multiple strings in one print
   * printf("%s %s %s", string1, string2, string3); */
 
  /* fprintf(stream, "text", arg?);
   *  streams:
   *    stderr - 'Standard Error' prints "text" to the terminal no matter what
   *    *FILESTREAM - when you open a writeable file you can use fprintf to 
   *    write text to it */
 
  fprintf(stderr, "Hello, World!\n");
 
  /* sprintf(string, "text", arg?); this will add text to a string or
   * add a string to a string */
 
  sprintf(text, "Hello, World\n");
 
  /* this command can also be used as: sprintf(text, "%s", string);
   * in more advanced coding you can set limits on the size of a string
   * snprintf(text, 80, "Hello, World\n");
   *  ^  */
 
  /* after using sprintf() you can then print the string */
 
  printf("%s", text);
 
  /* with print you can also declare a command and print the results
   * printf("text %d", command); */
 
  printf("1+1 = %d\n", 1+1);
 
}