The C Programming Language
Objective-C is based on a programming language called, quite simply, C. The origins of the C programming language can be traced back nearly 40 years to two engineers named Dennis Ritchie and Ken Thompson working at what is now known as AT&T Bell Labs. At the time, the two were working on developing the UNIX operating system on PDP-7 and PDP-11 systems. After attempts to write this operating system using assembly language (essentially using sequences of instruction codes understood by the processor), it was decided that a higher level, more programmer friendly programming language was required to handle the complexity of an operating system such as UNIX. The first attempt was a language called B. The B language, which was based on a language called BCPL, was found to be lacking. Taking the next initial from the BCPLname, the C language was created and subsequently used to write much of the UNIX operating system kernel and infrastructure. As far as we can tell, C was so successful that new languages named P and L never needed to be created.
a tutorial for the computer programming language C. Unlike BASIC or
Pascal, C was not written as a teaching aid, but as a professional tool. Programmers love
C! Moreover, C is a standard, widely-used language, and a single C program can often be
made to run on many dierent kinds of computer.
1 Introduction
What is a high-level language? Why is C unusual?
Any suciently complex object has levels of detail; the amount of detail we see depends
on how closely we scrutinize the object. A computer has many levels of detail.
The terms low level and high level are often used to describe these layers of complexity
in computers. The low level is buried in the computer's microchips and microcircuits. The
low level is the level at which the computer seems most primitive and mechanical, whereas
the high level describes the computer in less detail, and makes it easier to use.
You can see high levels and low levels in the workings of a car. In a car, the nuts, bolts,
and pistons of the low level can be grouped together conceptually to form the higher-level
engine. Without knowing anything about the nuts and bolts, you can treat the engine as a
black box: a simple unit that behaves in predictable ways. At an even higher level (the one
most people use when driving), you can see a car as a group of these black boxes, including
the engine, the steering, the brakes, and so on. At a high level, a computer also becomes a
group of black boxes.
C is a high-level language. The aim of any high-level computer language is to provide
an easy, natural way to give a list of instructions (a computer program) to a computer.
The native language of the computer is a stream of numbers called machine language. As
you might expect, the action resulting from a single machine language instruction is very
primitive, and many thousands of them can be required to do something substantial. A
high-level language provides a set of instructions you can recombine creatively and give
to the imaginary black boxes of the computer. The high-level language software will then
translate these high-level instructions into low-level machine language instructions.
1.1 The advantages of C
C is one of a large number of high-level languages designed for general-purpose program-
ming, in other words, for writing anything from small programs for personal amusement to
complex industrial applications.
C has many advantages:
Before C, machine-language programmers criticized high-level languages because, with
their black box approach, they shielded the user from the working details of the com-
puter and all its facilities. C, however, was designed to give access to any level of the
computer down to raw machine language, and because of this, it is perhaps the most
exible high-level language.
C has features that allow the programmer to organize programs in a clear, easy, logical
way. For example, C allows meaningful names for variables without any loss of eciency, yet it gives a complete freedom of programming style, including exible ways of making decisions, and a set of exible commands for performing tasks repetitively
1: Introduction
C is succinct. It permits the creation of tidy, compact programs. This feature can
be a mixed blessing, however, and the C programmer must balance simplicity and
readability.
C allows commands that are invalid in other languages. This is no defect, but a powerful
freedom which, when used with caution, makes many things possible. It does mean
that there are concealed di
culties in C, but if you write carefully and thoughtfully,you can create fast, e
cient programs. With C, you can use every resource your computer o
ers. C tries to link closely withthe local environment, providing facilities for gaining access to common peripherals
like disk drives and printers. When new peripherals are invented, the GNU community
quickly provides the ability to program them in C as well. In fact, most of the GNU
project is written in C (as are many other operating systems).
For the reasons outlined above, C is the preeminent high-level language. Clearly, no
language can guarantee good programs, but C can provide a framework in which it is easy
to program well.
1.2 Questions for Chapter 1
1. Explain the distinction between high levels and low levels.
2. What is a \black box"?
3. Name a few advantages to programming in the C language.The compiler 3
2 Using a compiler
How to use a compiler. What can go wrong.
The operating system is the layer of software that drives the hardware of a computer and
provides the user with a comfortable work environment. Operating systems vary, but most
have a shell, or text interface. You use the GNU shell every time you type in a command
that launches an email program or text editor under GNU.
In the following sections of this chapter, we will explore how to create a C program from
the GNU shell, and what might go wrong when you do.
2.1 Basic ideas about C
First a note about a programming language that is di
erent from the C programminglanguage, the GNU shell. When you enter commands in the GNU shell, they are executed
immediately. Moreover, the shell is a programming language, in that the commands you
type are a program, because you can also create a text
le containing many shell commands.When you run this
le, the commands will be executed in sequence.On the other hand, consider C. While a shell command
le can be executed directly, aC program must be created in two stages:
1. First, the program is written in the form of text
les with a text editor such as GNUEmacs. This form of the program is called the source code. A computer cannot execute
source code directly.
2. Second, the completed source code is processed with a compiler | a program that
generates a new
le containing a machine-language translation of the source code.This
le is called an executable le, or executable. The executable le is said to havebeen compiled from the source code.
To run the compiled program, you must usually type the name of the executable
lepreceded by a period and a slash, as in this example:
./myprogram
The \dot-slash" pre
x tells the GNU shell to look in the current directory for the executable.You usually do not need to type `./' in front of commands for programs that came with
your GNU system, such as emacs, because the computer already knows where to look for
the executables of those programs, which were placed in special directories when your GNU
system was installed.
A C program is made up of, among other components, variables and functions. A variable
is a way to hold some data which may vary, hence the name. For example, a variable might
hold the number 17, and later the number 41. Another variable might hold the word \Sue".
A function is a segment of text in the source code of a program that tells the computer
what to do. Programming consists, in large part, of writing functions.4 Chapter 2: Using a compiler
2.2 The compiler
When you compile a program, the compiler usually operates in an orderly sequence of
phases called passes. The sequence happens approximately like this:
1. First, the compiler reads the source code, perhaps generating an intermediate code
(such as pseudo-code) that simpli
es the source code for subsequent processing.2. Next, the compiler converts the intermediate code (if there is any) or the original
source code into an object code
le, which contains machine language but is not yetexecutable. The compiler builds a separate object
le for each source le. These areonly temporary and are deleted by the compiler after compilation.
3. Finally, the compiler runs a linker. The linker merges the newly-created object code
with some standard, built-in object code to produce an executable
le that can standalone.
GNU environments use a simple command to invoke the C compiler: gcc, which stands for
\GNU Compiler Collection". (It used to stand for \GNU C Compiler", but now GCC can
compile many more languages than just C.) Thus, to compile a small program, you will
usually type something like the following command:
gcc
le nameOn GNU systems, this results in the creation of an executable program with the default
name `a.out'. To tell the compiler you would like the executable program to be called
something else, use the `-o' option for setting the name of the object code:
gcc -o program name
le nameFor example, to create a program called `myprog' from a
le called `myprog.c', writegcc -o myprog myprog.c
To launch the resulting program `myprog' from the same directory, type
./myprog
2.3 File names
GCC uses the following
le name conventions:Source code
le program name.cObject
le program name.oExecutable
le program name (no ending)Header
le name.hLibrary
le libname.a or libname.soThe
le name endings, or le extensions, identify the contents of les to the compiler. Forexample, the `.c' su
x tells the compiler that the le contains C source code, and the otherletters indicate other kinds of
les in a similar way.Errors 52.4 Errors
Errors are mistakes that programmers make in their code. There are two main kinds of
errors.
Compile-time errors are errors caught by the compiler. They can be syntax errors, such
as typing fro instead of for, or they can be errors caused by the incorrect construction
of your program. For example, you might tell the compiler that a certain variable is
an integer, then attempt to give it a non-integer value such as 5.23. (See Section 2.4.2
[Type errors], page 6.)
The compiler lists all compile-time errors at once, with the line number at which each
error occurred in the source code, and a message that explains what went wrong.
For example, suppose that, in your
le `eg.c' you writey = sin (x];
instead of
y = sin (x);
(By the way, this is an example of assignment. With the equals sign (`='), you are
assigning the variable y (causing the variable y to contain) the sine of the variable x.
This is somewhat di
erent from the way equals signs work in mathematics. In math,an equals sign indicates that the numbers and variables on either side of it are already
equal; in C, an equals sign makes things equal. Sometimes it is useful to think of the
equals sign as an abbreviation for the phrase \becomes the value of ".)
Ignore the syntactic details of the statements above for now, except to note that clos-
ing the (x) with a square bracket instead of a parenthesis is an error in C. Upon
compilation, you will see something like this error message:
error
eg.c: In function `main':
eg.c:8: parse error before `]'
(If you compile the program within Emacs, you can jump directly to the error. We will
discuss this feature later. See Chapter 23 [Debugging], page 231, for more information.)
A program with compile-time errors will cause the compiler to halt, and will not produce
an executable. However, the compiler will check the syntax up to the last line of your
source code before stopping, and it is common for a single real error, even something
as simple as a missing parenthesis, to result in a huge and confusing list of nonexistent
\errors" from the compiler. This can be shocking and disheartening to novices, but
you'll get used to it with experience. (We will provide an example later in the book.
See Chapter 23 [Debugging], page 231.)
As a rule, the best way to approach this kind of problem is to look for the
rst error,x that, and then recompile. You will soon come to recognize when subsequent error
messages are due to independent problems and when they are due to a cascade.
Run-time errors are errors that occur in a compiled and running program, sometimes
long after it has been compiled.
One kind of run-time error happens when you write a running program that does not
do what you intend. For example, you intend to send a letter to all drivers whose6 Chapter 2: Using a compiler
licenses will expire in June, but instead, you send a letter to all drivers whose licenses
will ever expire.
Another kind of run-time error can cause your program to crash, or quit abruptly. For
example, you may tell the computer to examine a part of its memory that doesn't exist,
or to divide some variable by zero. Fortunately, the GNU environment is extremely
stable, and very little will occur other than an error message in your terminal window
when you crash a program you are writing under GNU.
If the compilation of a program is successful, then a new executable
le is created.When a programmer wishes to make alterations and corrections to a C program, these
must be made in the source code, using a text editor; after making the changes, the pro-
grammer must recompile the program, or its salient parts.
2.4.1 Typographical errors
The compiler can sometimes fail for very simple reasons, such as typographical errors,
including the misuse of upper- and lower-case characters. The C language is case-sensitive.
Unlike languages such as Pascal and some versions of BASIC, C distinguishes between
upper- and lower-case letters, such as `A' and `a'. If a letter is typed in the wrong case in a
critical place in the source code, compilation will fail. This is a potential source of errors
that are di
cult to nd.2.4.2 Type errors
C supports a variety of variable types (di
erent kinds of variables for di erent kinds ofdata), such as integer for integer numbers, and float for numbers with fractional parts.
You can even de
ne your own types, such as total for a sum, or surname for someone'slast name. You can also convert a variable of one type into other types. (This is called type
coercion.) Consequently, the type of a variable is of great importance to the compiler.
C requires us to list the names and types of all variables that will be used in a program,
and provide information about where they are going to be used. This is called declaring
variables. If you fail to declare a variable, or use it as if it were a di
erent type from thetype it is declared to be, for example, by assigning a non-integer value to an integer variable,
you will receive a compile-time error.
See Chapter 5 [Variables and declarations], page 19, for more information on variable
declarations. See Chapter 3 [The form of a C program], page 9, for some simple examples
of variable declarations.
2.5 Questions for Chapter 2
1. What is a compiler?
2. How does one run a C program?Questions for Chapter 2 7
3. How does one usually compile a C program?
4. Are upper and lower case equivalent in C?
5. What the two main kinds of error that can occur in a program?
6. If you had some C source code that you wished to call \accounts", under what name
would you save it?
7. What would be the name of the executable
le for the program in the last question?8. How would you run this program?8 Chapter 2: Using a compilerA word about style 9
3 The form of a C program
What goes into a C program? What does one look like?
The basic building block of a C program is the function. Every C program is a collection
of one or more functions. Functions are made of variable declarations and statements, or
complex commands, and are surrounded by curly brackets (`{' and `}').
One and only one of the functions in a program must have the name main. This function
is always the starting point of a C program, so the simplest C program is a single function
de
nition:main ()
{
}
The parentheses `()' that follow the name of the function must be included. This is how C
distinguishes functions from ordinary variables.
The function main does not need to be at the top of a program, so a C program does not
necessarily start at line 1, but wherever the function called main is located. The function
main cannot be called, or started, by any other function in the program. Only the operating
system can call main; this is how a C program is started.
The next most simple C program is perhaps a program that starts, calls a function that
does nothing, and then ends.
/******************************************************/
/* */
/* Program : do nothing */
/* */
/******************************************************/
main() /* Main program */
{
do_nothing();
}
/******************************************************/
do_nothing() /* Function called */
{
}
(Any text sandwiched between `/*' and `*/' in C code is a comment for other humans to
read. See the section on comments below for more information.)
There are several things to notice about this program.
First, this program consists of two functions, one of which calls the other.
Second, the function do_nothing is called by simply typing the main part of its name
followed by `()' parentheses and a semicolon.
Third, the semicolon is vital; every simple statement in C ends with one. This is a signal
to the compiler that the end of a statement has been reached and that anything that follows
is part of another statement. This signal helps the compiler diagnose errors.10 Chapter 3: The form of a C program
Fourth, the curly bracket characters `{' and `}' outline a block of statements. When
this program meets the closing `}' of the second function's block, it transfers control back
to `main', where it meets another `}', and the program ends.
3.1 A word about style
The code examples above are simple, but they illustrate the control
ow of a C program,
or the order in which its statements are executed. You should note that these programs are
written in \old-fashioned" C, as the language existed before ANSI Standard C | the version
in which most C programs are now written. The above programs are also missing several
key elements that most C programs have, such as header
les and function prototypes.Finally, they do not show good style; if you wish to submit programs you write to the Free
Software Foundation, you should consult its advice on how best to use the C language.
You may wonder why we chose old-style C for these
rst few examples, even thoughpeople proverbially learn best what they learn
rst. We did so because pre-ANSI C is con-siderably simpler than the present form, and also because as you develop as a C programmer,
you will probably run across some old C code that you will want to read.
You may also wonder why a savvy programmer would want to follow the ANSI Standard,
which was drafted by committee, or even the GNU guidelines. Isn't programming free
software all about freedom? Yes, but following the ANSI Standard ensures that your code
can be easily compiled on many other computer platforms, and the GNU guidelines ensure
that your code can be read by other programmers. (We will introduce good C style in our
examples soon. Meanwhile, you can examine the GNU guidelines later in the book. See
Chapter 22 [Style], page 227.)
3.2 Comments
Annotating programs.
Comments are a way of inserting remarks and reminders into code without a
ecting itsbehavior. Since comments are only read by other humans, you can put anything you wish
to in a comment, but it is better to be informative than humorous.
The compiler ignores comments, treating them as though they were whitespace (blank
characters, such as spaces, tabs, or carriage returns), and they are consequently ignored.
During compilation, comments are simply stripped out of the code, so programs can contain
any number of comments without losing speed.
Because a comment is treated as whitespace, it can be placed anywhere whitespace is
valid, even in the middle of a statement. (Such a practice can make your code di
cult toread, however.)
Any text sandwiched between `/*' and `*/' in C code is a comment. Here is an example
of a C comment:Questions for Chapter 3 11
/* ...... comment ......*/
Comments do not necessarily terminate at the end of a line, only with the characters
`*/'. If you forget to close a comment with the characters `*/', the compiler will display an
`unterminated comment' error when you try to compile your code.
3.3 Example 1
#include <stdio.h> /* header file */
main () /* Trivial program */
{
/* This little line has no effect */
/* This little line has none */
/* This little line went all the way down
to the next line,
And so on...
And so on...
And so on... */
do_little();
printf ("Function 'main' completing.\n");
}
/**********************************************/
/* A bar like the one above can be used to */
/* separate functions visibly in a program */
do_little ()
{
/* This function does little. */
printf ("Function 'do_little' completing.\n");
}
Again, this example is old-fashioned C, and in mediocre style. To make it more compliant
with the ANSI Standard and GNU guidelines, we would declare the variable type each
function returns (int for main, which also requires an exit or return statement), and we
would create function prototypes at the beginning of the
le. (See Chapter 4 [Functions],page 13.)
3.4 Questions for Chapter 312 Chapter 3: The form of a C program
1. What is a block?
2. Does a C program start at the beginning? Where is the beginning?
3. What happens when a program comes to a `}' character? What does this character
signify?
4. What vital piece of punctuation goes at the end of every simple C statement?
5. What happens if a comment is not ended? That is if the programmer types `/*' .. to
start but forgets the ..`*/' to close?Function examples 13
4 Functions
Solving problems and getting results.
A function is a section of program code that performs a particular task. Making functions
is a way of isolating one section of code from other independent sections. Functions allow a
programmer to separate code by its purpose, and make a section of code reusable | that
is, make it so the section can be called in many di
erent contexts.Functions should be written in the following form:
type function name (type parameter1 name, type parameter2 name, ...)
{
variable declarations
statements
...
...
...
}
You may notice when reading the examples in this chapter that this format is somewhat
di
erent from the one we have used so far. This format conforms to the ANSI Standardand is better C. The other way is old-fashioned C, although GCC will still compile it.
Nevertheless, GCC is not guaranteed to do so in the future, and we will use ANSI Standard
C in this text from now on.
As shown above, a function can have a number of parameters, or pieces of information
from outside, and the function's body consists of a number of declarations and statements,
enclosed by curly brackets: `{...}'.
4.1 Function names
Every function has a name by which it is known to the rest of the program. The name
of a function in C can be anything from a single letter to a long word. The ANSI Standard,
however, only guarantees that C will be able to distinguish the
rst 31 letters of identi ers, orfunction and variable names. (Identi
ers are therefore said to have 31 signi cant characters.)In some cases, identi
ers may have as few as six signi cant characters, to stay compatiblewith older linkers, but this part of the ANSI Standard is becoming obsolete.
A function name must begin with an alphabetic letter or the underscore `_' character,
but the other characters in the name can be chosen from the following groups:
Any lower-case letter from `a' to `z'
Any upper-case letter from `A' to `Z'
Any digit from `0' to `9'
The underscore character `_'
Note that with GCC, you can also use dollar signs (`$') in identi
ers. This is one ofGCC's extensions to the C language, and is not part of the ANSI standard. It also may not
be supported under GCC on certain hardware platforms.14 Chapter 4: Functions
4.2 Function examples
Here is an example of a function that adds two integers and prints the sum with C's \print
formatted" function named printf, using the characters `%d' to specify integer output.
void add_two_numbers (int a, int b) /* Add a and b */
{
int c;
c = a + b;
printf ("%d\n", c);
}
The variables a and b are parameters passed in from outside the function. The code de
nesa, b, and c to be of type int, or integer.
The function above is not much use standing alone. Here is a main function that calls
the add_two_numbers function:
int main()
{
int var1, var2;
var1 = 1;
var2 = 53;
add_two_numbers (var1, var2);
add_two_numbers (1, 2);
exit(0);
}
When these functions are incorporated into a C program, together they print the number
54, then they print the number 3, and then they stop.
4.3 Functions with values
In mathematics, a function takes one or more values and calculates, or returns, another
value. In C, some functions return values and others do not; whether a function you write
does or does not will depend on what you want the function to do. For example, a function
that calculates a value should probably return that value, while a function that merely
prints something out may not need to.
The add_two_numbers function above did not return a value. We will now examine a
function that does.
Here is an example of calling a function that returns a value:
bill = calculate_bill (data1, data2, data3);
When this statement is executed, control is passed to the function calculate_bill, that
function executes, and then it returns control and some value to the original statement.
The value returned is assigned to bill, and the program continues.Function prototyping 15
In C, returning a value from a function is a simple matter. Consider the function
calculate_bill as it might be written in a program that contains the statement above:
int calculate_bill (int a, int b, int c)
{
int total;
total = a + b + c;
return total;
}
As soon as the return statement is met, calculate_bill stops executing and returns the
value total.
A function that returns a value must have a return statement. Forgetting it can ruin a
program. For instance if calculate_bill had read as follows, then the variable bill would
have had no meaningful value assigned to it, and you might have received a warning from
the compiler as well. (The word void below indicates that the function does not return a
value. In ANSI C, you must place it before the name of any such function.)
void calculate_bill (int a, int b, int c)
{
int total;
total = a + b + c;
}
On the other hand, you do not need to actually use a value when a function returns
one. For example, the C input/output functions printf and scanf return values, but the
values are rarely used. See hunde
nedi [ les], page hunde nedi, for more information onthese functions.
If we use the
rst version of the calculate_bill function (the one that contains theline return total;), the value of the function can simply be discarded. (Of course, the
resulting program is not very useful, since it never displays a value to the user!)
int main()
{
calculate_bill (1, 2, 3);
exit (0);
}
4.4 Function prototyping
Functions do not have to return integer values, as in the above examples, but can return
almost any type of value, including
oating point and character values. (See Chapter 5
[Variables and declarations], page 19, for more information on variable types.)
A function must be declared to return a certain variable type (such as an integer),
just as variables must be. (See Chapter 5 [Variables and declarations], page 19, for more
information about variable types.) To write code in good C style, you should declare what
type of value a function returns (and what type of parameters it accepts) in two places:16 Chapter 4: Functions
1. At the beginning of the program, in global scope. (See Chapter 6 [Scope], page 27.)
2. In the de
nition of the function itself.Function declarations at the beginning of a program are called prototypes. Here is an
example of a program in which prototypes are used:
#include <stdio.h>
void print_stuff (int foo, int bar);
int calc_value (int bas, int quux);
void print_stuff (int foo, int bar)
{
int var_to_print;
var_to_print = calc_value (foo, bar);
printf ("var_to_print = %d\n", var_to_print);
}
int calc_value (int bas, int quux)
{
return bas * quux;
}
int main()
{
print_stuff (23, 5);
exit (0);
}
The above program will print the text `var_to_print = 115' and then quit.
Prototypes may seem to be a nuisance, but they overcome a problem intrinsic to com-
pilers, which is that they compile functions as they come upon them. Without function
prototypes, you usually cannot write code that calls a function before the function itself is
de
ned in the program. If you place prototypes for your functions in a header le, however,you can call the functions from any source code
le that includes the header. This is onereason C is considered to be such a
exible programming language.
Some compilers avoid the use of prototypes by making a
rst pass just to see whatfunctions are there, and a second pass to do the work, but this takes about twice as long.
Programmers already hate the time compilers take, and do not want to use compilers
that make unnecessary passes on their source code, making prototypes a necessity. Also,
prototypes enable the C compiler to do more rigorous error checking, and that saves an
enormous amount of time and grief.
4.5 The exit functionQuestions for Chapter 4 17
GNU coding standards specify that you should always use exit (or return) within your
main function. (See Chapter 22 [Style], page 227.)
You can use the exit function to terminate a program at any point, no matter how
many function calls have been made. Before it terminates the program, it calls a number
of other functions that perform tidy-up duties such as closing open
les.exit is called with a return code, like this:
exit(0);
In the example above, the return code is 0. Any program that calls your program can read
the return code from your program. The return code is like a return value from another
function that is not main; in fact, most of the time you can use the return command within
your main, instead of exit.
Conventionally, a return code of 0 speci
es that your program has ended normally andall is well. (You can remember this as \zero errors", although for technical reasons, you
cannot use the number of errors your program found as the return code. See Chapter 22
[Style], page 227.) A return code other than 0 indicates that some sort of error has occurred.
If your code terminates when it encounters an error, use exit, and specify a non-zero return
code.
4.6 Questions for Chapter 4
1. Write a function that takes two values a and b, then returns the value of a * b (that
is, a times b.)
2. Is there anything wrong with a function that returns no value?
3. What happens if a function returns a value but you do not assign that value to anything?
4. What happens if a variable is assigned the result of a function, but the function does
not return a value?
5. How can you make a program terminate, anywhere in the program?18 Chapter 4: FunctionsInteger variables 19
5 Variables and declarations
Storing data. Discriminating types. Declaring data.
Variable names in C follow the same rules as function names, as far as what characters
they can contain. (See Section 4.1 [Function names], page 13.) Variables work di
erentlyfrom functions, however. Every variable in C has a data type, or type, that conveys to the
the compiler what sort of data will be stored in it. Functions in C are sometimes said to
have types, but a function's type is actually the data type of the variable it returns.
In some older computer languages like BASIC, and even some newer ones like Perl, you
can tell what type a variable is because its name begins or ends with a special character.
For example, in many versions of BASIC, all integer variable names end with a percent sign
(`%') | for example, `YEAR%'. No such convention exists in C. Instead, we declare variables,
or tell the compiler that they are of a certain type, before they are used. This feature of C
has the following advantages (among others):
It gives a compiler precise information about the amount of memory that will have to
be allotted to a variable when a program is run, and what sort of arithmetic will have
to be used with it (e.g. integer,
oating point, or none at all).
It provides the compiler with a list of the variables so that it can catch errors in the
code, such as assigning a string to an integer variable.
There are a lot of variable types in C. In fact, you can de
ne your own, but there aresome basic types ready for use. We will discuss them in the following sections.
5.1 Integer variables
C has
ve kinds of integer. An integer is a whole number (a number without a fractionalpart). In C, there are a limited number of integers possible; how many depends on the type
of integer. In arithmetic, you can have as large a number as you like, but C integer types
always have a largest (and smallest) possible number.
char: A single byte, usually one ASCII character. (See the section on the char type
below.)
short: A short integer (16 bits long on most GNU systems). Also called short int.
Rarely used.
int: A standard integer (32 bits long on most GNU systems).
long: A long integer (32 bits long on most GNU systems, the same as int). Also called
long int.
long long: A long long integer (64 bits long on most GNU systems). Also called long
long int.
64-bit operating systems are now appearing in which long integers are 64 bits long. With
GCC, long integers are normally 32 bits long and long long integers are 64 bits long, but
it varies with the computer hardware and implementation of GCC, so check your system's
documentation.20 Chapter 5: Variables and declarations
These integer types di
er in the size of the integer they can hold and the amount ofstorage required for them. The sizes of these variables depend on the hardware and operating
system of the computer. On a typical 32-bit GNU system, the sizes of the integer types are
as follows.
Type Bits Possible Values
char 8 -127 to 127
unsigned char 8 0 to 255
short 16 -32,767 to 32,767
unsigned short 16 0 to 65,535
int 32 -2,147,483,647 to 2,147,483,647
unsigned int 32 0 to 4,294,967,295
long 32 -2,147,483,647 to 2,147,483,647
unsigned long 32 0 to 4,294,967,295
long long 64 -9,223,372,036,854,775,807 to 9,223,372,036,854,775,807
unsigned long long 64 0 to 18,446,744,073,709,551,615
On some computers, the lowest possible value may be 1 less than shown here; for example,
the smallest possible short may be -32,768 rather than -32,767.
The word unsigned, when placed in front of integer types, means that only positive or
zero values can be used in that variable (i.e. it cannot have a minus sign). The advantage
is that larger numbers can then be stored in the same variable. The ANSI standard also
allows the word signed to be placed before an integer, to indicate the opposite of unsigned.
5.1.1 The char type
char is a special integer type designed for storing single characters. The integer value of
a char corresponds to an ASCII character. For example, a value of 65 corresponds to the
letter `A', 66 corresponds to `B', 67 to `C', and so on.
As in the table above, unsigned char permits values from 0 to 255, and signed char
permits values from -127 (or -128) to 127. The char type is signed by default on some
computers, but unsigned on others. (See Appendix E [Character conversion table], page 259.
See Appendix D [Special characters], page 257.)
char is used only within arrays; variables meant to hold one character should be declared
int. (See Chapter 15 [Strings], page 105, for more information on character arrays. See
Section 5.4.1 [Cast operator demo], page 23, for an example of how to use an integer variable
to hold a character value.)
5.1.2 Floating point variablesDeclarations 21
Floating point numbers are numbers with a decimal point. There are di
erent sizesof
oating point numbers in C. The float type can contain large
oating point numbers
with a small degree of precision, but the double-precision double type can hold even larger
numbers with a higher degree of precision. (Precision is simply the number of decimal
places to which a number can be computed with accuracy. If a number can be computed
to
ve decimal places, it is said to have ve signi cant digits.)All
oating point mathematical functions built into C require double or long float
arguments (long float variables are generally the same as double variables on GNU sys-
tems), so it is common to use float only for storage of small
oating point numbers, and
to use double everywhere else.
Here are the
oating point variable types available in C:
float: A single-precision
oating point number, with at least 6 signi
cant decimaldigits.
double: A double-precision
oating point number. Usually the same as long float
on GNU systems. Has at least 10 signi
cant decimal digits. long double: Usually the same as double on GNU systems, but may be a 128-bit
number in some cases.
On a typical 32-bit GNU system, the sizes of the di
erentoating point types are as
follows.
Type Bits Possible values (approx.)
float 32 1e-38 to 1e+38
double 64 2e-308 to 1e+308
long double 64 2e-308 to 1e+308
You may
nd the gures in the right-hand column confusing. They use a form of short-hand for large numbers. For example, the number 5e2 means 5
10^2, or 500. 5e-2 means
5
10^ 2 (5=100, or 1=20). You can see, therefore, that the float, double, and long
double types can contain some very large and very small numbers indeed. (When you work
with large and small numbers in C, you will use this notation in your code.)
5.2 Declarations
To declare a variable, write the type followed by a list of variables of that type:
type name variable name 1, ..., variable name n;
For example:
int last_year, cur_year;
long double earth_mass, mars_mass, venus_mass;
unsigned int num_pets;
No comments:
Post a Comment