You are on page 1of 4

University of Nairobi School of Computing and Informatics CSC112 - Introduction to Programming CAT 1 - Friday 23rd November 2012 Period

of the Exam - 9.00-2.00pm Venues: Chiromo Campus, Seminar Room and Kenya Science Campus ANSWER ALL QUESTIONS WITHIN 2.00 HOURS ONLY Question 1: With the aid of a diagram, describe a typical C program development environment right from editing to execution. [10 marks] Phase 1: Creating a Program Phase 1 consists of editing a file. This is accomplished with an editor program. Two editors widely used on Linux systems are vi and emacs. Software packages for the C/C++ integrated program development environments such as Eclipse and Microsoft Visual Studio have editors that are integrated into the programming environment. You type a C program with the editor, make corrections if necessary, then store the program on a secondary storage device such as a hard disk. C program file names should end with the .c extension. Phases 2 and 3: Preprocessing and Compiling a C Program In Phase 2, the you give the command to compile the program. The compiler translates the C program into machine language-code (also referred to as object code). In a C system, a preprocessor program executes automatically before the compilers translation phase begins. The C preprocessor obeys special commands called preprocessor directives, which indicate that certain manipulations are to be performed on the program before compilation. These manipulations usually consist of including other files in the file to be compiled and performing various text replacements. The most common preprocessor directives are discussed in the early chapters; a detailed discussion of preprocessor features appears in Chapter 13. In Phase 3, the compiler translates the C program into machinelanguage code. Phase 4: Linking The next phase is called linking. C programs typically contain references to functions definedelsewhere, such as in the standard libraries or in the private libraries of groups of programmers working on a particular project. The object code produced by the C compiler typically contains holes due to these missing parts. A linker links the

object code with the code for the missing functions to produce an executable image (with no missing pieces). On a typical Linux system, the command to compile and link a program is called cc (or gcc). To compile and link a program named welcome.c type at the Linux prompt and press the Enter key (or Return key). [Note: Linux commands are case sensitive; make sure that you type lowercase cs and that the letters in the filename are in the appropriate case.] If the program compiles and links correctly, a file called a.out is produced. This is the executable image of our welcome.c program. Phase 5: Loading The next phase is called loading. Before a program can be executed, the program must first be placed in memory. This is done by the loader, which takes the executable image from disk and transfers it to memory. Additional components from shared libraries that support the program are also loaded. Phase 6: Execution Finally, the computer, under the control of its CPU, executes the program one instruction at a time. To load and execute the program on a Linux system, type ./a.out at the Linux prompt and press Enter.

Question 2 List and describe the usage of any five header files in a C program.

In computer programming, a header file is a file that allows programmers to separate certain elements of a program's source code into reusable files. Header files commonly contain forward declarations of classes, subroutines, variables, and other identifiers. Header files almost always have a .h extension. The purpose of a header file is to hold declarations for other files to use. <assert.h> <complex.h> <ctype.h> Contains the assert macro, used to assist with detecting logical errors and other types of bug in debugging versions of a program. A set of functions for manipulating complex numbers. Defines set of functions used to classify characters by their types or to convert between upper and lower case in a way that is independent of the used character set (typically ASCII or one of its extensions, although implementations utilizing EBCDIC are also known). For testing error codes reported by library functions. Defines a set of functions for controlling floating-point environment. Defines macro constants specifying the implementation-specific properties of the floating-point library. Defines exact width integer types. Defines several macros that implement alternative ways to express several standard tokens. For programming in ISO 646 variant character sets. Defines macro constants specifying the implementation-specific properties of the integer types. Defines localization functions. Defines common mathematical functions. Declares the macros setjmp and longjmp, which are used for nonlocal exits. Defines signal handling functions. For querying and specifying the alignment of objects. For accessing a varying number of arguments passed to functions. For atomic operations on data shared between threads. Defines a boolean data type. Defines several useful types and macros. Defines exact width integer types. Defines core input and output functions Defines numeric conversion functions, pseudo-random numbers generation functions, memory allocation, process control functions For specifying non-returning functions. Defines string handling functions. Defines type-generic mathematical functions. Defines functions for managing multiple Threads as well as mutexes and condition variables. Defines date and time handling functions

<errno.h> <fenv.h> <float.h> <inttypes.h> <iso646.h> <limits.h> <locale.h> <math.h> <setjmp.h> <signal.h> <stdalign.h> <stdarg.h> <stdatomic.h> <stdbool.h> <stddef.h> <stdint.h> <stdio.h> <stdlib.h> <stdnoreturn.h> <string.h> <tgmath.h> <threads.h> <time.h>

<uchar.h> <wchar.h> <wctype.h>

Types and functions for manipulating Unicode characters. Defines wide string handling functions. Defines set of functions used to classify wide characters by their types or to convert between upper and lower case

You might also like