C - a first program that does something useful for you
Archive - Originally posted on "The Horse's Mouth" - 2011-04-09 06:58:29 - Graham EllisAny 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):
float height;
Reading a value into a variable:
scanf("%f",&height);
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
munchkin:capr grahamellis$
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++.