Problems 1

  1. Theory.
    1. What is the main function?
    2. What is compilation and linking (of a C-program)? What is source code, object code, executable code?
    3. How do you compile and link a C-program which is wholly contained in one file, say main.c?
    4. How do you run the resulting executable code?
    5. How do you tell the compiler the description of the printf function? Why do you need to do this? How do you link the printf function?
    6. What is stdio.h? Why angle-brackets in #include<stdio.h>? Hint: C preprocessor: including_files.
    7. Where can you get the description of the printf function (the more possibilities you name, the better)?
    8. What are the built-in data types for numbers in the C-language?
    9. What is the result of the operation 1/2 in C?
    10. Can a variable hold values of different types in the C-language?
    11. In order to use C mathematical functions, do you need to i) include any header files? ii) link any library?
    12. What is tgmath.h? If you use tgmath.h do you need to link any library?
  2. Practice.
    1. Build a program that
      1. declares variables of types int, float, double, long double, double complex, long double complex;
      2. assigns some values to these variables;
      3. applies some binary operations and mathematical functions to these variables and stores the results in new variables;
      4. prints the values of the new variables to the standard output using printf function. You might want to read the printf format string article, section format placeholders.
      Hints:
      • There is no built-in format for complex numbers, you have to print separately the real and the complex parts using creal (double), creall (long double), cimag (double), and cimagl (long double) functions.
      • The header file complex.h defines the imaginary unit as I.
      • You can get the manual page for the printf function with the command man 3 printf although the abovementioned wikipedia article https://en.wikipedia.org/wiki/Printf is probably a bit more readable.
    2. Find out how many signigicant digits can variables of types float, double, and long double hold in them by, for example, trying to store the number 0.7777777777777777777777777777 in the variable and then printing out what is stored. The long double number must end with L:
      long double x = 0.7777777777777777777777777777L;
      
      otherwise it will be considered as double and truncated. Print numbers with format placeholders "%.20g" for float, "%.20lg" for double, "%.20Lg" for long double.

All you need to know about printf function

  1. Before using printf you need to #include<stdio.h>.

  2. The first parameter to printf function is a string of characters which is called 'format string' or 'template'. If it is the only parameter, this string is simply printed to the standard output (which by default is your terminal). For example,

    printf("hello\n");

    produces

    hello

    (the string \n represents the newline character).

  3. Beside the first parameter --- the format string --- printf can have more parameters which then must be the names of the variables (or expressions) which you want to print out. In this case the format string must contain placeholders for the parameters to be printed. Placeholders must correspond to the types of parameters to be printed!

    By default the first parameter after the format string takes the first placeholder, the second parameter after the format string takes the second placeholder and so on.

    We shall mostly print out numbers, therefore here are the simplest forms of placeholders for numbers,

  4. The full description of format placeholders can be found in wikipedia or in the man pages (man 3 printf).