100% found this document useful (2 votes)
87 views

PDF An Introduction To C GUI Programming Simon Long Download

GUI

Uploaded by

giraseazhoum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
87 views

PDF An Introduction To C GUI Programming Simon Long Download

GUI

Uploaded by

giraseazhoum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 52

Download the full version of the textbook now at textbookfull.

com

An Introduction to C GUI Programming Simon


Long

https://textbookfull.com/product/an-introduction-
to-c-gui-programming-simon-long/

Explore and download more textbook at https://textbookfull.com


Recommended digital products (PDF, EPUB, MOBI) that
you can download immediately if you are interested.

Effective C An introduction to professional C programming


1st Edition Robert C. Seacord

https://textbookfull.com/product/effective-c-an-introduction-to-
professional-c-programming-1st-edition-robert-c-seacord/

textbookfull.com

An Introduction to Orthodontics Simon J. Littlewood

https://textbookfull.com/product/an-introduction-to-orthodontics-
simon-j-littlewood/

textbookfull.com

Introduction to Programming with C 1st Edition Nhce

https://textbookfull.com/product/introduction-to-programming-
with-c-1st-edition-nhce/

textbookfull.com

The Prison Boundary: Between Society and Carceral Space


1st Edition Jennifer Turner (Auth.)

https://textbookfull.com/product/the-prison-boundary-between-society-
and-carceral-space-1st-edition-jennifer-turner-auth/

textbookfull.com
Envisioning Empire The New British World from 1763 to 1773
James M. Vaughn

https://textbookfull.com/product/envisioning-empire-the-new-british-
world-from-1763-to-1773-james-m-vaughn/

textbookfull.com

Miller’s Review of Orthopaedics 7th Edition Mark Miller

https://textbookfull.com/product/millers-review-of-orthopaedics-7th-
edition-mark-miller/

textbookfull.com

The Startup Owner s Manual The Step By Step Guide for


Building a Great Company 1st Edition Steve Blank Bob Dorf

https://textbookfull.com/product/the-startup-owner-s-manual-the-step-
by-step-guide-for-building-a-great-company-1st-edition-steve-blank-
bob-dorf/
textbookfull.com

The Cowboy s Cop Caston Springs Colorado Book 2 1st


Edition Melissa Williams Williams Melissa

https://textbookfull.com/product/the-cowboy-s-cop-caston-springs-
colorado-book-2-1st-edition-melissa-williams-williams-melissa/

textbookfull.com

Beginning Blockchain: A Beginner's Guide To Building


Blockchain Solutions (paperback Or Softback) 1st Edition
Bikramaditya Singhal
https://textbookfull.com/product/beginning-blockchain-a-beginners-
guide-to-building-blockchain-solutions-paperback-or-softback-1st-
edition-bikramaditya-singhal/
textbookfull.com
Freshwater ecosystems in protected areas conservation and
management 1st Edition Arthington

https://textbookfull.com/product/freshwater-ecosystems-in-protected-
areas-conservation-and-management-1st-edition-arthington/

textbookfull.com
AN INTRODUCTION TO

& GUI
PROGRAMMING
Simon Long
2 AN INTRODUCTION TO C AND GUI PROGRAMMING
AN INTRODUCTION TO C AND GUI PROGRAMMING

3
First published in 2019 by Raspberry Pi Trading Ltd, Maurice Wilkes Building,
St. John's Innovation Park, Cowley Road, Cambridge, CB4 0DS

Publishing Director: Russell Barnes • Editor: Phil King


Author: Simon Long • Design: Critical Media
CEO: Eben Upton

ISBN: 978-1-912047-65-9

The publisher, and contributors accept no responsibility in respect of any omissions


or errors relating to goods, products or services referred to or advertised in this book.
Except where otherwise noted, the content of this book is licensed under a Creative
Commons Attribution-NonCommercial-ShareAlike 3.0 Unported
(CC BY-NC-SA 3.0)

The GTK logo is copyright The GNOME Foundation and licensed under LGPL v2.1+

4 AN INTRODUCTION TO C AND GUI PROGRAMMING


AN INTRODUCTION TO C AND GUI PROGRAMMING

Welcome to
An Introduction to C
& GUI Programming
T
he C programming language was invented in the early 1970s, and since then has
become one of the most popular and widely used general-purpose languages. C can
be used to create simple command-line programs, or embedded code to operate the
tiny microcontrollers in toasters and watches. At the other extreme, it can be used to create
rich graphical desktop applications – in fact, most of Linux (and Raspbian itself) is written
in it. It can give you control over the smallest details of how a processor operates, but is still
simple to learn and read. The first part of this book is an introduction to programming in C for
absolute beginners; the second part shows how to use C to create desktop applications for
Raspbian, using the GTK toolkit. You don’t need any programming experience, and a Raspberry
Pi running Raspbian is all you need to get started.

