Arman Akbarian
UNIVERSITY OF BRITISH COLUMBIA
PHYSICS & ASTRONOMY DEPT.

/* AAK:
 * This is an example code to learn C programming
 * Mon Nov  7 10:19:55 PST 2011
 */

//This is a simple line comment

/* This
 * is a multi-line comment
 */


/* "Includes" the standard I/O routines to be used in the code */
#include <stdio.h>

/* Standard math functions in C included */
#include <math.h>

/* Replaces character 'PI' with '3.141592' in the entire code */
#define PI 3.141592 



/* Declaration of functions and procedures: */
double square(double r);
void findmax(int a, int b, int *c);

int main(void) {

/* Variable declaration: */
int i, j; /* integer */
float x;  /* 4 byte real number */
double y; /* 8 byte real number */
char s;   /* character */
int k;

int *p;      /* pointer p to an integer */
double u[10]; /* an array of doubles with size 10, index starts from 0 */


/* Simple output routines: */
  printf("Hello, world!\n");
  printf("Please enter an integer variable: ");

/* Input routine: */
  scanf("%d",&i);  /* %d means: "look" for an integer */

/* Note that scanf requires the address of the variable
 * to store the input, &i is the pointer to i (address of i)
 * */

/* 
 * %d : integer type
 * %f : double
 * %c:  character
 * %s:  string
 * \n:  line break
 */
  printf("You enterted: %d\n",i);


/* Math operations in C: */
  j = 5 + i;
  x = 2.5 * j; /* conversion happens */
  j++; /* increase by 1 */
  i--;
  i += j; /* equivalent to > i = i + j */
  x *= 0.3e-1;
  k = j % i; /* remainder operation  */
/* Note that there is not any power function built in C */
  y = -x;

/* Logical operators:
 * AND: &&
 * OR : ||
 * NOT:  !
 */

/* Math Comparison:
 * > < == != <= >= 
 * Note that there is no "logical" variable in C
 * the comparison and logical operators return:
 * 0 if false
 * 1 if true
 * see below:
 */
  k = ( (1 < 2) || ( 5 % 2 == 0) );
  printf("k = %d\n",k);


/* Control structure: */
  s = 'a';
  if ( 1 > 2 ) {
   printf("That's not possible!\n");

  } else if (!(s =='b')) {
     printf("!('a' == 'b') = %d\n",!(s == 'b'));
     printf("statement k=5 actually returns value: %d\n",(k=5));
  } else {
     printf("This shouldn't print\n");
  }

/* A quick control and assignment in C */
  i = 1; j = 2;
  k = ( i > j) ? i : j; /* if statement correct, i otherwise j */
  printf("k = %d\n",k);

  x = 2.3; /* float variables has to have "f" */
  int X;
  /* C is case sensitive!
   * Also note that you can define variables inline (along the program)
   */
  double h = PI; /* See line: #define PI 3.141592 */
  printf("PI = %f\n",h);

/* Switch structure: */
  X = 5;
  switch (X) {
   case 3:
     printf("Will not print this!\n");
     break;
   case 5:
     printf("C is case sensitive: x = %f  X = %d\n",x,X);
     break;
   default:
     printf("This is the default action, if none of the cases happen\n");
     break;

  }

/* Loop structures: */
  /* WHILE LOOP: */
  i = 1;
  j = 1;
  while (i < 1000) {
    i *= (j+1);
    j++;
    if ( i > 200 ) {
   break; /* abort */
    }
    else if ( j == 4 ) {
   printf("4! will not be printed\n");
   continue; /* immediate return to top */
    }
    printf("%d! = %d\n",j,i);
  }

  /* DO WHILE LOOP: */
  i = 2;
  do {
    printf("DO WHILE loop checks the condition at the end\n");
  } while (i > 3);

  /* FOR LOOP: */
  for ( i = 1; i < 10; i++ ){
     printf("%d ",i);
  }
  printf("\n");
  /* You can do more with for loop: */
  for ( i = 1, j = 1; i < 100; i*=2 , j++){
    printf("2^%d = %d \n",j-1,i);
  }

/* Calling a function: */
  double z;
  x = 2.1;
  z = square(x);
  printf("2.1^2 = %f\n",z);

/* Calling a procedure: */
  j=2;
  i=3;
  findmax(j,15,&i);
  printf("Max(%d,%d) = %d\n",j,15,i);
  /* Note that the value of j DID NOT change! */


/* Pointers in C */
  p = &i; /* p is a pointer, &i is the address of variable i */
  printf("pointer p = %p , value stored at p = %d\n",p,*p);



/*More on math functions:
 * The following functions are provided in math.h library
 * sin, cos, tan, asin, acos, atan,
 * exp, log, log10, pow, sqrt
 */
  z = 4.0*atan(1.0e0);
  printf("PI = %f\n",z);

  return 0;
}

/* Functions: */

double square(double r) {
   return r*r;
}

/* Procedure: */
/* Note that C "passes the argument by value" so
 * to be able to change the value of the passed 
 * variable in the procedure one must pass the 
 * address, not the value and use the address 
 * to change the actual value.
 */

void findmax(int a, int b, int *c) {
   if (a > b) { *c = a; } else { *c = b;}
   a = 5;
}

/* Procedure in C is simply a function with "void" return */


last update: Wed May 11, 2016