- Joined
- 3 Nov 2010
- Messages
- 27,845
- Solutions
- 8
- Reaction score
- 38,396
[/b]It is considered to be the most commonly used programming language. If you want a job in programmming, you will need to know this language. But what makes C the language that everyone wants to use. Well, its been around for quite some time. The original C since the 70s, and C++ since the early 80s, which is like eons in computer time. C is the most versatile high level language. C permits just about anything, allowing programmers to write straight to the metal. Its code runs very fast. Note that this tutorial will not cover C++ (C with classes).
Learning a programming language is a lot like learning a language that people speak like German or French. The best way to learn a 'human' language is to start speaking, listening, and repeating, and then leaving the grammar for later. The same can be applied to C, you need to start writing programs as quickly as possible. So, this tutorial will provided a lot of well commented sample programs that you can run and tinker with.
> Your First Program
If you've ever read another tutorial or a book on a programming language, then you've probably seen a program like this before. The hello world program. It is a perfect starting place for our tutorial. So, type the following program into your favorite editor. If you are using Windows then try notepad or DOS Edit, if using a *Nix variant use VI or emacs. Without further adew here is the program :
-------------------------------------------------------
/* hello world program */
#include "stdio.h"
void main()
{
printf("\nHello World\n"); // print to screen
}
-------------------------------------------------------
Save this code into a file, and call the file hello.c, then compile it by typing at a command prompt:
-------------------------------------------------------
gcc hello.c
-------------------------------------------------------
A C Program contains functions and variables. The functions specify the tasks to be performed by the program. The above program has one function called main. This function tells your program where to start running. main functions are normally kept short and calls different functions to perform the necessary sub-tasks. All C codes must have a main function.
Also notice that C is case-sensitive. The commands have to be written like they are above. C also denotes the end of statement with a semi-colon like Java & Pascal. Brackets signify either to "{" begin a group of statements, or "}" end a group of statements. The // or /* comment */ designates a comment. Anything after two slashes the compiler ignores. The last part of the program you should notice is the #include. This simply includes a group of functions from the filename specified between then less than and greater than signs (<...>). The file above stdio.h contains a list of standard functions for C to use, the function the our above program uses is printf. Printf takes a string of characters between quotation marks, and outputs them to the screen.
Now that you understand the hello world program it's time to learn more about data types, and the printf statement.
> Data Types and Printf
Here is our next program to discuss. It declares several variables, performs some computations, and then outputs the results of those computations to the screen.
-------------------------------------------------------
#include <stdio.h>
void main()
{
int numcandy; // declare a number variable
double cost; // declare a variable that can store decimals
printf("How many lollipops do you want: ");
scanf("%d", &numcandy); // get input from user
cost = 0.55 * numcandy; // do some math
printf("\nPlease pay %f to the cashier!\n", cost);
}
-------------------------------------------------------
This program when run, well prompt the user for how many lollipops they would like. After they enter a number and press enter, it will print out how much it will cost them to buy that many lollipops assuming lollipops cost 55 cents each.
The main, {} brackets, comments, and include should be familiar. The new part of this program is the scanf, variables, and the use of printf to print out numbers. of this program that we haven't seen before.
First lets talk about the variables. This program has two variables numcandy, and cost. These are the names of the variables. Before a variable can be used it must be declared. Declaring a variable in C is easy, you simply tell the compiler the type, and the name you want the variable to have. So in the above example. The line int numcandy; tells the compiler you want a variable of type integer with the name numcandy. Then you can simply use numcandy through out your program to store int's (integers). Integers are non decimal numbers like -13, 0, 10, 5, etc. The other variable in the above program is a double which is used for storing numbers with decimals.
Next the scanf, and printf statements. After running the program you will notice that the scanf function simply gets a value from the user. It waits for the user to enter a number, and then press enter. It puts this value into our variable numcandy. Next notice some special character sequences contained in both the scanf and printf. First the printf has the sequence "\n". This sequence simply means move to the next line. Next we have the %f and %d. These tell printf and scanf what type of variables to expect. %f corresponds to double, and %d is for int. So if you changed the above programs printf line to ...pay $%d to... then you would get wierd output since printf would try to print cost as an integer, which doesn't make sense, because cost is a double. You need to make sure the "%" go with the right types.
Variable names are arbitrary (with some compiler-defined maximum length, typically 32 characters). C uses the following standard variable types:
-------------------------------------------------------
int -> integer variable
short -> short integer
long -> long integer
float -> single precision real (floating point) variable
double -> double precision real (floating point) variable
char -> character variable (single byte)
-------------------------------------------------------
The printf function can be instructed to print integers, floats and strings properly. The general syntax is
-------------------------------------------------------
printf( "format", variables );
-------------------------------------------------------
where "format" specifies the converstion specification and variables is a list of quantities to print. Some useful formats are
-------------------------------------------------------
%.nd integer (optional n = number of columns; if 0, pad with zeroes)
%m.nf float or double (optional m = number of columns,
n = number of decimal places)
%ns string (optional n = number of columns)
%c character
\n \t to introduce new line or tab
-------------------------------------------------------
So using this new knowledge, you could change the above program where it prints the cost to ...pay $.2f to... which would print the number, and then only two decimal places of the double. Look back up at the program one more time at the line cost = 0.55 * numcandy;. This is the math of the program. This line is fairly self explanatory. It takes the value which is stored in numcandy multiplies it with 0.55 and puts the resulting value into cost. Here are a list of the standard math operators you can use in your programs:
-------------------------------------------------------
+ add
- subtract
* multiply
/ divide
= equals (assignment operator)
-------------------------------------------------------
Now that we've covered printf and the basic data types, we can move onto program control meaning loops and conditions.
> Loops and Conditions
Real programs are than just getting input, performing some math, and then printing out the results. The next obvoius step would be to add some sort of control. Like for the above example program if they put in that they wanted to buy more than 1000 lollipops. We could print out, that the store doesn't have that many. This section will describe the basic loop and condition statements available in C, and then will end with a short example program putting a few to use.
First Conditional statements. C has three types of conditional statements if, else, and switch.
Here is the syntax for if and else:
-------------------------------------------------------
if (condition_1)
{
...block of statements executed if condition_1 is true
...statements are either more ifs and conditions or normal
normal lines of code that end with semi-colons
}
else if (condition_2)
{
...statements executed if condition_2 is true and condition_1 was false
}
else
{
...statements executed otherwise meaning 1 and 2 were false
}
-------------------------------------------------------
and any variant that derives from the above. Either by omitting branches or by including nested conditionals. Meaning having an if inside of another if. Now that we can make an if we need to make the condition that goes inside of the parenthesis of the if. Conditions are logical operators involving the comparison of quantities (of the same c type). Here are a list of the basic conditional operators.
-------------------------------------------------------
< smaller than
<= smaller than or equal to
== equal to
!= not equal to
>= greater than or equal to
> greater than
-------------------------------------------------------
and the boolean operators
-------------------------------------------------------
&& and
|| or
! not
-------------------------------------------------------
Now that you know the operators here are some sample conditions. For instance using the above example.
-------------------------------------------------------
if (numcandy > 1000)
{
printf("The store does not have that much candy in stock!");
}
else
.... print out the cost .....
-------------------------------------------------------
The boolean operators are for combining more than one condition into a single if statement. So for instance
-------------------------------------------------------
if (money>500 && cost<10) ...
-------------------------------------------------------
This condition would only be true if both money was greater than 500, and cost less than 10. If you replace the && with || then the condition would be true if either money is bigger than 500 OR cost less than 10. I think you get the picture. Another couple things to notice about conditional statements in C is that the equality operator is == (two equals) and not = one equals sign. The single equals sign as we learned in lesson two is the assignment operator. You use it to assign values to variables so when you want to test if a variable is equal to something use two equals signs == and not one. Remember THIS! It is very important.
Next with C conditional statements you can also omit (leave out) the operator. if you had a conditional statement:
-------------------------------------------------------
if (numcandy) {...}
-------------------------------------------------------
This would evaluate to true and the statements inside the {} brackets would be executed, if numcandy was not equal to zero. Understand? If you have no operators inside the parenthesis then C will simply check to see if the value is anything but zero.
Another conditional use is in the switch construct:
-------------------------------------------------------
switch (variable)
{
case const_1:
{
...block of statements...
break;
}
case const_2:
{
...block of statements...
break;
}
default:
{
...block of statements..
}
}
-------------------------------------------------------
The appropriate block of statements is executed according to the value of the expression, compared with the constant expressions in the case statement. The break statements insure that the statements in the cases following the chosen one will not be executed. If you would want to execute these statements, then you would leave out the break statements. This construct is particularly useful in handling input variables.
Here is an example, if you had a integer variable called race position.
-------------------------------------------------------
switch (race_position) {
case 1:
{
printf("First place, you get gold!\n");
break;
}
case 2:
{
printf("Second place, you get silver!\n");
break;
}
case 3:
{
printf("Third place, you get bronze!\n");
break;
}
default: // all other numbers (anything but 1,2,3)
{
printf("You dont get anything, you lose!\n");
break;
}
} // end switch
-------------------------------------------------------
You could have easily done this same thing with three if's and and else. But we wanted to demonstrate the switch. As an excersize rewrite the above code segment to use if's instead of the switch.
That's it for conditional statements, now we come to the next part of lesson three loops. C provides two basic types of loops a while and a for loop. While and for loops continue to repeat basically until certain conditions are met. Here is the syntax for while and for loops:
-------------------------------------------------------
while (condition)
{
...block of statements to execute...
}
-------------------------------------------------------
and the for loop:
-------------------------------------------------------
for (expression_1; expression_2; expression_3)
{
...block of statements to execute...
}
-------------------------------------------------------
The while loop continues to loop until the condition becomes false. The condition is tested upon entering the loop. Any logical construction (see below for a list) can be used in this context.
The for loop is a special case, and is equivalent to the following while loop:
-------------------------------------------------------
expression_1;
while (expression_2)
{
...block of statements...
expression_3;
}
-------------------------------------------------------
For instance, the following structure is often encountered:
-------------------------------------------------------
i = initial_i;
while (i <= i_max)
{
...block of statements...
i = i + i_increment;
}
-------------------------------------------------------
This structure may be rewritten in the easier syntax of the for loop as:
-------------------------------------------------------
for (i = initial_i; i <= i_max; i = i + i_increment)
{
...block of statements...
}
-------------------------------------------------------
For example, consider the following (admittedly not-so-clean) re-write of the previous loop:
-------------------------------------------------------
angle_degree = 0;
for ( ; ; )
{
...block of statements...
angle_degree = angle_degree + 10;
if (angle_degree == 360) break;
}
-------------------------------------------------------
The conditional if simply asks whether angle_degree is equal to 360 or not; if yes, the loop is stopped. Finally, to wrap up lesson three we will introduce #define's and then give the sample program. You can define constants of any type by using the #define compiler directive. Its syntax is simple--for instance
-------------------------------------------------------
#define ANGLE_MIN 0
#define ANGLE_MAX 360
-------------------------------------------------------
would define ANGLE_MIN and ANGLE_MAX to the values 0 and 360, respectively. C distinguishes between lowercase and uppercase letters in variable names. It is customary to use only capital letters in defining global constants. Then inside your program you could insert ANGLE_MAX anytime you wanted to use the number 360.
Now here is the sample program. You should be able to understand all the code there in. Basically there is a #define for a number of lollipopps, and you sell lollipopps until you run it, but there is also a limit too how many you can get at one time. Then after all are sold, the amount of money for all purchases is printed out.
-------------------------------------------------------
#include <stdio.h>
#define START_NUMBERLOLLIPOPS 100
#define MAX_AT_ONCE 30
void main()
{
int numcandy;
double cost;
int numberlollipopps;
double totalcost;
numberlollipopps = START_NUMBERLOLLIPOPS; // set start value to our constant
totalcost = 0;
while (numberlollipopps > 0)
{
printf("(%d left) How many lollipops do you want (-1 quits): ", numberlollipopps);
scanf("%d", &numcandy);
if (numcandy == -1) // since this if has only one statement brackets are not needed
break; // exit out of the while loop
else if (numcandy > MAX_AT_ONCE || numcandy <= 0 || numcandy > numberlollipopps)
{
printf("You cannot have that many, enter another number\n");
}
else
{
cost = 0.55 * numcandy;
printf("\nPlease pay $%.2f to the cashier!\n", cost);
totalcost = totalcost + cost;
numberlollipopps = numberlollipopps - numcandy;
}
}
printf("All the lollipopps have been sold for : $%.2f\n", totalcost);
}
-------------------------------------------------------
> Pointers and Arrays
The C language takes a lot of flack for its ability to peek and poke directly into memory. This gives great flexibility and power to the language, but it also makes it one of the great hurdles that the beginner must have in using the language. Arrays are very interesting since they can be accessed through pointers or array syntax, that is why they are grouped into the same lesson. With that said, let's get started.
All variables obviously have to be stored into memory, but where are they stored? Imagine memory as this big long street with houses on it. Each variable is a house on a street. Each house can hold a number of people (value of variable). But how do you find out how many people (what value is stored) are in a particular house. You have to have some kind of address. Then with the address, you can go to the house and then ask it: How many are there? Then you can get the value of a variable. This is the basic concept behind pointers. This should seem very logical, if not please reread this paragraph tell makes sense.
Let us use this new knowledge to examine a couple of statements.
-------------------------------------------------------
int var_x;
int* ptrX;
var_x = 6;
ptrX = &var_x;
*ptrX = 12;
printf("value of x : %d", var_x);
-------------------------------------------------------
The fourth line is a bit more complex. An asterick * in front of a variable tells C to dereference the pointer, and go to memory. Then you can make assignments to variable stored at that location. Since ptrX points to var_x, line 4 is equivalent to this command: var_x = 12; Pretty cool, eh? You can reference a variable and access its data through a pointer. Windows and all sorts of programs do this all the time. They hand pointers of data to each other, and allow other applications to access the memory they have.
Now you may be wondering, why are pointers so comlpex, or I've heard the using pointers can cause problems. It can, and for those who aren't careful misuse of pointer can do a lot of damage. Suppose that we forget to type in line 3 ptrX = &var_x; when we entered the program. What would happen if we executed it, who knows? Without this line ptrX is never assigned an address. Basically it points to some random data anywhere in memory. If you executed line 4 without line 3. You could get very wierd results. Since ptrX hasn't been pointed to our var_x, maybe its points to system memory, and then you assign a value someplace you shouldn't and your computer crashes. This may not always happen, but it is certainly a possibility, so be very careful when using pointers. Make sure they are assigned to something before you use them.
With that basic understanding of pointers, it is time to move to arrays. The most obvious use of arrays would be an array of characters also commonly knows as a string. The following program will make a string, access some data in it, print it out. Access it again using pointers, and then print the string out. It should print out "Hi!" and "012345678" on different lines. The explanation of the code and arrays will follow
-------------------------------------------------------
#include <stdio.h>
#define STR_LENGTH 10
void main()
{
char Str[STR_LENGTH];
char* pStr;
int i;
Str[0] = 'H';
Str[1] = 'i';
Str[2] = '!';
Str[3] = '\0'; // special end string character
printf("The string in Str is : %s\n", Str);
pStr = &Str[0];
for (i = 0; i < STR_LENGTH; i++)
{
*pStr = '0'+i;
pStr++;
}
Str[STR_LENGTH-1] = '\0';
printf("The string in Str is : %s\n", Str);
}
-------------------------------------------------------
First off, let's talk about the array notation to declare an array in C, you use [] square braces. The line of the program char Str[STR_LENGTH]; declares an array of ten characters. Basically this is just ten individual chars which are all put together in memory into the same place. An apartment complex in memory to use our pointer metaphor. They can all be access through our variable name Str along with a [n] where n is the element number (apartment number at same address). Also notice that when C declares an array of ten. The elements you can access are numbered 0 to 9. Accessing the first apartment corresponds to accessing the zeroeth element in C. Arrays are always like this, so learn to deal with it. Always count from 0 to size of array - 1.
Next notice that we put the letters "Hi!" into the array, but then we put in a '\0' You are probably wondering what this is. If you recall in lesson two on printf, there are special charcter sequences that do special things like "\n" stands for new line. Well time to learn a new one. "\0" stands for end string. All character strings need to end with this special character '\0'. If they do not, and then someone calls printf on the string. Then printf would start at the memory location of your string, and continue printing tell it encounters '\0' So you will end up with a bunch of garbage at the end of your string. So make sure to terminate your strings properly.
The next part of the code to discuss is our pointer access to the string. Just like we learned, I declared a character pointer with an asterick and gave it the name pStr. I then pointed pStr to the starting address of our character string using the line pStr = &Str[0];. Now pStr points to the start of our char array Str. Then I used a for loop, and started at 0, went through 10 elements of the array (STR_LENGTH) and assigned the corresponding value of i. The line pStr++; may seem a bit confusing. C has a bunch of short cuts to manipulate variables the ++ just means add one to the variable (in this case it moves the pointer to the next element in the array). The ++ syntax here is equivalent to pStr = pStr + 1;. After manipulating the string, I terminated it with '\0' and printed it out. That about does it for pointers and arrays, here are a few quick notes. You should note that you will see other shortcuts in C like -- (subtracts one) or +=3; (adds three). Here is a sample line to make an array of five integers: int arrayofint[5];.
> Functions
This part should be quite simple compared to the last part. Functions are easy to use; they allow complicated programs to be parcelled up into small blocks, each of which is easier to write, read, and maintain. We have already encountered the function main and made use of printf from the standard library. Now let's look at writing and using our own functions.
Calling a Function
The call to a function in C simply entails referencing its name with the appropriate arguments. The C compiler checks for compatibility between the arguments in the calling sequence and the definition of the function. When someone writes a function for someone to use that funciton will often be contained in a different C source file. Sometimes, however we may not have access to all the source code for all the functions. This is the case for most standard library functions like printf. However, we still know how to use printf, and the arguments it requires because those arguments are listed in the header file called stdio.h, which we have been including in our programs.
Library functions are generally not available to us in source form. Argument type checking is accomplished through the use of header files (like stdio.h) which contain all the necessary information. The most commonly used header files are
-------------------------------------------------------
<stdio.h> -> defining I/O routines
<string.h> -> defining string manipulation routines
<math.h> -> defining mathematical routines
<stdlib.h> -> defining number conversion, storage allocation
and similar tasks
<stdarg.h> -> defining libraries to handle routines with variable
numbers of arguments
<time.h> -> defining time-manipulation routines
-------------------------------------------------------
As an example, consider exchanging two numbers between variables. First let's illustrate what happen if the variables are passed by value:
-------------------------------------------------------
#include <stdio.h>
void exchange(int a, int b);
void main()
{ /* WRONG CODE */
int a, b;
a = 5;
b = 7;
printf("From main: a = %d, b = %d\n", a, b);
exchange(a, b);
printf("Back in main: ");
printf("a = %d, b = %d\n", a, b);
}
void exchange(int a, int b)
{
int temp;
temp = a;
a = b;
b = temp;
printf(" From function exchange: ");
printf("a = %d, b = %d\n", a, b);
}
-------------------------------------------------------
Run this code and observe that a and b are NOT exchanged! Only the copies of the arguments are exchanged. The RIGHT way to do this is of course to use pointers. Also note that in the above code how the function exchange was prototyped. It was declared with a semicolon, and ZERO statements before the main function. This is called forward declaration. This allows the C Compiler to compile main, without not yet knowing the code for exchange. All it needs to know is what exchange arguments look like. This way we can put the exchange function after our main function. We could have easily put exchange before main and gotten rid of the declaration. The next code segment will fix exchange to use pointers, and move exchange above main to eliminate the need for the forward declaration.
Sorce- CProgramming book
Learning a programming language is a lot like learning a language that people speak like German or French. The best way to learn a 'human' language is to start speaking, listening, and repeating, and then leaving the grammar for later. The same can be applied to C, you need to start writing programs as quickly as possible. So, this tutorial will provided a lot of well commented sample programs that you can run and tinker with.
> Your First Program
If you've ever read another tutorial or a book on a programming language, then you've probably seen a program like this before. The hello world program. It is a perfect starting place for our tutorial. So, type the following program into your favorite editor. If you are using Windows then try notepad or DOS Edit, if using a *Nix variant use VI or emacs. Without further adew here is the program :
-------------------------------------------------------
/* hello world program */
#include "stdio.h"
void main()
{
printf("\nHello World\n"); // print to screen
}
-------------------------------------------------------
Save this code into a file, and call the file hello.c, then compile it by typing at a command prompt:
-------------------------------------------------------
gcc hello.c
-------------------------------------------------------
A C Program contains functions and variables. The functions specify the tasks to be performed by the program. The above program has one function called main. This function tells your program where to start running. main functions are normally kept short and calls different functions to perform the necessary sub-tasks. All C codes must have a main function.
Also notice that C is case-sensitive. The commands have to be written like they are above. C also denotes the end of statement with a semi-colon like Java & Pascal. Brackets signify either to "{" begin a group of statements, or "}" end a group of statements. The // or /* comment */ designates a comment. Anything after two slashes the compiler ignores. The last part of the program you should notice is the #include. This simply includes a group of functions from the filename specified between then less than and greater than signs (<...>). The file above stdio.h contains a list of standard functions for C to use, the function the our above program uses is printf. Printf takes a string of characters between quotation marks, and outputs them to the screen.
Now that you understand the hello world program it's time to learn more about data types, and the printf statement.
> Data Types and Printf
Here is our next program to discuss. It declares several variables, performs some computations, and then outputs the results of those computations to the screen.
-------------------------------------------------------
#include <stdio.h>
void main()
{
int numcandy; // declare a number variable
double cost; // declare a variable that can store decimals
printf("How many lollipops do you want: ");
scanf("%d", &numcandy); // get input from user
cost = 0.55 * numcandy; // do some math
printf("\nPlease pay %f to the cashier!\n", cost);
}
-------------------------------------------------------
This program when run, well prompt the user for how many lollipops they would like. After they enter a number and press enter, it will print out how much it will cost them to buy that many lollipops assuming lollipops cost 55 cents each.
The main, {} brackets, comments, and include should be familiar. The new part of this program is the scanf, variables, and the use of printf to print out numbers. of this program that we haven't seen before.
First lets talk about the variables. This program has two variables numcandy, and cost. These are the names of the variables. Before a variable can be used it must be declared. Declaring a variable in C is easy, you simply tell the compiler the type, and the name you want the variable to have. So in the above example. The line int numcandy; tells the compiler you want a variable of type integer with the name numcandy. Then you can simply use numcandy through out your program to store int's (integers). Integers are non decimal numbers like -13, 0, 10, 5, etc. The other variable in the above program is a double which is used for storing numbers with decimals.
Next the scanf, and printf statements. After running the program you will notice that the scanf function simply gets a value from the user. It waits for the user to enter a number, and then press enter. It puts this value into our variable numcandy. Next notice some special character sequences contained in both the scanf and printf. First the printf has the sequence "\n". This sequence simply means move to the next line. Next we have the %f and %d. These tell printf and scanf what type of variables to expect. %f corresponds to double, and %d is for int. So if you changed the above programs printf line to ...pay $%d to... then you would get wierd output since printf would try to print cost as an integer, which doesn't make sense, because cost is a double. You need to make sure the "%" go with the right types.
Variable names are arbitrary (with some compiler-defined maximum length, typically 32 characters). C uses the following standard variable types:
-------------------------------------------------------
int -> integer variable
short -> short integer
long -> long integer
float -> single precision real (floating point) variable
double -> double precision real (floating point) variable
char -> character variable (single byte)
-------------------------------------------------------
The printf function can be instructed to print integers, floats and strings properly. The general syntax is
-------------------------------------------------------
printf( "format", variables );
-------------------------------------------------------
where "format" specifies the converstion specification and variables is a list of quantities to print. Some useful formats are
-------------------------------------------------------
%.nd integer (optional n = number of columns; if 0, pad with zeroes)
%m.nf float or double (optional m = number of columns,
n = number of decimal places)
%ns string (optional n = number of columns)
%c character
\n \t to introduce new line or tab
-------------------------------------------------------
So using this new knowledge, you could change the above program where it prints the cost to ...pay $.2f to... which would print the number, and then only two decimal places of the double. Look back up at the program one more time at the line cost = 0.55 * numcandy;. This is the math of the program. This line is fairly self explanatory. It takes the value which is stored in numcandy multiplies it with 0.55 and puts the resulting value into cost. Here are a list of the standard math operators you can use in your programs:
-------------------------------------------------------
+ add
- subtract
* multiply
/ divide
= equals (assignment operator)
-------------------------------------------------------
Now that we've covered printf and the basic data types, we can move onto program control meaning loops and conditions.
> Loops and Conditions
Real programs are than just getting input, performing some math, and then printing out the results. The next obvoius step would be to add some sort of control. Like for the above example program if they put in that they wanted to buy more than 1000 lollipops. We could print out, that the store doesn't have that many. This section will describe the basic loop and condition statements available in C, and then will end with a short example program putting a few to use.
First Conditional statements. C has three types of conditional statements if, else, and switch.
Here is the syntax for if and else:
-------------------------------------------------------
if (condition_1)
{
...block of statements executed if condition_1 is true
...statements are either more ifs and conditions or normal
normal lines of code that end with semi-colons
}
else if (condition_2)
{
...statements executed if condition_2 is true and condition_1 was false
}
else
{
...statements executed otherwise meaning 1 and 2 were false
}
-------------------------------------------------------
and any variant that derives from the above. Either by omitting branches or by including nested conditionals. Meaning having an if inside of another if. Now that we can make an if we need to make the condition that goes inside of the parenthesis of the if. Conditions are logical operators involving the comparison of quantities (of the same c type). Here are a list of the basic conditional operators.
-------------------------------------------------------
< smaller than
<= smaller than or equal to
== equal to
!= not equal to
>= greater than or equal to
> greater than
-------------------------------------------------------
and the boolean operators
-------------------------------------------------------
&& and
|| or
! not
-------------------------------------------------------
Now that you know the operators here are some sample conditions. For instance using the above example.
-------------------------------------------------------
if (numcandy > 1000)
{
printf("The store does not have that much candy in stock!");
}
else
.... print out the cost .....
-------------------------------------------------------
The boolean operators are for combining more than one condition into a single if statement. So for instance
-------------------------------------------------------
if (money>500 && cost<10) ...
-------------------------------------------------------
This condition would only be true if both money was greater than 500, and cost less than 10. If you replace the && with || then the condition would be true if either money is bigger than 500 OR cost less than 10. I think you get the picture. Another couple things to notice about conditional statements in C is that the equality operator is == (two equals) and not = one equals sign. The single equals sign as we learned in lesson two is the assignment operator. You use it to assign values to variables so when you want to test if a variable is equal to something use two equals signs == and not one. Remember THIS! It is very important.
Next with C conditional statements you can also omit (leave out) the operator. if you had a conditional statement:
-------------------------------------------------------
if (numcandy) {...}
-------------------------------------------------------
This would evaluate to true and the statements inside the {} brackets would be executed, if numcandy was not equal to zero. Understand? If you have no operators inside the parenthesis then C will simply check to see if the value is anything but zero.
Another conditional use is in the switch construct:
-------------------------------------------------------
switch (variable)
{
case const_1:
{
...block of statements...
break;
}
case const_2:
{
...block of statements...
break;
}
default:
{
...block of statements..
}
}
-------------------------------------------------------
The appropriate block of statements is executed according to the value of the expression, compared with the constant expressions in the case statement. The break statements insure that the statements in the cases following the chosen one will not be executed. If you would want to execute these statements, then you would leave out the break statements. This construct is particularly useful in handling input variables.
Here is an example, if you had a integer variable called race position.
-------------------------------------------------------
switch (race_position) {
case 1:
{
printf("First place, you get gold!\n");
break;
}
case 2:
{
printf("Second place, you get silver!\n");
break;
}
case 3:
{
printf("Third place, you get bronze!\n");
break;
}
default: // all other numbers (anything but 1,2,3)
{
printf("You dont get anything, you lose!\n");
break;
}
} // end switch
-------------------------------------------------------
You could have easily done this same thing with three if's and and else. But we wanted to demonstrate the switch. As an excersize rewrite the above code segment to use if's instead of the switch.
That's it for conditional statements, now we come to the next part of lesson three loops. C provides two basic types of loops a while and a for loop. While and for loops continue to repeat basically until certain conditions are met. Here is the syntax for while and for loops:
-------------------------------------------------------
while (condition)
{
...block of statements to execute...
}
-------------------------------------------------------
and the for loop:
-------------------------------------------------------
for (expression_1; expression_2; expression_3)
{
...block of statements to execute...
}
-------------------------------------------------------
The while loop continues to loop until the condition becomes false. The condition is tested upon entering the loop. Any logical construction (see below for a list) can be used in this context.
The for loop is a special case, and is equivalent to the following while loop:
-------------------------------------------------------
expression_1;
while (expression_2)
{
...block of statements...
expression_3;
}
-------------------------------------------------------
For instance, the following structure is often encountered:
-------------------------------------------------------
i = initial_i;
while (i <= i_max)
{
...block of statements...
i = i + i_increment;
}
-------------------------------------------------------
This structure may be rewritten in the easier syntax of the for loop as:
-------------------------------------------------------
for (i = initial_i; i <= i_max; i = i + i_increment)
{
...block of statements...
}
-------------------------------------------------------
For example, consider the following (admittedly not-so-clean) re-write of the previous loop:
-------------------------------------------------------
angle_degree = 0;
for ( ; ; )
{
...block of statements...
angle_degree = angle_degree + 10;
if (angle_degree == 360) break;
}
-------------------------------------------------------
The conditional if simply asks whether angle_degree is equal to 360 or not; if yes, the loop is stopped. Finally, to wrap up lesson three we will introduce #define's and then give the sample program. You can define constants of any type by using the #define compiler directive. Its syntax is simple--for instance
-------------------------------------------------------
#define ANGLE_MIN 0
#define ANGLE_MAX 360
-------------------------------------------------------
would define ANGLE_MIN and ANGLE_MAX to the values 0 and 360, respectively. C distinguishes between lowercase and uppercase letters in variable names. It is customary to use only capital letters in defining global constants. Then inside your program you could insert ANGLE_MAX anytime you wanted to use the number 360.
Now here is the sample program. You should be able to understand all the code there in. Basically there is a #define for a number of lollipopps, and you sell lollipopps until you run it, but there is also a limit too how many you can get at one time. Then after all are sold, the amount of money for all purchases is printed out.
-------------------------------------------------------
#include <stdio.h>
#define START_NUMBERLOLLIPOPS 100
#define MAX_AT_ONCE 30
void main()
{
int numcandy;
double cost;
int numberlollipopps;
double totalcost;
numberlollipopps = START_NUMBERLOLLIPOPS; // set start value to our constant
totalcost = 0;
while (numberlollipopps > 0)
{
printf("(%d left) How many lollipops do you want (-1 quits): ", numberlollipopps);
scanf("%d", &numcandy);
if (numcandy == -1) // since this if has only one statement brackets are not needed
break; // exit out of the while loop
else if (numcandy > MAX_AT_ONCE || numcandy <= 0 || numcandy > numberlollipopps)
{
printf("You cannot have that many, enter another number\n");
}
else
{
cost = 0.55 * numcandy;
printf("\nPlease pay $%.2f to the cashier!\n", cost);
totalcost = totalcost + cost;
numberlollipopps = numberlollipopps - numcandy;
}
}
printf("All the lollipopps have been sold for : $%.2f\n", totalcost);
}
-------------------------------------------------------
> Pointers and Arrays
The C language takes a lot of flack for its ability to peek and poke directly into memory. This gives great flexibility and power to the language, but it also makes it one of the great hurdles that the beginner must have in using the language. Arrays are very interesting since they can be accessed through pointers or array syntax, that is why they are grouped into the same lesson. With that said, let's get started.
All variables obviously have to be stored into memory, but where are they stored? Imagine memory as this big long street with houses on it. Each variable is a house on a street. Each house can hold a number of people (value of variable). But how do you find out how many people (what value is stored) are in a particular house. You have to have some kind of address. Then with the address, you can go to the house and then ask it: How many are there? Then you can get the value of a variable. This is the basic concept behind pointers. This should seem very logical, if not please reread this paragraph tell makes sense.
Let us use this new knowledge to examine a couple of statements.
-------------------------------------------------------
int var_x;
int* ptrX;
var_x = 6;
ptrX = &var_x;
*ptrX = 12;
printf("value of x : %d", var_x);
-------------------------------------------------------
The fourth line is a bit more complex. An asterick * in front of a variable tells C to dereference the pointer, and go to memory. Then you can make assignments to variable stored at that location. Since ptrX points to var_x, line 4 is equivalent to this command: var_x = 12; Pretty cool, eh? You can reference a variable and access its data through a pointer. Windows and all sorts of programs do this all the time. They hand pointers of data to each other, and allow other applications to access the memory they have.
Now you may be wondering, why are pointers so comlpex, or I've heard the using pointers can cause problems. It can, and for those who aren't careful misuse of pointer can do a lot of damage. Suppose that we forget to type in line 3 ptrX = &var_x; when we entered the program. What would happen if we executed it, who knows? Without this line ptrX is never assigned an address. Basically it points to some random data anywhere in memory. If you executed line 4 without line 3. You could get very wierd results. Since ptrX hasn't been pointed to our var_x, maybe its points to system memory, and then you assign a value someplace you shouldn't and your computer crashes. This may not always happen, but it is certainly a possibility, so be very careful when using pointers. Make sure they are assigned to something before you use them.
With that basic understanding of pointers, it is time to move to arrays. The most obvious use of arrays would be an array of characters also commonly knows as a string. The following program will make a string, access some data in it, print it out. Access it again using pointers, and then print the string out. It should print out "Hi!" and "012345678" on different lines. The explanation of the code and arrays will follow
-------------------------------------------------------
#include <stdio.h>
#define STR_LENGTH 10
void main()
{
char Str[STR_LENGTH];
char* pStr;
int i;
Str[0] = 'H';
Str[1] = 'i';
Str[2] = '!';
Str[3] = '\0'; // special end string character
printf("The string in Str is : %s\n", Str);
pStr = &Str[0];
for (i = 0; i < STR_LENGTH; i++)
{
*pStr = '0'+i;
pStr++;
}
Str[STR_LENGTH-1] = '\0';
printf("The string in Str is : %s\n", Str);
}
-------------------------------------------------------
First off, let's talk about the array notation to declare an array in C, you use [] square braces. The line of the program char Str[STR_LENGTH]; declares an array of ten characters. Basically this is just ten individual chars which are all put together in memory into the same place. An apartment complex in memory to use our pointer metaphor. They can all be access through our variable name Str along with a [n] where n is the element number (apartment number at same address). Also notice that when C declares an array of ten. The elements you can access are numbered 0 to 9. Accessing the first apartment corresponds to accessing the zeroeth element in C. Arrays are always like this, so learn to deal with it. Always count from 0 to size of array - 1.
Next notice that we put the letters "Hi!" into the array, but then we put in a '\0' You are probably wondering what this is. If you recall in lesson two on printf, there are special charcter sequences that do special things like "\n" stands for new line. Well time to learn a new one. "\0" stands for end string. All character strings need to end with this special character '\0'. If they do not, and then someone calls printf on the string. Then printf would start at the memory location of your string, and continue printing tell it encounters '\0' So you will end up with a bunch of garbage at the end of your string. So make sure to terminate your strings properly.
The next part of the code to discuss is our pointer access to the string. Just like we learned, I declared a character pointer with an asterick and gave it the name pStr. I then pointed pStr to the starting address of our character string using the line pStr = &Str[0];. Now pStr points to the start of our char array Str. Then I used a for loop, and started at 0, went through 10 elements of the array (STR_LENGTH) and assigned the corresponding value of i. The line pStr++; may seem a bit confusing. C has a bunch of short cuts to manipulate variables the ++ just means add one to the variable (in this case it moves the pointer to the next element in the array). The ++ syntax here is equivalent to pStr = pStr + 1;. After manipulating the string, I terminated it with '\0' and printed it out. That about does it for pointers and arrays, here are a few quick notes. You should note that you will see other shortcuts in C like -- (subtracts one) or +=3; (adds three). Here is a sample line to make an array of five integers: int arrayofint[5];.
> Functions
This part should be quite simple compared to the last part. Functions are easy to use; they allow complicated programs to be parcelled up into small blocks, each of which is easier to write, read, and maintain. We have already encountered the function main and made use of printf from the standard library. Now let's look at writing and using our own functions.
Calling a Function
The call to a function in C simply entails referencing its name with the appropriate arguments. The C compiler checks for compatibility between the arguments in the calling sequence and the definition of the function. When someone writes a function for someone to use that funciton will often be contained in a different C source file. Sometimes, however we may not have access to all the source code for all the functions. This is the case for most standard library functions like printf. However, we still know how to use printf, and the arguments it requires because those arguments are listed in the header file called stdio.h, which we have been including in our programs.
Library functions are generally not available to us in source form. Argument type checking is accomplished through the use of header files (like stdio.h) which contain all the necessary information. The most commonly used header files are
-------------------------------------------------------
<stdio.h> -> defining I/O routines
<string.h> -> defining string manipulation routines
<math.h> -> defining mathematical routines
<stdlib.h> -> defining number conversion, storage allocation
and similar tasks
<stdarg.h> -> defining libraries to handle routines with variable
numbers of arguments
<time.h> -> defining time-manipulation routines
-------------------------------------------------------
As an example, consider exchanging two numbers between variables. First let's illustrate what happen if the variables are passed by value:
-------------------------------------------------------
#include <stdio.h>
void exchange(int a, int b);
void main()
{ /* WRONG CODE */
int a, b;
a = 5;
b = 7;
printf("From main: a = %d, b = %d\n", a, b);
exchange(a, b);
printf("Back in main: ");
printf("a = %d, b = %d\n", a, b);
}
void exchange(int a, int b)
{
int temp;
temp = a;
a = b;
b = temp;
printf(" From function exchange: ");
printf("a = %d, b = %d\n", a, b);
}
-------------------------------------------------------
Run this code and observe that a and b are NOT exchanged! Only the copies of the arguments are exchanged. The RIGHT way to do this is of course to use pointers. Also note that in the above code how the function exchange was prototyped. It was declared with a semicolon, and ZERO statements before the main function. This is called forward declaration. This allows the C Compiler to compile main, without not yet knowing the code for exchange. All it needs to know is what exchange arguments look like. This way we can put the exchange function after our main function. We could have easily put exchange before main and gotten rid of the declaration. The next code segment will fix exchange to use pointers, and move exchange above main to eliminate the need for the forward declaration.
Sorce- CProgramming book