5
About the Author
S imon Long is an engineer working
for Raspberry Pi. He is responsible
for the Raspberry Pi Desktop and its
associated applications. Before joining
Raspberry Pi, he worked for Broadcom,
where he first met Eben Upton, and before
that spent ten years working as a software
engineer and user interface designer for
a major consultancy firm. In his spare
time, he enjoys solving those really hard
crosswords without any black squares.

6 AN INTRODUCTION TO C AND GUI PROGRAMMING


AN INTRODUCTION TO C AND GUI PROGRAMMING

Contents
Chapter 1: Getting started 010
Learn how to use C to program the Raspberry Pi

Chapter 2: Variables and arithmetic 014


Create variables and do maths

Chapter 3: Conditions and comparisons 019


Control the flow of your C programs

Chapter 4: More advanced flow control 024


For loops and case statements

Chapter 5: Pointers 030


Variables have addresses too

Chapter 6: Functions 035


Split your code into bite-sized chunks

Chapter 7: Arrays and strings 041


Handle lists of values, and letters

Chapter 8: The string l;ibrary 046


Simplify common operations on strings

Chapter 9: User Input 052


Reading and interpreting user input

Chapter 10: File input and output 058


Learn to read from and write to files

7
Chapter 11: More about types and variables 063
Type definitions, enumerations, and more

Chapter 12: Header files and the preprocessor 068


Splitting code up into multiple files

Chapter 13: Introduction to GTK 074


Get ready to start creating GUIs

Chapter 14: Your first GTK program 077


Start coding in C with the GTK library

Chapter 15: Buttons 081


Make your window more interesting by adding a button

Chapter 16: Labels and layout 085


Use a box widget to add a text label to your window

Chapter 17: More advanced layout 091


Expand your window, and position and resize buttons automatically

Chapter 18: GUI user input 097


Enable users to enter text and select options

Chapter 19: Combo boxes and list stores 103


Create combo boxes for user input and associate list stores with them

Chapter 20: Tree views 109


Use the GtkTreeView widget to display information

Chapter 21: Menus 115


Create menu bars with drop-down menus

Chapter 22: Dialogs 120


Give users information and ask them questions

8 AN INTRODUCTION TO C AND GUI PROGRAMMING


AN INTRODUCTION TO C AND GUI PROGRAMMING

Chapter 23: Built-in dialogs 123


GTK contains some ready-made dialogs

Chapter 24: Customising widgets 131


Change the properties of widgets

Chapter 25: Glade 135


Create window layouts for applications

Chapter 26: C quick reference 144


Cheat sheets and code examples

9
Chapter 1

Getting started
C is one of the most widely used programming languages
– learn how to use it to program the Raspberry Pi!

What’s so great about C?


C is a very versatile and widely used programming language. It has been used to write
pretty much everything, from low-level routines to control the hardware in embedded
microcontrollers to complete operating systems like Linux with graphical user interfaces. In
spite of this huge flexibility, it is also relatively simple – the language only has about 20 or so
keywords, but there are huge libraries of additional functions that you can call on when you
need them. In the first part of this book, we are going to concentrate on learning about the
keywords, with a few of the more commonly used library functions; the second part of the
book shows how to use the GTK library to make it easy to write graphical interfaces in C.
Many of the languages that you may have seen, such as Python, are what are called
interpreted languages. This means that the code you write is run directly: each line of code is
read in and interpreted as you run it. C is different: it’s a compiled language. This means that
the code you write, known as the source code, is never run directly. The source code is passed
through a program called a compiler, which converts it into a machine-readable version called
an executable or a binary; you then run the resulting executable.
This may seem complex, but it has a few big advantages. First, it means that you don’t need
to have a copy of C itself on every computer you want to run your program on; once compiled,
the executable is standalone and self-contained. Second, the compilation process will find
a lot of errors before you even run the program (but it won’t usually find all of them). Most
importantly, the compilation process means that the time-consuming translation of human-
readable code into machine-readable instructions has already happened, which means that
compiled code generally runs many times faster than interpreted code would.

CHOOSE YOUR EDITOR


You can use whatever editor you like to enter code, as long as it saves it as plain text. The
Geany editor included in Raspbian is a good choice, but you can also use Leafpad, nano, or
any others that you prefer.

