CS 551. Programming Graphical User Interfaces

Self-Test on Personal C Programming Skills

X applications require strong C programming skills, since they essentially involve object-oriented programming in a non-OOP language. How well do you know C?

Obviously, you can write C programs to find out the answers. But you should be able to answer most or all these "without cheating" if you are really ready for X programming. The best reference, by the way, is Harbison and Steele's C: A Reference Manual, published by Prentice Hall and available through the bookstore.

Function parameters

Passing parameters by-reference and by-value: What do the two terms mean?
What is the difference between the way you declare an integer that is passed by reference and one that is passed by value?
How does each appear in the statement that invokes your function?

Argument coercion: Suppose your routine receives an unsigned long as its argument, but you need to use it as an integer. How would you coerce it?

Sending functions as arguments: Suppose your routine receives a pointer to a function as its first argument, as in the example

	routine (foo, n);
	int foo ();
	int n;
	{ ... code ... }
How will the code invoke that function, passing it the integer value as its single argument?

Declarations and typedefs

Typedefs: What is the purpose of a typedef?
How would you define a type num_ptr, that is a pointer to an integer?
How would you define a type num_array, that is a pointer to an array of 100 integers?
How would you define a type complex, that is a structure for a complex number (containing two part, real and imag, each of which is a double)?

Forward declarations: Under what circumstances do you need to make a forward declaration of a function? Any other data type?

C declarators: Typedefs and declarations are two instances of C statements that use "declarators"; typecasts also do. What is x, according to each of the following declarators?

	int *x;
	int *x();
	int **x;
	int **x();
	int *x[10]();
(Hint: don't be too sure you know, unless you are aware of the so-called right-left rule for C declarators.) If you're really confident, what is
	int *(*(*(*x)()) [10]) ();
This is from Harbison/Steele, p. 83.

Structures and unions

Structure names: Is it wrong for a to appear twice in this declaration? Explain
	struct a {int num1, num2;} a;

Padding of structures: Why is the size of the following two structures different? How would you prove it?

	struct y {double big;
	          float med; int small;
	          char tiny; char tiny2};
	struct z {double big;
	          char tiny; float med;
	          char tiny2; int small;};

Unions: What is the difference between the definition for y above, and this one?

	union y {double big;
	          float med; int small;
	          char tiny; char tiny2};
How big is the union, in bytes?

Preprocessing features

Macros versus functions: Why is a macro preferable to a function for simple operations?
Under what circumstances is it necessary to use a function rather than a macro?

Macro names: By convention, how do macro names usually differ from variable or function names?

Conditional compilation: How is this specified, and what is it for?