Reserved words and an example
C programs are constructed from a set of reserved words which provide control and from libraries which perform special functions. The basic instructions are built up using a reserved set of words, such as main, for, if,while, default, double, extern, for, and int, to name just a few. These words may not be used in just any old way: C demands that they are used only for giving commands or making statements. You cannot use default, for example, as the name of a variable. An attempt to do so will result in a compilation error.
See All the Reserved Words, for a complete list of the reserverd words.
Words used in included libaries are also, effectively, reserved. If you use a word which has already been adopted in a library, there will be a conflict between your choice and the library.
Libraries provide frequently used functionality and, in practice, at least one library must be included in every program: the so-called C library, of standard functions. For example, the stdio library, which is part of the C library, provides standard facilities for input to and output from a program.
In fact, most of the facilities which C offers are provided as libraries that are included in programs as plug-in expansion units. While the features provided by libraries are not strictly a part of the C language itself, they are essential and you will never find a version of C without them. After a library has been included in a program, its functions are defined and you cannot use their names.
printf:
Example 1:
Output 1:
Questions 2:
Node:printf, Next:Example 1, Previous:Reserved words & example, Up:Reserved words & example
The printf() function
One invaluable function provided by the standard input/output library is called printf or `print-formatted'. It provides an superbly versatile way of printing text. The simplest way to use it is to print out a literal string: printf ("..some string...");
Text is easy, but we also want to be able to print out the contents of variables. These can be inserted into a text string by using a `control sequence' inside the quotes and listing the variables after the string which get inserted into the string in place of the control sequence. To print out an integer, the control sequence %d is used: printf ("Integer = %d",someinteger);
The variable someinteger is printed instead of %d. The printf function is described in full detail in the relevant chapter, but we'll need it in many places before that. The example program below is a complete program. If you are reading this in Info, you can copy this to a file, compile and execute it.
Node:Example 1, Next:Output 1, Previous:printf, Up:Reserved words & example
Example Listing/***********************************************************//* Short Poem *//***********************************************************/ #include /***********************************************************/ main () /* Poem */ { printf ("Astronomy is %dderful \n",1); printf ("And interesting %d \n",2); printf ("The ear%d volves around the sun \n",3); printf ("And makes a year %d you \n",4); printf ("The moon affects the sur %d heard \n",5); printf ("By law of phy%d great \n",6); printf ("It %d when the the stars so bright \n",7); printf ("Do nightly scintill%d \n",8); printf ("If watchful providence be%d \n",9); printf ("With good intentions fraught \n"); printf ("Should not keep up her watch divine \n"); printf ("We soon should come to %d \n",0); }
Node:Output 1, Next:Questions 2, Previous:Example 1, Up:Reserved words & example
OutputAstronomy is 1derful \n"And interesting 2The ear3 volves around the sunAnd makes a year 4 youThe moon affects the sur 5 heardBy law of phy6d greatIt 7 when the the stars so brightDo nightly scintill8If watchful providence be9With good intentions fraughtShould not keep up her watch divineWe soon should come to 0
Node:Questions 2, Previous:Output 1, Up:Reserved words & example
Questions
Write a command to print out the message "Wow big deal".
Write a command to print out the number 22?
Write two commands to print out "The 3 Wise Men" two different ways.
Why are there only a few reserved command words in C?
Node:Operating systems, Next:Libraries, Previous:Reserved words & example, Up:Top
Operating systems and environments