10 AN INTRODUCTION TO C AND GUI PROGRAMMING


Visit https://textbookfull.com
now to explore a rich
collection of eBooks, textbook
and enjoy exciting offers!
AN INTRODUCTION TO C AND GUI PROGRAMMING

WHITESPACE DOESN’T MATTER!


Unlike Python, whitespace has no significance in C – you can put spaces, tabs, and new lines
anywhere you like in a C program to make it readable.

Hello world – your first C program


With all that out of the way – which has hopefully made you think that C might be worth
learning – let’s have a look at the first program everyone writes in any language, the one that
prints ‘Hello World’ on the screen. Incidentally, the tradition of writing a Hello World program
was first introduced with the original documentation describing C itself. Just think: no C, no
Hello World…

#include <stdio.h>

void main (void)


{
/* A print statement */
printf ("Hello world!\n");
}

Hopefully not too frightening! Let’s look at it line by line.

#include <stdio.h>

This is known as a hash-include. As mentioned above, the C language has a large library of
functions that can be included, and we need to use one of them in this program: the formatted
print command printf. This is part of the standard input-output library, or stdio for short.
So what this line does is to warn the compiler that the program needs the stdio library to be
included as part of the compile process.

void main (void)

C is a function-based language; every program is made up of a number of functions.


Each function takes zero or more arguments, and returns a single value. A function
definition consists of a specification of what the function returns (in this case, a void), a
function name (in this case, main), and a list of arguments enclosed in round brackets (again,
a void).
Every C program has to include a function called main; when you run the compiled program,
the main function is the first thing that executes.

Chapter 1 Getting started 11


The word void is called a type specifier; a void is a special type which means ‘no value
required’. We’ll look more at types in the next chapter.
So this line defines the main function for this program; it states that the main function
takes no arguments, and returns no value.
The code which makes up the function itself is enclosed between the two curly brackets {}
that follow the function definition.

/* A print statement */

First, we have a comment telling us what’s going on. Comments in C start with the symbol
/*, and end with */ – anything between those two symbols is ignored by the compiler.
The code itself is just one line:

printf ("Hello world!\n");

This is a call to the printf (‘print formatted’) function from the stdio library. In this case,
it takes a single argument, which is a text string enclosed within double quotes. As mentioned
above, function arguments are enclosed in round brackets.
Note that the line ends with a semicolon. All statements in C must finish with a semicolon;
this tells the compiler that this is the end of a statement. One of the most common beginner
mistakes in C is to forget a semicolon somewhere!
What about the string itself? The Hello World! bit is straightforward enough, but what
about that \n at the end? Remember this function is called ‘print formatted’? Well, the \n is a
bit of formatting; it’s the symbol for a newline character. So this line will print the string ‘Hello
World!’, followed by a new line.

CHECK YOUR BRACKETS


Unlike whitespace, punctuation is very important in C – make sure you don’t use a curly
bracket where a round one is needed, or vice versa.

Compiling your program


Let’s compile and run this. Raspbian includes a C compiler called gcc, so there’s nothing to
install; just start up Raspbian on your Pi and you’re ready to go. Use your favourite text editor
to create a file called hello.c, copy the program above into it, and save it. Then, from a terminal,
go into the directory where you saved hello.c and enter:

gcc –o myprog hello.c

12 AN INTRODUCTION TO C AND GUI PROGRAMMING


AN INTRODUCTION TO C AND GUI PROGRAMMING

This calls the gcc C compiler with the option -o myprog, which tells it to create an
executable output file called myprog, and to use hello.c as the input source code.
If you entered your C code correctly (did you make sure the semicolon was there?), this
should take a second or so and then return you to the command line. There should now be a
file in the current directory called myprog – try running it by typing:

./myprog

Et voilà! You should now have…

Hello World!

…written in the terminal.

5You interact with both the C compiler and your compiled C programs from the command
line; you can either do this in a terminal window in the desktop, or by booting your
Raspberry Pi straight to the command line

RUNNING YOUR PROGRAM


You need to tell Linux that the program you want to run is in the current directory, so don’t
forget the ./ before myprog, or it won’t know where to look!

That’s your first C program written, compiled, and run. In the next chapter, we’ll start using C
for something a bit more useful…

Chapter 1 Getting started 13


Chapter 2

Variables
and arithmetic
Doing some real work in C: creating variables and performing
mathematical operations on them

I
n some languages, you can create variables as you go along and put whatever data you
want into them. C isn’t like that: to use a variable in C, you need to have created it first,
and at the time you create it, you have to set what type of value it’s going to store. By
doing this, a block of memory of the correct size can be allocated by the compiler to hold the
variable. This process of creating a variable is known as declaration.

