MC logo

ADT Declarations in C (Answer)

  Practice Questions

  1. Place #include "frob.h" at the top of frob.c and user.c.

  2. Do not #include "frob.c".

  3. Compile frob.c and user.c. When this is done, the compiler sees the #include directive, and looks at frob.h.

  4. To define the public interface function make_frob, place the declaration int make_frob(int m); in frob.h. In frob.c place the function definition int make_frob(int m) { ... }. You may also use the extern keyword in frob.h, giving extern int make_frob(int m);.

  5. If make_frob is mis-used, the compiler will issue an error message when it sees the mis-use: make_frob("Hi", 4.5) in user.c. This is because the call does not agree with the definition of make_frob in the #include-ed header file, frob.h. If you forget to include frob.h, then the compiler will not be able to detect the error.

  6. To define the private ruminate_frob, place the definition static void ruminate_frob(int m) { ... } in frob.c.

  7. To define the public integer, place extern int frob_count; in frob.h. Also place int frob_count = 0; in frob.c, outside the body of any function. You may use any initial value which is appropriate, but you should give frob_count an initial value in frob.c. Most C compilers do not require one, but some do, and all permit one. Therefore giving an initial value is the safest course.

  8. To define the private integer, place static int frob_code_id; in frob.c. Place it outside the body of any function.

  9. When function bogus is defined and used inconsistently, and no declaration is available in the header file to co-ordinate its use, the results are as follows. Both frob.c and user.c compile correctly. Since the compiler does not know that bogus expects an integer when it sees the call bogus(2.5), it will not give any message about the argument type mismatch, nor introduce any automatic type conversion. Some compilers will give a warning that bogus is not defined, or that a default is being used. If you are very lucky, the error will be caught at the link step, when the two compiled files are combined. Most compiler systems will not detect an error here, and will created a program that will do "interesting" things when user.c calls bogus. The 2.5 will be passed without conversion, so the function bogus will either misinterpret its parameter value, or take it from the wrong place entirely. In either case, the result will not generally be 2.5, or anything close.

<<Dynamic Allocation in C Question