/*
 *  Idioms in C
 *
 *  A collection of things you might wish to do in C that differ
 *  from the way you would do them in Java or C++.
 */

#include <stdio.h>
#include <string.h>

/*
 *  Global variables
 *
 *  Listed outside any function, these are accessible to all functions.
 *  Use them sparingly and carefully, since they can make your code confusing.
 *  However, they are invaluable for certain applications.
 */
int MyGlobal = 22;

int main(int argc, char *argv[]) {
  int i = 1, j = 2;
  float x = 3.14, y = 1.42;
  double z = 7.6816;
  char *s = "This is a string.";
  char t[255];

  /*
   *  Printing output
   *
   *  Requires stdio.h
   *  %d, %s, %f, %g, %lf are escape sequences representing variable values.
   *  The \n represents a newline character.  If you do not terminate 
   *  output with a newline, it may be buffered and not appear on the screen 
   *  until the next newline character is seen.  To force output, use
   *  fflush(stdout).
   */
  printf("This is a simple line of output.\n");
  printf("This is how you print integers:  %d, %d, %d.\n",i,j,MyGlobal);
  printf("This is how you print floats and doubles:  %f, %g, %lf.\n",x,y,z);
  printf("This is how you print a string:  %s\n",s);

  /*
   *  Reading input
   *
   *  Requires stdio.h
   *  %d, %s, %f, %g, %lf are escape sequences representing variable values.
   *  You need & to get a pointer type, unless you have one already.
   */
  printf("Please enter an integer:  ");
  scanf("%d",&i);
  printf("Please enter a float:  ");
  scanf("%f",&x);
  printf("Please enter a string:  ");
  scanf("%s",t);

  /*
   *  Comparing two strings
   *
   *  Requires string.h
   *  strcmp() returns 0 if the strings are the same, or +/-1 depending on
   *  their alphabetical relationship if they are unequal.
   */
  if (strcmp(t,"Hello") == 0) {
    printf("String said Hello.\n");
  } else {
    printf("String did not say Hello.\n");
  }

  /*
   *  Reading command line arguments
   *
   *  Main can have two input arguments, always of the types shown above.
   *  argc gives the number of arguments, and argv is an array of strings.
   *  atoi() and atof() convert from string to integer/float, respectively.
   */
  printf("%d command line arguments read.\n",argc);
  for (i = 0; i < argc; i++) {
    printf("Arg %d:  %s\n",i,argv[i]);
  }
}