Integers
There are several fundamental data types in C, but we’ll start by looking at one of the most
commonly used: the int type, used to store an integer value.

#include <stdio.h>

void main (void)


{
int a;
int b = 3;
int c;

a = 2;
c = a + b;
printf ("The sum of adding %d and %d is %d\n", a, b, c);
}

The top three lines inside the main function here are declarations. They tell the compiler
that we would like to use variables called a, b, and c respectively, and that each one is of type
int, i.e. an integer.
In the second line, we see an example of an initialisation at the same time as a declaration:
this stores an initial value of 3 in the variable b. Note that the values of a and c at this point are

14 AN INTRODUCTION TO C AND GUI PROGRAMMING


AN INTRODUCTION TO C AND GUI PROGRAMMING

undefined; you might assume that a variable which hasn’t had a value stored in it is always 0, but
that isn’t the case in C. Before reading the value from a variable or using it in a calculation, you
must store a value in it; reading a variable before initialising it is a common error in C.
The next two lines do some actual work with the variables we have declared.

a = 2;

This stores a value of 2 in the variable a, which will now have this value until it’s changed.
The reason a is called a variable is that it can vary: you can change its value as often as you
like, but only to another integer. The value of a variable can change, but its type is fixed when it
is declared.

c = a + b;

This line adds a to b, and stores the result in c.

printf ("The sum of adding %d and %d is %d\n", a, b, c);

This is another use of the formatted print function we saw in the previous chapter. Note the
three %d symbols inside the string: these are format specifiers, and they are how you output
numbers in C. When the printf function is executed, each %d is replaced by a decimal
representation (d for decimal integer) of the variable in the corresponding position in the list
after the string. So the first %d will be replaced by the value of a, the second with the value of
b, and the third with the value of c.
Compile the program above and then run it. You should see this in the terminal:

The sum of adding 2 and 3 is 5

MULTIPLE DECLARATIONS
You can declare multiple variables of the same type in one line, separated by commas.
For the example here, instead of three separate int declarations, you could type
int a, b = 3, c; on one line.

Floating-point numbers
So we can add two integers together; what else can we do? One thing we might want to do
is to use floating-point numbers: numbers with a decimal point. These have a different type,
called float. Try changing the code above so instead of:

int a;

Chapter 2 Variables and arithmetic 15


…you have:

float a;

This tells the compiler that a is now a floating-point value, rather than an integer. Compile
and run your program. What happens?
Oops! That doesn’t look right, does it? What has happened is that, while the maths is still
all correct, the printf statement is now wrong; you’re telling it to print a, which is a floating-
point value, as a decimal integer. To fix that, change the first %d in the printf function to %f,
which is the format specifier for a floating-point number, like this:

printf ("The sum of adding %f and %d is %d\n", a, b, c);

That should produce something a lot more sensible when you run it. This is an important
lesson about C: it will do exactly what you tell it to, even if it makes no sense. You told it to
show you a floating-point number as if it were a decimal integer, and the compiler assumed
that was what you wanted, even though the result was nonsense.
When you’re working with variables, always keep track of what values you’re putting in what
types, as it’s easy to introduce errors by assuming a variable is of one type when it’s actually
another. One common error is to put the results of a calculation on floating-point values into
an integer.
Try this: make b a float as well (not forgetting to change its format specifier in the printf),
but leave c as an int, and set the two floats to values with decimal points, like this:

float a;
float b = 3.641;
int c;

a = 2.897;
c = a + b;
printf ("The sum of adding %f and %f is %d\n", a, b, c);

You’ll see a result like:

The sum of adding 2.897000 and 3.641000 is 6

6? That’s not right! But it’s exactly what you’ve asked for. What the compiler did was to add
the two floating-point values together, and got the answer 6.538, but you then told the compiler
to put that into c, an integer variable. So the compiler just threw away everything after the
decimal point! If you change c to a float, and change the final %d to %f, you’ll find it gives the
correct answer.

16 AN INTRODUCTION TO C AND GUI PROGRAMMING


AN INTRODUCTION TO C AND GUI PROGRAMMING

ARITHMETIC SHORTHAND
C allows shortcuts for some common operations; for example, instead of typing a = a + 1,
you can just enter a++. Or for a = a * 3, you can enter a *= 3.

Don’t forget to use %f instead of %d as the print specifier when changing


the int values to float values in the example

