Problems 5
  1. Theory
    1. What is EOF in the context of fscanf?
      Hints:

      man scanf | grep -A 1 -B 4 EOF
      
      and wikipedia article end of file.
    2. What are standard input, standard output and standard error in the context of POSIX programming?
      Hints:
      apropos standard | grep streams
      man stdin
      Wikipedia: standard streams

    3. Where does the function printf write to?

    4. Where does the function scanf read from?

    5. How do you redirect the standard output of a program to a file?

    6. How do you send the content of a file into a program's standard input?

    7. How do you connect the standard output of a program to the standard input of another program?

    8. What is the meaning of the following makefile variables: $@, $<, $^, CFLAGS, LDFLAGS, LDLIBS.
      Hint: GNU make's automatic variables, implicit variables, implicit rules.

    9. What will the following makefile print (after running command make)

      CFLAGS = -Wall -Ofast -std=c1x
      C = F
      all:
      	echo CFLAGS
      	echo $CFLAGS
      	echo $(C)FLAGS
      	echo $(CFLAGS)
      
    10. Suppose you have your whole C-program in one file main.c. Which of the following makefiles will compile and link the program into the executable file main?

      1. all: main
        
      2. main: main.c
        
      3. main: main.o
        
      4. all: main
        main: main.o
        main.o: main.c
        
      5. all: main
        main.o: main.c
        main: main.o
        
      6. main.o: main.c
        all: main
        main: main.o
        
      7. main: main.c
        	cc main.c -o main
        
      8. main: main.c
        	$(CC) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@
        
      9. all: main
        main: main.o
        	cc main.o -o main
        main.o: main.c
        	cc -c main.c -o main.o
        
      Remember that the tabulator-sign before the command may not be copy-pasted correctly.
    11. Suppose your main function, contained in the file main.c, calls the functions foo and bar which are contained correspondingly in the files foo.c and bar.c. Which of the following makefiles will correctly compile and link your program into the executable file main after the command make?

      1. main: main.o foo.o bar.o
        
      2. main: foo.o main.o bar.o
        
      3. main: foo.o main.o bar.o
        	cc foo.o main.o bar.o -o main
        
      4. main: main.o foo.o bar.o
        	$(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@
        
      5. main: main.o foo.o bar.o
        main.o: main.c
        foo.o: foo.c
        	cc -c $^ -o $@
        bar.o: bar.c
        
      6. main: main.o foo.o bar.o
        	cc main.o foo.o bar.o -lm -o main
        main.o: main.c
        	cc -c main.c
        foo.o: foo.c
        	cc -c foo.c
        bar.o: bar.c
        	cc -c bar.c
        
      7. obj = main.o foo.o bar.o
        main: $(obj)
        
    12. Suppose there is the following preprocessor directive at the beginning of the file,
      #define PI 3.1415927
      
      what is the effect of it?
      1. The compiler creates a variable PI of type double and stores the value 3.1415927 in it.
      2. All occurences of the token 'PI' in the file are substituted with the token '3.1415927'.
  2. Practice. Remember that exercises should be done using the make untility and Makefile.

    1. Build a program that reads a set of numbers, and writes these numbers together with their cosines in a table form into a file. The program must use
      1. streams to files;

        Hints:

        double x;
        FILE* in = fopen("input.dat","r");
        assert(in != NULL);
        FILE* out = fopen("output.dat","w");
        while( fscanf(in,"%lg ",&x) != EOF ) fprintf(out,"%lg \t %lg\n",x,cos(x));
        
        In the makefile:
        CFLAGS = -Wall -std=c99
        LDLIBS = -lm
        output.dat : fileio input.dat  ; ./$<
        input.dat  : Makefile          ; echo 0.0 1.0 2.0 3.0 4.0 > $@
        clean      :                   ; $(RM) input.dat output.dat fileio *.o
        
      2. standard input, standard output and redirections;

        Hint:

        double x;
        while( fscanf(stdin,"%lg ",&x) != EOF ) fprintf(stdout,"%lg \t %lg\n",x,cos(x));
        
        or, shorter,
        while( scanf("%lg ",&x) != EOF ) printf("%lg \t %lg\n",x,cos(x));
        
        In the makefile:
        out.stdio.txt: stdio input.dat    ;    ./stdio < input.dat > out.stdio.txt
        
      3. via command-line-arguments.

        Hint:

        for(int i=1; i<argc; i++){double x=atof(argv[i]); printf("%lg\t%lg\n",x,cos(x));}
        
        In the makefile (the automatic variable $@ stands for the name of the target of the rule):
        out.cmdio.txt: cmdio makefile ; ./cmdio 1.0 1.1 1.2 1.3 > $@
        
    2. Implement a function, let's call it "eval", which has two arguments: i) a double function of double argument, and ii) a double number. The function has to evaluate the argument-function at the argument-number and return the result.

    3. Implement a program that reads a set a double numbers from its command-line and then prints out their average to the standrd output.

    4. Implement a program that reads a sequence of double numbers from the standard input and, after reading each number, prints the average of the numbers read so far to the standard output.