Plug-in C expansions. Header files.
The core of the C language is small and simple. Special functionality is provided in the form of libraries of ready-made functions. This is what makes C so portable. Some libraries are provided for you, giving you access to many special abilities without needing to reinvent the wheel. You can also make your own, but to do so you need to know how your operating system builds libraries. We shall return to this later.
Libraries are files of ready-compiled code which we can merge with a C program at compilation time. Each library comes with a number of associated header files which make the functions easier to use. For example, there are libraries of mathematical functions, string handling functions and input/output functions and graphics libraries. It is up to every programmer to make sure that libraries are added at compilation time by typing an optional string to the compiler. For example, to merge with the math library libm.a you would type cc -o program_name prog.c -lm
when you compile the program. The -lm means: add in libm. If we wanted to add in the socket library libsocket.a to do some network programming as well, we would type cc -o program_name prog.c -lm -lsocket
and so on.
Why are these libraries not just included automatically? Because it would be a waste for the compiler to add on lots of code for maths functions, say, if they weren't needed. When library functions are used in programs, the appropriate library code is included by the compiler, making the resulting object code often much longer.
Libraries are supplemented by header files which define macros, data types and external data to be used in conjunction with the libraries. Once a header file has been included, it has effectively added to the list of reserved words and commands in the language. You cannot then use the names of functions or macros which have already been defined in libraries or header files to mean anything other than what the library specifies.
The most commonly used header file is the standard input/output library which is called stdio.h. This belongs to a subset of the standard C library which deals with file handling. The math.h header file belongs to the mathematics library libm.a. Header files for libraries are included by adding to the source code: #include header.h
at the top of a program file. For instance: #include "myheader.h"
includes a personal header file which is in the current directory. Or #include
includes a file which lies in a standard directory like /usr/include.
The #include directive is actually a command to the C preprocessor, which is dealt with more fully later, See Preprocessor.
Some functions can be used without having to include library files or special libraries explicitly since every program is always merged with the standard C library, which is called libc. #include
A program wishing to use a mathematical function such as cos would need to include a mathematics library header file. #include
A particular operating system might require its own special library for certain operations such as using a mouse or for opening windows in a GUI environment, for example. These details will be found in the local manual for a particular C compiler or operating system.
Although there is no limit, in principle, to the number of libraries which can be included in a program, there may be a practical limit: namely memory, since every library adds to the size of both source and object code. Libraries also add to the time it takes to compile a program. Some operating systems are smarter than others when running programs and can load in only what they need of the large libraries. Others have to load in everything before they can run a program at all, so many libraries would slow them down.
To know what names libraries have in a particular operating system you have to search through its documentation. Unix users are lucky in having an online manual which is better than most written ones.
Questions 4:
Node:Questions 4, Previous:Libraries, Up:Libraries
Questions
How is a library file incorporated into a C program?
Name the most common library file in C.
Is it possible to define new functions with the same names as standard library functions?
What is another name for a library file?
Node:Programming style, Next:Form of a C program, Previous:Libraries, Up:Top
Programming style
The shape of programs to come.
C is actually a free format language. This means that there are no rules about how it must be typed, when to start new lines, where to place brackets or whatever. This has both advantages and dangers. The advantage is that the user is free to choose a style which best suits him or her and there is freedom in the way in which a program can be structured. The disadvantage is that, unless a strict style is adopted, very sloppy programs can be the result. The reasons for choosing a well structured style are that:
Long programs are manageable only if programs are properly organized.
Programs are only understandable if care is taken in choosing the names of variables and functions.
It is much easier to find parts of a program if a strict ordering convention is maintained. Such a scheme becomes increasingly difficult to achieve with the size and complexity of the problem.
No simple set of rules can ever provide the ultimate solution to writing good programs. In the end, experience and good judgement are the factors which decide whether a program is written well or poorly written. The main goal of any style is to achieve clarity. Previously restrictions of memory size, power and of particular compilers often forced restrictions upon style, making programs clustered and difficult. All computers today are equipped with more than enough memory for their purposes, and have very good optimizers which can produce faster code than most programmers could write themselves without help, so there are few good reasons not to make programs as clear as possible.
Node:Form of a C program, Next:Comments, Previous:Programming style, Up:Top
The form of a C program
What goes into a C program? What will it look like?
C is made up entirely of building blocks which have a particular `shape' or form. The form is the same everywhere in a program, whether it is the form of the main program or of a subroutine. A program is made up of functions, functions are made up of statements and declarations surrounded by curly braces { }.
The basic building block in a C program is the function. Every C program is a collection of one or more functions, written in some arbitrary order. One and only one of these functions in the program must have the name main(). This function is always the starting point of a C program, so the simplest C program would be just a single function definition: main () {}
The parentheses () which follow the name of the function must be included even though they apparently serve no purpose at this stage. This is how C distinguishes functions from ordinary variables.
The function main() does not have to be at the top of a program so a C program does not necessarily start at line 1. It always starts where main() is. Also, the function main() cannot be called from any other function in the program. Only the operating system can call the function main(): this is how a C program is started.
The next most simple C program is perhaps a program which calls a function do_nothing and then ends. /******************************************************//* *//* Program : do nothing *//* *//******************************************************/ main() /* Main program */ {do_nothing();} /******************************************************/ do_nothing() /* Function called */ {}
The program now consists of two functions, one of which is called by the other. There are several new things to notice about this program. Firstly the function do_nothing() is called by typing its name followed by the characteristic () brackets and a semi-colon. This is all that is required to transfer control to the new function. In some languages, words like CALL or PROC are used, or even a symbol like &. No such thing is needed in C. The semi-colon is vital however. All instructions in C must end with a semi-colon. This is a signal to inform the compiler that the end of a statement has been reached and that anything which follows is meant to be a part of another statement. This helps the compiler diagnose errors.
The `brace' characters { and } mark out a block into which instructions are written. When the program meets the closing brace } it then transfers back to main() where it meets another } brace and the program ends. This is the simplest way in which control flows between functions in C. All functions have the same status as far as a program is concerned. The function main() is treated just as any other function. When a program is compiled, each function is compiled as a separate entity and then at the end the linker phase in the compiler attempts to sew them all together.
The examples above are obviously very simple but they illustrate how control flows in a C program. Here are some more basic elements which we shall cover.
comments
preprocessor commands
functions
declarations
variables
statements
The skeleton plan of a program, shown below, helps to show how the elements of a C program relate. The following chapters will then expand upon this as a kind of basic plan. /****************************************************//* *//* Skeleton program plan *//* *//****************************************************/ #include
Question 5:
Neither comments nor preprocessor commands have a special place in this list: they do not have to be in any one particular place within the program.
Node:Question 5, Previous:Form of a C program, Up:Form of a C program
Questions
What is a block?
Name the six basic things which make up a C program.
Does a C program start at the beginning? (Where is the beginning?)
What happens when a program comes to a } character? What does this character signify?
What vital piece of punctuation goes at the end of every simple C statement?
Node:Comments, Next:Functions, Previous:Form of a C program, Up:Top
0 comments: on " "
Post a Comment