DECIMAL PLACES
You can set the number of decimal places to display for a floating-point type specifier in
printf by putting a decimal point and the number of places between the % and the f – so
%.3f will show a float value with three digits after the decimal point.

Other types
Another common variable type is char, a character value. This is used, as the name suggests,
to store a single character. The ASCII character encoding uses a single value between 0 and
127 for each letter, number, and punctuation symbol, so a char is a single byte; it's really just
an integer value which can only hold small numbers. The compiler allocates several bytes to
store an int or a float, but only allocates a single byte of memory to store a char.

Chapter 2 Variables and arithmetic 17


There are also modifiers which can be applied to variable types. Both char and int can
be used to store both positive and negative numbers, but by applying the unsigned modifier
when one is declared, they can be restricted to only store positive values. So…

char a;

…declares a variable which can hold values from -128 to 127, while…

unsigned char a;

…declares a variable which can hold values from 0 to 255.

When doing arithmetic with chars, it is important to make sure that the answers to any
calculation will fit into the variable. If, say, you have a char containing the value 100, and you
add 30 to it, you would expect to end up with the result 130 – but as above, a char can only
hold values up to 127. So the value your char will actually end up containing is -126, because
values over 127 – the highest value a char can store – wrap around to the lowest value (-128)
and start counting up from there. This 'overflow' behaviour is a common cause of bugs in C
programs which do arithmetic.

REMEMBER PRECEDENCE
C obeys the common rules for operator precedence – so a = a + 2 * 3 evaluates
the multiply first and then adds the result, 6, to a. You can use round brackets to change
precedence – a = (a + 2) * 3 gives 3a + 6.

That gives you some idea about how C handles numbers, and how you can use it for
arithmetic; in the next chapter, we’ll look at how to use the results of calculations to
make decisions.

18 AN INTRODUCTION TO C AND GUI PROGRAMMING


AN INTRODUCTION TO C AND GUI PROGRAMMING

Chapter 3

Conditions
and comparisons
Branches and loops: controlling the flow of your C program

O
ne of the fundamentals of any programming language is the ability to make
conditional operations – to change the program’s flow depending on the result
of a test.
In this chapter, we’ll look at how you test conditions within your C programs, and how you
use the results to determine what happens next.
In C, the mechanism for controlling flow based on testing a condition is the if-else
statement. Here’s a simple example:

#include <stdio.h>

void main (void)


{
int a = 0;

if (a == 0)
{
printf ("a is equal to 0\n");
}
else
{
printf ("a is not equal to 0\n");
}
}

Here, the keyword if is followed by a test enclosed in round brackets, in this case
(a == 0). If the test evaluates as true, the operations enclosed by the curly brackets after
the test are executed.
This example also shows the use of an else clause. At the end of the curly brackets around
the operations which you want to execute if the test is true, there’s an else followed by

Chapter 3 Conditions and comparisons 19


another set of curly brackets; these contain the operations you want to execute if the original
test evaluated as false.
Try compiling the code above, and change the value with which a is initialised to make sure
it does what you expect.

CURLY BRACKETS
Curly brackets are used to group together a set of statements which always execute together.
If your loop or if statement only needs to execute one single statement, you can leave out the
curly brackets after the test, but this can make the code's purpose less obvious to a human!

= or ==
That’s all fine, but what’s this a == 0 all about? Surely if we want to know whether a is equal
to 0, we just put a = 0? Why the two equals signs? Well, try replacing the double equals sign
with a single equals and see what happens.
This is a very important aspect of C syntax, and a common source of bugs. The equals sign
is used for two different things: one is to assign a value to a variable, whereas the other is to
test whether a variable is equal to a value. A single equals sign (=) assigns a variable; a double
equals sign (==) tests a variable.
So the statement…

if (a == 0)

…tests to see if a is equal to 0. If it is, then the test evaluates as true, and the code
immediately after the if is executed.
But the statement…

if (a = 0)

…doesn’t compare a against 0 at all: it just sets a to 0. So how does the compiler decide
what to do next? In this case, it just looks at the value of what’s in the brackets; you’ve set a to
0, so the value inside the brackets is 0.
In C, a value of 0 is equivalent to false, and a non-zero value is equivalent to true. So by
replacing the double equals with a single equals, you’ve changed the value of a, and then you
look to see if the value you’ve set a to is equivalent to true or false; neither of which were what
you wanted to do! If a C program is behaving strangely, check very carefully that all your tests
are actually tests and not assignments: this is a very easy mistake to make.
So == is the test to see if a value is equal to another one. There are other useful
symbols that can be used in a test. The symbol !=, for example, means ‘is not equal to’.

