getting started

platforms and compilers

classic examples

Some c compilers don't require the include statement for standard librarys.

The line

 #include<stdio.h>
tells the compiler that you want to use the functions which are declared in the library for standard input/output. printf and scanf don't belong to the core C language, so you've to add this line in the beginning of the program if you're doing in/output.

program 1

#include<stdio.h>

int main() {

  /* this is the classic 
   * hello world example
   */

  printf("hello, world!\n");
}

program 2

#include<stdio.h>

int main() {

  /* what's the difference to the
   * program above?
   * what are the rules for placing
   * semicola?
   */

  printf("hello ");
  printf("world!");
  printf("\n");
}

program 3

#include<stdio.h>

int main(int argc, char** argv) {


  /* you don't have to understand 
   * all of this...
   */

  printf("%s\n",argv[1]);
}

program 4

#include<stdio.h>

int main() {

  int i;
  int j;
  int sum;

  i = 1;
  j = 2;
  sum = i+j;

  printf("i, j, i+j, %d %d %d\n",i,j,i+j);
}

program 5

#include<stdio.h>

int main() {

  /* what does this compute?
   * do you remember more digits of pi?
   */

  float pi, r, A; 

  pi = 3.1415;
  r = 2.0;
  A = pi * r * r;

  printf("r, A, %f %f\n",r,A);
}

program 6

#include<stdio.h>

int main() {

  float r;
  scanf("%f",&r);
  printf("%f",r);
}

program 7

#include<stdio.h>

int main() {

  float pi;
  float r;
  float A;

  pi = 3.1415;

  printf("enter radius:");
  scanf("%f",&r);
  A = pi * r * r;
  printf("r, A, %f %f\n",r,A);
}  

 

summary

compiling under unix