Beej S Guide To C Programming Brian Beej Jorgensen Hall PDF Download
Beej S Guide To C Programming Brian Beej Jorgensen Hall PDF Download
https://ebookmeta.com/product/beej-s-guide-to-c-programming-
brian-beej-jorgensen-hall-2/
https://ebookmeta.com/product/beej-s-guide-to-c-programming-
brian-beej-jorgensen-hall-5/
https://ebookmeta.com/product/beej-s-guide-to-c-programming-
brian-beej-jorgensen-hall-6/
https://ebookmeta.com/product/beej-s-guide-to-c-programming-
brian-beej-jorgensen-hall/
https://ebookmeta.com/product/summary-of-a-mind-of-your-own-by-
kelly-brogan-with-kristin-loberg-instaread/
Human Relations: Strategies for Success Lowell
Lamberton
https://ebookmeta.com/product/human-relations-strategies-for-
success-lowell-lamberton/
https://ebookmeta.com/product/dk-eyewitness-top-10-greek-islands-
pocket-travel-guide-dk-eyewitness/
https://ebookmeta.com/product/the-ethics-of-social-punishment-
the-enforcement-of-morality-in-everyday-life-1st-edition-linda-
radzik/
https://ebookmeta.com/product/the-boss-s-secret-bossy-
billionaires-1-1st-edition-layla-valentine-ana-sparks/
https://ebookmeta.com/product/food-microbiology-laboratory-for-
the-food-science-student-a-practical-approach-2nd-edition-
cangliang-shen/
Managing Infodemics in the 21st Century: Addressing New
Public Health Challenges in the Information Ecosystem
Tina D. Purnat
https://ebookmeta.com/product/managing-infodemics-in-the-21st-
century-addressing-new-public-health-challenges-in-the-
information-ecosystem-tina-d-purnat/
Beej’s Guide to C Programming
1 Foreword 1
1.1 Audience . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 How to Read This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3 Platform and Compiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.4 Official Homepage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.5 Email Policy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.6 Mirroring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.7 Note for Translators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.8 Copyright and Distribution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.9 Dedication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2 Hello, World! 5
2.1 What to Expect from C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.2 Hello, World! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.3 Compilation Details . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.4 Building with gcc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.5 Building with clang . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.6 Building from IDEs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.7 C Versions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
4 Functions 25
4.1 Passing by Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
4.2 Function Prototypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
4.3 Empty Parameter Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
ii
CONTENTS iii
5 Pointers—Cower In Fear! 29
5.1 Memory and Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
5.2 Pointer Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
5.3 Dereferencing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
5.4 Passing Pointers as Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
5.5 The NULL Pointer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
5.6 A Note on Declaring Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
5.7 sizeof and Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
6 Arrays 37
6.1 Easy Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
6.2 Getting the Length of an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
6.3 Array Initializers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
6.4 Out of Bounds! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
6.5 Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
6.6 Arrays and Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
6.6.1 Getting a Pointer to an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
6.6.2 Passing Single Dimensional Arrays to Functions . . . . . . . . . . . . . . . . . . 42
6.6.3 Changing Arrays in Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
6.6.4 Passing Multidimensional Arrays to Functions . . . . . . . . . . . . . . . . . . . . 44
7 Strings 47
7.1 String Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
7.2 String Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
7.3 String Variables as Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
7.4 String Initializers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
7.5 Getting String Length . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
7.6 String Termination . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
7.7 Copying a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
8 Structs 53
8.1 Declaring a Struct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
8.2 Struct Initializers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
8.3 Passing Structs to Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
8.4 The Arrow Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
8.5 Copying and Returning structs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
8.6 Comparing structs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
9 File Input/Output 57
9.1 The FILE* Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
9.2 Reading Text Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
9.3 End of File: EOF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
9.3.1 Reading a Line at a Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
9.4 Formatted Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
9.5 Writing Text Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
9.6 Binary File I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
9.6.1 struct and Number Caveats . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
13 Scope 89
13.1 Block Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
13.1.1 Where To Define Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
13.1.2 Variable Hiding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
13.2 File Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
13.3 for-loop Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
13.4 A Note on Function Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
31 goto 235
31.1 A Simple Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
31.2 Labeled continue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
31.3 Bailing Out . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
31.4 Labeled break . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
31.5 Multi-level Cleanup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
31.6 Tail Call Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
31.7 Restarting Interrupted System Calls . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
31.8 goto and Thread Preemption . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
31.9 goto and Variable Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
31.10 goto and Variable-Length Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
39 Multithreading 281
39.1 Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
39.2 Things You Can Do . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
39.3 Data Races and the Standard Library . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
39.4 Creating and Waiting for Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
39.5 Detaching Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
39.6 Thread Local Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
39.6.1 _Thread_local Storage-Class . . . . . . . . . . . . . . . . . . . . . . . . . . 289
39.6.2 Another Option: Thread-Specific Storage . . . . . . . . . . . . . . . . . . . . . 289
39.7 Mutexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
39.7.1 Different Mutex Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294
39.8 Condition Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
39.8.1 Timed Condition Wait . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
39.8.2 Broadcast: Wake Up All Waiting Threads . . . . . . . . . . . . . . . . . . . . . 299
39.9 Running a Function One Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
40 Atomics 301
40.1 Testing for Atomic Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
40.2 Atomic Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
40.3 Synchronization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
40.4 Acquire and Release . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
40.5 Sequential Consistency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
x CONTENTS
Foreword
1.1 Audience
This guide assumes that you’ve already got some programming knowledge under your belt from another
language, such as Python2 , JavaScript3 , Java4 , Rust5 , Go6 , Swift7 , etc. (Objective-C8 devs will have a par-
1
https://www.ioccc.org/
2
https://en.wikipedia.org/wiki/Python_(programming_language)
3
https://en.wikipedia.org/wiki/JavaScript
4
https://en.wikipedia.org/wiki/Java_(programming_language)
5
https://en.wikipedia.org/wiki/Rust_(programming_language)
6
https://en.wikipedia.org/wiki/Go_(programming_language)
7
https://en.wikipedia.org/wiki/Swift_(programming_language)
8
https://en.wikipedia.org/wiki/Objective-C
1
2 Chapter 1. Foreword
As a rule, the more complex the question, the less likely I am to respond. If you can narrow down your
question before mailing it and be sure to include any pertinent information (like platform, compiler, error
messages you’re getting, and anything else you think might help me troubleshoot), you’re much more likely
to get a response.
If you don’t get a response, hack on it some more, try to find the answer, and if it’s still elusive, then write
me again with the information you’ve found and hopefully it will be enough for me to help out.
Now that I’ve badgered you about how to write and not write me, I’d just like to let you know that I fully
appreciate all the praise the guide has received over the years. It’s a real morale boost, and it gladdens me to
hear that it is being used for good! :-) Thank you!
1.6 Mirroring
You are more than welcome to mirror this site, whether publicly or privately. If you publicly mirror the site
and want me to link to it from the main page, drop me a line at [email protected].
1.9 Dedication
The hardest things about writing these guides are:
• Learning the material in enough detail to be able to explain it
• Figuring out the best way to explain it clearly, a seemingly-endless iterative process
• Putting myself out there as a so-called authority, when really I’m just a regular human trying to make
sense of it all, just like everyone else
• Keeping at it when so many other things draw my attention
4 Chapter 1. Foreword
A lot of people have helped me through this process, and I want to acknowledge those who have made this
book possible.
• Everyone on the Internet who decided to help share their knowledge in one form or another. The free
sharing of instructive information is what makes the Internet the great place that it is.
• The volunteers at cppreference.com16 who provide the bridge that leads from the spec to the real world.
• The helpful and knowledgeable folks on comp.lang.c17 and r/C_Programming18 who got me through
the tougher parts of the language.
• Everyone who submitted corrections and pull-requests on everything from misleading instructions to
typos.
Thank you! ♥
16
https://en.cppreference.com/
17
https://groups.google.com/g/comp.lang.c
18
https://www.reddit.com/r/C_Programming/
Chapter 2
Hello, World!
5
Exploring the Variety of Random
Documents with Different Content
an Asiatic foe. We may also see in our own British Museum how the
birth of Athênê was recorded in a marble group on one pediment of
the Parthenon, and the foundation of her chosen city on the other.
The very temple which these majestic sculptures once adorned was
a petrified memorial of antiquity, and, by the mere form of its
architecture, must have carried back men’s thoughts to the earliest
Hellenic habitation, the simple structure in which a gabled roof was
supported by cross-beams on a row of upright wooden posts.
Turning back once more from art and literature to philosophy, is it
not abundantly clear that if the Greeks speculated at all, they must
at first have speculated according to some such method as that
which history proves them to have actually followed? They must
have begun by fixing their thoughts, as Thales and his successors
did, on the world’s remotest past; they must have sought for a first
cause of things, and conceived it, not as any spiritual power, but as
a kind of natural ancestor homogeneous with the forms which issued
from it, although greater and more comprehensive than they were;
in short, as an elemental body—water, air, fire, or, more vaguely, as
an infinite substance. Did not the steady concatenation of cause and
effect resemble the unrolling of a heroic genealogy? And did not the
reabsorption of every individual existence in a larger whole translate
into more general terms that subordination of personal to family and
civic glory which is the diapason of Pindar’s music?
Nor was this all. Before philosophising, the Greeks did not think
only in the order of time; they learned at a very early period to think
also in the order of space, their favourite idea of a limit being made
especially prominent here. Homer’s geographical notions, however
erroneous, are, for his age, singularly well defined. Aeschylus has a
wide knowledge of the earth’s surface, and exhibits it with perhaps
unnecessary readiness. Pindar delights to follow his mythological
heroes about on their travels. The same tendency found still freer
scope when prose literature began. Hecataeus, one of the earliest
prose-writers, was great both as a genealogist and as a geographer;
and in this respect also Herodotus carried out on a great scale the
enquiries most habitually pursued by his countrymen. Now, it will be
remembered that we have had occasion to characterise early Ionian
speculation as being, to a great extent, cosmography. The element
from which it deduced all things was, in fact, that which was
supposed to lie outside and embrace the rest. The geographical limit
was conceived as a genealogical ancestor. Thus, the studies which
men like Hecataeus carried on separately, were combined, or rather
confused, in a single bold generalisation by Anaximenes and
Heracleitus.
Yet, however much may be accounted for by these considerations,
they still leave something unexplained. Why should one thinker after
another so unhesitatingly assume that the order of Nature as we
know it has issued not merely from a different but from an exactly
opposite condition, from universal confusion and chaos? Their
experience was far too limited to tell them anything about those vast
cosmic changes which we know by incontrovertible evidence to have
already occurred, and to be again in course of preparation. We can
only answer this question by bringing into view what may be called
the negative moment of Greek thought. The science of contraries is
one, says Aristotle, and it certainly was so to his countrymen. Not
only did they delight to bring together the extremes of weal and
woe, of pride and abasement, of security and disaster, but whatever
they most loved and clung to in reality seemed to interest their
imagination most powerfully by its removal, its reversal, or its
overthrow. The Athenians were peculiarly intolerant of regal
government and of feminine interference in politics. In Athenian
tragedy the principal actors are kings and royal ladies. The Athenian
matrons occupied a position of exceptional dignity and seclusion.
They are brought upon the comic stage to be covered with the
coarsest ridicule, and also to interfere decisively in the conduct of
public affairs. Aristophanes was profoundly religious himself, and
wrote for a people whose religion, as we have seen, was pushed to
the extreme of bigotry. Yet he shows as little respect for the gods as
for the wives and sisters of his audience. To take a more general
example still, the whole Greek tragic drama is based on the idea of
family kinship, and that institution was made most interesting to
Greek spectators by the violation of its eternal sanctities, by
unnatural hatred, and still more unnatural love; or by a fatal
misconception which causes the hands of innocent persons, more
especially of tender women, to be armed against their nearest and
dearest relatives in utter unconsciousness of the awful guilt about to
be incurred. By an extension of the same psychological law to
abstract speculation we are enabled to understand how an early
Greek philosopher who had come to look on Nature as a cosmos, an
orderly whole, consisting of diverse but connected and
interdependent parts, could not properly grasp such a conception
until he had substituted for it one of a precisely opposite character,
out of which he reconstructed it by a process of gradual evolution.
And if it is asked how in the first place did he come by the idea of a
cosmos, our answer must be that he found it in Greek life, in
societies distinguished by a many-sided but harmonious
development of concurrent functions, and by voluntary obedience to
an impersonal law. Thus, then, the circle is complete; we have
returned to our point of departure, and again recognise in Greek
philosophy a systematised expression of the Greek national genius.
We must now bring this long and complicated, but it is hoped not
uninteresting, study to a close. We have accompanied philosophy to
a point where it enters on a new field, and embraces themes
sufficiently important to form the subject of a separate chapter. The
contributions made by its first cultivators to our positive knowledge
have already been summarised. It remains to mention that there
was nothing of a truly transcendental character about their
speculations. Whatever extension we may give to that terrible
bugbear, the Unknowable, they did not trespass on its domain.
Heracleitus and his compeers, while penetrating far beyond the
horizon of their age and country, kept very nearly within the limits of
a possible experience. They confused some conceptions which we
have learned to distinguish, and separated others which we have
learned to combine; but they were the lineal progenitors of our
highest scientific thought; and they first broke ground on a path
where we must continue to advance, if the cosmos which they won
for us is not to be let lapse into chaos and darkness again.
CHAPTER II.
THE GREEK HUMANISTS: NATURE AND LAW.
I.
In the preceding chapter we traced the rise and progress of
physical philosophy among the ancient Greeks. We showed how a
few great thinkers, borne on by an unparalleled development of
intellectual activity, worked out ideas respecting the order of nature
and the constitution of matter which, after more than two thousand
years, still remain as fresh and fruitful as ever; and we found that, in
achieving these results, Greek thought was itself determined by
ascertainable laws. Whether controlling artistic imagination or
penetrating to the objective truth of things, it remained always
essentially homogeneous, and worked under the same forms of
circumscription, analysis, and opposition. It began with external
nature, and with a far distant past; nor could it begin otherwise, for
only so could the subjects of its later meditations be reached. Only
after less sacred beliefs have been shaken can ethical dogmas be
questioned. Only when discrepancies of opinion obtrude themselves
on man’s notice is the need of an organising logic experienced. And
the mind’s eye, originally focussed for distant objects alone, has to
be gradually restricted in its range by the pressure of accumulated
experience before it can turn from past to present, from successive
to contemporaneous phenomena. We have now to undertake the not
less interesting task of showing how the new culture, the new
conceptions, the new power to think obtained through those earliest
speculations, reacted on the life from which they sprang,
transforming the moral, religious, and political creeds of Hellas, and
preparing, as nothing else could prepare, the vaster revolution which
has given a new dignity to existence, and substituted, in however
imperfect a form, for the adoration of animalisms which lie below
man, the adoration of an ideal which rises above him, but only
personifies the best elements of his own nature, and therefore is
possible for a perfected humanity to realise.
While most educated persons will admit that the Greeks are our
masters in science and literature, in politics and art, some even
among those who are free from theological prejudices will not be
prepared to grant that the principles which claim to guide our
conduct are only a wider extension or a more specific application of
Greek ethical teaching. Hebraism has been opposed to Hellenism as
the educating power whence our love of righteousness is derived,
and which alone prevents the foul orgies of a primitive nature-
worship from being still celebrated in the midst of our modern
civilisation. And many look on old Roman religion as embodying a
sense of duty higher than any bequeathed to us by Greece. The
Greeks have, indeed, suffered seriously from their own sincerity.
Their literature is a perfect image of their life, reflecting every blot
and every flaw, unveiled, uncoloured, undisguised. It was, most
fortunately, never subjected to the revision of a jealous priesthood,
bent on removing every symptom inconsistent with the hypothesis of
a domination exercised by themselves through all the past. Nor yet
has their history been systematically falsified to prove that they
never wrongfully attacked a neighbour, and were invariably obliged
to conquer in self-defence. Still, even taking the records as they
stand, it is to Greek rather than to Hebrew or Roman annals that we
must look for examples of true virtue; and in Greek literature, earlier
than in any other, occur precepts like those which are now held to be
most distinctively characteristic of Christian ethics. Let us never
forget that only by Stoical teaching was the narrow and cruel
formalism of ancient Roman law elevated into the ‘written reason’ of
the imperial jurists; only after receiving successive infiltrations of
Greek thought was the ethnic monotheism of Judaea expanded into
a cosmopolitan religion. Our popular theologians are ready enough
to admit that Hellenism was providentially the means of giving
Christianity a world-wide diffusion; they ignore the fact that it gave
the new faith not only wings to fly, but also eyes to see and a soul to
love. From very early times there was an intuition of humanity in
Hellas which only needed dialectical development to become an all-
sufficient law of life. Homer sympathises ardently with his own
countrymen, but he never vilifies their enemies. He did not, nor did
any Greek, invent impure legends to account for the origin of hostile
tribes whose kinship could not be disowned; unlike Samuel, he
regards the sacrifice of prisoners with unmixed abhorrence. What
would he, whose Odysseus will not allow a shout of triumph to be
raised over the fallen, have said to Deborah’s exultation at the
murder of a suppliant fugitive? Courage was, indeed, with him the
highest virtue, and Greek literature abounds in martial spirit-stirring
tones, but it is nearly always by the necessities of self-defence that
this enthusiasm is invoked; with Pindar and Simonides, with
Aeschylus and Sophocles, it is resistance to an invader that we find
so proudly commemorated; and the victories which make Greek
history so glorious were won in fighting to repel an unjust
aggression perpetrated either by the barbarians or by a tyrant state
among the Greeks themselves. There was, as will be shown
hereafter, an unhappy period when right was either denied, or, what
comes to the same thing, identified with might; but this offensive
paradox only served to waken true morality into a more vivid self-
consciousness, and into the felt need of discovering for itself a
stronger foundation than usage and tradition, a loftier sanction than
mere worldly success could afford. The most universal principle of
justice, to treat others as we should wish to be treated ourselves,
seems before the Rabbi Hillel’s time to have become almost a
common-place of Greek ethics;43 difficulties left unsolved by the
Book of Job were raised to a higher level by Greek philosophy; and
long before St. Paul, a Plato reasoned of righteousness, temperance,
and judgment to come.
No one will deny that the life of the Greeks was stained with foul
vices, and that their theory sometimes fell to the level of their
practice. No one who believes that moral truth, like all truth, has
been gradually discovered, will wonder at this phenomenon. If moral
conduct is a function of social life, then, like other functions, it will
be subject, not only to growth, but also to disease and decay. An
intense and rapid intellectual development may have for its condition
a totally abnormal state of society, where certain vices, unknown to
ruder ages, spring up and flourish with rank luxuriance. When men
have to take women along with them on every new path of enquiry,
progress will be considerably retarded, although its benefits will
ultimately be shared among a greater number, and will be better
insured against the danger of a violent reaction. But the work that
Hellas was commissioned to perform could not wait; it had to be
accomplished in a few generations, or not at all. The barbarians
were forcing their way in on every side, not merely with the weight
of invading armies, but with the deadlier pressure of a benumbing
superstition, with the brute-worship of Egypt and the devil-worship
of Phoenicia, with their delirious orgies, their mutilations, their
crucifixions, and their gladiatorial contests. Already in the later
dramas of Euripides and in the Rhodian school of sculpture, we see
the awful shadow coming nearer, and feel the poisonous breath of
Asia on our faces. Reason, the reason by which these terrors have
been for ever exorcised, could only arrive at maturity under the
influence of free and uninterrupted discussion carried on by men
among themselves in the gymnasium, the agora, the ecclêsia, and
the dicastery. The resulting and inevitable separation of the sexes
bred frightful disorders, which through all changes of creed have
clung like a moral pestilence to the shores of the Aegean, and have
helped to complicate political problems by joining to religious hatred
the fiercer animosity of physical disgust. But whatever were the
corruptions of Greek sentiment, Greek philosophy had the power to
purge them away. ‘Follow nature’ became the watchword of one
school after another; and a precept which at first may have meant
only that man should not fall below the brutes, was finally so
interpreted as to imply an absolute control of sense by reason. No
loftier standard of sexual purity has ever been inculcated than that
fixed by Plato in his latest work, the Laws. Isocrates bids husbands
set an example of conjugal fidelity to their wives. Socrates had
already declared that virtue was the same for both sexes. Xenophon
interests himself in the education of women. Plato would give them
the same training, and everywhere associate them in the same
functions with men. Equally decisive evidence of a theoretical
opposition to slavery is not forthcoming, and we know that it was
unfortunately sanctioned by Plato and Aristotle, in this respect no
better inspired than the early Christians; nevertheless, the germ of
such an opposition existed, and will hereafter be pointed out.
It has been said that the Greeks only worshipped beauty; that
they cultivated morality from the aesthetic side; that virtue was with
them a question, not of duty, but of taste. Some very strong texts
might be quoted in support of this judgment. For example, we find
Isocrates saying, in his encomium on Helen, that ‘Beauty is the first
of all things in majesty, and honour, and divineness. It is easy to see
its power: there are many things which have no share of courage, or
wisdom, or justice, which yet will be found honoured above things
which have each of these, but nothing which is devoid of beauty is
prized; all things are scorned which have not been given their part of
that attribute; the admiration for virtue itself comes to this, that of
all manifestations of life virtue is the most beautiful.’44 And Aristotle
distinguishes the highest courage as willingness to die for the καλόν.
So also Plato describes philosophy as a love ‘that leads one from fair
forms to fair practices, and from fair practices to fair notions, until
from fair notions he arrives at the notion of absolute beauty, and at
last knows what the essence of beauty is. And this is that life beyond
all others which man should live in the contemplation of beauty
absolute.’45 Now, first of all, we must observe that, while loveliness
has been worshipped by many others, none have conceived it under
a form so worthy of worship as the Greeks. Beauty with them was
neither little, nor fragile, nor voluptuous; the soul’s energies were
not relaxed but exalted by its contemplation; there was in it an
element of austere and commanding dignity. The Argive Hêrê,
though revealed to us only through a softened Italian copy, has
more divinity in her countenance than any Madonna of them all; and
the Melian Aphroditê is distinguished by majesty of form not less
than by purity and sweetness of expression. This beauty was the
unreserved information of matter by mind, the visible rendering of
absolute power, wisdom, and goodness. Therefore, what a Greek
worshipped was the perpetual and ever-present energising of mind;
but he forgot that beauty can only exist as a combination of spirit
with sense; and, after detaching the higher element, he continued to
call it by names and clothe it in attributes proper to its earthly
manifestations alone. Yet such an extension of the aesthetic
sentiment involved no weakening of the moral fibre. A service
comprehending all idealisms in one demanded the self-effacement of
a laborious preparation and the self-restraint of a gradual
achievement. They who pitched the goal of their aspiration so high,
knew that the paths leading up to it were rough, and steep, and
long; they felt that perfect workmanship and perfect taste, being
supremely precious, must be supremely difficult as well; χαλεπὰ τὰ
καλά they said, the beautiful is hard—hard to judge, hard to win,
and hard to keep. He who has passed through that stern discipline
need tremble at no other task; nor has duty anything to fear from a
companionship whose ultimate requirements are coincident with her
own, and the abandonment of which for a joyless asceticism can
only lead to the reappearance as an invading army of forces that
should have been cherished as indispensable allies.
It may be urged that beauty, however difficult of attainment or
severe in form, is, after all, essentially superficial; and that a
morality elaborated on the same principles will be equally superficial
—will, in fact, be little more than the art of keeping up appearances,
of displaying fine sentiments, of avoiding those actions the
consequences of which are immediately felt to be disagreeable, and,
above all, of not needlessly wounding anyone’s sensibilities. Such an
imitation of morality—which it would be a mistake to call hypocrisy—
has no doubt been common enough among all civilised nations; but
there is no reason to believe that it was in any way favoured by the
circumstances of Greek life. There is even evidence of a contrary
tendency, as, indeed, might be expected among a people whose
most important states were saved from the corrupting influences of
a court. Where the sympathetic admiration of shallow and excitable
spectators is the effect chiefly sought after, the showy virtues will be
preferred to the solid, and the appearance to the reality of all virtue;
while brilliant and popular qualities will be allowed to atone for the
most atrocious crimes. But, among the Greeks of the best period,
courage and generosity rank distinctly lower than temperance and
justice; their poets and moralists alike inculcate the preference of
substance to show; and in no single instance, so far as we can
judge, did they, as modern nations often do, for the sake of great
achievements condone great wrongs. It was said of a Greek and by
a Greek that he did not wish to seem but to be just.46 We follow the
judgment of the Greeks themselves in preferring Leonidas to
Pausanias, Aristeides to Themistocles, and Socrates to Alcibiades.
And we need only compare Epameinondas with David or Pericles
with Solomon as national heroes, to perceive at once how much
nearer the two Greeks come to our own standard of perfection, and
how futile are the charges sometimes brought against those from
whose traditions we have inherited their august and stainless fame.
Moreover, we have not here to consider what was the average
level of sentiment and practice among the Greeks; we have to study
what alone was of importance for the races which came under their
tuition, and that is the highest moral judgment to which they rose.
Now, the deliberate verdict of their philosophy on the relation
between beauty and virtue is contained in the following passage
from Plato’s Laws:—
III.
This process was conceived by Aeschylus as a conflict between
two generations of gods, ending with their complete reconciliation.
In the Prometheus Bound we have the commencement of the
conflict, in the Eumenides its close. Our sympathies are apparently
at first intended to be enlisted on behalf of the older divinities, but
at last are claimed exclusively by the younger. As opposed to
Prometheus, Zeus is evidently in the wrong, and seeks to make up
for his deficiencies by arbitrary violence. In the Oresteia he is the
champion of justice against iniquity, and through his interpreter,
Apollo, he enforces a revised moral code against the antiquated
claims of the Erinyes; these latter, however, ultimately consenting to
become guardians of the new social order. The Aeschylean drama
shows us Greek religion at the highest level it could reach, unaided
by philosophical reflection. With Sophocles a perceptible decline has
already begun. We are loth to say anything that may sound like
disparagement of so noble a poet. We yield to none in admiration for
one who has combined the two highest qualities of art—sweetness
and strength—more completely than any other singer, Homer alone
excepted, and who has given the primordial affections their definitive
expression for all time. But we cannot help perceiving an element of
superstition in his dramas, which, so far, distinguishes them
unfavourably from those of his Titanic predecessor. With Sophocles,
when the gods interfere, it is to punish disrespect towards
themselves, not to enforce justice between man and man. Ajax
perishes by his own hand because he has neglected to ask for divine
assistance in battle. Laius and Jocastê come to a tragic end through
disobedience to a perfectly arbitrary oracle; and as a part of the
same divine purpose Oedipus encounters the most frightful
calamities by no fault of his own. The gods are, moreover,
exclusively objects of fear; their sole business is to enforce the
fulfilment of enigmatic prophecies; they give no assistance to the
pious and virtuous characters. Antigonê is allowed to perish for
having performed the last duties to her brother’s corpse.
Neoptolemus receives no aid in that struggle between ambition on
the one hand with truthfulness and pity on the other which makes
his character one of the most interesting in all imaginative literature.
When Athênê bids Odysseus exult over the degradation of Ajax, the
generous Ithacan refuses to her face, and falls back on the
consciousness of a common humanity uniting him in sympathy with
his prostrate foe.
The rift within the lute went on widening till all its music was
turned to jarring discord. With the third great Attic dramatist we
arrive at a period of complete dissolution. Morality is not only
separated from mythological tradition, but is openly at war with it.
Religious belief, after becoming almost monotheistic, has relapsed
into polytheism. With Euripides the gods do not, as with his
predecessors, form a common council. They lead an independent
existence, not interfering with each other, and pursuing private ends
of their own—often very disreputable ones. Aphrodite inspires
Phaedra with an incestuous passion for her stepson. Artemis is
propitiated by human sacrifices. Hêrê causes Heraclês to kill his
children in a fit of delirium. Zeus and Poseidôn are charged with
breaking their own laws, and setting a bad example to mortals.
Apollo, once so venerated, fares the worst of any. He outrages a
noble maiden, and succeeds in palming off her child on the man
whom she subsequently marries. He instigates the murder of a
repentant enemy who has come to seek forgiveness at his shrine. He
fails to protect Orestes from the consequences of matricide,
committed at his own unwise suggestion. Political animosity may
have had something to do with these attacks on a god who was
believed to side with the Dorian confederacy against Athens.
Doubtless, also, Euripides disbelieved many of the scandalous stories
which he selected as appropriate materials for dramatic
representation. But a satire on immoral beliefs would have been
unnecessary had they not been generally accepted. Nor was the
poet himself altogether a freethinker. One of his latest and most
splendid works, the Bacchae, is a formal submission to the orthodox
creed. Under the stimulus of an insane delusion, Pentheus is torn to
pieces by his mother Agavê and her attendant Maenads, for having
presumed to oppose the introduction of Dionysus-worship into
Thebes. The antecedents of the new divinity are questionable, and
the nature of his influence on the female population extremely
suspicious. Yet much stress is laid on the impiety of Pentheus, and
we are clearly intended to consider his fate as well-deserved.
Euripides is not a true thinker, and for that very reason fitly
typifies a period when religion had been shaken to its very
foundation, but still retained a strong hold on men’s minds, and
might at any time reassert its ancient authority with unexpected
vigour. We gather, also, from his writings, that ethical sentiment had
undergone a parallel transformation. He introduces characters and
actions which the elder dramatists would have rejected as unworthy
of tragedy, and not only introduces them, but composes elaborate
speeches in their defence. Side by side with examples of devoted
heroism we find such observations as that everyone loves himself
best, and that those are most prosperous who attend most
exclusively to their own interests. It so happens that in one instance
where Euripides has chosen a subject already handled by Aeschylus,
the difference of treatment shows how great a moral revolution had
occurred in the interim. The conflict waged between Eteoclês and
Polyneicês for their father’s throne is the theme both of the Seven
against Thebes and of the Phoenician Women. In both, Polyneicês
bases his claim on grounds of right. It had been agreed that he and
his brother should alternately hold sway over Thebes. His turn has
arrived, and Eteoclês refuses to give way. Polyneicês endeavours to
enforce his pretensions by bringing a foreign army against Thebes.
Aeschylus makes him appear before the walls with an allegorical
figure of Justice on his shield, promising to restore him to his
father’s seat. On hearing this, Eteoclês exclaims:—
‘Aye, if Jove’s virgin daughter Justice shared
In deed or thought of his, then it might be.
But neither when he left the darkling womb,
Nor in his childhood, nor in youth, nor when
The clustering hair first gathered round his chin,
Hath Justice turned approving eyes on him;
Nor deem I that she comes as his ally,
Now that he wastes his native land with war,
Or Justice most unjustly were she called
If ruthless hearts could claim her fellowship.’56
Euripides, with greater dramatic skill, brings the two brothers
together in presence of their mother, Jocastê. When Polyneicês has
spoken, Eteoclês replies:—
‘Honour and wisdom are but empty names
That mortals use, each with a different meaning,
Agreeing in the sound, not in the sense.
Hear, mother, undisguised my whole resolve!
Were Sovereignty, chief goddess among gods,
Far set as is the rising of a star,
Or buried deep in subterranean gloom,
There I would seek and win her for mine own.