20 AN INTRODUCTION TO C AND GUI PROGRAMMING


Visit https://textbookfull.com
now to explore a rich
collection of eBooks, textbook
and enjoy exciting offers!
AN INTRODUCTION TO C AND GUI PROGRAMMING

The mathematical operators > and < are used to test for ‘is greater than’ and ‘is less than’
respectively, and they can also be combined with an equals sign to give >= and <=, the tests
for ‘is greater than or equal to’ and ‘is less than or equal to’.
You can combine tests with logical operators. The symbol && is a Boolean AND (i.e. test
whether both sides are true), and || is Boolean OR (i.e. test if either side is true). So, to
execute code only if both a and b are 0, you would use if (a == 0 && b == 0). To test if
either a or b is 0, you use if (a == 0 || b == 0).
Similarly, you can use the operator ! as a Boolean NOT to invert the result of a test, so
if (!(a == 0)) is the same as if (a != 0) .

’ve set a to
0, so the value inside the brackets is 0.

Make sure that you use a double equals sign in the brackets after the if, not a single one!
5

ELSE-IF
You can have multiple else statements in one test. Instead of one simple else for one
alternative, use else if () with a new test for each alternative you want. We’ll look more
at this in the next chapter.

Looping
The if statement is useful for making a single decision, but what if you want to do something
repeatedly until a test is true or false? We use a while loop for this, and here’s an example:

Chapter 3 Conditions and comparisons 21


#include <stdio.h>

void main (void)


{
int a = 0;

while (a < 5)
{
printf ("a is equal to %d\n", a);
a++;
}
printf ("a is equal to %d and I've finished\n", a);
}

This is very similar to an if statement, but the code in the curly brackets is executed
repeatedly for as long as the test in the round brackets is true, not just once.
So in our example code, a is initialised to 0. We enter the while loop, and test to see if a
is less than 5, which it is, so the code inside the curly brackets is executed. The value of a is
printed out, then we have one of C’s useful shortcuts to save too much typing…
a++ is the same as a=a+1; the double plus means ‘add one to this variable’. Similarly, a--
means ‘subtract one from this variable’; these are very commonly used to count the times
around a loop. The notation a+=1 can also be used to add a value to a variable; this also
works for other arithmetic operators, so a*=3 multiplies a by 3, and so on.
In the while loop, each time the code in the curly brackets has been executed, the test in the
round brackets is repeated; if it’s still true, the loop code is repeated again. As soon as the test
is false, execution continues with the line after the closing curly bracket.

INFINITE LOOPS
Make sure your loops always finish! If the condition you test in a while loop never evaluates to
false, your program will sit in the loop forever and never finish. If a program appears to be doing
nothing when you run it, check your loop tests.

Sometimes, we might want a loop which always runs at least once before a test is made.
We do this with a small modification to the syntax to create a do-while loop:

#include <stdio.h>

void main (void)


{

22 AN INTRODUCTION TO C AND GUI PROGRAMMING


AN INTRODUCTION TO C AND GUI PROGRAMMING

int a = 0;

do
{
printf ("a is equal to %d\n", a);
a++;
} while (a < 5);
printf ("a is equal to %d and I've finished\n", a);
}

The keyword do now goes before the curly bracket, and the while and test go after the
closing curly bracket. When this runs, the code in the loop always executes once before the
test; you can test this by running both the loop examples above with a initialised to 5 rather
than 0, and seeing how the behaviour differs.

MORE ABOUT SEMICOLONS


Unlike the test in an if statement or a while loop, you need to put a semicolon after the test in
a do-while loop. This indicates the end of the loop code; in a while loop, the loop code doesn't
end until the last statement inside the curly brackets.

In the next chapter, we’ll look at some more complex examples of looping and flow control.

5A loop executes the same code multiple times until the loop test is false

Chapter 3 Conditions and comparisons 23


Chapter 4

More advanced
flow control
For loops and case statements – more advanced
ways of controlling the flow of a program

T
he if statement and while loop described in the previous chapter are fairly simple
control structures. In this chapter, we’re going to look at a few more complex
structures that can help to make your code shorter and reduce the amount of typing
you need to do…
Although the while loop we saw in the previous article is very useful, the for loop tends to be
favoured by many programmers, as it puts all the logic controlling the loop in one place. Here’s
an example:

#include <stdio.h>

void main (void)


{
int a;

for (a = 0; a < 5; a++)


{
printf ("a is equal to %d\n", a);
}
printf ("a is equal to %d and I've finished\n", a);
}

