C - a first program that does something useful for you
Any language has a whole number of different types of element that come together to make a whole.
Let's take a spoken language such as English. You need nouns - they're 'thing' words like "platypus" and "road", "carrot" and "Walter". Then you need verbs to join them together - they're 'doing' words like "ate" and "squash", "is" and "craved". Using those basics you can achieve, but in a very limited and stilted way, some real meaning - "Walter ate carrot", but you need more to get the full depth of expression that we use in day to day life - add in your prepositions, adverbs and adjectives, and also add different structure in which you can use, qualify and modify the elements you've already got. For example "Some carrots were squashed on the road" is using the same nouns and verbs, but within other structures and you'll notice that "carrot" has been plurasised into "carrots", and "squash" moved back into the past in the form of "squashed".
There's a similar thing with programming languages - you need more that just one basic element to get you started with something meaningful, and even when you get to that first meaningful program, you'll be very limited in what you can do. That's just an early limit, though - as you learn about more language elements, and as you also learn how to adopt / adapt / tailor the basic elements you learned early on, you end up finding that the sky's the limit.
Programming courses always start with a program that outputs a message such as "Hello World". Just as a French teacher might start "Bonjour" and an Arabic teacher would start "Al salaam a'alaykum" - litereally meaning "peace be with you". The second step is to do something useful - convey a message, write a piece of code which reads an input from the user of your program, does some sort of calculation, and outputs the result.
On our programming courses, we take this "one, two" step as well ... and on the C Programming course I ran at the beginning of last week, I wrote a program that asked the user to enter his weight and height, calculated his Body Mass Index (BMI) and output the result. I'm told that if this number is under 20, it indicates "underweight" and over 25, you're getting a bit chubby. ((I'm also reminded to tell you I'm NOT a doctor and you should seek medical advise if anything concerns you, whatever your BMI is!))
Anyway - the program ... highlights ... features used / added beyond "Hello World":
Setting up a named memory location (a variable):
Reading a value into a variable:
Doing a calculation and saving the answer into another variable:
bmi = weight / (height * height);
Outputting the contents of a variable (in amongst descriptive text):
printf ("And my BMI is %.2f\n",bmi);
The full program is [here]
. And here's an example of it running:
munchkin:capr grahamellis$ ./second
How high are you (metres): 1.75
How hefty are you (kilograms): 66
I am here
My height is 1.75 and I weigh 66.0
And my BMI is 21.55
Very clunky ... a bit long winded ... no error checking for inputs that are obviously wrong ... no conditional checks which would allow me to add an extra warning for my users if the BMI was a bit high. But a first practical program!
If you would like to learn more about our C and C++ courses, see [here]
. There are five different courses, ranging from 2 to 5 days in length - choose the course that's right for YOU based on your background and whether you need to learn only the C language, or C++. (written 2011-04-09)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articlesC202 - C and C based languages - A first practical program 
Learning to program sample program - past its prime, but still useful - (2014-12-02) 
Defining, declaring and initialising variables in C - (2012-01-24) 
New year, new C Course - (2011-01-05) 
C course inspires new teaching examples - (2010-01-16) 
Turning C from source to a running program - (2006-10-06)
Some other Articles
Melksham Town Council - vacancy in the Spa WardTrainWest, 2011 in pictures - Christie Miller, Bowerhill, MelkshamBradshaw, Ben and Bill. And some C and C++ pointers and references too.Using functions to keep look and feel apart from calculations - simple C exampleC - a first program that does something useful for youHow we make our programming courses both time and cost effectiveYour program - you just provide the filling in the sandwichC / C++ Course Lunch - sitting out at the West EndAround and about Melksham in more picturesFootpath, Bridleway, Byway, Road used as Public Path
4573 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92 at 50 posts per page
This is a page archived from The Horse's Mouth at
the diary and writings of Graham Ellis.
Every attempt was made to provide current information at the time the
page was written, but things do move forward in our business - new software
releases, price changes, new techniques. Please check back via
our main site for current courses,
prices, versions, etc - any mention of a price in "The Horse's Mouth"
cannot be taken as an offer to supply at that price.
Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).