01.md

Introduction

  • all information on http://mff.devnull.cz/c-prog-lang
    • see the References section for materials
  • make sure you DO SUBSCRIBE TO THE MAILING LIST (see the seminar http page above)
  • getting credits - see the seminar page
  • what is C
  • C89, C99, C11 (ie. 2011) standards. Due to time constraints, we will focus on C99.
  • why it's worth to learn C
    • helps with better understanding of computers
    • lingua franca of programming
    • lots of important code in C (Linux and Android kernel, Apple iOS kernel (Darwin), major parts of Windows kernel, OpenSSH, OpenSSL, Apache, NGINX, cURL, etc. etc.)
    • C programmers will be needed virtually for ever
    • fast, very portable
    • still cool and fun
  • objectives of the seminar
  • we are here to help you understand concepts, see the big picture, and learn new stuff. SO, IF YOU HAVE A QUESTION, ASK.
    • Ideally use the mailing list so that it's beneficial for others, too.
  • please do read the C style document and DO USE the C style. The link is on the seminar page, or you can get it here: http://mff.devnull.cz/cstyle/.
  • source code files are in https://github.com/devnull-cz/c-prog-lang/tree/master/src

First C program: "Hello, world"

  • 👀 hello-world1.c will compile with warnings
  • use gcc hello-world1.c to compile it, ./a.out to run it
    • or gcc -o hello-world1 hello-world1.c if you want a specific output
  • we recommend vim(1) to edit your files (use ":syntax on" for syntax highlighting if not the default settings)
  • C runtime system is very small, printf is not part of it, that's why you need an include file to provide the function prototype (ie. the return type and its parameters)
  • fixed code with no warnings: 👀 hello-world2.c

Basics

  • to get the source code for the examples, do the following on the command line. However, do not look at it until you write your own version. Just compile and run it first to see what each program does.

     git clone https://github.com/devnull-cz/c-prog-lang.git
    
  • each program must have a main() function

  • string literals: 👀 printf.c

  • use the return operator to return a function value

    • in the main() funtion, return exits the program
    • in the shell, use echo $? to see the return value of the most recently run program on the foreground
    • only the least significant byte taken as an unsigned integer (0-255) is relevant
    • 👀 return.c
    • if you do not use return from main() and the ending } is reached, the program returns 0 (in C89 it would be a random number though).
  • you must declare a variable before you can use it

  • printf() can use conversion specifications, each starts with %

    • int i; printf("%d\n", i);
      • a character like d is called a conversion specifier
    • see man 3 printf for the gory details
    • number of conversions must match the number of arguments
      • 👀 👀 printf2.c
      • the compiler will warn you but it will let you do it (use -Werror to treat warnings as errors)
      • it will print garbage for conversions without a matching argument (whatever is on the stack is printed).
  • you can declare and initialize a variable at the same time

    • int i = 13;
    • 13 is called an initializer
    • you can initialize a variable with an expression as well
      • int i = 13; int j = i;
  • arithmetics

    • == is for equality, = for an assignment
      • memory was precious in the past, and programs usually had more assignments than comparisons
    • +, -, /, *
    • ++, --
      • int i; i = 13; printf("%d\n", i++); will print 13 and then increment i
      • int i; i = 13; printf("%d\n", ++i); will first increment i then print 14
      • ++i is an expression, not a variable, so you cannot assign to it
        • this will not compile: ++i = 13;
    • 👀 👀 arithmetics.c
  • save for an assignment, anywhere you can use a variable, you can use an expression

     printf("%d\n", 100 * 2);
  • if both operands are ints, the result is an int

    • printf("%d\n", 9 / 5) will print 1
  • while loop

  • if statement

  • floating point numbers

    • 👀 👀 float.c
    • see the optional minimum field width and precision
    • experiment!!!
  • 🔧 print out a table for inch to centimeter conversion

    • 👀 👀 inches-to-cm.c
    • use "\t" escape sequence for printf to print tabelators
    • like this:
     printf("\tX\tY\n");
  • 🔧 use floats for the conversion code

    • 👀 👀 inches-to-cm2.c
      • '\t' in a string will print a tab
      • 6 is the minimum field width
      • .2 is the precision
      • see the printf(3) man page for details
  • 🔧 print fahrenheit to centigrade table. Use floats.

Assignment for the first class

  /*
   * Implement a moving star that zick zacks on the __SAME__ line between some
   * boundary (say 50 character wide).
   *
   * You will only need:
   *
   *	- while loop
   *	- if statement (do not use else)
   *	- printf()
   *	- use a character '\r' to return to the beginning of a line
   *
   *	- use "poll(NULL, 0, <ms>)" to sleep <ms> miliseconds, do not worry
   *	  about not understanding what exactly it does.  To make the compiler
   *	  not complain, use "#include <poll.h>".  Alternatively, you can use
   *	  "sleep(1)" (#include <unistd.h>) but it is too slow then.
   *
   *    - you will also need "fflush(stdout)" after each line is printed.  As
   *      standard error is buffered in the C library, the text will generally
   *      not be printed by printf() until a new line is printed, which will
   *      never be the case here. So, the fflush() call makes sure all buffered
   *      text is printed out.
   *
   * We expect something like this:
   *
   * |                                            *     |
   */