This isn’t all that different from a while loop, but all of the control for the loop lives in
the round brackets after the for keyword. This contains three statements, separated by
semicolons: in order, these are the initial condition, the test, and the increment.
a = 0 is the initial condition; the variable a is initialised to 0 at the start of the loop.
a < 5 is the test, just like in a while loop. This is checked on each iteration of the loop,
and the loop code is only executed if the test evaluates to true; as soon as the test is false,
execution continues after the curly bracket at the end of the loop code.

24 AN INTRODUCTION TO C AND GUI PROGRAMMING


AN INTRODUCTION TO C AND GUI PROGRAMMING

a++ is the increment; this is code which is executed at the end of each iteration of the loop,
before the test is evaluated again. In this case, it adds 1 to a.
So when this for loop runs, what happens? First, a is set to 0. The test is then checked: is
a (which is 0) less than 5? Yes it is, so the code inside the curly brackets is executed, and the
value of a is printed. Finally, the increment is applied, meaning 1 is added to a.
The test is then repeated. If true, the loop code is executed again, and the increment is again
applied; this repeats over and over until the test is false, at which point execution continues
after the closing curly bracket.

5The output when the for loop runs is identical to that of the while loop in
the previous chapter – they both do exactly the same thing

In terms of what they do, for loops and while loops are pretty much identical; both wrap up a
section of code you want to run more than once in some logic that controls how many times it
runs. You can use whichever makes the most sense, or whichever looks tidiest to you!

MULTIPLE INITIALISATIONS
You can initialise multiple variables in a for loop – just separate them by commas. So if you
want to set two variables at the start of the loop, you can use:
for (a = 0, b = 1; <test>; <increment>)

Chapter 4 More advanced flow control 25


Random documents with unrelated
content Scribd suggests to you:
The Project Gutenberg eBook of Christmas
carols
This ebook is for the use of anyone anywhere in the United
States and most other parts of the world at no cost and with
almost no restrictions whatsoever. You may copy it, give it away
or re-use it under the terms of the Project Gutenberg License
included with this ebook or online at www.gutenberg.org. If you
are not located in the United States, you will have to check the
laws of the country where you are located before using this
eBook.

Title: Christmas carols


Old English carols for Christmas and other festivals

Contributor: Lucy Etheldred Broadwood

Editor: L. Edna Walter

Illustrator: J. H. Hartley

Release date: December 23, 2023 [eBook #72492]

Language: English

Original publication: New York: The MacMillian Company, 1922

Credits: Robin Monks, Linda Cantoni, and the Online Distributed


Proofreading Team at https://www.pgdp.net (This file
was produced from images generously made available
by The Internet Archive)

*** START OF THE PROJECT GUTENBERG EBOOK CHRISTMAS


CAROLS ***
Transcriber’s Note: In the HTML version of this e-book, you can click on the [Listen] link to
hear an mp3 audio file of the carol. Click on the [MusicXML] link to download the notation
in MusicXML format. These music files are the music transcriber’s interpretation of the
printed notation and are placed in the public domain.
CHRISTMAS CAROLS
CONTENTS

IN THE SAME SERIES.

ENGLISH NURSERY
RHYMES.

Selected and Edited by L. EDNA


WALTER. B.Sc.
Harmonized by LUCY E.
BROADWOOD.
Illustrated by DOROTHY M.
WHEELER.
Containing 32 full-page
illustrations in colour, decorative
borders, and about 60
decorative headings and tail-
pieces. Demy 4to (11½ × 8¾
inches).

SONGS FROM
ALICE IN
WONDERLAND
AND
THROUGH THE
LOOKING-GLASS.
Words by LEWIS CARROLL.
Music by LUCY E. BROADWOOD.
Illustrations by CHARLES
FOLKARD.
Containing 12 full-page
illustrations in colour, decorative
borders, and many small
illustrations. Demy 4to, cloth.

Published by A. & C. BLACK, Ltd., 4, 5, & 6, Soho


Square, London, W.1.
CHRISTMAS
CAROLS
Old English Carols for
Christmas and other
Festivals.
SELECTED AND EDITED BY
L. EDNA WALTER M.B.E., B.Sc.,
A.C.G.I.

HARMONISED BY
LUCY E. BROADWOOD
ILLUSTRATED BY
J.H. HARTLEY

NEW YORK: THE MACMILLAN COMPANY,


FIFTH AVENUE.
LONDON: A. & C. BLACK, LIMITED, 4, 5, & 6,
SOHO SQUARE.

This book is dedicated to


ELIZABETH
because she rather liked it.

