01.md

Introduction

  • All information on http://mff.devnull.cz/c-prog-lang
    • see the References section for recommended materials
  • Make sure you DO SUBSCRIBE TO THE MAILING LIST (see the seminar http page above).
  • Getting credits - see the seminar web page above.
  • What is C.
  • Popularity of C: TIOBE_index on wikipedia, direct link to the current TIOBE_index
  • C89, C99, C11 (ie. 2011), C17 (only fixes issues found in C11) standards, and upcoming C23. Due to time constraints, we will focus on C99.
  • Why it's worth learning 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, also see PureDarwin), major parts of Windows kernel, OpenSSH, OpenSSL, Apache, NGINX, cURL, etc. etc.)
    • Based on the previous, C programmers will be needed virtually for ever
    • Fast, very portable
    • Battle proven
    • Great cost/benefit ratio wrt spent time learning the language
    • Still cool and fun
  • Objectives of the seminar
    1. You should be able to write and understand non-trivial C code (we focus on C99).
    2. You should be able to recognise whether C is appropriate for solving a specific problem.
    3. You should understand why it may be so easy to get burned when working in C.
  • 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: https://devnull-cz.github.io/unix-linux-prog-in-c/cstyle.html
  • Source code files are in https://github.com/devnull-cz/c-prog-lang/tree/master/src

Objective of the first class

  • The objective of today's class is to provide all the basic building blocks so that you can write code for moving a star (*) in a loop from left to right, and back, on a terminal line, as a home assignment.
  • To see what we mean, compile it via cc src/moving-star.c (clone the git repo first, see Introduction for more info) and run it via ./a.out.
  • Obviously, do not look at our code until you have written your own.
  • Now, let's move on!

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 the ViM editor 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

    • well, there are exceptions: see freestanding environment if interested. Another exception is implementation defined. It is out of scope for this seminar though.
  • string literals (AKA string constants): 👀 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 may or may not warn you but it will let you do it (use -Werror to treat warnings as errors). With gcc, use the -Wall option to show all warnings.
      • it will print garbage for conversions without a matching argument -- whatever is on the stack (x86 32 bit) or in a specific register (x86 64 bit) 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 another variable, and so on
	int i = 13;
	int j = i;
	int k = i + j;
  • 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 (e.g. you cannot do i + 1 = j)

     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 for 1-9 inches, use ints only (not floats)

    • 👀 inches-to-cm.c
    • use \t escape sequence for printf to print tabelators
    • like this:
     printf("\tX\tY\n");
    • example output:
     Inches	Centimeters
     1	2
     2	5
     3	7
     4	10
     5	12
     6	15
     7	17
     8	20
     9	22
    
  • 🔧 use floats for the conversion code

    • 👀 inches-to-cm2.c
      • '\t' in a string will print a tab
      • 5 is the minimum field width
      • .2 is the precision
      • see the printf(3) man page for details
    • example output:
     Inches	Centimeters
     1	 2.54
     2	 5.08
     3	 7.62
     4	10.16
     5	12.70
     6	15.24
     7	17.78
     8	20.32
     9	22.86
    
  • 🔧 print fahrenheit to centigrade table. Use floats.

    • the formula to convert F to C is: (F - 32) × 5/9. E.g. 72F is 22.22C.
    • 👀 fahr-to-cent.c
    • example output:
       0	-17.78
      20	 -6.67
      40	  4.44
      60	 15.56
      80	 26.67
     100	 37.78
     120	 48.89
     140	 60.00
     160	 71.11
     180	 82.22
     200	 93.33
     220	104.44
     240	115.56
     260	126.67
     280	137.78
    

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.  For
   *	  example:
   *
   *		poll(NULL, 0, 50);
   *
   *    - 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, with the star moving between those two
   * column-like barriers:
   *
   * |                                            *     |
   */

When you have a working solution, you can check out our code at src/moving-star.c.