The original "C programming for beginners" thread is located under Backtrack2 -> Tutorials (yes I made a mistake in putting it under Backtrack2, so if a moderator would care to move it to the programming forum I'd be much obliged).
---------------------------------
Let's take our original program for converting Fahrenheit to Celsius, now let's change it so that it converts five temperatures instead of just one. One way of doing that might be as follows:
Code:
#include <stdio.h>
int main(void)
{
float i;
printf("Enter the temperature in Fahrenheit: ");
fflush(stdout);
scanf("%f", &i);
i -= 32; /* Short-hand for i = i - 32 */
i *= 5; /* Short-hand for i = i * 5 */
i /= 9; /* Short-hand for i = i / 9 */
printf("Answer is: %f\n", i);
printf("Enter the temperature in Fahrenheit: ");
fflush(stdout);
scanf("%f", &i);
i -= 32; /* Short-hand for i = i - 32 */
i *= 5; /* Short-hand for i = i * 5 */
i /= 9; /* Short-hand for i = i / 9 */
printf("Answer is: %f\n", i);
printf("Enter the temperature in Fahrenheit: ");
fflush(stdout);
scanf("%f", &i);
i -= 32; /* Short-hand for i = i - 32 */
i *= 5; /* Short-hand for i = i * 5 */
i /= 9; /* Short-hand for i = i / 9 */
printf("Answer is: %f\n", i);
printf("Enter the temperature in Fahrenheit: ");
fflush(stdout);
scanf("%f", &i);
i -= 32; /* Short-hand for i = i - 32 */
i *= 5; /* Short-hand for i = i * 5 */
i /= 9; /* Short-hand for i = i / 9 */
printf("Answer is: %f\n", i);
printf("Enter the temperature in Fahrenheit: ");
fflush(stdout);
scanf("%f", &i);
i -= 32; /* Short-hand for i = i - 32 */
i *= 5; /* Short-hand for i = i * 5 */
i /= 9; /* Short-hand for i = i / 9 */
printf("Answer is: %f\n", i);
return 0;
}
Now isn't that just a tiny bit annoying to have to write the same code out 5 times? Writing code out five times has three problems:
1) It's takes up a lot of room and takes time to read down through.
2) It's more difficult to maintain and more prone to bugs, because if you decide to change the code a little, you have to change it in five places.
3) The code takes up five times as much space in memory, and five times as much space in your executable file.
To get around this problem, someone devised the idea of having "reusable code" and putting it in a "function". To make the usable code run, you call the function. So let's take our code for converting Fahrenheit to Celsius and let's put it into a function:
Code:
#include <stdio.h>
void ConvFtoC(void)
{
printf("Enter the temperature in Fahrenheit: ");
fflush(stdout);
scanf("%f", &i);
i -= 32; /* Short-hand for i = i - 32 */
i *= 5; /* Short-hand for i = i * 5 */
i /= 9; /* Short-hand for i = i / 9 */
printf("Answer is: %f\n", i);
}
The "void" at the start specifies that this function does not return a value. The "void" between the parentheses specifies that this function does not take any values. Between the two chain brackets (i.e. '{' and '}'), you have the code for the function. Now in "main", we can call this function five times:
Code:
#include <stdio.h>
void ConvFtoC(void)
{
printf("Enter the temperature in Fahrenheit: ");
fflush(stdout);
scanf("%f", &i);
i -= 32; /* Short-hand for i = i - 32 */
i *= 5; /* Short-hand for i = i * 5 */
i /= 9; /* Short-hand for i = i / 9 */
printf("Answer is: %f\n", i);
}
int main(void)
{
ConvFtoC();
ConvFtoC();
ConvFtoC();
ConvFtoC();
ConvFtoC();
return 0;
}
Isn't that much nicer to read? You can read it quicker, you can make changes to it quicker, and also it takes up much less space in the exectuable file and also in RAM because the function code is only stored once and gets called five times.
Next we can play around with having a function take a value and also return a value. Let's write a function that gives you the square of a number:
Code:
unsigned Sqr(unsigned i)
{
return i * i;
}
At this point, I'm going to introduce the concept of "const". When you're defining a variable, you can make it "const" to say to the compiler that the value of this variable won't change after it has gotten its initial value. If you try to change its value afterwards, you'll get a compiler error. Our function "Sqr" above contains a variable called "i" and its value never changes, so we can make it "const":
Code:
unsigned Sqr(unsigned const i)
{
return i * i;
}
It's good practise to use const wherever you can, because:
1) It clearly expresses your intent that this value should not change
2) It reduces the possibility of bugs that come about from altering a value that shouldn't be altered.
So moving on, let's write a program that calculates the square of an unsigned integer:
Code:
#include <stdio.h>
unsigned Sqr(unsigned const i)
{
return i * i;
}
int main(void)
{
unsigned i;
printf("Enter a non-negative integer value: ");
fflush(stdout);
scanf("%u", &i); /* We use "%u" to get an unsigned int */
printf( "Answer is: %u\n", Sqr(i) );
return 0;
}
Now in this example, the function "Sqr" is pretty stupid because it's handier just to write "i * i" directly in our "main" function, but I wanted to start off with an easy example.
Functions can only return one value, but they can take as many values as you want. (There is a way of getting a function to return more than one value, but we won't get into that now). We can write a function that calculates "a cubed, plus b squared, plus c":
Code:
unsigned Func(unsigned a, unsigned b, unsigned c)
{
return a * a * a + b * b + c;
}
int main(void)
{
Func(4,5,9);
return 0;
}