Published, Autumn, 1922.


FOREWORD

Special times or events have been celebrated from time immemorial


by feasting, dancing, and singing. Often the dancers formed a ring
and sang as they danced, first the dance and later the song being
called a carol. The carol was not always strictly religious, although in
the old times both the singing and dancing often took place in
cathedrals and churches. Some of the carols that we still know are
connected with times before the Christian era. They have now lost
their dance and the melody has changed, but the ideas are very
ancient. The Holly and the Ivy suggest the old Druids, and we still
put up Holly and Ivy in our houses just as people did before the time
of Christ. We put them up at Christmas, and we sing the carol at
Christmas—but the idea at the back of it is older than Christmas, for
the Church accepted all that was found to be of value in the old
customs, and adapted them to set forth the newer faith. The carrying
in of the Boar’s Head is an old ceremony, too. It was considered a
Royal Dish, and Henry II. ordered it to appear at a special feast
which he gave in honour of his son.
In the old days people thought of the New Year as the time when the
trees and flowers began to come out—that is about May Day—so
the May Day Carols celebrate the New Year’s Day of ever so long
ago. Gradually, however, carols have centred more and more round
events in the life of Christ, and especially round the wonderful story
of His Birth. Many of them have just been handed on from one
person to another through hundreds of years, some have only been
written down at all during the last century. For example, the version
given here of the “Black Decree” was sung into my phonograph by
an old man of seventy-five. All the carols chosen for this book are
those which have been sung through many, many years at times of
festival and mirth (note how often food and drink are referred to), so
don’t expect them to be pious in the modern way or to be at all like
our present-day hymns.
The Publishers desire to acknowledge their indebtedness to Miss
Lucy E. Broadwood for kindly permitting them to reproduce in this
collection the following carols from her ENGLISH TRADITIONAL
SONGS AND CAROLS: “King Pharaoh,” “The Moon Shines Bright,”
“The Sussex Mummers’ Carol,” and “I’ve been Rambling all the
Night.” Also to Miss A.G. Gilchrist for the “Pace Egging Song” and
“The Seven Joys of Mary,” and to the Rev. S. Baring-Gould and his
publishers (Messrs. Methuen & Co., Ltd.) for the “Somersetshire
Wassail” from A GARLAND OF COUNTRY SONG.

CONTENTS
PAGE
GOOD KING WENCESLAS 12
AS JOSEPH WAS A-WALKING 14
CHRISTMAS DAY IN THE MORNING 15
GOD REST YOU MERRY, GENTLEMEN 16
THE HOLY WELL 18
THE FIRST NOWELL 20
THE CHERRY TREE CAROL 23
DIVES AND LAZARUS 24
THE HOLLY AND THE IVY 25
A VIRGIN MOST PURE 26
THE WASSAIL SONG. Part I. 28
THE WASSAIL SONG. Part II. 29
THE BOAR’S HEAD CAROL 30
ALL THAT ARE TO MIRTH INCLINED 33
KING PHARAOH: Part I. The Miracle of the Cock 34
KING PHARAOH: Part II. The Miraculous Harvest 37
THE BLACK DECREE 38
SOMERSETSHIRE WASSAIL 40
A CHILD THIS DAY IS BORN 43
THE MOON SHINES BRIGHT 44
A CAROL FOR TWELFTH DAY 47
THE LORD AT FIRST DID ADAM MAKE 48
THE SEVEN JOYS OF MARY 50
THE SUSSEX MUMMERS’ CAROL 52
AS I SAT ON A SUNNY BANK 53
PACE-EGGING SONG 54
I’VE BEEN RAMBLING ALL THE NIGHT 57
GOOD CHRISTIAN MEN, REJOICE 58
ILLUSTRATIONS IN COLOUR
BY

J.H. HARTLEY

Page and Monarch forth they went Frontispiece


PAGE

In fields where they lay keeping their sheep 21


Mary said to cherry tree, “Bow down to my
22
knee”
The Boar’s head in hand bear I 31
Let all your songs and praises be unto His
32
Heavenly Majesty
“Say, where did you come from, good man?” 35
“Come, husbandman,” cried Jesus, “cast all your
36
seed away”
O maid, fair maid, in holland smock 41
Glad tidings to all men 42
Awake, Awake, good people all! 45
For I perforce must take my leave of all my
46
dainty cheer
Oh, here come we jolly boys, all of one mind 55
A branch of May, my dear, I say, before your
56
door I stand
Now to the Lord sing praises, all you within this On the
place Cover
Good King Wenceslas

You might also like