Questions 3
  1. How are arguments passed to functions in C: by-value or by-reference? And what does that mean?
    Hint: C syntax → argument passing.
  2. If double x=1.23, what is *(&x)?
  3. What is NULL? Hint: null pointer.
  4. What happens to variables declared inside a function when the function exits (returns)?
  5. What is a static variable? Hint: static variable.
  6. What will the following three programs print out and why?
    #include<stdio.h>
    void f(int i){i=0;}
    int main(){
    	int i=1; f(i); printf("i=%i\n",i);
    	return 0; }
    
    #include<stdio.h>
    void f(int* i){*i=0;}
    int main(){
    	int i=1; f(&i); printf("i=%i\n",i);
    	return 0; }
    
    #include<stdio.h>
    void f(int* i){i=NULL;}
    int main(){
    	int i=1; f(&i); printf("i=%i\n",i);
    	return 0; }
    
  7. If you pass an array to a function with the signature void f(double a[]) – what is actually passed to the function:
    1. the copy of the array?
    2. the pointer to the first element?
    3. the copy of the pointer to the first element?
    4. something else?
    Hint: C syntax → argument passing → array parameters.
  8. When the function with the signature void f(double a[]) gets the array as parameter – can it figure out the size of the array?
  9. At which stage—compilation or execution of the program—is the memory for the following arrays allocated? And which of these arrays "know" their sizes in the scope where they are declared?
    int a[] = {0,1,2,3,4};
    int b[5];
    int n = (argc>1 ? atoi(argv[1]):5); /* get n from the argument to the program */
    int c[n];
    int *d=(int*)malloc(5*sizeof(int));
    
  10. If you declare an array as int a[5]; and then try a[7]=1; what will happen? Hint: Segmentation fault / causes.
  11. If you declare an i) static, ii) variable-length, iii) dynamic array inside a function, can the function return it?
  12. What will the following C-program print?
    #include<stdio.h>
    int i=2; /* file scope */
    void f(){printf("i=%i\n",i);}
    int main(){
    	int i=1; /* function scope */
    	{
    		int i=0; /* block scope */
    		printf("i=%i\n",i);
    	}
    	printf("i=%i\n",i);
    	f();
    	return 0; }