Theoretical questions

  1. What is the main function?
  2. What is source code, object code, executable code?
  3. What is compilation and linking (of a C-program)?
  4. How do you compile and link a C-program which is wholly contained in one file, say main.c?
  5. How do you run the resulting executable code?
  6. How do you tell the compiler the description of the printf function? Why do you need to do this?
  7. How do you link the printf function?
  8. What is stdio.h?
  9. Why angle-brackets in #include<stdio.h>?
  10. Where can you get the description of the printf function (the more possibilities you name, the better)?
  11. What are the primitive data types for numbers in the C-language?
  12. Do you need to declare a variable before use in the C-language? In the Octave/MATLAB language?
  13. Can a variable hold values of different types in the C-language? In the Octave/MATLAB language?
  14. Is there any difference in how the binary operators (*,/,+,-) work in the C-language and Octave/MATLAB? For example, what is 1/2 in C and in MATLAB/Octave?
  15. How do you run an Octave script in the batch mode?
  16. How do you write to a terminal from an Octave script?
  17. What is the option that instructs the gcc compiler to print out all warnings?
  18. Name all comparison operators in C.
  19. What does this piece of code do: a>b?a:b; ?
  20. In your terminal, how can you read the manual page for the creall function?
  21. What is the function prototype of the cimagl function?
  22. If you use functions from complex.h, do you need to link your program with -lm option?
  23. Rewrite the loop while(condition)body using the for loop.
  24. Rewrite the loop for(init;cond;inc)body using the while loop.
  25. Rewrite the loop do body while(condition); using the for loop.
  26. How are the arguments to functions passed in the C-language: according to the pass-by-value or the pass-by-reference strategy? And what does that mean?
  27. If you pass an array to a subroutine with the signature void do_something_with_an_array(int a[]); – is it the copy of the whole rray that is passed, or only a copy of the pointer to the first element of the array?
  28. Do arrays in C know their sizes?
  29. At which stage—compilation or execution of the program—is the memory for the following arrays allocated?
    int a[] = {0,1,2,3,4};
    int b[5];
    int n = (double)rand()/RAND_MAX*10+1;
    int c[n];
    int *d=(int*)malloc(n*sizeof(int)); 
  30. If you declare a static/variable-length/dynamic array inside a function, can the function return it?
  31. Will these functions leak memory?
    void stat(){
    	double a[500];
    }
    
    void vla(size_t n){
    	double a[n];
    }
    
    void mal(){
    	double* a=(double*)malloc(500*sizeof(double));
    }
    
    void maf(){
    	double* a=(double*)malloc(500*sizeof(double));
    	free(a);
    }
    
    double* mar(){
    	double* a=(double*)malloc(500*sizeof(double));
    	return a;
    }
    
  32. Which of these are valid C-declarations of variables?
    struct vector {double x,y,z;};
    struct vector v = {1,2,3};
    struct vector x = {1,2,3};
    struct vector v = [1,2,3];
    struct vector u = {.x=1,.y=2,.z=3};
    struct vector w = {.z=3,.y=2,.x=1};
    vector y;
    typedef struct vector vector;
    vector z;
    
  33. What is the meaning of the arguments of the following main function?
    int main(int argc, char* argv[]) { /* do stuff */ }
    
  34. How can you convert a parameter of the main function into double or int number?
  35. What is the purpose of the following makefile variables CC, CFLAGS, CXXFLAGS, CPPFLAGS, LDFLAGS, LDLIBS.
  36. 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
      
  37. 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)
      
  38. 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'.
  39. What will the following code print?
    double k=0; 
    void foo(double x){ printf( "k*x= %g\n", k*x); }
    
    int main(void) {
      k=1; foo(1);
      k=2; foo(1);
      k=3; foo(1);
    return 0;}
    
  40. Suppose you are using 'gcc' with which you can define functions inside functions. What will the following code print?
    double k=0; 
    void foo(double x){ printf( "k*x= %g\n", k*x); }
    
    int main(void) {
      double k=0;
      void foo(double x){ printf( "k*x= %g\n", 500*k*x); }
      k=1; foo(1);
      k=2; foo(1);
      k=3; foo(1);
    return 0;}
    
  41. How can you find out the declaration of the struct gsl_odeiv2_system?
  42. Find the file on your box which contains the declaration of the object gsl_multiroot_function and read the declaration.
  43. Suppose you want to use the revtex4 document class from American Physical Society. How do you install it on your box from Ubuntu repositories?