Where is a C program born? How is it created?
The basic control of a computer rests with its operating system. This is a layer of software which drives the hardware and provides users with a comfortable environment in which to work. An operating system has two main components which are of interest to users: a user interface (often a command language) and a filing system. The operating system is the route to all input and output, whether it be to a screen or to files on a disk. A programming language has to get at this input and output easily so that programs can send out and receive messages from the user and it has to be in contact with the operating system in order to do this. In C the link between these two is very efficient.
Operating systems vary widely but most have a command language or shell which can be used to type in commands. Recently the tendency has been to try to eliminate typing completely by providing graphical user interfaces (GUIs) for every purpose. GUIs are good for carrying out simple procedures like editing, but they are not well suited to giving complicated instructions to a computer. For that one needs a command language. In the network version of this book we shall concentrate on Unix shell commands since they are the most important to programmers. On microcomputers command languages are usually very similar in concept, though more primitive, with only slightly different words for essentially the same commands. (This is a slightly superficial view).
When most compiler languages were developed, they were intended to be run on large mainframe computers which operated on a multi-user, time-sharing principle and were incapable of interactive communication with the user. Many compiler languages still have this inadequacy when carried over to modern computers, but C is an exception, because of its unique design. Input and output are not actually defined as a fixed, unchanging part of the C language. Instead there is a standard file which has to be included in programs and defines the input/output commands that are supported by the language for a particular computer and operating system. This file is called a standard C library. (See the next chapter for more information.) The library is standard in the sense that C has developed a set of functions which all computers and operating systems must implement, but which are specially adapted to your system.
Files devices:
Filenames:
Command languages:
Questions 3:
Node:Files devices, Next:Filenames, Previous:Operating systems, Up:Operating systems
Files and Devices
The filing system is also a part of input/output. In many operating systems all routes in and out of the computer are treated by the operating system as though they were files or data streams (even the keyboard!). C does this implicitly (it comes from Unix). The file from which C normally gets its input from is called stdin or standard input file and it is usually the keyboard. The corresponding route for output is called "stdout" or standard output file and is usually a monitor screen. Both of these are parts of stdio or standard input output. The keyboard and the monitor screen are not really files, of course, they are `devices', (it is not possible to re-read what has been sent to the monitor", or write to the keyboard.), but devices are represented by files with special names, so that the keyboard is treated as a read-only file, the monitor as a write only file... The advantage of treating devices like this is that it is not necessary to know how a particular device works, only that it exists somewhere, connected to the computer, and can be written to or read from. In other words, it is exactly the same to read or write from a device as it is to read or write from a file. This is a great simplification of input/output! The filenames of devices (often given the lofty title `pseudo device names') depend upon your particular operating system. For instance, the printer might be called "PRN" or "PRT". You might have to open it explicitly as a file. When input is taken solely from the keyboard and output is always to the screen then these details can just be forgotten.
Node:Filenames, Next:Command languages, Previous:Files devices, Up:Operating systems
Filenames
The compiler uses a special convention for the file names, so that we do not confuse their contents. The name of a source program (the code which you write) is filename.c. The compiler generates a file of object code from this called filename.o, as yet unlinked. The final program, when linked to libraries is called filename on Unix-like operating systems, and filename.EXE on Windows derived systems. The libraries themselves are also files of object code, typically called liblibraryname.a or liblibraryname.so. Header files are always called libname.h.
The endings `dot something' (called file extensions) identify the contents of files for the compiler. The dotted endings mean that the compiler can generate an executable file with the same name as the original source - just a different ending. The quad file and the object file are only working files and should be deleted by the compiler at the end of compilation. The .c suffix is to tell the compiler that the file contains a C source program and similarly the other letters indicate non-source files in a convenient way. To execute the compiler you type, cc filename
For example, cc foo.c
Node:Command languages, Next:Questions 3, Previous:Filenames, Up:Operating systems
Command Languages and Consoles
In order to do anything with a compiler or an editor you need to know a little about the command language of the operating system. This means the instructions which can be given to the system itself rather than the words which make up a C program. e.g. ls -l less filename emacs filename
In a large operating system (or even a relatively small one) it can be a major feat of recollection to know all of the commands. Fortunately it is possible to get by with knowing just handful of the most common ones and having the system manual around to leaf through when necessary.
Another important object is the `panic button' or program interruption key. Every system will have its own way of halting or terminating the operation of a program or the execution of a command. Commonly this will involve two simultaneous key presses, such as CTRL C, CTRL Z or CTRL-D etc. In GNU/Linux, CTRL-C is used.
Node:Questions 3, Previous:Command languages, Up:Operating systems
Questions
What is an operating system for?
What is a pseudo-device name?
If you had a C source program which you wanted to call `accounts' what name would you save it under?
What would be the name of the file produced by the compiler of the program in 3?
How would this program be run?
Node:Libraries, Next:Programming style, Previous:Operating systems, Up:Top
Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

0 comments: on " "