<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-5670527689344996196</id><updated>2012-02-16T05:29:43.608-08:00</updated><title type='text'>programming of C</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://rao-programmingofc.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5670527689344996196/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://rao-programmingofc.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Ramjaan</name><uri>http://www.blogger.com/profile/07862712225011522143</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='http://1.bp.blogspot.com/_uyQpXxI_GeY/SQy7Z8dn6II/AAAAAAAAACI/94M3auWVt4A/S220/images.jpeg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>16</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-5670527689344996196.post-8704868298341614688</id><published>2008-02-10T00:29:00.001-08:00</published><updated>2008-02-10T00:34:22.702-08:00</updated><title type='text'>Thank you for visiting</title><content type='html'>If you need help just do post.&lt;br /&gt;I will help you.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;thank you&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5670527689344996196-8704868298341614688?l=rao-programmingofc.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://rao-programmingofc.blogspot.com/feeds/8704868298341614688/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5670527689344996196&amp;postID=8704868298341614688' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5670527689344996196/posts/default/8704868298341614688'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5670527689344996196/posts/default/8704868298341614688'/><link rel='alternate' type='text/html' href='http://rao-programmingofc.blogspot.com/2008/02/thank-you-for-visiting.html' title='Thank you for visiting'/><author><name>Ramjaan</name><uri>http://www.blogger.com/profile/07862712225011522143</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='http://1.bp.blogspot.com/_uyQpXxI_GeY/SQy7Z8dn6II/AAAAAAAAACI/94M3auWVt4A/S220/images.jpeg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5670527689344996196.post-3793953363378639514</id><published>2008-02-08T04:44:00.003-08:00</published><updated>2008-02-08T05:00:30.588-08:00</updated><title type='text'>Strings</title><content type='html'>&lt;span style="font-weight:bold;"&gt;&lt;/span&gt;Strings &lt;br /&gt;Objectives&lt;br /&gt;&lt;br /&gt;This section brings together the use of two of C's fundamental data types, ponters and arrays, in the use of handling strings.&lt;br /&gt;&lt;br /&gt;Having read this section you should be able to:&lt;br /&gt;&lt;br /&gt;   1. handle any string constant by storing it in an array.&lt;br /&gt;&lt;br /&gt;Stringing Along&lt;br /&gt;Now that we have mastered pointers and the relationship between arrays and pointers we can take a second look at strings. A string is just a character array with the convention that the end of the valid data is marked by a null '\0'. Now you should be able to see why you can read in a character string using scanf("%s", name) rather than scanf("%s",&amp;name) - name is already a pointer variable. Manipulating strings is very much a matter of pointers and special string functions. For example, the strlen(str) function returns the number of characters in the string str. It does this simply by counting the number of characters up to the first null in the character array - so it is important that you are using a valid null-terminated string. Indeed this is important with all of the C string functions.&lt;br /&gt;&lt;br /&gt;You might not think that you need a function to copy strings, but simple assignment between string variables doesn't work. For example:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;char a[l0],b[10];&lt;br /&gt;b = a;&lt;br /&gt;&lt;br /&gt;does not appear to make a copy of the characters in a, but this is an illusion. What actually happens is that the pointer b is set to point to the same set of characters that a points to, i.e. a second copy of the string isn't created.&lt;br /&gt;&lt;br /&gt;To do this you need strcopy(a,b) which really does make a copy of every character in a in the array b up to the first null character. In a similar fashion strcat(a,b) adds the characters in b to the end of the string stored in a. Finally there is the all-important strcmp(a,b) which compares the two strings character by character and returns true - that is 0 - if the results are equal.&lt;br /&gt;&lt;br /&gt;Again notice that you can't compare strings using a==b because this just tests to see if the two pointers a and b are pointing to the same memory location. Of course if they are then the two strings are the same, but it is still possible for two strings to be the same even if they are stored at different locations.&lt;br /&gt;&lt;br /&gt;You can see that you need to understand pointers to avoid making simple mistakes using strings. One last problem is how to initialise a character array to a string. You can't use:&lt;br /&gt;&lt;br /&gt;a = "hello";&lt;br /&gt;&lt;br /&gt;because a is a pointer and "hello" is a string constant. However, you can use:&lt;br /&gt;&lt;br /&gt;strcopy(a,"hello")&lt;br /&gt;&lt;br /&gt;because a string constant is passed in exactly the same way as a string variable, i.e. as a pointer. If you are worried where the string constant is stored, the answer is in a special area of memory along with all of the constants that the program uses. The main disadvantage of this method is that many compilers use an optimisation trick that results in only a single version of identical constants being stored. For example:&lt;br /&gt;&lt;br /&gt;strcopy(b,"hello");&lt;br /&gt;&lt;br /&gt;usually ends up with b pointing to the same string as a. In other words, this method isn't particularly safe!&lt;br /&gt;&lt;br /&gt;A much better method is to use array initialisation. You can specify constants to be used to initialise any variable when it is declared. For example:&lt;br /&gt;&lt;br /&gt;int a=10;&lt;br /&gt;&lt;br /&gt;declares a to be an integer and initialises it to 10. You can initialise an array using a similar notation. For example:&lt;br /&gt;&lt;br /&gt;int a[5] = {1,2,3,4,5};&lt;br /&gt;&lt;br /&gt;declares an integer array and initialises it so that a[0]= 1, a[1] = 2 and so on. A character array can be initialised in the same way. For example:&lt;br /&gt;&lt;br /&gt;char a[5]={'h','e','l','l','o'};&lt;br /&gt;&lt;br /&gt;but a much better way is to write:&lt;br /&gt;&lt;br /&gt;char a[6]="hello";&lt;br /&gt;&lt;br /&gt;which also automatically stores a null character at the end of the string - hence a[6] and not a[5]. If you really want to be lazy you can use:&lt;br /&gt;&lt;br /&gt;char a[] = "hello";&lt;br /&gt;&lt;br /&gt;and let the compiler work out how many array elements are needed. Some compilers cannot cope with the idea of initialising a variable that doesn't exist for the entire life of the program. For those compilers to make initialisation work you need to add the keyword static to the front of the string declaration, therefore:&lt;br /&gt;&lt;br /&gt;static char a[] = "hello";&lt;br /&gt;As easy as... B or C?&lt;br /&gt;A few words of warning. If you are familiar with BASIC then you will have to treat C strings, and even C arrays, with some caution. They are not as easy or as obvious to use and writing a program that manipulates text is harder in C than in BASIC. If you try to use C strings as if it were BASIC strings you are sure to create some very weird and wonderful bugs!&lt;br /&gt;A Sort Of Bubble Program&lt;br /&gt;This sections program implements a simple bubble sort - which is notorious for being one of the worst sorting methods known to programmer-kind, but it does have the advantage of being easy and instructive. Some of the routines have already been described in the main text and a range of different methods of passing data in functions have also been used.&lt;br /&gt;&lt;br /&gt;The main routine is sort which repeats the scan function on the array until the variable done is set to 0. The scan function simply scans down the array comparing elements that are next door to each other. If they are in the wrong order then function swap is called to swap them over.&lt;br /&gt;&lt;br /&gt;Study this program carefully with particular attention to the way arrays, array elements and variables are passed. It is worth saying that in some cases there are better ways of achieving the same results. In particular, it would have been easier not to use the variable done, but to have returned the state as the result of the scan function.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#include &lt;stdio.h&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;void randdat(int a[] , int n);&lt;br /&gt;void sort(int a[] , int n);&lt;br /&gt;void scan(int a[] , int n , int *done);&lt;br /&gt;void swap(int *a ,int *b);&lt;br /&gt;&lt;br /&gt;main()&lt;br /&gt; {&lt;br /&gt;   int i;&lt;br /&gt;   int a[20];&lt;br /&gt;&lt;br /&gt;   randdat(a , 20);&lt;br /&gt;   sort(a , 20);&lt;br /&gt;&lt;br /&gt;   for(i=0;i&lt;20;++i) printf("%d\n" ,a[i]);&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;void randdat(int a[1] , int n)&lt;br /&gt; {&lt;br /&gt;  int i;&lt;br /&gt;  for (i=0 ; i&lt;n ; ++i)&lt;br /&gt;   a[i] = rand()%n+1;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;void sort(int a[1] , int n)&lt;br /&gt; {&lt;br /&gt;  int done;&lt;br /&gt;  done = 1;&lt;br /&gt;  while(done == 1) scan(a , n , &amp;done);&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;void scan(int a[1] , int n , int *done)&lt;br /&gt; {&lt;br /&gt;  int i;&lt;br /&gt;  *done=0;&lt;br /&gt;  for(i=0 ; i&lt;n-1 ; ++i)&lt;br /&gt;   {&lt;br /&gt;     if(a[i]&lt;a[i+1])&lt;br /&gt;      {&lt;br /&gt;       swap(&amp;a[i],&amp;a[i+1]);&lt;br /&gt;       *done=1;&lt;br /&gt;      }&lt;br /&gt;   }&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;void swap(int *a ,int *b)&lt;br /&gt; {      &lt;br /&gt;  int temp;&lt;br /&gt;  temp = *a;&lt;br /&gt;  *a   = *b;&lt;br /&gt;  *b   = temp;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;[program]&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5670527689344996196-3793953363378639514?l=rao-programmingofc.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://rao-programmingofc.blogspot.com/feeds/3793953363378639514/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5670527689344996196&amp;postID=3793953363378639514' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5670527689344996196/posts/default/3793953363378639514'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5670527689344996196/posts/default/3793953363378639514'/><link rel='alternate' type='text/html' href='http://rao-programmingofc.blogspot.com/2008/02/strings.html' title='Strings'/><author><name>Ramjaan</name><uri>http://www.blogger.com/profile/07862712225011522143</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='http://1.bp.blogspot.com/_uyQpXxI_GeY/SQy7Z8dn6II/AAAAAAAAACI/94M3auWVt4A/S220/images.jpeg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5670527689344996196.post-9209790724306071519</id><published>2008-02-08T04:44:00.001-08:00</published><updated>2008-02-08T04:44:49.623-08:00</updated><title type='text'></title><content type='html'>&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5670527689344996196-9209790724306071519?l=rao-programmingofc.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://rao-programmingofc.blogspot.com/feeds/9209790724306071519/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5670527689344996196&amp;postID=9209790724306071519' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5670527689344996196/posts/default/9209790724306071519'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5670527689344996196/posts/default/9209790724306071519'/><link rel='alternate' type='text/html' href='http://rao-programmingofc.blogspot.com/2008/02/blog-post.html' title=''/><author><name>Ramjaan</name><uri>http://www.blogger.com/profile/07862712225011522143</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='http://1.bp.blogspot.com/_uyQpXxI_GeY/SQy7Z8dn6II/AAAAAAAAACI/94M3auWVt4A/S220/images.jpeg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5670527689344996196.post-4384202204704249811</id><published>2008-02-08T04:04:00.000-08:00</published><updated>2008-02-08T04:06:56.554-08:00</updated><title type='text'>Pointers</title><content type='html'>&lt;h1&gt;                                   Pointers&lt;/h1&gt;&lt;br /&gt;&lt;h2&gt;Objectives&lt;/h2&gt;  &lt;p&gt;Having read this section you should be able to:&lt;/p&gt;  &lt;ol&gt;&lt;li&gt;program using pointers&lt;/li&gt;&lt;li&gt;understand how C uses pointers with arrays&lt;/li&gt;&lt;/ol&gt;  &lt;hr /&gt;  &lt;a name="PP"&gt;&lt;h3&gt;Point to Point&lt;/h3&gt;&lt;/a&gt;  Pointers are a very powerful, but primitive facility contained in the C language. Pointers are a throwback to the days of low-level assembly language programming and as a result they are sometimes difficult to understand and subject to subtle and difficult-to-find errors. Still it has to be admitted that pointers are one of the great attractions of the C language and there will be many an experienced C programmer spluttering and fuming at the idea that we would dare to refer to pointers as 'primitive'!   &lt;p&gt;In an ideal world we would avoid telling you about pointers until the very last minute, but without them many of the simpler aspects of C just don't make any sense at all. So, with apologies, let's get on with pointers.&lt;/p&gt;  &lt;p&gt;A &lt;i&gt;variable&lt;/i&gt; is an area of &lt;i&gt;memory&lt;/i&gt; that has been given a name. For example:&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;int x;&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;is an area of memory that has been given the name &lt;tt&gt;&lt;b&gt;x&lt;/b&gt;&lt;/tt&gt;. The advantage of this scheme is that you can use the name to specify where to store data. For example:&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;x=lO;&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;is an instruction to store the data value &lt;tt&gt;&lt;b&gt;10&lt;/b&gt;&lt;/tt&gt; in the area of memory named &lt;tt&gt;&lt;b&gt;x&lt;/b&gt;&lt;/tt&gt;. The variable is such a fundamental idea that using it quickly becomes second nature, but there is another way of working with memory.&lt;/p&gt;  &lt;p&gt;The computer access its own memory not by using variable names but by using a memory map with each location of memory uniquely defined by a number, called the &lt;i&gt;address&lt;/i&gt; of that memory location.&lt;/p&gt;  &lt;p&gt;A pointer is a variable that stores this location of memory. In more fundamental terms, a pointer stores the &lt;i&gt;address&lt;/i&gt; of a variable . In more picturesque terms, a pointer points to a variable.&lt;/p&gt;  &lt;p&gt;A pointer has to be declared just like any other variable - remember a pointer is just a variable that stores an address. For example,&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;int *p;&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;is a pointer to an integer. Adding an asterisk in front of a variable's name declares it to be a pointer to the declared type. Notice that the asterisk applies only to the single variable name that it is in front of, so:&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;int *p , q;&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;declares a pointer to an &lt;b&gt;&lt;tt&gt;int&lt;/tt&gt;&lt;/b&gt; and an &lt;b&gt;&lt;tt&gt;int&lt;/tt&gt;&lt;/b&gt; variable, not two pointers.&lt;/p&gt;  &lt;p&gt;Once you have declared a pointer variable you can begin using it like any other variable, but in practice you also need to know the meaning of two new operators: &lt;tt&gt;&lt;b&gt;&amp;amp;&lt;/b&gt;&lt;/tt&gt; and &lt;tt&gt;&lt;b&gt;*&lt;/b&gt;&lt;/tt&gt;. The &lt;tt&gt;&lt;b&gt;&amp;amp;&lt;/b&gt;&lt;/tt&gt; operator returns the address of a variable. You can remember this easily because &lt;tt&gt;&lt;b&gt;&amp;amp;&lt;/b&gt;&lt;/tt&gt; is the &lt;b&gt;'A'&lt;/b&gt;mpersand character and it gets you the &lt;b&gt;'A'&lt;/b&gt;ddress. For example:&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;int *p , q;&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;declares &lt;tt&gt;&lt;b&gt;p&lt;/b&gt;&lt;/tt&gt;, a pointer to &lt;b&gt;&lt;tt&gt;int&lt;/tt&gt;&lt;/b&gt;, and &lt;tt&gt;&lt;b&gt;q&lt;/b&gt;&lt;/tt&gt; an &lt;b&gt;&lt;tt&gt;int&lt;/tt&gt;&lt;/b&gt; and the instruction:&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;p=&amp;q;&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;stores the address of &lt;tt&gt;&lt;b&gt;q&lt;/b&gt;&lt;/tt&gt; in &lt;tt&gt;&lt;b&gt;p&lt;/b&gt;&lt;/tt&gt;. After this instruction you can think of &lt;tt&gt;&lt;b&gt;p&lt;/b&gt;&lt;/tt&gt; as &lt;i&gt;pointing&lt;/i&gt; at &lt;tt&gt;&lt;b&gt;q&lt;/b&gt;&lt;/tt&gt;. Compare this to:&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;p=q;&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;which attempts to store the value in &lt;tt&gt;&lt;b&gt;q&lt;/b&gt;&lt;/tt&gt; in the pointer &lt;b&gt;&lt;tt&gt;p&lt;/tt&gt;&lt;/b&gt; - something which has to be considered an error.&lt;/p&gt;  &lt;p&gt;The second operator &lt;tt&gt;&lt;b&gt;*&lt;/b&gt;&lt;/tt&gt; is a little more difficult to understand. If you place &lt;tt&gt;&lt;b&gt;*&lt;/b&gt;&lt;/tt&gt; in front of a pointer variable then the result is the value stored in the variable pointed at. That is, &lt;tt&gt;&lt;b&gt;p&lt;/b&gt;&lt;/tt&gt; stores the &lt;i&gt;address&lt;/i&gt;, or &lt;i&gt;pointer&lt;/i&gt;, to another variable and &lt;tt&gt;&lt;b&gt;*p&lt;/b&gt;&lt;/tt&gt; is the &lt;i&gt;value&lt;/i&gt; stored in the variable that &lt;tt&gt;&lt;b&gt;p&lt;/b&gt;&lt;/tt&gt; &lt;i&gt;points&lt;/i&gt; at.&lt;/p&gt;  &lt;p&gt;The &lt;tt&gt;&lt;b&gt;*&lt;/b&gt;&lt;/tt&gt; operator is called the &lt;i&gt;de-referencing&lt;/i&gt; operator and it helps not to confuse it with multiplication or with its use in declaring a pointer.&lt;/p&gt;  &lt;p&gt;This multiple use of an operator is called &lt;i&gt;operator overload&lt;/i&gt;.&lt;/p&gt;  &lt;p&gt;Confused? Well most C programmers are confused when they first meet pointers. There seems to be just too much to take in on first acquaintance. However there are only three basic ideas:&lt;/p&gt;  &lt;ol&gt;&lt;li&gt;To declare a pointer add an &lt;tt&gt;&lt;b&gt;*&lt;/b&gt;&lt;/tt&gt; in front of its name.&lt;/li&gt;&lt;li&gt;To obtain the address of a variable us &lt;tt&gt;&lt;b&gt;&amp;amp;&lt;/b&gt;&lt;/tt&gt; in front of its name.&lt;/li&gt;&lt;li&gt;To obtain the value of a variable use &lt;tt&gt;&lt;b&gt;*&lt;/b&gt;&lt;/tt&gt; in front of a pointer's name.&lt;/li&gt;&lt;/ol&gt;  &lt;p&gt;Now see if you can work out what the following means:&lt;/p&gt;  &lt;pre&gt;&lt;kbd&gt;&lt;br /&gt;&lt;b&gt;int *a , b , c;&lt;br /&gt;b = 10;&lt;br /&gt;a = &amp;b;&lt;br /&gt;c = *a;&lt;br /&gt;&lt;/b&gt; &lt;/kbd&gt;&lt;/pre&gt;  &lt;p&gt;Firstly three variables are declared - &lt;tt&gt;&lt;b&gt;a&lt;/b&gt;&lt;/tt&gt; (a pointer to &lt;b&gt;&lt;tt&gt;int&lt;/tt&gt;&lt;/b&gt;), and &lt;tt&gt;&lt;b&gt;b&lt;/b&gt;&lt;/tt&gt; and &lt;tt&gt;&lt;b&gt;c&lt;/b&gt;&lt;/tt&gt; (both standard integers). The instruction stores the value l0 in the variable &lt;tt&gt;&lt;b&gt;b&lt;/b&gt;&lt;/tt&gt; in the usual way. The first 'difficult' instruction is &lt;b&gt;&lt;tt&gt;a=&amp;amp;b&lt;/tt&gt;&lt;/b&gt; which stores the address of &lt;tt&gt;&lt;b&gt;b&lt;/b&gt;&lt;/tt&gt; in &lt;tt&gt;&lt;b&gt;a&lt;/b&gt;&lt;/tt&gt;. After this &lt;tt&gt;&lt;b&gt;a&lt;/b&gt;&lt;/tt&gt; points to &lt;tt&gt;&lt;b&gt;b&lt;/b&gt;&lt;/tt&gt;.&lt;/p&gt;  &lt;p&gt;Finally &lt;b&gt;&lt;tt&gt;c = *a&lt;/tt&gt;&lt;/b&gt; stores the value in the varable pointed to by &lt;tt&gt;&lt;b&gt;a&lt;/b&gt;&lt;/tt&gt; in &lt;tt&gt;&lt;b&gt;c&lt;/b&gt;&lt;/tt&gt;. As &lt;tt&gt;&lt;b&gt;a&lt;/b&gt;&lt;/tt&gt; points to &lt;tt&gt;&lt;b&gt;b&lt;/b&gt;&lt;/tt&gt;, its value i.e. 1O is stored in &lt;tt&gt;&lt;b&gt;c&lt;/b&gt;&lt;/tt&gt;. In other words, this is a long winded way of writing&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;c = b;&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Notice that if &lt;tt&gt;&lt;b&gt;a&lt;/b&gt;&lt;/tt&gt; is an &lt;b&gt;&lt;tt&gt;int&lt;/tt&gt;&lt;/b&gt; and &lt;tt&gt;&lt;b&gt;p&lt;/b&gt;&lt;/tt&gt; is a pointer to an &lt;b&gt;&lt;tt&gt;int&lt;/tt&gt;&lt;/b&gt; then&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;a = p;&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;is nonsense because it tries to store the &lt;i&gt;address&lt;/i&gt; of an &lt;b&gt;&lt;tt&gt;int&lt;/tt&gt;&lt;/b&gt;, i.e. a &lt;i&gt;pointer value&lt;/i&gt;, in an &lt;b&gt;&lt;tt&gt;int&lt;/tt&gt;&lt;/b&gt;. Similarly:&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;a = &amp;p;&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;tries to store the &lt;i&gt;address&lt;/i&gt; of a &lt;i&gt;pointer variable&lt;/i&gt; in &lt;b&gt;&lt;tt&gt;a&lt;/tt&gt;&lt;/b&gt; and is equally wrong! The only assignment between an &lt;b&gt;&lt;tt&gt;int&lt;/tt&gt;&lt;/b&gt; and a pointer to &lt;b&gt;&lt;tt&gt;int&lt;/tt&gt;&lt;/b&gt; that makes sense is:&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;a = *p;&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;hr /&gt;  &lt;a name="SS"&gt;&lt;h3&gt;Swap Shop&lt;/h3&gt;&lt;/a&gt;  At the moment it looks as if pointers are just a complicated way of doing something we can already do by a simpler method. However, consider the following simple problem - write a function which swaps the contents of two variables. That is, write &lt;b&gt;&lt;tt&gt;swap(a,b)&lt;/tt&gt;&lt;/b&gt; which will swaps over the contents of &lt;tt&gt;&lt;b&gt;a&lt;/b&gt;&lt;/tt&gt; and &lt;tt&gt;&lt;b&gt;b&lt;/b&gt;&lt;/tt&gt;. In principle this should be easy:   &lt;pre&gt;&lt;kbd&gt;&lt;br /&gt;&lt;b&gt;function swap(int a , int b);&lt;br /&gt;{&lt;br /&gt; int temp;&lt;br /&gt; temp = a;&lt;br /&gt; a    = b;&lt;br /&gt; b    = temp;&lt;br /&gt;}&lt;br /&gt;&lt;/b&gt; &lt;/kbd&gt;&lt;/pre&gt;  &lt;p&gt;the only complication being the need to use a third variable &lt;b&gt;&lt;tt&gt;temp&lt;/tt&gt;&lt;/b&gt; to hold the value of &lt;tt&gt;&lt;b&gt;a&lt;/b&gt;&lt;/tt&gt; while the value of &lt;tt&gt;&lt;b&gt;b&lt;/b&gt;&lt;/tt&gt; overwrites it. However, if you try this function you will find that it doesn't work. You can use it - &lt;b&gt;&lt;tt&gt;swap(a,b)&lt;/tt&gt;&lt;/b&gt;; - until you are blue in the face, but it just will not change the values stored in &lt;tt&gt;&lt;b&gt;a&lt;/b&gt;&lt;/tt&gt; and &lt;tt&gt;&lt;b&gt;b&lt;/b&gt;&lt;/tt&gt; back in the calling program. The reason is that all parameters in C are &lt;i&gt;passed by value&lt;/i&gt;. That is, when you use &lt;b&gt;&lt;tt&gt;swap(a,b)&lt;/tt&gt;&lt;/b&gt; function the values in &lt;tt&gt;&lt;b&gt;a&lt;/b&gt;&lt;/tt&gt; and &lt;tt&gt;&lt;b&gt;b&lt;/b&gt;&lt;/tt&gt; are passed into the function &lt;b&gt;&lt;tt&gt;swap&lt;/tt&gt;&lt;/b&gt; via the parameters and any changes that are made to the parameters do not alter &lt;tt&gt;&lt;b&gt;a&lt;/b&gt;&lt;/tt&gt; and &lt;tt&gt;&lt;b&gt;b&lt;/b&gt;&lt;/tt&gt; back in the main program. The function &lt;tt&gt;&lt;b&gt;swap&lt;/b&gt;&lt;/tt&gt; does swap over the values in &lt;tt&gt;&lt;b&gt;a&lt;/b&gt;&lt;/tt&gt; and &lt;tt&gt;&lt;b&gt;b&lt;/b&gt;&lt;/tt&gt; within the function, but doesn't do so in the main program.&lt;/p&gt;  &lt;p&gt;The solution to this very common problem is to pass not the &lt;i&gt;values stored&lt;/i&gt; in the variables, but the &lt;i&gt;addresses of the variables&lt;/i&gt;. The function can then use pointers to get at the values in the variables in the main program and modify them. That is, the function should be:&lt;/p&gt;  &lt;pre&gt;&lt;kbd&gt;&lt;br /&gt;&lt;b&gt;function swap(int *a , int *b);&lt;br /&gt;{&lt;br /&gt; int temp;&lt;br /&gt; temp = *a;&lt;br /&gt; *a   = *b;&lt;br /&gt; *b   = temp;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/b&gt; &lt;/kbd&gt;&lt;/pre&gt;  &lt;p&gt;Notice that now the two parameters &lt;tt&gt;&lt;b&gt;a&lt;/b&gt;&lt;/tt&gt; and &lt;tt&gt;&lt;b&gt;b&lt;/b&gt;&lt;/tt&gt; are pointers and the assignments that effect the swap have to use the &lt;i&gt;de-reference&lt;/i&gt; operator to make sure that it is the &lt;i&gt;values&lt;/i&gt; of the &lt;i&gt;variables&lt;/i&gt; pointed at that are swapped. You should have no difficulty with:&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;temp = *a;&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;this just stores the value pointed at by &lt;tt&gt;&lt;b&gt;a&lt;/b&gt;&lt;/tt&gt; into &lt;b&gt;&lt;tt&gt;temp&lt;/tt&gt;&lt;/b&gt;. However,&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;*a = *b;&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;is a little more unusual in that it stores that value pointed at by &lt;tt&gt;&lt;b&gt;b&lt;/b&gt;&lt;/tt&gt; in place of the value pointed at by &lt;tt&gt;&lt;b&gt;a&lt;/b&gt;&lt;/tt&gt;. There is one final complication. When you use &lt;tt&gt;&lt;b&gt;swap&lt;/b&gt;&lt;/tt&gt; you have to remember to pass the addresses of the variables that you want to swap. That is not:&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;swap(a,b)&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;but&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;swap(&amp;amp;a,&amp;amp;b)&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;The rule is that whenever you want to pass a variable so that the function can modify its contents you have to pass it as an address. Equally the function has to be ready to accept an address and work with it. You can't take any old function and suddenly decide to pass it the address of a variable instead of its value. If you pass an address to a function that isn't expecting it the result is usually disaster and the same is true if you fail to pass an address to a function that is expecting one.&lt;/p&gt;  &lt;p&gt;For example, calling swap as &lt;tt&gt;&lt;b&gt;swap(a,b)&lt;/b&gt;&lt;/tt&gt; instead of &lt;tt&gt;&lt;b&gt;swap(&amp;amp;a,&amp;amp;b)&lt;/b&gt;&lt;/tt&gt; will result in two arbitrary areas of memory being swapped over, usually with the result that the entire system, not just your program, crashes.&lt;/p&gt;  &lt;p&gt;The need to pass an address to a function also explains the difference between the two I/O functions that we have been using since the beginning of this course. &lt;b&gt;&lt;tt&gt;printf&lt;/tt&gt;&lt;/b&gt; doesn't change the values of its parameters so it is called as &lt;b&gt;&lt;tt&gt;printf("%d",a)&lt;/tt&gt;&lt;/b&gt; but &lt;b&gt;&lt;tt&gt;scanf&lt;/tt&gt;&lt;/b&gt; does, because it is an input function, and so it is called as &lt;b&gt;&lt;tt&gt;scanf("%d",&amp;amp;a)&lt;/tt&gt;&lt;/b&gt;.&lt;/p&gt;  &lt;hr /&gt;  &lt;a name="PA"&gt;&lt;h3&gt;Pointers And Arrays&lt;/h3&gt;&lt;/a&gt;  In C there is a very close connection between pointers and arrays. In fact they are more or less one and the same thing! When you declare an array as:   &lt;p&gt;&lt;b&gt;&lt;tt&gt;int a[10];&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;you are in fact declaring a pointer &lt;tt&gt;&lt;b&gt;a&lt;/b&gt;&lt;/tt&gt; to the first element in the array. That is, &lt;tt&gt;&lt;b&gt;a&lt;/b&gt;&lt;/tt&gt; is exactly the same as &lt;b&gt;&lt;tt&gt;&amp;amp;a[0]&lt;/tt&gt;&lt;/b&gt;. The only difference between &lt;tt&gt;&lt;b&gt;a&lt;/b&gt;&lt;/tt&gt; and &lt;i&gt;a pointer variable&lt;/i&gt; is that the array name is a constant pointer - you cannot change the location it points at. When you write an expression such as &lt;b&gt;&lt;tt&gt;a[i]&lt;/tt&gt;&lt;/b&gt; this is converted into a pointer expression that gives the value of the appropriate element. To be more precise, &lt;b&gt;&lt;tt&gt;a[i]&lt;/tt&gt;&lt;/b&gt; is exactly equivalent to &lt;b&gt;&lt;tt&gt;*(a+i)&lt;/tt&gt;&lt;/b&gt; i.e. the value pointed at by &lt;b&gt;&lt;tt&gt;a + i&lt;/tt&gt;&lt;/b&gt; . In the same way &lt;b&gt;&lt;tt&gt;*(a+ 1)&lt;/tt&gt;&lt;/b&gt; is the same as &lt;b&gt;&lt;tt&gt;a[1]&lt;/tt&gt;&lt;/b&gt; and so on.&lt;/p&gt;  &lt;p&gt;Being able to add one to a pointer to get the next element of an array is a nice idea, but it does raise the question of what it means to add 'one' to a pointer. For example, in most implementations an &lt;b&gt;&lt;tt&gt;int&lt;/tt&gt;&lt;/b&gt; takes two memory locations and a &lt;b&gt;&lt;tt&gt;float&lt;/tt&gt;&lt;/b&gt; takes four. So if you declare an &lt;b&gt;&lt;tt&gt;int&lt;/tt&gt;&lt;/b&gt; array and add one to a pointer to it, then in fact the pointer will move on by two memory locations. However, if you declare a &lt;b&gt;&lt;tt&gt;float&lt;/tt&gt;&lt;/b&gt; array and add one to a pointer to it then the pointer has to move on by four memory locations. In other words, adding one to a pointer moves it on by an amount of storage depending on the type it is a pointer to.&lt;/p&gt;  &lt;p&gt;This is, of course, precisely why you have to declare the type that the pointer is to point at! Only by knowing that &lt;tt&gt;&lt;b&gt;a&lt;/b&gt;&lt;/tt&gt; is a pointer to &lt;tt&gt;&lt;b&gt;int&lt;/b&gt;&lt;/tt&gt; and &lt;tt&gt;&lt;b&gt;b&lt;/b&gt;&lt;/tt&gt; is a pointer to &lt;tt&gt;&lt;b&gt;float&lt;/b&gt;&lt;/tt&gt; can the compiler figure out that&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;a + 1&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;means move the pointer on by two memory locations i.e. add 2, and&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;b + 1&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;means move the pointer on by four memory locations i.e. add 4. In practice you don't have to worry about how much storage a pointer's base type takes up. All you do need to remember is that pointer arithmetic works in units of the data type that the pointer points at. Notice that you can even use &lt;tt&gt;&lt;b&gt;++&lt;/b&gt;&lt;/tt&gt; and &lt;tt&gt;&lt;b&gt;--&lt;/b&gt;&lt;/tt&gt; with a pointer, but not with an array name because this is a constant pointer and cannot be changed. So to summarise:&lt;/p&gt;  &lt;ol&gt;&lt;li&gt;An array's name is a &lt;b&gt;&lt;i&gt;constant pointer&lt;/i&gt;&lt;/b&gt; to the first element in the array that is &lt;b&gt;&lt;tt&gt;a==&amp;amp;a[0]&lt;/tt&gt;&lt;/b&gt; and &lt;b&gt;&lt;tt&gt;*a==a[0]&lt;/tt&gt;&lt;/b&gt;.&lt;/li&gt;&lt;li&gt;Array indexing is equivalent to pointer arithmetic - that is &lt;b&gt;&lt;tt&gt;a+i=&amp;amp;a[i]&lt;/tt&gt;&lt;/b&gt; and &lt;b&gt;&lt;tt&gt;*(a+i)==a[i]&lt;/tt&gt;&lt;/b&gt;.&lt;/li&gt;&lt;/ol&gt;  It is up to you whether you want to think about an array as an array or an area of storage associated with a &lt;b&gt;&lt;i&gt;constant pointer&lt;/i&gt;&lt;/b&gt;. The view of it as an array is the more sophisticated and the further away from the underlying way that the machine works. The view as a pointer and pointer arithmetic is more primitive and closer to the hardware. In most cases the distinction is irrelevant and purely a matter of taste.   &lt;p&gt;One final point connected with both arrays and &lt;b&gt;&lt;tt&gt;functions&lt;/tt&gt;&lt;/b&gt; is that when you pass an entire &lt;b&gt;&lt;tt&gt;array&lt;/tt&gt;&lt;/b&gt; to a &lt;b&gt;&lt;tt&gt;function&lt;/tt&gt;&lt;/b&gt; then by default you pass a pointer. This allows you to write functions that process entire arrays without having to pass every single value stored in the array - just a pointer to the first element. However, it also temps you to write some very strange code unless you keep a clear head. Try the following - write a function that will fill an array with random values &lt;b&gt;&lt;tt&gt;randdat(a,n)&lt;/tt&gt;&lt;/b&gt; where &lt;tt&gt;&lt;b&gt;a&lt;/b&gt;&lt;/tt&gt; is the array and &lt;tt&gt;&lt;b&gt;n&lt;/b&gt;&lt;/tt&gt; is its size. Your first attempt might be something like:&lt;/p&gt;  &lt;pre&gt;&lt;kbd&gt;&lt;br /&gt;&lt;b&gt;void randdat(int *pa , int n)&lt;br /&gt; {&lt;br /&gt;  for (pa = 0 ; pa &lt; n ; pa++ ) *pa = rand()%n + 1;&lt;br /&gt; }&lt;br /&gt;&lt;/b&gt; &lt;/kbd&gt;&lt;/pre&gt;  &lt;p&gt;Well I hope your first attempt wouldn't be like this because it is wrong on a number of counts! The problem is that the idea of a pointer and the idea of an index have been confused. The pointer &lt;b&gt;&lt;tt&gt;pa&lt;/tt&gt;&lt;/b&gt; is supposed to point to the first element of the array, but the &lt;b&gt;&lt;tt&gt;for&lt;/tt&gt;&lt;/b&gt; loop sets it to zero and then increments it though a series of memory locations nowhere near the array. A lesser error is to suppose that &lt;tt&gt;&lt;b&gt;n-1&lt;/b&gt;&lt;/tt&gt; is the correct final value of the array pointer! As before, you will be lucky if this program doesn't crash the system, let alone itself! The correct way of doing the job is to use a &lt;b&gt;&lt;tt&gt;for&lt;/tt&gt;&lt;/b&gt; loop to step from &lt;b&gt;&lt;tt&gt;0&lt;/tt&gt;&lt;/b&gt; to &lt;b&gt;&lt;tt&gt;n-1&lt;/tt&gt;&lt;/b&gt;, but to use pointer arithmetic to access the correct array element:&lt;/p&gt;  &lt;pre&gt;&lt;kbd&gt;&lt;br /&gt;&lt;b&gt;int randdat(int *pa , int n)&lt;br /&gt;{&lt;br /&gt; int i;&lt;br /&gt; for ( i=0 ; i&lt; n ; ++i)&lt;br /&gt;  {&lt;br /&gt;    *pa = rand()%n + 1;&lt;br /&gt;    ++pa;&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/b&gt; &lt;/kbd&gt;&lt;/pre&gt;  &lt;p&gt;Notice the way that the &lt;b&gt;&lt;tt&gt;for&lt;/tt&gt;&lt;/b&gt; loop looks just like the standard way of stepping through an array. If you want to make it look even more like indexing an array using a &lt;b&gt;&lt;tt&gt;for&lt;/tt&gt;&lt;/b&gt; loop you could write:&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;for(i=0 ; i&lt;n&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;or even:&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;for(i=0 ; i&lt;n&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;In other words, as long as you define &lt;tt&gt;&lt;b&gt;pa&lt;/b&gt;&lt;/tt&gt; as a pointer you can use array indexing notation with it and it looks as if you have actually passed an array. You can even declare a pointer variable using the notation:&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;int pa[];&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  that is, as an array with no size information. In this way the illusion of passing an array to a function is complete&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5670527689344996196-4384202204704249811?l=rao-programmingofc.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://rao-programmingofc.blogspot.com/feeds/4384202204704249811/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5670527689344996196&amp;postID=4384202204704249811' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5670527689344996196/posts/default/4384202204704249811'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5670527689344996196/posts/default/4384202204704249811'/><link rel='alternate' type='text/html' href='http://rao-programmingofc.blogspot.com/2008/02/pointers.html' title='Pointers'/><author><name>Ramjaan</name><uri>http://www.blogger.com/profile/07862712225011522143</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='http://1.bp.blogspot.com/_uyQpXxI_GeY/SQy7Z8dn6II/AAAAAAAAACI/94M3auWVt4A/S220/images.jpeg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5670527689344996196.post-4863058642475964077</id><published>2008-02-08T03:54:00.000-08:00</published><updated>2008-02-08T04:03:32.021-08:00</updated><title type='text'>Arrays</title><content type='html'>&lt;h1&gt;                            Arrays&lt;/h1&gt;&lt;br /&gt;&lt;h2&gt;Objectives&lt;/h2&gt; &lt;p&gt;Having read this section you should have a good understanding of the use of arrays in C.&lt;/p&gt; &lt;hr /&gt; &lt;p&gt; &lt;/p&gt; &lt;h3&gt;Advanced Data Types&lt;/h3&gt; Programming in any language takes a quite significant leap forwards as soon as you learn about more &lt;i&gt;advanced data types&lt;/i&gt; - &lt;i&gt;arrays&lt;/i&gt; and &lt;i&gt;strings of characters&lt;/i&gt;. In C there is also a third more general and even more powerful advanced data type - the &lt;b&gt;&lt;tt&gt;pointer&lt;/tt&gt;&lt;/b&gt; but more about that later. In this section we introduce the &lt;b&gt;&lt;tt&gt;array&lt;/tt&gt;&lt;/b&gt;, but the first question is, why bother? &lt;p&gt;There are times when we need to store a complete &lt;i&gt;list&lt;/i&gt; of numbers or other data items. You could do this by creating as many individual variables as would be needed for the job, but this is a hard and tedious process. For example, suppose you want to read in five numbers and print them out in reverse order. You could do it the hard way as:&lt;/p&gt; &lt;pre&gt;&lt;kbd&gt;&lt;br /&gt;&lt;b&gt;main()&lt;br /&gt;{&lt;br /&gt;int al,a2,a3,a4,a5;&lt;br /&gt;scanf("%d %d %d %d %d",&amp;amp;a1,&amp;amp;a2,&amp;amp;a3,&amp;amp;a4,&amp;amp;a5);&lt;br /&gt;printf("%d %d %d %d %d'',a5,a4,a3,a2,a1);&lt;br /&gt;}&lt;br /&gt;&lt;/b&gt; &lt;/kbd&gt;&lt;/pre&gt; &lt;p&gt;Doesn't look very pretty does it, and what if the problem was to read in 100 or more values and print them in reverse order? Of course the clue to the solution is the use of the regular variable names &lt;b&gt;&lt;tt&gt;a1&lt;/tt&gt;&lt;/b&gt;, &lt;b&gt;&lt;tt&gt;a2&lt;/tt&gt;&lt;/b&gt; and so on. What we would really like to do is to use a name like &lt;b&gt;&lt;tt&gt;a[i]&lt;/tt&gt;&lt;/b&gt; where &lt;tt&gt;&lt;b&gt;i&lt;/b&gt;&lt;/tt&gt; is a variable which specifies which particular value we are working with. This is the basic idea of an &lt;b&gt;array&lt;/b&gt; and nearly all programming languages provide this sort of facility - only the details alter.&lt;/p&gt; &lt;p&gt;In the case of C you have to declare an &lt;b&gt;array&lt;/b&gt; before you use it - in the same way you have to declare any sort of variable. For example,&lt;/p&gt; &lt;p&gt;&lt;b&gt;&lt;tt&gt;int a[5];&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt; &lt;p&gt;declares an array called &lt;tt&gt;&lt;b&gt;a&lt;/b&gt;&lt;/tt&gt; with five elements. Just to confuse matters a little the first element is &lt;b&gt;&lt;tt&gt;a[0]&lt;/tt&gt;&lt;/b&gt; and the last &lt;tt&gt;&lt;b&gt;a[4]&lt;/b&gt;&lt;/tt&gt;. C programmer's always start counting at zero! Languages vary according to where they start numbering arrays. Less technical, i.e. simpler, languages start counting from 1 and more technical ones usually start counting from 0. Anyway, in the case of C you have to remember that&lt;/p&gt; &lt;p&gt;&lt;b&gt;&lt;tt&gt;type array[size]&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt; &lt;p&gt;declares an &lt;tt&gt;&lt;b&gt;array&lt;/b&gt;&lt;/tt&gt; of the specified type and with size elements. The first &lt;b&gt;&lt;tt&gt;array&lt;/tt&gt;&lt;/b&gt; element is &lt;b&gt;&lt;tt&gt;array[0]&lt;/tt&gt;&lt;/b&gt; and the last is &lt;b&gt;&lt;tt&gt;array[size-1]&lt;/tt&gt;&lt;/b&gt;.&lt;/p&gt; &lt;p&gt;Using an &lt;b&gt;&lt;tt&gt;array&lt;/tt&gt;&lt;/b&gt;, the problem of reading in and printing out a set of values in reverse order becomes simple:&lt;/p&gt; &lt;pre&gt;&lt;kbd&gt;&lt;br /&gt;            &lt;br /&gt;&lt;b&gt;main()&lt;br /&gt; {&lt;br /&gt;  int a[5];&lt;br /&gt;  int i;&lt;br /&gt;  for(i =0;i &lt; 5; ++i) scanf("%d",&amp;amp;a[i]);&lt;br /&gt;  for(i =4;i&gt; =0;--i) printf("%d",a[i]);&lt;br /&gt; }&lt;br /&gt;&lt;/b&gt; &lt;/kbd&gt;&lt;/pre&gt; &lt;p&gt;[&lt;a href="http://www.le.ac.uk/cc/tutorials/c/programs/array1.c"&gt;program&lt;/a&gt;]&lt;/p&gt; &lt;p&gt;Well we said simple but I have to admit that the pair of &lt;b&gt;&lt;tt&gt;for&lt;/tt&gt;&lt;/b&gt; loops looks a bit intimidating. The &lt;b&gt;&lt;tt&gt;for&lt;/tt&gt;&lt;/b&gt; loop and the array data type were more or less made for each other. The &lt;b&gt;&lt;tt&gt;for&lt;/tt&gt;&lt;/b&gt; loop can be used to generate a sequence of values to pick out and process each element in an array in turn. Once you start using arrays, &lt;b&gt;&lt;tt&gt;for&lt;/tt&gt;&lt;/b&gt; loops like:&lt;/p&gt; &lt;p&gt;&lt;b&gt;&lt;tt&gt;for (i=0 ; i&lt;5&gt;&lt;/b&gt;&lt;/p&gt; &lt;p&gt;to generate values in the order 0,1,2 and so forth, and&lt;/p&gt; &lt;p&gt;&lt;b&gt;&lt;kbd&gt;for(i=4;i&gt;&lt;tt&gt;=0;--i)&lt;/tt&gt;&lt;/kbd&gt;&lt;/b&gt;&lt;/p&gt; &lt;p&gt;to generate values in the order 4,3,2... become very familiar.&lt;/p&gt; &lt;hr /&gt; &lt;h3&gt;In Dis-array&lt;/h3&gt; An array of character variables is in no way different from an array of numeric variables, but programmers often like to think about them in a different way. For example, if you want to read in and reverse five characters you could use: &lt;pre&gt;&lt;kbd&gt;&lt;br /&gt;&lt;b&gt;main()&lt;br /&gt; {&lt;br /&gt;  char a[5];&lt;br /&gt;  int i;&lt;br /&gt;  for(i=0; i&lt;5; ++i) scanf("%c",&amp;amp;a[i]);&lt;br /&gt;  for(i=4;i&gt;=0;--i) printf("%c",a[i]);&lt;br /&gt; }&lt;br /&gt;&lt;/b&gt; &lt;/kbd&gt;&lt;/pre&gt; &lt;p&gt;Notice that the only difference, is the declared type of the array and the &lt;b&gt;&lt;tt&gt;%c&lt;/tt&gt;&lt;/b&gt; used to specify that the data is to be interpreted as a character in &lt;b&gt;&lt;tt&gt;scanf&lt;/tt&gt;&lt;/b&gt; and &lt;b&gt;&lt;tt&gt;printf&lt;/tt&gt;&lt;/b&gt;. The trouble with character arrays is that to use them as if they were text strings you have to remember how many characters they hold. In other words, if you declare a character array 40 elements long and store H E L L O in it you need to remember that after element 4 the array is empty. This is such a nuisance that C uses the simple convention that the end of a string of characters is marked by a &lt;b&gt;&lt;i&gt;null&lt;/i&gt;&lt;/b&gt; character. A &lt;b&gt;&lt;i&gt;null&lt;/i&gt;&lt;/b&gt; character is, as you might expect, the character with ASCII code 0. If you want to store the &lt;b&gt;&lt;i&gt;null&lt;/i&gt;&lt;/b&gt; character in a character variable you can use the notation &lt;b&gt;&lt;tt&gt;\0&lt;/tt&gt;&lt;/b&gt; - but most of the time you don't have to actually use the &lt;b&gt;&lt;i&gt;null&lt;/i&gt;&lt;/b&gt; character. The reason is that C will automatically add a &lt;b&gt;&lt;i&gt;null&lt;/i&gt;&lt;/b&gt; character and store each character in a separate element when you use a string constant. A string constant is indicated by double quotes as opposed to a character constant which is indicated by a single quote. For example:&lt;/p&gt; &lt;p&gt;&lt;tt&gt;&lt;b&gt;"A"&lt;/b&gt;&lt;/tt&gt;&lt;/p&gt; &lt;p&gt;is a string constant, but&lt;/p&gt; &lt;p&gt;&lt;b&gt;'A'&lt;/b&gt;&lt;/p&gt; &lt;p&gt;is a character constant. The difference between these two superficially similar types of text is confusing at first and the source of many errors. All you have to remember is that "A" consists of two characters, the letter &lt;b&gt;&lt;tt&gt;A&lt;/tt&gt;&lt;/b&gt; followed by &lt;b&gt;&lt;tt&gt;\0&lt;/tt&gt;&lt;/b&gt; whereas 'A' is just the single character A. If you are familiar with other languages you might think that you could assign string constants to character arrays and work as if a string was a built-in data type. In C however the fundamental data type is the &lt;i&gt;array&lt;/i&gt; and &lt;i&gt;strings&lt;/i&gt; are very much grafted on. For example, if you try something like:&lt;/p&gt; &lt;pre&gt;&lt;kbd&gt;&lt;br /&gt;&lt;b&gt;char name[40];&lt;br /&gt;name="Hello"&lt;br /&gt;&lt;/b&gt; &lt;/kbd&gt;&lt;/pre&gt; &lt;p&gt;it will not work. However, you can print strings using &lt;b&gt;&lt;tt&gt;printf&lt;/tt&gt;&lt;/b&gt; and read them into character arrays using &lt;b&gt;&lt;tt&gt;scanf&lt;/tt&gt;&lt;/b&gt;. For example,&lt;/p&gt; &lt;pre&gt;&lt;kbd&gt;&lt;br /&gt;&lt;b&gt;main()&lt;br /&gt; {&lt;br /&gt;&lt;br /&gt;   static char name[40] ="hello";&lt;br /&gt;&lt;br /&gt;   printf("%s",name);&lt;br /&gt;   scanf("%s",name);&lt;br /&gt;   printf("%s",name);&lt;br /&gt;  }&lt;br /&gt;&lt;/b&gt; &lt;/kbd&gt;&lt;/pre&gt; &lt;p&gt;[&lt;a href="http://www.le.ac.uk/cc/tutorials/c/programs/array2.c"&gt;program&lt;/a&gt;]&lt;/p&gt; &lt;p&gt;This program reads in the text that you type, terminating it with a &lt;i&gt;null&lt;/i&gt; and stores it in the character array &lt;b&gt;&lt;tt&gt;name&lt;/tt&gt;&lt;/b&gt;. It then prints the character array treating it as a string, i.e. stopping when it hits the first &lt;i&gt;null&lt;/i&gt; string. Notice the use of the "&lt;tt&gt;&lt;b&gt;%s&lt;/b&gt;&lt;/tt&gt;" format descriptor in &lt;b&gt;&lt;tt&gt;scanf&lt;/tt&gt;&lt;/b&gt; and &lt;b&gt;&lt;tt&gt;printf&lt;/tt&gt;&lt;/b&gt; to specify that what is being printed is a string.&lt;/p&gt; &lt;p&gt;At this point the way that strings work and how they can be made a bit more useful and natural depends on understanding &lt;b&gt;&lt;tt&gt;pointers&lt;/tt&gt;&lt;/b&gt; which is covered in the next section.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5670527689344996196-4863058642475964077?l=rao-programmingofc.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://rao-programmingofc.blogspot.com/feeds/4863058642475964077/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5670527689344996196&amp;postID=4863058642475964077' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5670527689344996196/posts/default/4863058642475964077'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5670527689344996196/posts/default/4863058642475964077'/><link rel='alternate' type='text/html' href='http://rao-programmingofc.blogspot.com/2008/02/arrays.html' title='Arrays'/><author><name>Ramjaan</name><uri>http://www.blogger.com/profile/07862712225011522143</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='http://1.bp.blogspot.com/_uyQpXxI_GeY/SQy7Z8dn6II/AAAAAAAAACI/94M3auWVt4A/S220/images.jpeg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5670527689344996196.post-7094824499463238432</id><published>2008-02-08T03:52:00.000-08:00</published><updated>2008-02-08T03:54:25.124-08:00</updated><title type='text'>Data Types Part II</title><content type='html'>&lt;h1&gt;                     Data Types Part II&lt;/h1&gt;&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;Objectives&lt;/h2&gt; &lt;p&gt;So far we have looked at &lt;i&gt;local&lt;/i&gt; variable now we switch our attention to other types of variables supported by the C programming language:&lt;/p&gt; &lt;ol&gt;&lt;li&gt;Global Variables&lt;/li&gt;&lt;li&gt;Constant Data Types&lt;/li&gt;&lt;/ol&gt; &lt;hr /&gt;  &lt;a name="GV"&gt;&lt;h3&gt;Global variables&lt;/h3&gt;&lt;/a&gt;  &lt;p&gt;Variables can be declared as either &lt;i&gt;local variables&lt;/i&gt; which can be used inside the function it has been declared in (more on this in further sections) and &lt;i&gt;global variables&lt;/i&gt; which are known throughout the entire program. &lt;i&gt;Global variables&lt;/i&gt; are created by declaring them outside any function. For example:&lt;/p&gt;  &lt;pre&gt;&lt;kbd&gt;&lt;br /&gt;&lt;b&gt;int max;&lt;br /&gt;&lt;br /&gt;main()&lt;br /&gt; {&lt;br /&gt;   .....&lt;br /&gt; }&lt;br /&gt;f1()&lt;br /&gt; {&lt;br /&gt;   .....&lt;br /&gt; }&lt;br /&gt;&lt;/b&gt; &lt;/kbd&gt;&lt;/pre&gt; The &lt;b&gt;&lt;kbd&gt;int max&lt;/kbd&gt;&lt;/b&gt; can be used in both main and function &lt;tt&gt;&lt;b&gt;f1&lt;/b&gt;&lt;/tt&gt; and any changes made to it will remain consistent for both functions. The understanding of this will become clearer when you have studied the section on functions but I felt I couldn't complete a section on data types without mentioning &lt;i&gt;global&lt;/i&gt; and &lt;i&gt;local&lt;/i&gt; variables. &lt;hr /&gt;  &lt;a name="CT"&gt;&lt;/a&gt;&lt;h3&gt;&lt;a name="CT"&gt;Constant Data Types&lt;/a&gt;&lt;/h3&gt;  &lt;b&gt;Constants&lt;/b&gt; refer to fixed values that may not be altered by the program. All the data types we have previously covered can be defined as &lt;b&gt;constant data types&lt;/b&gt; if we so wish to do so. The &lt;b&gt;constant&lt;/b&gt; data types must be defined before the main function. The format is as follows: &lt;p&gt;&lt;b&gt;&lt;kbd&gt;#define CONSTANTNAME value&lt;/kbd&gt;&lt;/b&gt;&lt;/p&gt; &lt;p&gt;for example:&lt;/p&gt; &lt;p&gt;&lt;b&gt;&lt;kbd&gt;#define SALESTAX 0.05&lt;/kbd&gt;&lt;/b&gt;&lt;/p&gt; &lt;p&gt;The &lt;b&gt;constant&lt;/b&gt; name is normally written in capitals and does not have a semi-colon at the end. The use of &lt;b&gt;constants&lt;/b&gt; is mainly for making your programs easier to be understood and modified by others and yourself in the future. An example program now follows:&lt;/p&gt; &lt;pre&gt;&lt;kbd&gt;&lt;br /&gt;&lt;b&gt;#define SALESTAX 0.05&lt;br /&gt;#include &lt;stdio.h&gt;&lt;br /&gt;main()&lt;br /&gt;{&lt;br /&gt;  float amount, taxes, total;&lt;br /&gt;  printf("Enter the amount purchased : ");&lt;br /&gt;  scanf("%f",&amp;amp;amount);&lt;br /&gt;  taxes = SALESTAX*amount;&lt;br /&gt;  printf("The sales tax is £%4.2f",taxes);&lt;br /&gt;  printf("\n The total bill is £%5.2f",total);&lt;br /&gt;}&lt;br /&gt;&lt;/b&gt; &lt;/kbd&gt;&lt;/pre&gt; &lt;p&gt;The float constant &lt;b&gt;&lt;tt&gt;SALESTAX&lt;/tt&gt;&lt;/b&gt; is defined with value 0.05. Three &lt;i&gt;float variables&lt;/i&gt; are declared &lt;b&gt;&lt;tt&gt;amount&lt;/tt&gt;&lt;/b&gt;, &lt;b&gt;&lt;tt&gt;taxes&lt;/tt&gt;&lt;/b&gt; and &lt;b&gt;&lt;tt&gt;total&lt;/tt&gt;&lt;/b&gt;. Display message to the screen is achieved using &lt;tt&gt;&lt;b&gt;printf&lt;/b&gt;&lt;/tt&gt; and user input handled by &lt;tt&gt;&lt;b&gt;scanf&lt;/b&gt;&lt;/tt&gt;. Calculation is then performed and results sent to the screen. If the value of &lt;b&gt;&lt;tt&gt;SALESTAX&lt;/tt&gt;&lt;/b&gt; alters in the future it is very easy to change the value where it is defined rather than go through the whole program changing the individual values separately, which would be very time consuming in a large program with several references. The program is also improved when using constants rather than values as it improves the clarity.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5670527689344996196-7094824499463238432?l=rao-programmingofc.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://rao-programmingofc.blogspot.com/feeds/7094824499463238432/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5670527689344996196&amp;postID=7094824499463238432' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5670527689344996196/posts/default/7094824499463238432'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5670527689344996196/posts/default/7094824499463238432'/><link rel='alternate' type='text/html' href='http://rao-programmingofc.blogspot.com/2008/02/data-types-part-ii.html' title='Data Types Part II'/><author><name>Ramjaan</name><uri>http://www.blogger.com/profile/07862712225011522143</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='http://1.bp.blogspot.com/_uyQpXxI_GeY/SQy7Z8dn6II/AAAAAAAAACI/94M3auWVt4A/S220/images.jpeg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5670527689344996196.post-4775240390379398770</id><published>2008-02-08T03:48:00.000-08:00</published><updated>2008-02-08T03:50:53.624-08:00</updated><title type='text'>Functions and Prototypes</title><content type='html'>&lt;h1&gt;          Functions and Prototypes&lt;/h1&gt;&lt;br /&gt;&lt;h2&gt;Objectives&lt;/h2&gt;  &lt;p&gt;Having read this section you should be able to:&lt;/p&gt;  &lt;ol&gt;&lt;li&gt;program using correctly defined C functions&lt;/li&gt;&lt;li&gt;pass the value of local variables into your C functions&lt;/li&gt;&lt;/ol&gt;  &lt;hr /&gt;  &lt;a name="FB"&gt;&lt;h3&gt;Functions - C's Building Blocks&lt;/h3&gt;&lt;/a&gt;  Some programmers might consider it a bit early to introduce the C function - but we think you can't get to it soon enough. It isn't a difficult idea and it is incredibly useful. You could say that you only really start to find out what C programming is all about when you start using functions.   &lt;p&gt;C functions are the equivalent of what in other languages would be called &lt;i&gt;subroutines&lt;/i&gt; or &lt;i&gt;procedures&lt;/i&gt;. If you are familiar with another language you also need to know that C only has functions, so don't spend time looking for the definition of subroutines or procedures - in C the function does everything!&lt;/p&gt;  &lt;p&gt;A function is simply a chunk of C code (statements) that you have grouped together and given a name. The value of doing this is that you can use that "chunk" of code repeatedly simply by writing its name. For example, if you want to create a function that prints the word "&lt;tt&gt;&lt;b&gt;Hello&lt;/b&gt;&lt;/tt&gt;" on the screen and adds one to variable called &lt;b&gt;&lt;tt&gt;total&lt;/tt&gt;&lt;/b&gt; then the chunk of C code that you want to turn into a function is just:&lt;/p&gt;  &lt;pre&gt;&lt;kbd&gt;&lt;br /&gt;&lt;b&gt;printf("Hello");&lt;br /&gt;total = total + l;&lt;br /&gt;&lt;/b&gt; &lt;/kbd&gt;&lt;/pre&gt;  &lt;p&gt;To turn it into a function you simply wrap the code in a pair of curly brackets to convert it into a single &lt;i&gt;compound statement&lt;/i&gt; and write the name that you want to give it in front of the brackets:&lt;/p&gt;    &lt;pre&gt;&lt;kbd&gt;&lt;br /&gt;&lt;b&gt;demo()&lt;br /&gt;{&lt;br /&gt; printf("Hello");&lt;br /&gt; total = total + 1;&lt;br /&gt;}&lt;br /&gt;&lt;/b&gt; &lt;/kbd&gt;&lt;/pre&gt;  &lt;p&gt;Don't worry for now about the curved brackets after the function's name. Once you have defined your function you can use it within a program:&lt;/p&gt;  &lt;pre&gt;&lt;kbd&gt;&lt;br /&gt;&lt;b&gt;main()&lt;br /&gt;{&lt;br /&gt; demo();&lt;br /&gt;}&lt;br /&gt;&lt;/b&gt; &lt;/kbd&gt;&lt;/pre&gt;  In this program the instruction &lt;b&gt;&lt;tt&gt;demo ()&lt;/tt&gt;&lt;/b&gt;; is entirely equivalent to writing out all of the statements in the function. What we have done is to create an new C function and this, of course, is the power of functions. When you are first introduced to the idea of functions, or their equivalent in other languages, it is easy to fall into the trap of thinking that they are only useful when you want to use a block of code more than once.   &lt;p&gt;Functions are useful here but they have a more important purpose. If you are creating a long program then functions allow you to split it into "bite-sized" chunks which you can work on in isolation. As every C programmer knows, "&lt;i&gt;functions are the building blocks of programs&lt;/i&gt;."&lt;/p&gt;  &lt;hr /&gt;  &lt;a name="FL"&gt;&lt;h3&gt;Functions and Local Variables&lt;/h3&gt;&lt;/a&gt;  Now that the philosophy session is over we have to return to the details - because as it stands the &lt;b&gt;&lt;tt&gt;demo&lt;/tt&gt;&lt;/b&gt; function will not work. The problem is that the variable &lt;b&gt;&lt;tt&gt;total&lt;/tt&gt;&lt;/b&gt; isn't declared anywhere. A function is a complete program sub-unit in its own right and you can declare variables within it just as you can within the &lt;b&gt;&lt;tt&gt;main&lt;/tt&gt;&lt;/b&gt; program. If you look at the &lt;b&gt;&lt;tt&gt;main&lt;/tt&gt;&lt;/b&gt; program we have been using you will notice it is in fact a function that just happens to be called "main"! So to make &lt;b&gt;&lt;tt&gt;demo&lt;/tt&gt;&lt;/b&gt; work we have to add the declaration of the variable total:   &lt;pre&gt;&lt;kbd&gt;&lt;br /&gt;&lt;b&gt;demo()&lt;br /&gt;{&lt;br /&gt; int total;&lt;br /&gt; printf("Hello");&lt;br /&gt; total=total+1;&lt;br /&gt;}&lt;br /&gt;&lt;/b&gt; &lt;/kbd&gt;&lt;/pre&gt;  &lt;p&gt;Now this raises the question of where exactly &lt;b&gt;&lt;tt&gt;total&lt;/tt&gt;&lt;/b&gt; is a valid variable. You can certainly use &lt;b&gt;&lt;tt&gt;total&lt;/tt&gt;&lt;/b&gt; within the function that declares it - this much seems reasonable - but what about other functions and, in particular, what about the &lt;b&gt;&lt;tt&gt;main&lt;/tt&gt;&lt;/b&gt; program? The simple answer is that &lt;tt&gt;&lt;b&gt;total&lt;/b&gt;&lt;/tt&gt; is a variable that belongs to the &lt;b&gt;&lt;tt&gt;demo&lt;/tt&gt;&lt;/b&gt; function. It cannot be used in other functions, it doesn't even exist in other functions and it certainly has nothing to do with any variable of the same name that you declare within other functions.&lt;/p&gt;  &lt;p&gt;This is what we hinted at when we said that functions were &lt;i&gt;isolated chunks of code&lt;/i&gt;. Their isolation is such that variables declared within the function can only be used within that function. These variables are known as &lt;i&gt;&lt;b&gt;local variables&lt;/b&gt;&lt;/i&gt; and as their name suggests are local to the function they have been declared in. If you are used to a language where every variable is usable all the time this might seem silly and restrictive - but it isn't. It's what makes it possible to break a large program down into smaller and more manageable chunks.&lt;/p&gt;  &lt;p&gt;The fact that &lt;b&gt;&lt;tt&gt;total&lt;/tt&gt;&lt;/b&gt; is only usable within the &lt;b&gt;&lt;tt&gt;demo&lt;/tt&gt;&lt;/b&gt; function is one thing - but notice we said that it only existed within this function, which is a more subtle point. The variables that a function declares are created when the function is started and destroyed when the function is finished. So if the intention is to use &lt;b&gt;&lt;tt&gt;total&lt;/tt&gt;&lt;/b&gt; to count the number of times the &lt;b tt=""&gt;&gt;demo&lt;/b&gt; function is used - forget it! Each time &lt;tt&gt;&lt;b&gt;demo&lt;/b&gt;&lt;/tt&gt; is used the variable &lt;b&gt;&lt;tt&gt;total&lt;/tt&gt;&lt;/b&gt; is created afresh, and at the end of the function the variable goes up in a puff of smoke along with its value. So no matter how many times you run &lt;b&gt;&lt;tt&gt;demo total&lt;/tt&gt;&lt;/b&gt; will only ever reach a value of 1, assuming that it's initialised to 0.&lt;/p&gt;  &lt;hr /&gt;  &lt;a name="MC"&gt;&lt;h3&gt;Making The Connections&lt;/h3&gt;&lt;/a&gt;  Functions are isolated, and whats more nothing survives after they have finished. Put like this a function doesn't seem to be that useful because you can't get data values in, you can't get data values out, and they don't remember anything that happens to them!   &lt;p&gt;To be useful there has to be a way of getting data into and out of a function, and this is the role of the curved brackets. You can define special variables called &lt;i&gt;parameters&lt;/i&gt; which are used to carry data values into a function. Parameters are listed and declared in between the () brackets in the function's definition. For example:&lt;/p&gt;  &lt;pre&gt;&lt;kbd&gt;&lt;br /&gt;&lt;b&gt;sum( int a, int b)&lt;br /&gt;{&lt;br /&gt; int result;&lt;br /&gt; result=a + b;&lt;br /&gt;}&lt;br /&gt;&lt;/b&gt; &lt;/kbd&gt;&lt;/pre&gt;  defines a function called &lt;b&gt;&lt;tt&gt;sum&lt;/tt&gt;&lt;/b&gt; with two parameters &lt;tt&gt;&lt;b&gt;a&lt;/b&gt;&lt;/tt&gt; and &lt;tt&gt;&lt;b&gt;b&lt;/b&gt;&lt;/tt&gt;, both &lt;b&gt;integers&lt;/b&gt;. Notice that the result variable is declared in the usual way within the body of the function. Also, notice that the parameters &lt;tt&gt;&lt;b&gt;a&lt;/b&gt;&lt;/tt&gt; and &lt;tt&gt;&lt;b&gt;b&lt;/b&gt;&lt;/tt&gt; are used within the function in the same way as normal variables - which indeed they are. What is more, they are still &lt;i&gt;&lt;b&gt;local variables&lt;/b&gt;&lt;/i&gt; and have nothing at all to do with any variables called &lt;tt&gt;&lt;b&gt;a&lt;/b&gt;&lt;/tt&gt; and &lt;tt&gt;&lt;b&gt;b&lt;/b&gt;&lt;/tt&gt; defined in any other function.   &lt;p&gt;The only way in which parameters are any different is that you can give them initial values when the function starts by writing the values between the round brackets. So&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;sum(l,2);&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;is a call to the &lt;tt&gt;&lt;b&gt;sum&lt;/b&gt;&lt;/tt&gt; function with &lt;tt&gt;&lt;b&gt;a&lt;/b&gt;&lt;/tt&gt; set to 1 and &lt;tt&gt;&lt;b&gt;b&lt;/b&gt;&lt;/tt&gt; set to 2 and so &lt;b&gt;&lt;tt&gt;result&lt;/tt&gt;&lt;/b&gt; is set to 3. You can also initialise parameters to the result of expressions such as:&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;sum(x+2,z*10);&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;which will set &lt;tt&gt;&lt;b&gt;a&lt;/b&gt;&lt;/tt&gt; equal to whatever &lt;tt&gt;&lt;b&gt;x+2&lt;/b&gt;&lt;/tt&gt; works out to be and &lt;tt&gt;&lt;b&gt;b&lt;/b&gt;&lt;/tt&gt; equal to whatever &lt;b&gt;&lt;tt&gt;z*10&lt;/tt&gt;&lt;/b&gt; works out to be.&lt;/p&gt;  &lt;p&gt;As a simpler case you can also set a parameter to the value in a single variable - for example:&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;sum(x,y);&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;will set &lt;tt&gt;&lt;b&gt;a&lt;/b&gt;&lt;/tt&gt; to the value stored in &lt;tt&gt;&lt;b&gt;x&lt;/b&gt;&lt;/tt&gt; and &lt;tt&gt;&lt;b&gt;b&lt;/b&gt;&lt;/tt&gt; to the value stored in &lt;tt&gt;&lt;b&gt;y&lt;/b&gt;&lt;/tt&gt;.&lt;/p&gt;  &lt;p&gt;Parameters are the main way of getting values into a function, but how do we get values out? There is no point in expecting the &lt;b tt=""&gt;&gt;result&lt;/b&gt; variable to somehow magically get its value out of the &lt;b&gt;&lt;tt&gt;sum&lt;/tt&gt;&lt;/b&gt; function - after all, it is a &lt;i&gt;local variable&lt;/i&gt; and is destroyed when &lt;b&gt;&lt;tt&gt;sum&lt;/tt&gt;&lt;/b&gt; is finished. You might try something like:&lt;/p&gt;   &lt;pre&gt;&lt;kbd&gt;&lt;br /&gt;&lt;b&gt;sum(int a, int b, int result)&lt;br /&gt;{&lt;br /&gt; int result;&lt;br /&gt; result = a + b;&lt;br /&gt;}&lt;br /&gt;&lt;/b&gt; &lt;/kbd&gt;&lt;/pre&gt;  &lt;p&gt;but it doesn't work. Parameters are just ordinary variables that are set to an initial value when the function starts running - they don't pass values back to the program that used the function. That is:&lt;/p&gt;   &lt;p&gt;&lt;b&gt;&lt;tt&gt;sum(l,2,r);&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;doesn't store 1+2 in &lt;tt&gt;&lt;b&gt;r&lt;/b&gt;&lt;/tt&gt; because the value in &lt;tt&gt;&lt;b&gt;r&lt;/b&gt;&lt;/tt&gt; is used to initialise the value in &lt;b&gt;&lt;tt&gt;result&lt;/tt&gt;&lt;/b&gt; and not vice versa. You can even try&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;sum(l,2,result);&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;and it still will not work - the variable &lt;b&gt;&lt;tt&gt;result&lt;/tt&gt;&lt;/b&gt; within the function has nothing to do with the variable &lt;b&gt;&lt;tt&gt;result&lt;/tt&gt;&lt;/b&gt; used in any other program.&lt;/p&gt;  &lt;p&gt;The simplest way to get a value out of a function is to use the &lt;b&gt;&lt;tt&gt;return&lt;/tt&gt;&lt;/b&gt; instruction. A function can return a value via its name - it's as if the name was a variable and had a value. The value that is returned is specified by the instruction:&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;return &lt;i&gt;value&lt;/i&gt;;&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;which can occur anywhere within the function, not just as the last instruction - however, a &lt;b&gt;&lt;tt&gt;return&lt;/tt&gt;&lt;/b&gt; always terminates the function and returns control back to the calling function. The only complication is that as the function's name is used to return the value it has to be given a &lt;i&gt;data type&lt;/i&gt;. This is achieved by writing the data type in front of the function's name. For example:&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;int sum(a,b);&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;So now we can at last write the correct version of the &lt;tt&gt;&lt;b&gt;sum&lt;/b&gt;&lt;/tt&gt; function:&lt;/p&gt;  &lt;pre&gt;&lt;kbd&gt;&lt;br /&gt;&lt;b&gt;int sum(int a, int b)&lt;br /&gt; {&lt;br /&gt;  int result;&lt;br /&gt;  result = a + b;&lt;br /&gt;  return result;&lt;br /&gt; }&lt;br /&gt;&lt;/b&gt; &lt;/kbd&gt;&lt;/pre&gt;  &lt;p&gt;and to use it you would write something like:&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;r=sum(1,2);&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;which would add 1 to 2 and store the result in &lt;tt&gt;&lt;b&gt;r&lt;/b&gt;&lt;/tt&gt;. You can use a function anywhere that you can use a variable. For example,&lt;/p&gt;  &lt;p&gt;&lt;tt&gt;&lt;b&gt;r=sum(1,2)*3&lt;/b&gt;&lt;/tt&gt;&lt;/p&gt;  &lt;p&gt;is perfectly OK, as is&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;r=3+sum(1,2)/n-10&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Obviously, the situation with respect to the number of &lt;i&gt;inputs&lt;/i&gt; and &lt;i&gt;outputs&lt;/i&gt; of a function isn't equal. That is you can create as many parameters as you like but a function can &lt;b&gt;&lt;tt&gt;return&lt;/tt&gt;&lt;/b&gt; only a single value. (Later on we will have to find ways of allowing functions to return more than one value.)&lt;/p&gt;  &lt;p&gt;So to summarise: a function has the general form:&lt;/p&gt;  &lt;pre&gt;&lt;kbd&gt;&lt;br /&gt;&lt;b&gt;type FunctionName(type declared parameter list)&lt;br /&gt; {&lt;br /&gt;  statements that make up the function&lt;br /&gt; }&lt;br /&gt;&lt;/b&gt; &lt;/kbd&gt;&lt;/pre&gt;  and of course a function can contain any number of &lt;b&gt;&lt;tt&gt;return&lt;/tt&gt;&lt;/b&gt; statements to specify its return value and bring the function to an end.   &lt;p&gt;There are some special cases and defaults we need to look at before moving on. You don't have to specify a parameter list if you don't want to use any parameters - but you still need the empty brackets! You don't have to assign the function a type in which case it defaults to &lt;b&gt;&lt;tt&gt;int&lt;/tt&gt;&lt;/b&gt;. A function doesn't have to return a value and the program that makes use of a function doesn't have to save any value it does return. For example, it is perfectly OK to use:&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;sum(1,2);&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;which simply throws away the result of adding 1 to 2. As this sort of thing offends some programmers you can use the data type &lt;b&gt;&lt;tt&gt;void&lt;/tt&gt;&lt;/b&gt; to indicate that a function doesn't return a value. For example:&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;void demo();&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;is a function with no parameters and no return value.&lt;/p&gt;  &lt;p&gt;&lt;tt&gt;&lt;b&gt;void&lt;/b&gt;&lt;/tt&gt; is an &lt;b&gt;ANSI C&lt;/b&gt; standard data type.&lt;/p&gt;  &lt;p&gt;The &lt;b&gt;&lt;tt&gt;break&lt;/tt&gt;&lt;/b&gt; statement covered in a previous section can be used to exit a function. The &lt;b&gt;&lt;tt&gt;break&lt;/tt&gt;&lt;/b&gt; statement is usually linked with an &lt;b&gt;&lt;tt&gt;if&lt;/tt&gt;&lt;/b&gt; statement checking for a particular value. For example:&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;if (x==1) break;&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;If &lt;tt&gt;&lt;b&gt;x&lt;/b&gt;&lt;/tt&gt; contained &lt;b&gt;&lt;tt&gt;1&lt;/tt&gt;&lt;/b&gt; then the fuction would exit and return to the calling program.&lt;/p&gt;  &lt;hr /&gt;  &lt;a name="FP"&gt;&lt;h3&gt;Functions and Prototypes&lt;/h3&gt;&lt;/a&gt;  Where should a function's definition go in relation to the entire program - before or after &lt;tt&gt;&lt;b&gt;main()&lt;/b&gt;&lt;/tt&gt;? The only requirement is that the function's &lt;b&gt;type&lt;/b&gt; has to be known before it is actually used. One way is to place the function definition earlier in the program than it is used - for example, before &lt;b&gt;&lt;tt&gt;main()&lt;/tt&gt;&lt;/b&gt;. The only problem is that most C  programmers would rather put the main program at the top of the program listing. The solution is to declare the function separately at the start of the program. For example:    &lt;pre&gt;&lt;kbd&gt;&lt;br /&gt;&lt;b&gt;int sum();&lt;br /&gt;main()&lt;br /&gt; {&lt;br /&gt;  etc...&lt;/b&gt; &lt;/kbd&gt;&lt;/pre&gt;  declares the name &lt;b&gt;&lt;tt&gt;sum&lt;/tt&gt;&lt;/b&gt; to be a function that returns an &lt;b&gt;integer&lt;/b&gt;. As long as you declare functions before they are used you can put the actual definition anywhere you like.  &lt;p&gt;By default if you don't declare a function before you use it then it is assumed to be an &lt;b&gt;&lt;tt&gt;int&lt;/tt&gt;&lt;/b&gt; function - which is usually, but not always, correct. It is worth getting into the habit of putting function declarations at the start of your programs because this makes them easier to convert to full &lt;b&gt;ANSI C&lt;/b&gt;.&lt;/p&gt;  &lt;hr /&gt;  &lt;a name="ANSI"&gt;&lt;h3&gt;What is ANSI C?&lt;/h3&gt;&lt;/a&gt;  When C was first written the standard was set by its authors Kernighan and Ritche - hence "K&amp;amp;R C". In 1990, an international &lt;b&gt;&lt;a href="http://www.le.ac.uk/CWIS/AS/CC/GL/ccgla.html#24"&gt;ANSI&lt;/a&gt;&lt;/b&gt; standard for C was established which differs from K&amp;amp;AMPR C in a number of ways.   &lt;p&gt;The only really important difference is the use of function prototypes. To allow the compiler to check that you are using functions correctly &lt;b&gt;ANSI C&lt;/b&gt; allows you to include a &lt;b&gt;function prototype&lt;/b&gt; which gives the type of the function and the type of each parameter before you define the function. For example, a prototype for the &lt;b&gt;&lt;tt&gt;sum&lt;/tt&gt;&lt;/b&gt; function would be:&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;int sum(int,int);&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;meaning &lt;b&gt;&lt;tt&gt;sum&lt;/tt&gt;&lt;/b&gt; is an &lt;b&gt;&lt;tt&gt;int&lt;/tt&gt;&lt;/b&gt; function which takes two &lt;b&gt;&lt;tt&gt;int&lt;/tt&gt;&lt;/b&gt; parameters. Obviously, if you are in the habit of declaring functions then this is a small modification. The only other major change is that you can declare parameter types along with the function as in:&lt;/p&gt;  &lt;pre&gt;&lt;kbd&gt;&lt;br /&gt;&lt;b&gt;int sum(int a, int b);&lt;br /&gt;{&lt;/b&gt; &lt;/kbd&gt;&lt;/pre&gt;  rather than:   &lt;pre&gt;&lt;kbd&gt;&lt;br /&gt;&lt;b&gt;int sum(a,b)&lt;br /&gt;int a,b;&lt;br /&gt;{&lt;/b&gt; &lt;/kbd&gt;&lt;/pre&gt;  &lt;p&gt;was used in the original K&amp;amp;R C. Again, you can see that this is just a small change. Notice that even if you are using an &lt;b&gt;ANSI&lt;/b&gt; compiler you don't have to use prototypes and the K&amp;amp;R version of the code will work perfectly well.&lt;/p&gt;  &lt;hr /&gt;  &lt;a name="SL"&gt;&lt;h3&gt;The Standard Library Functions&lt;/h3&gt;&lt;/a&gt;  Some of the "commands" in C are not really "commands" at all but are functions. For example, we have been using &lt;b&gt;&lt;tt&gt;printf&lt;/tt&gt;&lt;/b&gt; and &lt;b&gt;&lt;tt&gt;scanf&lt;/tt&gt;&lt;/b&gt; to do input and output, and we have used &lt;b&gt;&lt;tt&gt;rand&lt;/tt&gt;&lt;/b&gt; to generate random numbers - all three are functions.   &lt;p&gt;There are a great many standard functions that are included with C compilers and while these are not really part of the language, in the sense that you can re-write them if you really want to, most C programmers think of them as fixtures and fittings. Later in the course we will look into the mysteries of how C gains access to these standard functions and how we can extend the range of the standard library. But for now a list of the most common libraries and a brief description of the most useful functions they contain follows:&lt;/p&gt;  &lt;ul&gt;&lt;li&gt;&lt;b&gt;stdio.h: I/O functions:&lt;/b&gt;   &lt;ul&gt;&lt;li&gt;&lt;b&gt;getchar()&lt;/b&gt; returns the next character typed on the keyboard.&lt;/li&gt;&lt;li&gt;&lt;b&gt;putchar()&lt;/b&gt; outputs a single character to the screen.&lt;/li&gt;&lt;li&gt;&lt;b&gt;printf()&lt;/b&gt; as previously described&lt;/li&gt;&lt;li&gt;&lt;b&gt;scanf()&lt;/b&gt; as previously described&lt;/li&gt;&lt;/ul&gt; &lt;/li&gt;&lt;li&gt;&lt;b&gt;string.h: String functions&lt;/b&gt;   &lt;ul&gt;&lt;li&gt;&lt;b&gt;strcat()&lt;/b&gt; concatenates a copy of str2 to str1&lt;/li&gt;&lt;li&gt;&lt;b&gt;strcmp()&lt;/b&gt; compares two strings&lt;/li&gt;&lt;li&gt;&lt;b&gt;strcpy()&lt;/b&gt; copys contents of str2 to str1&lt;/li&gt;&lt;/ul&gt; &lt;/li&gt;&lt;li&gt;&lt;b&gt;ctype.h: Character functions&lt;/b&gt;   &lt;ul&gt;&lt;li&gt;&lt;b&gt;isdigit()&lt;/b&gt; returns non-0 if arg is digit 0 to 9&lt;/li&gt;&lt;li&gt;&lt;b&gt;isalpha()&lt;/b&gt; returns non-0 if arg is a letter of the alphabet&lt;/li&gt;&lt;li&gt;&lt;b&gt;isalnum()&lt;/b&gt; returns non-0 if arg is a letter or digit&lt;/li&gt;&lt;li&gt;&lt;b&gt;islower()&lt;/b&gt; returns non-0 if arg is lowercase letter&lt;/li&gt;&lt;li&gt;&lt;b&gt;isupper()&lt;/b&gt; returns non-0 if arg is uppercase letter&lt;/li&gt;&lt;/ul&gt; &lt;/li&gt;&lt;li&gt;&lt;b&gt;math.h: Mathematics functions&lt;/b&gt;   &lt;ul&gt;&lt;li&gt;&lt;b&gt;acos()&lt;/b&gt; returns arc cosine of arg&lt;/li&gt;&lt;li&gt;&lt;b&gt;asin()&lt;/b&gt; returns arc sine of arg&lt;/li&gt;&lt;li&gt;&lt;b&gt;atan()&lt;/b&gt; returns arc tangent of arg&lt;/li&gt;&lt;li&gt;&lt;b&gt;cos()&lt;/b&gt; returns cosine of arg&lt;/li&gt;&lt;li&gt;&lt;b&gt;exp()&lt;/b&gt; returns natural logarithim e&lt;/li&gt;&lt;li&gt;&lt;b&gt;fabs()&lt;/b&gt; returns absolute value of num&lt;/li&gt;&lt;li&gt;&lt;b&gt;sqrt()&lt;/b&gt; returns square root of num&lt;/li&gt;&lt;/ul&gt; &lt;/li&gt;&lt;li&gt;&lt;b&gt;time.h: Time and Date functions&lt;/b&gt;   &lt;ul&gt;&lt;li&gt;&lt;b&gt;time()&lt;/b&gt; returns current calender time of system&lt;/li&gt;&lt;li&gt;&lt;b&gt;difftime()&lt;/b&gt; returns difference in secs between two times&lt;/li&gt;&lt;li&gt;&lt;b&gt;clock()&lt;/b&gt; returns number of system clock cycles since program execution&lt;/li&gt;&lt;/ul&gt; &lt;/li&gt;&lt;li&gt;&lt;b&gt;stdlib.h:Miscellaneous functions&lt;/b&gt;   &lt;ul&gt;&lt;li&gt;&lt;b&gt;malloc()&lt;/b&gt; provides dynamic memory allocation, covered in future sections&lt;/li&gt;&lt;li&gt;&lt;b&gt;rand()&lt;/b&gt; as already described previously&lt;/li&gt;&lt;li&gt;&lt;b&gt;srand()&lt;/b&gt; used to set the starting point for rand()&lt;/li&gt;&lt;/ul&gt; &lt;/li&gt;&lt;/ul&gt;  &lt;hr /&gt;   &lt;h3&gt;Throwing The Dice&lt;/h3&gt;  As an example of how to use functions, we conclude this section with a program that, while it isn't state of the art, does show that there are things you can already do with C. It also has to be said that some parts of the program can be written more neatly with just a little more C - but that's for later. All the program does is to generate a random number in the range 1 to 6 and displays a dice face with the appropriate pattern.   &lt;p&gt;The main program isn't difficult to write because we are going to adopt the traditional programmer's trick of assuming that any function needed already exists. This approach is called &lt;i&gt;stepwise refinement&lt;/i&gt;, and although its value as a programming method isn't clear cut, it still isn't a bad way of organising things:&lt;/p&gt;  &lt;pre&gt;&lt;kbd&gt;&lt;br /&gt;&lt;b&gt;main()&lt;br /&gt;{&lt;br /&gt;  int r;&lt;br /&gt;  char ans;&lt;br /&gt;&lt;br /&gt;  ans = getans();&lt;br /&gt;&lt;br /&gt;  while(ans== 'y')&lt;br /&gt;   {&lt;br /&gt;     r = randn(6);&lt;br /&gt;     blines(25);&lt;br /&gt;     if (r==1) showone();&lt;br /&gt;     if (r==2) showtwo();&lt;br /&gt;     if (r==3) showthree();&lt;br /&gt;     if (r==4) showfour();&lt;br /&gt;     if (r==5) showfive();&lt;br /&gt;     if (r==6) showsix();&lt;br /&gt;     blines(21);&lt;br /&gt;     ans = getans();&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;  blines(2);&lt;br /&gt;}&lt;br /&gt;&lt;/b&gt; &lt;/kbd&gt;&lt;/pre&gt;  &lt;p&gt;If you look at &lt;tt&gt;&lt;b&gt;main()&lt;/b&gt;&lt;/tt&gt; you might be a bit mystified at first. It is clear that the list of &lt;b&gt;&lt;tt&gt;if&lt;/tt&gt;&lt;/b&gt; statements pick out one of the functions &lt;b&gt;&lt;tt&gt;showone&lt;/tt&gt;&lt;/b&gt;, &lt;b&gt;&lt;tt&gt;showtwo&lt;/tt&gt;&lt;/b&gt; etc. and so these must do the actual printing of the dot patterns - but what is &lt;b&gt;&lt;tt&gt;blines&lt;/tt&gt;&lt;/b&gt;, what is &lt;b&gt;&lt;tt&gt;getans&lt;/tt&gt;&lt;/b&gt; and why are we using &lt;tt&gt;&lt;b&gt;randn()&lt;/b&gt;&lt;/tt&gt;? The last time we used a random number generator it was called &lt;b&gt;&lt;tt&gt;rand()&lt;/tt&gt;&lt;/b&gt;!&lt;/p&gt;  &lt;p&gt;The simple answers are that &lt;tt&gt;&lt;b&gt;blines(n)&lt;/b&gt;&lt;/tt&gt; will print &lt;tt&gt;&lt;b&gt;n&lt;/b&gt;&lt;/tt&gt; blank lines, &lt;tt&gt;&lt;b&gt;getans()&lt;/b&gt;&lt;/tt&gt; asks the user a question and waits for the single letter answer, and &lt;tt&gt;&lt;b&gt;randn(n)&lt;/b&gt;&lt;/tt&gt; is a new random number generator function that produces a random integer in the range 1 to &lt;tt&gt;&lt;b&gt;n&lt;/b&gt;&lt;/tt&gt; - but to know this you would have written the main program. We decided what functions would make our task easier and named them. The next step is to write the code to fill in the details of each of the functions. There is nothing to stop me assuming that other functions that would make my job easier already exist. This is the main principle of &lt;i&gt;stepwise refinement&lt;/i&gt; - never write any code if you can possibly invent another function! Let's start with &lt;tt&gt;&lt;b&gt;randn()&lt;/b&gt;&lt;/tt&gt;.&lt;/p&gt;  &lt;p&gt;This is obviously an &lt;b&gt;&lt;tt&gt;int&lt;/tt&gt;&lt;/b&gt; function and it can make use of the existing &lt;tt&gt;&lt;b&gt;rand()&lt;/b&gt;&lt;/tt&gt; function in the standard library&lt;/p&gt;  &lt;pre&gt;&lt;kbd&gt;&lt;br /&gt;&lt;b&gt;int randn(int n)&lt;br /&gt;{&lt;br /&gt;  return rand()%n + 1;&lt;br /&gt;}&lt;br /&gt;&lt;/b&gt; &lt;/kbd&gt;&lt;/pre&gt;  &lt;p&gt;The single line of the body of the function just returns the remainder of the random number after dividing by &lt;tt&gt;&lt;b&gt;n&lt;/b&gt;&lt;/tt&gt; - &lt;tt&gt;&lt;b&gt;%&lt;/b&gt;&lt;/tt&gt; is the remainder operator - plus 1. An alternative would be to use a temporary variable to store the result and then return this value. You can also use functions within the body of other functions.&lt;/p&gt;  &lt;p&gt;Next &lt;b&gt;&lt;tt&gt;getans()&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;pre&gt;&lt;kbd&gt;&lt;br /&gt;&lt;b&gt;char getans()&lt;br /&gt;{&lt;br /&gt;  int ans;&lt;br /&gt;&lt;br /&gt;  printf("Throw y/n ?");&lt;br /&gt;  ans = -1;&lt;br /&gt;  while (ans == -1)&lt;br /&gt;   {&lt;br /&gt;     ans=getchar();&lt;br /&gt;   }&lt;br /&gt;  return ans;&lt;br /&gt;}&lt;br /&gt;&lt;/b&gt; &lt;/kbd&gt;&lt;/pre&gt;  &lt;p&gt;This uses the standard &lt;b&gt;&lt;tt&gt;int&lt;/tt&gt;&lt;/b&gt; function &lt;b&gt;&lt;tt&gt;getchar()&lt;/tt&gt;&lt;/b&gt; which reads the next character from the keyboard and returns its ASCII code or -1 if there isn't a key pressed. This function tends to vary in its behaviour according to the implementation you are using. Often it needs a carriage return pressed before it will return anything - so if you are using a different compiler and the program just hangs, try pressing "y" followed the by &lt;b&gt;Enter&lt;/b&gt; or &lt;b&gt;Return&lt;/b&gt; key.&lt;/p&gt;  &lt;p&gt;The &lt;tt&gt;&lt;b&gt;blines(n)&lt;/b&gt;&lt;/tt&gt; function simply has to use a &lt;b&gt;&lt;tt&gt;for&lt;/tt&gt;&lt;/b&gt; loop to print the specified number of lines:&lt;/p&gt;  &lt;pre&gt;&lt;kbd&gt;&lt;br /&gt;&lt;b&gt;void blines(int n)&lt;br /&gt; {&lt;br /&gt;   int i;&lt;br /&gt;&lt;br /&gt;   for(i=1 ; i&lt;=n ; i++) printf("\n");&lt;br /&gt; }&lt;br /&gt;&lt;/b&gt; &lt;/kbd&gt;&lt;/pre&gt;  &lt;p&gt;Last but not least are the functions to print the dot patterns. These are just boring uses of &lt;b&gt;&lt;tt&gt;printf&lt;/tt&gt;&lt;/b&gt; to show different patterns. Each function prints exactly three lines of dots and uses blank lines if necessary. The reason for this is that printing 25 blank lines should clear a standard text screen and after printing three lines printing 21 blank lines will scroll the pattern to the top of the screen. If this doesn't happen on your machine make sure you are using a 29 line text mode display.&lt;/p&gt;  &lt;pre&gt;&lt;kbd&gt;&lt;br /&gt;&lt;b&gt;void showone()&lt;br /&gt; {&lt;br /&gt;   printf("\n * \n");&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;void showtwo()&lt;br /&gt; {&lt;br /&gt;   printf(" * \n\n");&lt;br /&gt;   printf(" * \n");&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;void showthree()&lt;br /&gt; {&lt;br /&gt;   printf(" *  \n");&lt;br /&gt;   printf("  * \n");&lt;br /&gt;   printf("   *\n");&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;void showfour()&lt;br /&gt; {&lt;br /&gt;   printf(" * * \n\n");&lt;br /&gt;   printf(" * * \n");&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;void showfive()&lt;br /&gt; {&lt;br /&gt;   printf(" * * \n");&lt;br /&gt;   printf("  *  \n");&lt;br /&gt;   printf(" * * \n");&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;void showsix()&lt;br /&gt; {&lt;br /&gt;   int i;&lt;br /&gt;&lt;br /&gt;   for(i=1 ; i&gt;=3 ; i++) printf(" * * \n");&lt;br /&gt; }&lt;br /&gt;&lt;/b&gt; &lt;/kbd&gt;&lt;/pre&gt;  &lt;p&gt;The only excitement in all of this is the use of a &lt;b&gt;&lt;tt&gt;for&lt;/tt&gt;&lt;/b&gt; loop in &lt;b&gt;&lt;tt&gt;showsix&lt;/tt&gt;&lt;/b&gt;! Type this all in and add:&lt;/p&gt;  &lt;pre&gt;&lt;kbd&gt;&lt;br /&gt;&lt;b&gt;void showone();&lt;br /&gt;void showtwo();&lt;br /&gt;void showthree();&lt;br /&gt;void showfour();&lt;br /&gt;void showfive();&lt;br /&gt;void showsix();&lt;br /&gt;int randn();&lt;br /&gt;char getans();&lt;br /&gt;void blines();&lt;/b&gt; &lt;/kbd&gt;&lt;/pre&gt;  &lt;p&gt;before the &lt;b&gt;&lt;tt&gt;main&lt;/tt&gt;&lt;/b&gt; function if you type the other functions in after.&lt;/p&gt;  &lt;p&gt;[&lt;a href="http://www.le.ac.uk/cc/tutorials/c/programs/dice.c"&gt;program&lt;/a&gt;]&lt;/p&gt;  &lt;p&gt;Once you have the program working try modifying it. For example, see if you can improve the look of the patterns. You might also see if you can reduce the number of &lt;b&gt;&lt;tt&gt;show&lt;i&gt;x&lt;/i&gt;&lt;/tt&gt;&lt;/b&gt; functions in use - the key is that the patterns are built up of combinations of two horizontal dots and one centred dot. Best of luck.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5670527689344996196-4775240390379398770?l=rao-programmingofc.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://rao-programmingofc.blogspot.com/feeds/4775240390379398770/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5670527689344996196&amp;postID=4775240390379398770' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5670527689344996196/posts/default/4775240390379398770'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5670527689344996196/posts/default/4775240390379398770'/><link rel='alternate' type='text/html' href='http://rao-programmingofc.blogspot.com/2008/02/functions-and-prototypes.html' title='Functions and Prototypes'/><author><name>Ramjaan</name><uri>http://www.blogger.com/profile/07862712225011522143</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='http://1.bp.blogspot.com/_uyQpXxI_GeY/SQy7Z8dn6II/AAAAAAAAACI/94M3auWVt4A/S220/images.jpeg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5670527689344996196.post-7421298412568662665</id><published>2008-02-08T03:45:00.000-08:00</published><updated>2008-02-08T03:46:24.108-08:00</updated><title type='text'>Structure and Nesting</title><content type='html'>&lt;h1&gt;               Structure and Nesting&lt;/h1&gt;&lt;br /&gt;&lt;br /&gt;Objectives&lt;br /&gt;&lt;br /&gt;This section brings together the various looping mechanisms available to the C programmer with the program control constructs we met in the last section.&lt;br /&gt;&lt;br /&gt;We also demonstrates a neat trick with random numbers.&lt;br /&gt;&lt;br /&gt;It is one of the great discoveries of programming that you can write any program using just simple while loops and if statements. You don't need any other control statements at all. Of course it might be nice to include some other types of control statement to make life easy - for example, you don't need the for loop, but it is good to have! So as long as you understand the if and the while loop in one form or another you can write any program you want to.&lt;br /&gt;&lt;br /&gt;If you think that a loop and an if statement are not much to build programs then you are missing an important point. It's not just the statements you have, but the way you can put them together. You can include an if statement within a loop, loops within loops are also OK, as are loops in ifs, and ifs in ifs and so on. This putting one control statement inside another is called nesting and it is really what allows you to make a program as complicated as you like.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Think of a number&lt;br /&gt;Now let's have a go at writing the following program: 'It thinks of a number in the range 0 to 99 and then asks the user to guess it'. This sounds complicated, especially the 'thinks of a number' part, but all you need to know is that the statement:&lt;br /&gt;&lt;br /&gt;r = rand()&lt;br /&gt;&lt;br /&gt;will store a random number in the integer variable r. The standard library function rand() randomly picks a number within the range 0 to 32767, but this might vary from machine to machine. Look upon rand() as being a large dice.&lt;br /&gt;&lt;br /&gt;Our problem is to select a number between 0 and 99 and not between 0 and 32767. How can we get our random number to within our range? The rand() function will produce numbers such as:&lt;br /&gt;&lt;br /&gt;2567&lt;br /&gt;134&lt;br /&gt;20678&lt;br /&gt;15789&lt;br /&gt;32001&lt;br /&gt;15987&lt;br /&gt;etc...&lt;br /&gt;&lt;br /&gt;If you look at the last two digits of all of these numbers they would form our random set! To select just these numbers we can use an arithmetic calculation of the following form:&lt;br /&gt;&lt;br /&gt;r = rand() % 100&lt;br /&gt;&lt;br /&gt;That is, to get the number into the right range you simply take the remainder on dividing by 100, ie a value in the range 0 to 99. You should remember this neat programming trick, you'll be surprised how often it is required.&lt;br /&gt;&lt;br /&gt;Our solution to the problem is as follows:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#include &lt;stdio.h&gt;&lt;br /&gt;&lt;br /&gt;main()&lt;br /&gt; {&lt;br /&gt;   int target;&lt;br /&gt;   int guess;&lt;br /&gt;   int again;&lt;br /&gt;&lt;br /&gt;   printf("\n Do you want to guess a number 1 =Yes, 0=No ");&lt;br /&gt;   scanf("%d",&amp;amp;again);&lt;br /&gt;&lt;br /&gt;   while (again)&lt;br /&gt;    {&lt;br /&gt;      target = rand() % 100;&lt;br /&gt;      guess  = target + l;&lt;br /&gt;&lt;br /&gt;      while(target!=guess)&lt;br /&gt;       {&lt;br /&gt;         printf('\n What is your guess ? ");&lt;br /&gt;         scanf("%d",&amp;amp;guess);&lt;br /&gt;&lt;br /&gt;         if (target&gt;guess) printf("Too low");&lt;br /&gt;         else printf("Too high");&lt;br /&gt;       }&lt;br /&gt;&lt;br /&gt;      printf("\n Well done you got it! \n");&lt;br /&gt;      printf("\nDo you want to guess a number 1=Yes, 0=No");&lt;br /&gt;      scanf("%d".&amp;amp;again);&lt;br /&gt;    }&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;[program]&lt;br /&gt;&lt;br /&gt;This looks like a very long and complicated program, but it isn't. Essentially it used two loops and an if/else which in English could be summarised as:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;while(again) {&lt;br /&gt;  think of a number&lt;br /&gt;  while (user hasn't guessed it)&lt;br /&gt;   {&lt;br /&gt;     get users guess.&lt;br /&gt;     if (target &lt; guess) tell the user the guess is low&lt;br /&gt;     else                tell the user the guess is high&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;The integer variable again is used to indicate that the user wants to carry on playing. If it is 0 then the loop stops so 0 = No, and 1, or any other non-zero value, = Yes.&lt;br /&gt;&lt;br /&gt;If you try this program out you will discover that it has a slight flaw - not so much a bug, more a feature. If the user guesses the correct value the program still tells the user that the guess is too high and then congratulates them that they have the correct value. Such problems with how loops end are common and you have to pay attention to details such as this. There are a number of possible solutions, but the most straight forward is to change the inner loop so that the first guess is asked for before the loop begins. This shifts the test for the loop to stop to before the test for a high or low guess:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#include &lt;stdio.h&gt;&lt;br /&gt;&lt;br /&gt;main()&lt;br /&gt; {&lt;br /&gt;   int target;&lt;br /&gt;   int guess;&lt;br /&gt;   int again;&lt;br /&gt;&lt;br /&gt;   printf("\n Do you want to guess a number 1 =Yes, 0=No ");&lt;br /&gt;   scanf("%d",&amp;amp;again);&lt;br /&gt;&lt;br /&gt;   while (again)&lt;br /&gt;    {&lt;br /&gt;      target = rand() % 100;&lt;br /&gt;&lt;br /&gt;      printf('\n What is your guess ? ");&lt;br /&gt;      scanf("%d",&amp;amp;guess);&lt;br /&gt;&lt;br /&gt;      while(target!=guess)&lt;br /&gt;       {&lt;br /&gt;         if (target&gt;guess) printf("Too low");&lt;br /&gt;         else printf("Too high");&lt;br /&gt;         printf('\n What is your guess ? ");&lt;br /&gt;         scanf("%d",&amp;amp;guess);&lt;br /&gt;       }&lt;br /&gt;&lt;br /&gt;      printf("\n Well done you got it! \n");&lt;br /&gt;      printf("\n Do you want to guess a number 1=Yes, 0=No");&lt;br /&gt;      scanf("%d".&amp;amp;again);&lt;br /&gt;    }&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;[program]&lt;br /&gt;&lt;br /&gt;If you want to be sure that you understand what is going on here, ask yourself why the line:&lt;br /&gt;&lt;br /&gt;guess = target + 1;&lt;br /&gt;&lt;br /&gt;was necessary in the first version of the program and not in the second?&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5670527689344996196-7421298412568662665?l=rao-programmingofc.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://rao-programmingofc.blogspot.com/feeds/7421298412568662665/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5670527689344996196&amp;postID=7421298412568662665' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5670527689344996196/posts/default/7421298412568662665'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5670527689344996196/posts/default/7421298412568662665'/><link rel='alternate' type='text/html' href='http://rao-programmingofc.blogspot.com/2008/02/structure-and-nesting.html' title='Structure and Nesting'/><author><name>Ramjaan</name><uri>http://www.blogger.com/profile/07862712225011522143</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='http://1.bp.blogspot.com/_uyQpXxI_GeY/SQy7Z8dn6II/AAAAAAAAACI/94M3auWVt4A/S220/images.jpeg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5670527689344996196.post-4402457340899047694</id><published>2008-02-08T03:08:00.000-08:00</published><updated>2008-02-08T03:13:55.920-08:00</updated><title type='text'>Conditional Execution</title><content type='html'>&lt;h1&gt;        Conditional Execution&lt;/h1&gt;&lt;br /&gt;Objectives&lt;br /&gt;&lt;br /&gt;Having read this section you should be able to:&lt;br /&gt;&lt;br /&gt;   1. Program control with if , if-else and switch structures&lt;br /&gt;   2. have a better idea of what C understands as true and false.&lt;br /&gt;&lt;br /&gt;Program Control&lt;br /&gt;It is time to turn our attention to a different problem - conditional execution. We often need to be able to choose which set of instructions are obeyed according to a condition. For example, if you're keeping a total and you need to display the message 'OK' if the value is greater than zero you would need to write something like:&lt;br /&gt;&lt;br /&gt;if (total&gt;O) printf("OK");&lt;br /&gt;&lt;br /&gt;This is perfectly reasonable English, if somewhat terse, but it is also perfectly good C. The if statement allows you to evaluate a &gt; condition and only carry out the statement, or compound statement, that follows if the condition is true. In other words the printf will only be obeyed if the condition total &gt; O is true.&lt;br /&gt;&lt;br /&gt;If the condition is false then the program continues with the next instruction. In general the if statement is of the following form:&lt;br /&gt;&lt;br /&gt;if (condition) statement;&lt;br /&gt;&lt;br /&gt;and of course the statement can be a compound statement.&lt;br /&gt;&lt;br /&gt;Here's an example program using two if statements:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#include&lt;br /&gt;&lt;br /&gt;main()&lt;br /&gt;{&lt;br /&gt;   int a , b;&lt;br /&gt;&lt;br /&gt;   do {&lt;br /&gt;&lt;br /&gt;       printf("\nEnter first number: ");&lt;br /&gt;       scanf("%d" , &amp;amp;a);&lt;br /&gt;&lt;br /&gt;       printf("\nEnter second number: ");&lt;br /&gt;       scanf("%d" , &amp;amp;b);&lt;br /&gt;&lt;br /&gt;       if (a&lt; 999); } &lt;br /&gt;&lt;br /&gt;[program]&lt;br /&gt;&lt;br /&gt;Here's another program using an if keyword and a compound statement or a block:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#include&lt;br /&gt;&lt;br /&gt;main()&lt;br /&gt;{&lt;br /&gt;   int a , b;&lt;br /&gt;&lt;br /&gt;   do {&lt;br /&gt;&lt;br /&gt;       printf("\nEnter first number: ");&lt;br /&gt;       scanf("%d" , &amp;amp;a);&lt;br /&gt;&lt;br /&gt;       printf("\nEnter second number: ");&lt;br /&gt;       scanf("%d" , &amp;amp;b);&lt;br /&gt;&lt;br /&gt;       if (a&lt; 999); }  &lt;br /&gt;&lt;br /&gt;[program]&lt;br /&gt;&lt;br /&gt;The if statement lets you execute or skip an instruction depending on the value of the condition. Another possibility is that you might want to select one of two possible statements - one to be obeyed when the condition is true and one to be obeyed when the condition is false. You can do this using the&lt;br /&gt;&lt;br /&gt;if (condition) statement1;&lt;br /&gt;else statement2;&lt;br /&gt;&lt;br /&gt;form of the if statement.&lt;br /&gt;&lt;br /&gt;In this case statement1 is carried out if the condition is true and statement2 if the condition is false.&lt;br /&gt;&lt;br /&gt;Notice that it is certain that one of the two statements will be obeyed because the condition has to be either true or false! You may be puzzled by the semicolon at the end of the if part of the statement. The if (condition) statement1 part is one statement and the else statement2 part behaves like a second separate statement, so there has to be semi-colon terminating the first statement.&lt;br /&gt;Logical Expressions&lt;br /&gt;&lt;br /&gt;So far we have assumed that the way to write the conditions used in loops and if statements is so obvious that we don't need to look more closely. In fact there are a number of deviations from what you might expect. To compare two values you can use the standard symbols:&lt;br /&gt;"&gt;"    (greater than)&lt;br /&gt;"&lt;"    (less than)&lt;br /&gt;"&gt;="    (for greater than or equal to )&lt;br /&gt;"&lt;="    (for less than or equal to)&lt;br /&gt;"=="    (to test for equality)&lt;br /&gt;&lt;br /&gt;The reason for using two equal signs for equality is that the single equals sign always means store a value in a variable - i.e. it is the assignment operator. This causes beginners lots of problems because they tend to write:&lt;br /&gt;&lt;br /&gt;if (a = 10) instead of if (a == 10)&lt;br /&gt;&lt;br /&gt;The situation is made worse by the fact that the statement if (a = 10) is legal and causes no compiler error messages! It may even appear to work at first because, due to a logical quirk of C, the assignment actually evaluates to the value being assigned and a non-zero value is treated as true (see below). Confused? I agree it is confusing, but it gets easier. . .&lt;br /&gt;&lt;br /&gt;Just as the equals condition is written differently from what you might expect so the non-equals sign looks a little odd. You write not equals as !=. For example:&lt;br /&gt;&lt;br /&gt;if (a != 0)&lt;br /&gt;&lt;br /&gt;is 'if a is not equal to zero'.&lt;br /&gt;&lt;br /&gt;An example program showing the if else construction now follows:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#include&lt;br /&gt;&lt;br /&gt;main ()&lt;br /&gt;{&lt;br /&gt;  int num1, num2;&lt;br /&gt;&lt;br /&gt;  printf("\nEnter first number ");&lt;br /&gt;  scanf("%d",&amp;amp;num1);&lt;br /&gt;&lt;br /&gt;  printf("\nEnter second number ");&lt;br /&gt;  scanf("%d",&amp;amp;num2);&lt;br /&gt;&lt;br /&gt;  if (num2 ==0) printf("\n\nCannot devide by zero\n\n");&lt;br /&gt;  else          printf("\n\nAnswer is %d\n\n",num1/num2);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;[program]&lt;br /&gt;&lt;br /&gt;This program uses an if and else statement to prevent division by 0 from occurring.&lt;br /&gt;True and False in C&lt;br /&gt;Now we come to an advanced trick which you do need to know about, but if it only confuses you, come back to this bit later. Most experienced C programmers would wince at the expression if(a!=0).&lt;br /&gt;&lt;br /&gt;The reason is that in the C programming language dosen't have a concept of a Boolean variable, i.e. a type class that can be either true or false. Why bother when we can use numerical values. In C true is represented by any numeric value not equal to 0 and false is represented by 0. This fact is usually well hidden and can be ignored, but it does allow you to write&lt;br /&gt;&lt;br /&gt;if(a != 0) just as if(a)&lt;br /&gt;&lt;br /&gt;because if a isn't zero then this also acts as the value true. It is debatable if this sort of shortcut is worth the three characters it saves. Reading something like&lt;br /&gt;&lt;br /&gt;if(!done)&lt;br /&gt;&lt;br /&gt;as 'if not done' is clear, but if(!total) is more dubious.&lt;br /&gt;Using break and continue Within Loops&lt;br /&gt;The break statement allows you to exit a loop from any point within its body, bypassing its normal termination expression. When the break statement is encountered inside a loop, the loop is immediately terminated, and program control resumes at the next statement following the loop. The break statement can be used with all three of C's loops. You can have as many statements within a loop as you desire. It is generally best to use the break for special purposes, not as your normal loop exit. break is also used in conjunction with functions and case statements which will be covered in later sections.&lt;br /&gt;&lt;br /&gt;The continue statement is somewhat the opposite of the break statement. It forces the next iteration of the loop to take place, skipping any code in between itself and the test condition of the loop. In while and do-while loops, a continue statement will cause control to go directly to the test condition and then continue the looping process. In the case of the for loop, the increment part of the loop continues. One good use of continue is to restart a statement sequence when an error occurs.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#include&lt;br /&gt;&lt;br /&gt;main()&lt;br /&gt;{&lt;br /&gt;   int x ;&lt;br /&gt;&lt;br /&gt;   for ( x=0 ; x&lt;=100 ; x++) {                               if (x%2) continue;                               printf("%d\n" , x);                              } }  &lt;br /&gt;&lt;br /&gt;[program]&lt;br /&gt;&lt;br /&gt;Here we have used C's modulus operator: %. A expression:&lt;br /&gt;&lt;br /&gt;a % b&lt;br /&gt;&lt;br /&gt;produces the remainder when a is divided by b; and zero when there is no remainder.&lt;br /&gt;&lt;br /&gt;Here's an example of a use for the break statement:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#include&lt;br /&gt;&lt;br /&gt;main()&lt;br /&gt;{&lt;br /&gt;   int t ;&lt;br /&gt;&lt;br /&gt;   for ( ; ; ) {&lt;br /&gt;                scanf("%d" , &amp;amp;t)   ;&lt;br /&gt;                if ( t==10 ) break ;&lt;br /&gt;               }&lt;br /&gt;   printf("End of an infinite loop...\n");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;[program]&lt;br /&gt;Select Paths with switch&lt;br /&gt;While if is good for choosing between two alternatives, it quickly becomes cumbersome when several alternatives are needed. C's solution to this problem is the switch statement. The switch statement is C's multiple selection statement. It is used to select one of several alternative paths in program execution and works like this: A variable is successively tested against a list of integer or character constants. When a match is found, the statement sequence associated with the match is executed. The general form of the switch statement is:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;switch(expression)&lt;br /&gt;{&lt;br /&gt; case constant1:   statement sequence; break;&lt;br /&gt; case constant2:   statement sequence; break;&lt;br /&gt; case constant3:   statement sequence; break;&lt;br /&gt; .&lt;br /&gt; .&lt;br /&gt; .&lt;br /&gt; default:   statement sequence; break;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Each case is labelled by one, or more, constant expressions (or integer-valued constants). The default statement sequence is performed if no matches are found. The default is optional. If all matches fail and default is absent, no action takes place.&lt;br /&gt;&lt;br /&gt;When a match is found, the statement sequence associated with that case are executed until break is encountered.&lt;br /&gt;&lt;br /&gt;An example program follows:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#include&lt;br /&gt;&lt;br /&gt;main()&lt;br /&gt;{&lt;br /&gt; int i;&lt;br /&gt;&lt;br /&gt; printf("Enter a number between 1 and 4");&lt;br /&gt; scanf("%d",&amp;amp;i);&lt;br /&gt;&lt;br /&gt; switch (i)&lt;br /&gt;  {&lt;br /&gt;    case 1:&lt;br /&gt;     printf("one");&lt;br /&gt;     break;&lt;br /&gt;    case 2:&lt;br /&gt;     printf("two");&lt;br /&gt;     break;&lt;br /&gt;    case 3:&lt;br /&gt;     printf("three");&lt;br /&gt;     break;&lt;br /&gt;    case 4:&lt;br /&gt;     printf("four");&lt;br /&gt;     break;&lt;br /&gt;    default:&lt;br /&gt;     printf("unrecognized number");&lt;br /&gt;  }    /* end of switch */&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;[program]&lt;br /&gt;&lt;br /&gt;This simple program recognizes the numbers 1 to 4 and prints the name of the one you enter. The switch statement differs from if, in that switch can only test for equality, whereas the if conditional expression can be of any type. Also switch will work with only int and char types. You cannot for example, use floating-point numbers. If the statement sequence includes more than one statement they will have to be enclosed with {} to form a compound statement.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5670527689344996196-4402457340899047694?l=rao-programmingofc.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://rao-programmingofc.blogspot.com/feeds/4402457340899047694/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5670527689344996196&amp;postID=4402457340899047694' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5670527689344996196/posts/default/4402457340899047694'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5670527689344996196/posts/default/4402457340899047694'/><link rel='alternate' type='text/html' href='http://rao-programmingofc.blogspot.com/2008/02/conditional-execution.html' title='Conditional Execution'/><author><name>Ramjaan</name><uri>http://www.blogger.com/profile/07862712225011522143</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='http://1.bp.blogspot.com/_uyQpXxI_GeY/SQy7Z8dn6II/AAAAAAAAACI/94M3auWVt4A/S220/images.jpeg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5670527689344996196.post-5522479614561769402</id><published>2008-02-08T02:55:00.000-08:00</published><updated>2008-02-08T03:04:51.195-08:00</updated><title type='text'>Control Loops</title><content type='html'>&lt;h1&gt;                     Control Loops&lt;/h1&gt;&lt;br /&gt;&lt;table summary="Table used for page content layout." border="0" cellpadding="0" cellspacing="0" width="100%"&gt; &lt;tbody&gt;&lt;tr&gt;&lt;td colspan="2" align="left" valign="top"&gt;    &lt;hr /&gt; &lt;h2&gt;Objectives&lt;/h2&gt;  &lt;p&gt;Having read this section you should have an idea about C's:&lt;/p&gt;  &lt;ol&gt;&lt;li&gt;Conditional, or Logical, Expressions as used in program control&lt;/li&gt;&lt;li&gt;the &lt;tt&gt;&lt;b&gt;do while&lt;/b&gt;&lt;/tt&gt; loop&lt;/li&gt;&lt;li&gt;the &lt;tt&gt;&lt;b&gt;while&lt;/b&gt;&lt;/tt&gt; loop&lt;/li&gt;&lt;li&gt;the &lt;tt&gt;&lt;b&gt;for&lt;/b&gt;&lt;/tt&gt; loop&lt;/li&gt;&lt;/ol&gt;  &lt;hr /&gt;  &lt;h3&gt;Go With The Flow&lt;/h3&gt;  Our programs are getting a bit more sophisticated, but they still lack that essential something that makes a computer so necessary. Exactly what they lack is the most difficult part to describe to a beginner. There are only two great ideas in computing. The first is the &lt;i&gt;variable&lt;/i&gt; and you've already met that. The second is &lt;i&gt;flow of control&lt;/i&gt;.   &lt;p&gt;When you write a list of instructions for someone to perform you usually expect them to follow the list from the top to the bottom, &lt;i&gt;one at a time&lt;/i&gt;. This is the simple default flow of control through a program. The C programs we have written so far use this one-after-another default flow of control.&lt;/p&gt;  &lt;p&gt;This is fine and simple, but it limits the running time of any program we can write. Why? Simply because there is a limit to the number of instructions you can write and it doesn't take long for a computer to read though and obey your list. So how is it that we have programs that run for hours on end if need be? The answer is statements that alter the one-after-another order of obeying instructions. Perhaps the most useful is the &lt;i&gt;loop&lt;/i&gt;.&lt;/p&gt;  &lt;p&gt;Suppose we ask you to display "Hello World!" five times on the screen. &lt;i&gt;Easy!&lt;/i&gt; you'd say:&lt;/p&gt;  &lt;pre&gt;&lt;kbd&gt;&lt;br /&gt;&lt;b&gt;#include &lt;stdio.h&gt;&lt;br /&gt;main()&lt;br /&gt;{&lt;br /&gt;  printf("Hello World!\n");&lt;br /&gt;  printf("Hello World!\n");&lt;br /&gt;  printf("Hello World!\n");&lt;br /&gt;  printf("Hello World!\n");&lt;br /&gt;  printf("Hello World!\n");&lt;br /&gt;}&lt;/b&gt; &lt;/kbd&gt;&lt;/pre&gt;  &lt;p&gt;Indeed, this does exactly what was asked. But now we up the bet and ask you to do the same job for 100 hellos or, if you're still willing to type that much code, maybe 1,000 Hello World's, 10,000 Hello World's, or whatever it takes you to realise this isn't a sensible method!&lt;/p&gt;  &lt;p&gt;What you really need is some way of repeating the &lt;b&gt;&lt;tt&gt;printf&lt;/tt&gt;&lt;/b&gt; statements without having to write it out each time. The solution to this problem is the &lt;tt&gt;&lt;b&gt;while&lt;/b&gt;&lt;/tt&gt; loop or the &lt;b&gt;&lt;tt&gt;do while&lt;/tt&gt;&lt;/b&gt; loop.&lt;/p&gt;  &lt;hr /&gt;  &lt;a name="DW"&gt;&lt;h3&gt;The &lt;tt&gt;while&lt;/tt&gt; and &lt;tt&gt;do while&lt;/tt&gt; Loops&lt;/h3&gt;&lt;/a&gt;  You can repeat any statement using either the &lt;b&gt;&lt;tt&gt;while&lt;/tt&gt;&lt;/b&gt; loop:   &lt;p&gt;&lt;b&gt;&lt;tt&gt;while(&lt;i&gt;condition&lt;/i&gt;) &lt;i&gt;compound statement&lt;/i&gt;;&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;or the &lt;tt&gt;&lt;b&gt;do while&lt;/b&gt;&lt;/tt&gt; loop:&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;do &lt;i&gt;compound statement&lt;/i&gt; while(&lt;i&gt;condition&lt;/i&gt;);&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;The &lt;i&gt;&lt;b&gt;&lt;tt&gt;condition&lt;/tt&gt;&lt;/b&gt;&lt;/i&gt; is just a test to control how long you want the &lt;i&gt;&lt;b&gt;&lt;tt&gt;compound statement&lt;/tt&gt;&lt;/b&gt;&lt;/i&gt; to carry on repeating.&lt;/p&gt;  &lt;p&gt;Each line of a C program up to the semicolon is called a statement. The semicolon is the statement's terminator. The braces { and } which have appeared at the beginning and end of our program unit can also be used to group together related declarations and statements into a &lt;i&gt;&lt;b&gt;&lt;tt&gt;compound statement&lt;/tt&gt;&lt;/b&gt;&lt;/i&gt; or a &lt;i&gt;block&lt;/i&gt;.&lt;/p&gt;  &lt;p&gt;In the case of the &lt;tt&gt;&lt;b&gt;while&lt;/b&gt;&lt;/tt&gt; loop before the &lt;i&gt;&lt;b&gt;&lt;tt&gt;compound statement&lt;/tt&gt;&lt;/b&gt;&lt;/i&gt; is carried out the &lt;i&gt;&lt;b&gt;&lt;tt&gt;condition&lt;/tt&gt;&lt;/b&gt;&lt;/i&gt; is checked, and if it is &lt;i&gt;true&lt;/i&gt; the &lt;i&gt;&lt;b&gt;&lt;tt&gt;statement&lt;/tt&gt;&lt;/b&gt;&lt;/i&gt; is obeyed one more time. If the &lt;i&gt;&lt;b&gt;&lt;tt&gt;condition&lt;/tt&gt;&lt;/b&gt;&lt;/i&gt; turns out to be &lt;i&gt;false&lt;/i&gt;, the looping isn't obeyed and the program moves on to the next statement. So you can see that the instruction really means &lt;i&gt;while something or other is true keep on doing the statement&lt;/i&gt;.&lt;/p&gt;  &lt;p&gt;In the case of the &lt;b&gt;&lt;tt&gt;do while&lt;/tt&gt;&lt;/b&gt; loop it will always execute the code within the loop at least once, since the &lt;i&gt;&lt;b&gt;&lt;tt&gt;condition&lt;/tt&gt;&lt;/b&gt;&lt;/i&gt; controlling the loop is tested at the bottom of the loop. The &lt;b&gt;&lt;tt&gt;do while&lt;/tt&gt;&lt;/b&gt; loop repeats the instruction while the &lt;i&gt;&lt;b&gt;&lt;tt&gt;condition&lt;/tt&gt;&lt;/b&gt;&lt;/i&gt; is &lt;i&gt;true&lt;/i&gt;. If the &lt;i&gt;&lt;b&gt;&lt;tt&gt;condition&lt;/tt&gt;&lt;/b&gt;&lt;/i&gt; turns out to be &lt;i&gt;false&lt;/i&gt;, the looping isn't obeyed and the program moves on to the next statement.&lt;/p&gt;  &lt;hr /&gt;  &lt;a name="LE"&gt;&lt;h3&gt;Conditions or Logical Expressions&lt;/h3&gt;&lt;/a&gt;  The only detail we need to clear up is what the &lt;tt&gt;&lt;b&gt;&lt;i&gt;condition&lt;/i&gt;&lt;/b&gt;&lt;/tt&gt; (or &lt;i&gt;Logical Expression&lt;/i&gt;) can be. How, for example, do we display 100 or 10,000 "Hello World!" messages? The &lt;tt&gt;&lt;b&gt;&lt;i&gt;condition&lt;/i&gt;&lt;/b&gt;&lt;/tt&gt; can be any test of one value against another. For example:   &lt;p&gt;&lt;b&gt;a&gt;0&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;is true if &lt;b&gt;a&lt;/b&gt; contains a value greater than zero;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;b&lt;0&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;is true if &lt;b&gt;b&lt;/b&gt; contains a value less than zero.&lt;/p&gt;  &lt;p&gt;The only complication is that the test for '&lt;i&gt;something equals something else&lt;/i&gt;' uses the character sequence &lt;tt&gt;&lt;b&gt;==&lt;/b&gt;&lt;/tt&gt; and not &lt;tt&gt;&lt;b&gt;=&lt;/b&gt;&lt;/tt&gt;. That's right: a test for equality uses two equal-signs, as in &lt;b&gt;&lt;tt&gt;a==0&lt;/tt&gt;&lt;/b&gt;, while an assignment, as in &lt;b&gt;&lt;tt&gt;a=0&lt;/tt&gt;&lt;/b&gt;, uses one. This use of the double or single equal sign to mean slightly different things is a cause of many a program bug for beginner and expert alike!&lt;/p&gt;  &lt;p&gt;So what about answering the question? What about the 100 "Hello World"s? Well, for the moment we know easily how to produce an infinite number of Hello Worlds! using &lt;tt&gt;&lt;b&gt;while&lt;/b&gt;&lt;/tt&gt; loop:&lt;/p&gt;  &lt;pre&gt;&lt;kbd&gt;&lt;br /&gt;&lt;b&gt;#include &lt;stdio.h&gt;&lt;br /&gt; main()&lt;br /&gt;  {&lt;br /&gt;   while (1 == 1) printf("Hello World!\n");&lt;br /&gt;  }&lt;/b&gt; &lt;/kbd&gt;&lt;/pre&gt;  and using the &lt;b&gt;&lt;tt&gt;do while&lt;/tt&gt;&lt;/b&gt; loop:   &lt;pre&gt;&lt;kbd&gt;&lt;br /&gt;&lt;b&gt;#include &lt;stdio.h&gt;&lt;br /&gt; main()&lt;br /&gt;  {&lt;br /&gt;   do&lt;br /&gt;     printf("Hello World!\n");&lt;br /&gt;   while (1 == 1)&lt;br /&gt;  }&lt;/b&gt; &lt;/kbd&gt;&lt;/pre&gt;  If you type either of these programs in and run it you will find that your screen fills with a never ending list of "Hello World!"s. Why? Because the condition to keep the repeat going is ( &lt;tt&gt;&lt;b&gt;1 == 1&lt;/b&gt;&lt;/tt&gt; ), &lt;i&gt;one equals one&lt;/i&gt; in plain English, which is always true! So how do we stop the loop? In some cases it could be by pulling the plug out - but usually you can stop an infinite loop by pressing &lt;b&gt;Ctrl-Break&lt;/b&gt; or &lt;b&gt;Ctrl-C&lt;/b&gt;.   &lt;p&gt;An infinite loop is sometimes useful - I certainly hope the program controlling the nearest nuclear power station is an infinite loop that never receives a &lt;b&gt;Ctrl-Break&lt;/b&gt; signal! Most loops, however, have to stop some time.&lt;/p&gt;  &lt;p&gt;To solve our problem of printing 100 "Hello World!"s we need a counter and a test for when that counter reaches 100. A counter is a simple variable that has one added to it each time through the loop, using an instruction like this:&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;a=a+1;&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;This always confuses beginners, because they aren't used to seeing the variable on both sides of the equal-sign. All this means is that &lt;tt&gt;&lt;b&gt;a&lt;/b&gt;&lt;/tt&gt; has one added to it to produce a new value, and this value is stored back in the location called &lt;strong&gt;&lt;tt&gt;a&lt;/tt&gt;&lt;/strong&gt;. If you're worried, try thinking about it as:&lt;/p&gt;  &lt;pre&gt;&lt;kbd&gt;&lt;br /&gt;&lt;b&gt;temp = a+l;&lt;br /&gt;a    = temp;&lt;br /&gt;&lt;/b&gt;&lt;br /&gt;&lt;/kbd&gt;&lt;/pre&gt;  The two approaches are more or less the same. C is a language where anything that's used often can be said concisely, so it lets you say "&lt;i&gt;add one to a variable&lt;/i&gt;" using the shorter notation:   &lt;p&gt;&lt;tt&gt;&lt;b&gt;++a;&lt;/b&gt;&lt;/tt&gt;&lt;/p&gt;  &lt;p&gt;The double plus is read "&lt;i&gt;increment &lt;b&gt;a&lt;/b&gt; by one&lt;/i&gt;". Make sure you know that &lt;b&gt;&lt;tt&gt;++a&lt;/tt&gt;&lt;/b&gt;; and &lt;b&gt;&lt;tt&gt;a=a+1&lt;/tt&gt;&lt;/b&gt;; are the same thing because you will often see both in typical C programs.&lt;/p&gt;  &lt;p&gt;The increment operator &lt;b&gt;&lt;tt&gt;++&lt;/tt&gt;&lt;/b&gt; and the equivalent decrement operator &lt;b&gt;&lt;tt&gt;--&lt;/tt&gt;&lt;/b&gt;, can be used as either &lt;a href="http://www.le.ac.uk/cc/tutorials/c/ccccppo.html"&gt;&lt;i&gt;prefix&lt;/i&gt;&lt;/a&gt; (before the variable) or &lt;a href="http://www.le.ac.uk/cc/tutorials/c/ccccppo.html"&gt;&lt;i&gt;postfix&lt;/i&gt;&lt;/a&gt; (after the variable). &lt;b&gt;Note:&lt;/b&gt; &lt;b&gt;&lt;tt&gt;++a&lt;/tt&gt;&lt;/b&gt; increments &lt;b&gt;&lt;tt&gt;a&lt;/tt&gt;&lt;/b&gt; before using its value; whereas &lt;b&gt;&lt;tt&gt;a++&lt;/tt&gt;&lt;/b&gt; which means use the value in &lt;tt&gt;&lt;b&gt;a&lt;/b&gt;&lt;/tt&gt; then increment the value stored in &lt;tt&gt;&lt;b&gt;a&lt;/b&gt;&lt;/tt&gt;.&lt;/p&gt;  &lt;p&gt;Now it is easy to print "Hello World!" 100 times using the &lt;b&gt;&lt;tt&gt;while&lt;/tt&gt;&lt;/b&gt; loop:&lt;/p&gt;    &lt;pre&gt;&lt;kbd&gt;&lt;br /&gt;&lt;b&gt;#include &lt;stdio.h&gt;&lt;br /&gt;main()&lt;br /&gt; {&lt;br /&gt;  int count;&lt;br /&gt;  count=0;&lt;br /&gt;  while (count &lt; 100)&lt;br /&gt;   {&lt;br /&gt;     ++count;&lt;br /&gt;     printf("Hello World!\n");&lt;br /&gt;   }&lt;br /&gt; }&lt;/b&gt; &lt;/kbd&gt;&lt;/pre&gt;  &lt;p&gt;[&lt;a href="http://www.le.ac.uk/cc/tutorials/c/programs/while.c"&gt;program&lt;/a&gt;]&lt;/p&gt;  &lt;p&gt;or the &lt;b&gt;&lt;tt&gt;do while&lt;/tt&gt;&lt;/b&gt; loop:&lt;/p&gt;    &lt;pre&gt;&lt;kbd&gt;&lt;br /&gt;&lt;b&gt;#include &lt;stdio.h&gt;&lt;br /&gt;main()&lt;br /&gt; {&lt;br /&gt;  int count;&lt;br /&gt;  count=0;&lt;br /&gt;  do&lt;br /&gt;   {&lt;br /&gt;     ++count;&lt;br /&gt;     printf("Hello, World!\n");&lt;br /&gt;   } while (count &lt; 100)&lt;br /&gt; }&lt;/b&gt; &lt;/kbd&gt;&lt;/pre&gt;  &lt;p&gt;[&lt;a href="http://www.le.ac.uk/cc/tutorials/c/programs/do.c"&gt;program&lt;/a&gt;]&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Note:&lt;/b&gt; the use of the { and } to form a &lt;i&gt;compound statement&lt;/i&gt;; all statements between the braces will be executed before the loop check is made.&lt;/p&gt;  &lt;p&gt;The integer variable &lt;tt&gt;&lt;b&gt;count&lt;/b&gt;&lt;/tt&gt; is declared and then set to zero, ready to count the number of times we have gone round the loop. Each time round the loop the value of count is checked against 100. As long as it is less, the loop carries on. Each time the loop carries on, count is incremented and "Hello World!" is printed - so eventually count does reach 100 and the loop stops. These little programs are just a bit more subtle than you might think. Ask yourself, do they really print exactly 100 times? Ask yourself: what is the final value of &lt;b&gt;&lt;tt&gt;count&lt;/tt&gt;&lt;/b&gt;? If you want to make sure you are right change the &lt;b&gt;&lt;tt&gt;printf&lt;/tt&gt;&lt;/b&gt; to:&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;printf("count is %d",count);&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;and add a &lt;b&gt;&lt;tt&gt;printf&lt;/tt&gt;&lt;/b&gt; after the loop:&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;printf("final value is %d",count);&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Make sure you understand why you get the results that you do. What would happen if you changed the initial value of count to be one rather than zero?&lt;/p&gt;  &lt;hr /&gt;   &lt;h3&gt;Looping the Loop&lt;/h3&gt;  We have seen that any list of statements enclosed in curly brackets is treated as a single statement, a &lt;i&gt;compound statement&lt;/i&gt;. So to repeat a list of statements all you have to do is put them inside a pair of curly brackets as in:   &lt;pre&gt;&lt;kbd&gt;&lt;br /&gt;&lt;b&gt;while (condition)&lt;br /&gt;{&lt;br /&gt;  statementl;&lt;br /&gt;  statement2;&lt;br /&gt;  statement3;&lt;br /&gt;}&lt;/b&gt; &lt;/kbd&gt;&lt;/pre&gt;  which repeats the list while the &lt;i&gt;&lt;b&gt;&lt;tt&gt;condition&lt;/tt&gt;&lt;/b&gt;&lt;/i&gt; is true. Notice that the statements within the curly brackets have to be terminated by semicolons as usual. Notice also that as the &lt;b&gt;&lt;tt&gt;while&lt;/tt&gt;&lt;/b&gt; statement is a complete statement it too has to be terminated by a semi-colon - except for the influence of one other punctuation rule. &lt;i&gt;You never have to follow a right curly bracket with a semi-colon&lt;/i&gt;. This rule was introduced to make C look tidier by avoiding things like   &lt;p&gt;&lt;b&gt;&lt;tt&gt;};};};}&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;at the end of a complicated program. You can write the semi-colon after the right bracket if you want to, but most C programmers don't. You can use a compound statement anywhere you can use a single statement.&lt;/p&gt;  &lt;hr /&gt;  &lt;a name="FL"&gt;&lt;h3&gt;The &lt;tt&gt;for&lt;/tt&gt; Loop&lt;/h3&gt;&lt;/a&gt;  The &lt;b&gt;&lt;tt&gt;while&lt;/tt&gt;&lt;/b&gt;, and &lt;b&gt;&lt;tt&gt;do-while&lt;/tt&gt;&lt;/b&gt;, loop is a completely general way of repeating a section of program over and over again - and you don't really need anything else but... The &lt;b&gt;&lt;tt&gt;while&lt;/tt&gt;&lt;/b&gt; loop repeats a list of instructions while some condition or other is &lt;i&gt;true&lt;/i&gt; and often you want to repeat something a given number of times.   &lt;p&gt;The traditional solution to this problem is to introduce a variable that is used to count the number of times that a loop has been repeated and use its value in the condition to end the loop. For example, the loop:&lt;/p&gt;  &lt;pre&gt;&lt;kbd&gt;&lt;br /&gt;&lt;b&gt;i=l;&lt;br /&gt;while (i&lt;=10)&lt;br /&gt;{&lt;br /&gt;  printf("%d \n",i);&lt;br /&gt;  ++i;&lt;br /&gt;}&lt;/b&gt; &lt;/kbd&gt;&lt;/pre&gt;  repeats while &lt;tt&gt;&lt;b&gt;i&lt;/b&gt;&lt;/tt&gt; is less than 10. As the &lt;tt&gt;&lt;b&gt;++&lt;/b&gt;&lt;/tt&gt; operator is used to add one to &lt;tt&gt;&lt;b&gt;i&lt;/b&gt;&lt;/tt&gt; each time through the loop you can see that &lt;b&gt;&lt;tt&gt;i&lt;/tt&gt;&lt;/b&gt; is a loop counter and eventually it will get bigger than 10, i.e. the loop will end.   &lt;p&gt;The question is how many times does the loop go round? More specifically what values of &lt;b&gt;&lt;tt&gt;i&lt;/tt&gt;&lt;/b&gt; is the loop carried out for? If you run this program snippet you will find that it prints 1,2,3... and finishes at 10. That is, the loop repeats 10 times for values of &lt;tt&gt;&lt;b&gt;i&lt;/b&gt;&lt;/tt&gt; from 1 to 10. This sort of loop - one that runs from a starting value to a finishing value going up by one each time - is so common that nearly all programming languages provide special commands to implement it. In C this special type of loop can be implemented as a &lt;b&gt;&lt;tt&gt;for&lt;/tt&gt;&lt;/b&gt; loop.&lt;/p&gt;  &lt;pre&gt;&lt;kbd&gt;&lt;br /&gt;&lt;b&gt;for ( counter=start_value; counter &lt;= finish_value; ++counter )&lt;br /&gt; &lt;i&gt;compound statement&lt;/i&gt;&lt;br /&gt;&lt;/b&gt;&lt;br /&gt;&lt;/kbd&gt;&lt;/pre&gt;  which is entirely equivalent to:   &lt;pre&gt;&lt;kbd&gt;&lt;br /&gt;&lt;b&gt;counter=start;&lt;br /&gt;while (couner &lt;= finish)&lt;br /&gt;{&lt;br /&gt; statements;&lt;br /&gt; ++counter;&lt;br /&gt;}&lt;br /&gt;&lt;/b&gt;&lt;br /&gt;&lt;/kbd&gt;&lt;/pre&gt;  The condition operator &lt;= should be interpreted as &lt;i&gt;less than or equal too&lt;/i&gt;. We will be covering all of C's conditions , or logical expressions, in the next section.   &lt;p&gt;For example to print the numbers 1 to 100 you could use:&lt;/p&gt;  &lt;p&gt;&lt;tt&gt;&lt;b&gt;for ( i=l; i &lt;= 100; ++i ) printf("%d \n",i);&lt;/b&gt;&lt;/tt&gt;&lt;/p&gt;  &lt;p&gt;You can, of course repeat a longer list of instructions simply by using a &lt;i&gt;compound statement&lt;/i&gt;.&lt;/p&gt;  &lt;p&gt;The C &lt;b&gt;&lt;tt&gt;for&lt;/tt&gt;&lt;/b&gt; loop is much more flexible than this simple description. Indeed, many would be horrified at the way we have described the &lt;b&gt;&lt;tt&gt;for&lt;/tt&gt;&lt;/b&gt; loop without displaying its true generality, but keep in mind that there is more to come.&lt;/p&gt;  &lt;p&gt;In the meantime consider the following program, it does a temperature conversion, but it also introduces one or two new concepts:&lt;/p&gt;  &lt;ol&gt;&lt;li&gt;our counter does not have to be incremented (decremented) by 1; we can use any value.&lt;/li&gt;&lt;li&gt;we can do calculations within the &lt;b&gt;&lt;tt&gt;printf&lt;/tt&gt;&lt;/b&gt; statement.&lt;/li&gt;&lt;/ol&gt;  &lt;pre&gt;&lt;kbd&gt;&lt;br /&gt;&lt;b&gt;#include &lt;stdio.h&gt;&lt;br /&gt;&lt;br /&gt;main()&lt;br /&gt;{&lt;br /&gt;   int fahr;&lt;br /&gt;&lt;br /&gt;   for ( fahr = 0 ; fahr &lt;= 300 ; fahr = fahr + 20)&lt;br /&gt;       printf("%4d %6.1f\n" , fahr , (5.0/9.0)*(fahr-32));&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;/b&gt;&lt;br /&gt;&lt;/kbd&gt;&lt;/pre&gt;  &lt;p&gt;[&lt;a href="http://www.le.ac.uk/cc/tutorials/c/programs/for.c"&gt;program&lt;/a&gt;]&lt;/p&gt;  &lt;p&gt;and here's another one for you to look at:&lt;/p&gt;  &lt;pre&gt;&lt;kbd&gt;&lt;br /&gt;&lt;b&gt;#include &lt;stdio.h&gt;&lt;br /&gt;&lt;br /&gt;main()&lt;br /&gt;{&lt;br /&gt;   int lower , upper , step;&lt;br /&gt;   float fahr , celsius;&lt;br /&gt;&lt;br /&gt;   lower = 0  ;&lt;br /&gt;   upper = 300;&lt;br /&gt;   step  = 20 ;&lt;br /&gt;&lt;br /&gt;   fahr  = lower;&lt;br /&gt;&lt;br /&gt;   while ( fahr &lt;= upper ) {&lt;br /&gt;                            celsius = (5.0 / 9.0) * (fahr - 32.0);&lt;br /&gt;                            printf("%4.0f %6.1f\n" , fahr , celsius);&lt;br /&gt;                            fahr = fahr + step;&lt;br /&gt;                           }&lt;br /&gt;}&lt;br /&gt;&lt;/b&gt;&lt;br /&gt;&lt;/kbd&gt;&lt;/pre&gt;  &lt;p&gt;[&lt;a href="http://www.le.ac.uk/cc/tutorials/c/programs/temp.c"&gt;program&lt;/a&gt;]&lt;/p&gt;  &lt;p&gt; &lt;/p&gt;       &lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt; &lt;/table&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5670527689344996196-5522479614561769402?l=rao-programmingofc.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://rao-programmingofc.blogspot.com/feeds/5522479614561769402/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5670527689344996196&amp;postID=5522479614561769402' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5670527689344996196/posts/default/5522479614561769402'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5670527689344996196/posts/default/5522479614561769402'/><link rel='alternate' type='text/html' href='http://rao-programmingofc.blogspot.com/2008/02/control-loops.html' title='Control Loops'/><author><name>Ramjaan</name><uri>http://www.blogger.com/profile/07862712225011522143</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='http://1.bp.blogspot.com/_uyQpXxI_GeY/SQy7Z8dn6II/AAAAAAAAACI/94M3auWVt4A/S220/images.jpeg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5670527689344996196.post-8061669821384504061</id><published>2008-02-08T02:48:00.000-08:00</published><updated>2008-02-08T02:54:58.967-08:00</updated><title type='text'>Input and Output Functions</title><content type='html'>&lt;h1&gt;          Input and Output Functions&lt;/h1&gt;&lt;br /&gt;&lt;h2&gt;Objectives&lt;/h2&gt;  &lt;p&gt;Having read this section you should have a clearer idea of &lt;i&gt;one&lt;/i&gt; of C's:&lt;/p&gt;  &lt;ol&gt;&lt;li&gt;input functions, called &lt;b&gt;&lt;tt&gt;scanf&lt;/tt&gt;&lt;/b&gt;&lt;/li&gt;&lt;li&gt;output functions, called &lt;b&gt;&lt;tt&gt;printf&lt;/tt&gt;&lt;/b&gt;&lt;/li&gt;&lt;/ol&gt;  &lt;hr /&gt; &lt;h3&gt;On The Run&lt;/h3&gt;  Even with arithmetic you can't do very much other than write programs that are the equivalent of a pocket calculator. The real break through comes when you can read values into variables as the program runs. Notice the important words here: "as the program runs". You can already store values in variables using assignment. That is:  &lt;p&gt;&lt;b&gt;&lt;tt&gt;a=100;&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;stores 100 in the variable &lt;b&gt;&lt;tt&gt;a&lt;/tt&gt;&lt;/b&gt; each time you run the program, no matter what you do. Without some sort of input command every program would produce exactly the same result every time it was run. This would certainly make debugging easy! But in practice, of course, we need programs to do different jobs each time they are run. There are a number of different C input commands, the most useful of which is the &lt;tt&gt;&lt;b&gt;scanf&lt;/b&gt;&lt;/tt&gt; command. To read a single integer value into the variable called &lt;b&gt;&lt;tt&gt;a&lt;/tt&gt;&lt;/b&gt; you would use:&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;scanf("%d",&amp;amp;a);&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;For the moment don't worry about what the &lt;b&gt;&lt;tt&gt;%d&lt;/tt&gt;&lt;/b&gt; or the &lt;b&gt;&lt;kbd&gt;&amp;amp;&lt;/kbd&gt;&lt;/b&gt;a means - concentrate on the difference between this and:&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;a=100;&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;When the program reaches the &lt;tt&gt;&lt;b&gt;scanf&lt;/b&gt;&lt;/tt&gt; statement it pauses to give the user time to type something on the keyboard and continues only when users press &lt;tt&gt;&lt;enter&gt;&lt;/enter&gt;&lt;/tt&gt;, or &lt;tt&gt;&lt;return&gt;&lt;/return&gt;&lt;/tt&gt;, to signal that he, or she, has finished entering the value. Then the program continues with the new value stored in &lt;tt&gt;&lt;b&gt;a&lt;/b&gt;&lt;/tt&gt;. In this way, each time the program is run the user gets a chance to type in a different value to the variable and the program also gets the chance to produce a different result!&lt;/p&gt;  &lt;p&gt;The final missing piece in the jigsaw is using the &lt;tt&gt;&lt;b&gt;printf&lt;/b&gt;&lt;/tt&gt; function, the one we have already used to print "Hello World", to print the value currently being stored in a variable. To display the value stored in the variable &lt;b&gt;&lt;tt&gt;a&lt;/tt&gt;&lt;/b&gt; you would use:&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;printf("The value stored in a is %d",a);&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;The &lt;tt&gt;&lt;b&gt;%d&lt;/b&gt;&lt;/tt&gt;, both in the case of &lt;b&gt;&lt;tt&gt;scanf&lt;/tt&gt;&lt;/b&gt; and &lt;tt&gt;&lt;b&gt;printf&lt;/b&gt;&lt;/tt&gt;, simply lets the compiler know that the value being read in, or printed out, is a &lt;i&gt;decimal integer&lt;/i&gt; - that is, a few digits but no decimal point.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Note:&lt;/b&gt; the &lt;b&gt;&lt;tt&gt;scanf&lt;/tt&gt;&lt;/b&gt; function does not prompt for an input. You should get in the habit of always using a &lt;tt&gt;&lt;b&gt;printf&lt;/b&gt;&lt;/tt&gt; function, informing the user of the program what they should type, before a &lt;b&gt;&lt;tt&gt;scanf&lt;/tt&gt;&lt;/b&gt; function.&lt;/p&gt;  &lt;hr /&gt;  &lt;h3&gt;Input and Output Functions in More Detail&lt;/h3&gt;  One of the advantages of C is that essentially it is a &lt;i&gt;small&lt;/i&gt; language. This means that you can write a complete description of the language in a few pages. It doesn't have many keywords or data types for that matter. What makes C so powerful is the way that these low-level facilities can be put together to make higher level facilities.   &lt;p&gt;The only problem with this is that C programmers have a tendency to reinvent the wheel each time they want to go for a ride. It is also possible to write C programs in a variety of styles which depend on the particular tricks and devices that a programmer chooses to use. Even after writing C for a long time you will still find the occasionally construction which makes you think, "I never thought of that!" or, "what is that doing?"&lt;/p&gt;  &lt;p&gt;One attempt to make C a more uniform language is the provision of standard libraries of functions that perform common tasks. We say standard but until the ANSI committee actually produced a standard there was, and still is, some variation in what the &lt;i&gt;standard&lt;/i&gt; libraries contained and exactly how the functions worked. Having said that we had better rush in quickly with the reassurance that in practice the situation isn't that bad and most of the functions that are used frequently really are standard on all implementations. In particular the I/O functions vary very little.&lt;/p&gt;  &lt;p&gt;It is now time to look at exactly how &lt;b&gt;&lt;tt&gt;scanf&lt;/tt&gt;&lt;/b&gt; and &lt;b&gt;&lt;tt&gt;printf&lt;/tt&gt;&lt;/b&gt; work and what they can do - you might be surprised at just how complex they really are!&lt;/p&gt;  &lt;p&gt;The original C specification did not include commands for input and output. Instead the compiler writers were supposed to implement library functions to suit their machines. In practice all chose to implement &lt;b&gt;&lt;tt&gt;printf&lt;/tt&gt;&lt;/b&gt; and &lt;b&gt;&lt;tt&gt;scanf&lt;/tt&gt;&lt;/b&gt; and after a while C programmers started to think of them as if these functions were I/O keywords! It sometimes helps to remember that they are functions on a par with any other functions you may care to define. If you want to you can provide your own implementations of &lt;b&gt;&lt;tt&gt;printf&lt;/tt&gt;&lt;/b&gt; or &lt;b&gt;&lt;tt&gt;scanf&lt;/tt&gt;&lt;/b&gt; or any of the other standard functions - we'll discover how later.&lt;/p&gt;  &lt;hr /&gt;   &lt;a name="printf"&gt;&lt;h3&gt;&lt;b&gt;&lt;tt&gt;printf&lt;/tt&gt;&lt;/b&gt;&lt;/h3&gt;&lt;/a&gt;  The &lt;b&gt;&lt;tt&gt;printf&lt;/tt&gt;&lt;/b&gt; (and &lt;b&gt;&lt;tt&gt;scanf&lt;/tt&gt;&lt;/b&gt;) functions do differ from the sort of functions that you will created for yourself in that they can take a variable number of &lt;i&gt;parameters&lt;/i&gt;. In the case of &lt;b&gt;&lt;tt&gt;printf&lt;/tt&gt;&lt;/b&gt; the first parameter is always a string (c.f. "Hello World") but after that you can include as many parameters of any type that you want to. That is, the &lt;b&gt;&lt;tt&gt;printf&lt;/tt&gt;&lt;/b&gt; function is usually of the form:   &lt;p&gt; &lt;b&gt;&lt;tt&gt;printf(&lt;i&gt;string,variable,variable,variable...&lt;/i&gt;)&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;where the ... means you can carry on writing a list of variables separated by commas as long as you want to. The &lt;i&gt;string&lt;/i&gt; is all-important because it specifies the type of each &lt;i&gt;variable&lt;/i&gt; in the list and how you want it printed. The string is usually called the &lt;i&gt;control string&lt;/i&gt; or the &lt;i&gt;format string&lt;/i&gt;. The way that this works is that &lt;b&gt;&lt;tt&gt;printf&lt;/tt&gt;&lt;/b&gt; scans the string from left to right and prints on the screen, or any suitable output device, any characters it encounters - except when it reaches a &lt;b&gt;&lt;tt&gt;%&lt;/tt&gt;&lt;/b&gt; character. The &lt;b&gt;&lt;tt&gt;%&lt;/tt&gt;&lt;/b&gt; character is a signal that what follows it is a specification for how the next variable in the list of variables should be printed. &lt;b&gt;&lt;tt&gt;printf&lt;/tt&gt;&lt;/b&gt; uses this information to convert and format the value that was passed to the function by the variable and then moves on to process the rest of the control string and anymore variables it might specify. For example:&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;printf("Hello World");&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;only has a control string and, as this contains no &lt;b&gt;&lt;tt&gt;%&lt;/tt&gt;&lt;/b&gt; characters it results in &lt;b&gt;&lt;tt&gt;Hello World&lt;/tt&gt;&lt;/b&gt; being displayed and doesn't need to display any variable values. The specifier &lt;b&gt;&lt;tt&gt;%d&lt;/tt&gt;&lt;/b&gt; means &lt;i&gt;convert the next value to a signed decimal integer&lt;/i&gt; and so:&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;printf("Total = %d",total);&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;will print &lt;b&gt;&lt;tt&gt;Total =&lt;/tt&gt;&lt;/b&gt; and then the value passed by &lt;b tt=""&gt;&gt;total&lt;/b&gt; as a decimal integer.&lt;/p&gt;  &lt;p&gt;If you are familiar other programming languages then you may feel happy about the &lt;b&gt;&lt;tt&gt;printf&lt;/tt&gt;&lt;/b&gt; function because something like:&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;printf("Total = %d",total);&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;looks like the sort of output command you might have used before. For example, in &lt;b&gt;BASIC&lt;/b&gt; you would write:&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;kbd&gt;PRINT "Total = ",total&lt;/kbd&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;but the C view of output is at a lower level than you might expect. The &lt;tt&gt;&lt;b&gt;%d&lt;/b&gt;&lt;/tt&gt; isn't just a format specifier, it is a &lt;i&gt;conversion specifier&lt;/i&gt;. It indicates the data type of the variable to be printed and how that data type should be converted to the characters that appear on the screen. That is &lt;b&gt;&lt;tt&gt;%d&lt;/tt&gt;&lt;/b&gt; says that the next value to be printed is a signed integer value (i.e. a value that would be stored in a standard &lt;b&gt;&lt;tt&gt;int&lt;/tt&gt;&lt;/b&gt; variable) and this should be converted into a sequence of characters (i.e. digits) representing the value in decimal. If by some accident the variable that you are trying to display happens to be a &lt;b&gt;&lt;tt&gt;float&lt;/tt&gt;&lt;/b&gt; or a &lt;b&gt;&lt;tt&gt;double&lt;/tt&gt;&lt;/b&gt; then you will still see a value displayed - but it will not correspond to the actual value of the &lt;b&gt;&lt;tt&gt;float&lt;/tt&gt;&lt;/b&gt; or &lt;b&gt;&lt;tt&gt;double&lt;/tt&gt;&lt;/b&gt;.&lt;/p&gt;  &lt;p&gt;The reason for this is twofold.&lt;/p&gt;  &lt;ol&gt;&lt;li&gt;The first difference is that an &lt;b&gt;&lt;tt&gt;int&lt;/tt&gt;&lt;/b&gt; uses two bytes to store its value, while a &lt;b&gt;&lt;tt&gt;float&lt;/tt&gt;&lt;/b&gt; uses four and a double uses eight. If you try to display a &lt;b&gt;&lt;tt&gt;float&lt;/tt&gt;&lt;/b&gt; or a &lt;b&gt;&lt;tt&gt;double&lt;/tt&gt;&lt;/b&gt; using &lt;tt&gt;&lt;b&gt;%d&lt;/b&gt;&lt;/tt&gt; then only the first two bytes of the value are actually used.&lt;/li&gt;&lt;li&gt;The second problem is that even if there wasn't a size difference &lt;b&gt;&lt;tt&gt;int&lt;/tt&gt;&lt;/b&gt;s, &lt;b&gt;&lt;tt&gt;float&lt;/tt&gt;&lt;/b&gt;s and &lt;b&gt;&lt;tt&gt;double&lt;/tt&gt;&lt;/b&gt;s use a different binary representation and &lt;tt&gt;&lt;b&gt;%d&lt;/b&gt;&lt;/tt&gt; expects the bit pattern to be a simple signed binary integer.&lt;/li&gt;&lt;/ol&gt;  &lt;p&gt;This is all a bit technical, but that's in the nature of C. You can ignore these details as long as you remember two important facts:&lt;/p&gt;    &lt;ol&gt;&lt;li&gt;The specifier following &lt;b&gt;&lt;tt&gt;%&lt;/tt&gt;&lt;/b&gt; indicates the type of variable to be displayed as well as the format in which that the value should be displayed;&lt;/li&gt;&lt;li&gt;If you use a specifier with the wrong type of variable then you will see some strange things on the screen and the error often propagates to other items in the &lt;b&gt;&lt;tt&gt;printf&lt;/tt&gt;&lt;/b&gt; list.&lt;/li&gt;&lt;/ol&gt;  If this seems complicated then I would agree but I should also point out that the benefit is being able to treat what is stored in a variable in a more flexible way than other languages allow. Other languages never let on to the programmer that what is in fact stored in a variable is a bit pattern, not the decimal value that appears to be stored there when you use a &lt;b&gt;&lt;tt&gt;printf&lt;/tt&gt;&lt;/b&gt; (or whatever) statement. Of course whether you view this as an advantage depends on what you are trying to do. It certainly brings you closer to the way the machine works.   &lt;p&gt;You can also add an '&lt;b&gt;&lt;tt&gt;l&lt;/tt&gt;&lt;/b&gt;' in front of a specifier to mean a &lt;i&gt;long&lt;/i&gt; form of the variable type and &lt;b&gt;&lt;tt&gt;h&lt;/tt&gt;&lt;/b&gt; to indicate a short form (&lt;a href="http://www.le.ac.uk/cc/tutorials/c/ccccvar2.html"&gt;&lt;b&gt;&lt;tt&gt;long&lt;/tt&gt;&lt;/b&gt; and &lt;b&gt;&lt;tt&gt;short&lt;/tt&gt;&lt;/b&gt; will be covered later in this course&lt;/a&gt;). For example, &lt;b&gt;&lt;tt&gt;%ld&lt;/tt&gt;&lt;/b&gt; means a long integer variable (usually four bytes) and &lt;b&gt;&lt;tt&gt;%hd&lt;/tt&gt;&lt;/b&gt; means short &lt;tt&gt;&lt;b&gt;int&lt;/b&gt;&lt;/tt&gt;. Notice that there is no distinction between a four-byte &lt;b&gt;&lt;tt&gt;float&lt;/tt&gt;&lt;/b&gt; and an eight-byte &lt;b&gt;&lt;tt&gt;double&lt;/tt&gt;&lt;/b&gt;. The reason is that a &lt;tt&gt;&lt;b&gt;float&lt;/b&gt;&lt;/tt&gt; is automatically converted to a &lt;b&gt;&lt;tt&gt;double&lt;/tt&gt;&lt;/b&gt; precision value when passed to &lt;b&gt;&lt;tt&gt;printf&lt;/tt&gt;&lt;/b&gt; - so the two can be treated in the same way. (In pre-ANSI all &lt;tt&gt;&lt;b&gt;floats&lt;/b&gt;&lt;/tt&gt; were converted to &lt;b&gt;&lt;tt&gt;double&lt;/tt&gt;&lt;/b&gt; when passed to a function but this is no longer true.) The only real problem that this poses is how to print the value of a pointer? The answer is that you can use &lt;b&gt;&lt;tt&gt;%x&lt;/tt&gt;&lt;/b&gt; to see the address in hex or &lt;tt&gt;&lt;b&gt;%o&lt;/b&gt;&lt;/tt&gt; to see the address in octal. Notice that the value printed is the segment offset and not the absolute address - to understand what we am going on about you need to know something about the structure of your processor.&lt;/p&gt;  &lt;hr /&gt;  &lt;a name="specifiers"&gt;&lt;h3&gt;The % Format Specifiers&lt;/h3&gt;&lt;/a&gt;  The &lt;b&gt;&lt;tt&gt;%&lt;/tt&gt;&lt;/b&gt; specifiers that you can use in ANSI C are:   &lt;pre&gt;      &lt;b&gt;&lt;kbd&gt;Usual variable type           Display&lt;br /&gt;&lt;br /&gt;%c        char                     single character&lt;br /&gt;%d (%i)   int                      signed integer&lt;br /&gt;%e (%E)   float or double          exponential format&lt;br /&gt;%f        float or double          signed decimal&lt;br /&gt;%g (%G)   float or double          use %f or %e as required&lt;br /&gt;%o        int                      unsigned octal value&lt;br /&gt;%p        pointer                  address stored in pointer&lt;br /&gt;%s        array of char            sequence of characters&lt;br /&gt;%u        int                      unsigned decimal&lt;br /&gt;%x (%X)   int                      unsigned hex value&lt;br /&gt;&lt;/kbd&gt;&lt;/b&gt; &lt;/pre&gt;  &lt;a name="modifiers"&gt;&lt;/a&gt;   &lt;hr /&gt; &lt;h3&gt;Formatting Your Output&lt;/h3&gt;  The type conversion specifier only does what you ask of it - it convert a given bit pattern into a sequence of characters that a human can read. If you want to format the characters then you need to know a little more about the &lt;b&gt;&lt;tt&gt;printf&lt;/tt&gt;&lt;/b&gt; function's &lt;i&gt;control string&lt;/i&gt;.   &lt;p&gt;Each &lt;i&gt;specifier&lt;/i&gt; can be preceded by a &lt;i&gt;modifier&lt;/i&gt; which determines how the value will be printed. The most general modifier is of the form:&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;&lt;i&gt;flag width.precision&lt;/i&gt;&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;The &lt;i&gt;&lt;b&gt;&lt;tt&gt;flag&lt;/tt&gt;&lt;/b&gt;&lt;/i&gt; can be any of:&lt;/p&gt;  &lt;pre&gt;&lt;b&gt;&lt;kbd&gt;flag          meaning&lt;br /&gt;&lt;br /&gt; -         left justify&lt;br /&gt; +         always display sign&lt;br /&gt; space     display space if there is no sign&lt;br /&gt; 0         pad with leading zeros&lt;br /&gt; #         use alternate form of specifier&lt;br /&gt;&lt;/kbd&gt;&lt;/b&gt; &lt;/pre&gt;  The &lt;b&gt;&lt;i&gt;&lt;tt&gt;width&lt;/tt&gt;&lt;/i&gt;&lt;/b&gt; specifies the number of characters used in total to display the value and &lt;i&gt;&lt;b&gt;&lt;tt&gt;precision&lt;/tt&gt;&lt;/b&gt;&lt;/i&gt; indicates the number of characters used after the decimal point.   &lt;p&gt;For example, &lt;b&gt;&lt;tt&gt;%10.3f&lt;/tt&gt;&lt;/b&gt; will display the &lt;b&gt;&lt;tt&gt;float&lt;/tt&gt;&lt;/b&gt; using ten characters with three digits after the decimal point. Notice that the ten characters includes the decimal point, and a &lt;b&gt;&lt;tt&gt;-&lt;/tt&gt;&lt;/b&gt; sign if there is one. If the value needs more space than the &lt;i&gt;&lt;b&gt;&lt;tt&gt;width&lt;/tt&gt;&lt;/b&gt;&lt;/i&gt; specifies then the additional space is used - &lt;i&gt;&lt;b&gt;&lt;tt&gt;width&lt;/tt&gt;&lt;/b&gt;&lt;/i&gt; specifies the smallest space that will be used to display the value. (This is quiet reassuring, you won't be the first programmer whose program takes hours to run but the output results can't be viewed because the wrong format width has been specified!)&lt;/p&gt;  &lt;p&gt;The specifier &lt;b&gt;&lt;tt&gt;%-1Od&lt;/tt&gt;&lt;/b&gt; will display an &lt;b&gt;&lt;tt&gt;int&lt;/tt&gt;&lt;/b&gt; left justified in a ten character space. The specifier &lt;b&gt;&lt;tt&gt;%+5d&lt;/tt&gt;&lt;/b&gt; will display an &lt;b&gt;&lt;tt&gt;int&lt;/tt&gt;&lt;/b&gt; using the next five character locations and will add a &lt;b&gt;&lt;tt&gt;+&lt;/tt&gt;&lt;/b&gt; or &lt;b&gt;&lt;tt&gt;-&lt;/tt&gt;&lt;/b&gt; sign to the value.&lt;/p&gt;  &lt;p&gt;The only complexity is the use of the &lt;b&gt;&lt;tt&gt;#&lt;/tt&gt;&lt;/b&gt; modifier. What this does depends on which type of format it is used with:&lt;/p&gt;  &lt;pre&gt;&lt;kbd&gt;&lt;br /&gt;&lt;b&gt;%#o    adds a leading 0 to the octal value&lt;br /&gt;%#x    adds a leading 0x to the hex value&lt;br /&gt;%#f or&lt;br /&gt;%#e    ensures decimal point is printed&lt;br /&gt;%#g    displays trailing zeros&lt;br /&gt;&lt;/b&gt; &lt;/kbd&gt;&lt;/pre&gt;  &lt;p&gt;Strings will be discussed later but for now remember: if you print a &lt;b&gt;&lt;i&gt;string&lt;/i&gt;&lt;/b&gt; using the &lt;b&gt;&lt;tt&gt;%s&lt;/tt&gt;&lt;/b&gt; specifier then all of the characters stored in the array up to the first &lt;i&gt;null&lt;/i&gt; will be printed. If you use a &lt;i&gt;&lt;b&gt;&lt;tt&gt;width&lt;/tt&gt;&lt;/b&gt;&lt;/i&gt; specifier then the &lt;b&gt;&lt;i&gt;string&lt;/i&gt;&lt;/b&gt; will be right justified within the space. If you include a &lt;i&gt;&lt;b&gt;&lt;tt&gt;precision&lt;/tt&gt;&lt;/b&gt;&lt;/i&gt; specifier then only that number of characters will be printed.&lt;/p&gt;  &lt;p&gt;For example:&lt;/p&gt;  &lt;p&gt;&lt;tt&gt;&lt;b&gt;printf("%s,Hello")&lt;/b&gt;&lt;/tt&gt;&lt;/p&gt;  &lt;p&gt;will print &lt;b&gt;&lt;tt&gt;Hello&lt;/tt&gt;&lt;/b&gt;,&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;printf("%25s ,Hello")&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;will print 25 characters with &lt;b&gt;&lt;tt&gt;Hello&lt;/tt&gt;&lt;/b&gt; right justified and&lt;/p&gt;  &lt;p&gt;&lt;tt&gt;&lt;b&gt;printf("%25.3s,Hello")&lt;/b&gt;&lt;/tt&gt;&lt;/p&gt;  &lt;p&gt;will print &lt;b&gt;&lt;tt&gt;Hello&lt;/tt&gt;&lt;/b&gt; right justified in a group of 25 spaces.&lt;/p&gt;  &lt;p&gt;Also notice that it is fine to pass a constant value to &lt;b&gt;&lt;tt&gt;printf&lt;/tt&gt;&lt;/b&gt; as in &lt;tt&gt;&lt;b&gt;printf("%s,Hello")&lt;/b&gt;&lt;/tt&gt;.&lt;/p&gt;  &lt;p&gt;Finally there are the &lt;i&gt;control&lt;/i&gt; codes:&lt;/p&gt;  &lt;pre&gt;&lt;kbd&gt;&lt;br /&gt;&lt;b&gt;\b       backspace&lt;br /&gt;\f       formfeed&lt;br /&gt;\n       new line&lt;br /&gt;\r       carriage return&lt;br /&gt;\t       horizontal tab&lt;br /&gt;\'       single quote&lt;br /&gt;\0       null&lt;br /&gt;&lt;/b&gt; &lt;/kbd&gt;&lt;/pre&gt;  If you include any of these in the control string then the corresponding ASCII control code is sent to the screen, or output device, which should produce the effect listed. In most cases you only need to remember &lt;b&gt;&lt;tt&gt;\n&lt;/tt&gt;&lt;/b&gt; for new line.   &lt;hr /&gt;  &lt;a name="scanf"&gt;&lt;h3&gt;&lt;b&gt;&lt;tt&gt;scanf&lt;/tt&gt;&lt;/b&gt;&lt;/h3&gt;&lt;/a&gt;  Now that we have mastered the intricacies of &lt;b&gt;&lt;tt&gt;printf&lt;/tt&gt;&lt;/b&gt; you should find &lt;b&gt;&lt;tt&gt;scanf&lt;/tt&gt;&lt;/b&gt; very easy. The &lt;b&gt;&lt;tt&gt;scanf&lt;/tt&gt;&lt;/b&gt; function works in much the same way as the &lt;b&gt;&lt;tt&gt;printf&lt;/tt&gt;&lt;/b&gt;. That is it has the general form:   &lt;p&gt;&lt;b&gt;&lt;tt&gt;scanf(&lt;i&gt;control string,variable,variable,...&lt;/i&gt;)&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;In this case the &lt;i&gt;&lt;b&gt;&lt;tt&gt;control string&lt;/tt&gt;&lt;/b&gt;&lt;/i&gt; specifies how strings of characters, usually typed on the keyboard, should be converted into values and stored in the listed variables. However there are a number of important differences as well as similarities between &lt;b&gt;&lt;tt&gt;scanf&lt;/tt&gt;&lt;/b&gt; and &lt;b&gt;&lt;tt&gt;printf&lt;/tt&gt;&lt;/b&gt;.&lt;/p&gt;  &lt;p&gt;The most obvious is that &lt;b&gt;&lt;tt&gt;scanf&lt;/tt&gt;&lt;/b&gt; has to change the values stored in the parts of computers memory that is associated with parameters (variables).&lt;/p&gt;  &lt;p&gt;To understand this fully you will have to wait until we have covered functions in more detail. But, just for now, bare with us when we say to do this the &lt;b&gt;&lt;tt&gt;scanf&lt;/tt&gt;&lt;/b&gt; function has to have the &lt;i&gt;addresses&lt;/i&gt; of the variables rather than just their values. This means that simple variables have to be passed with a preceding &lt;b tt=""&gt;&gt;&amp;amp;&lt;/b&gt;. &lt;i&gt;(&lt;b&gt;Note for future reference:&lt;/b&gt; There is no need to do this for strings stored in arrays because the array name is already a pointer.)&lt;/i&gt;&lt;/p&gt;  &lt;p&gt;The second difference is that the &lt;i&gt;&lt;b&gt;&lt;tt&gt;control string&lt;/tt&gt;&lt;/b&gt;&lt;/i&gt; has some extra items to cope with the problems of reading data in. However, all of the conversion specifiers listed in connection with &lt;b&gt;&lt;tt&gt;printf&lt;/tt&gt;&lt;/b&gt; can be used with &lt;tt&gt;&lt;b&gt;scanf&lt;/b&gt;&lt;/tt&gt;.&lt;/p&gt;  &lt;p&gt;The rule is that &lt;tt&gt;&lt;b&gt;scanf&lt;/b&gt;&lt;/tt&gt; processes the control string from left to right and each time it reaches a specifier it tries to interpret what has been typed as a value. If you input multiple values then these are assumed to be separated by white space - i.e. spaces, newline or tabs. This means you can type:&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;3 4 5&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;or&lt;/p&gt;  &lt;pre&gt;&lt;b&gt;3&lt;br /&gt;4&lt;br /&gt;5&lt;br /&gt;&lt;/b&gt; &lt;/pre&gt;  &lt;p&gt;and it doesn't matter how many spaces are included between items. For example:&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;scanf("%d %d",&amp;amp;i,&amp;amp;j);&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;will read in two integer values into &lt;b&gt;&lt;tt&gt;i&lt;/tt&gt;&lt;/b&gt; and &lt;b&gt;&lt;tt&gt;j&lt;/tt&gt;&lt;/b&gt;. The integer values can be typed on the same line or on different lines as long as there is at least one white space character between them.&lt;/p&gt;  &lt;p&gt;The only exception to this rule is the &lt;b&gt;&lt;tt&gt;%c&lt;/tt&gt;&lt;/b&gt; specifier which always reads in the next character typed no matter what it is. You can also use a &lt;i&gt;&lt;b&gt;&lt;tt&gt;width&lt;/tt&gt;&lt;/b&gt;&lt;/i&gt; modifier in &lt;tt&gt;&lt;b&gt;scanf&lt;/b&gt;&lt;/tt&gt;. In this case its effect is to limit the number of characters accepted to the &lt;i&gt;&lt;b&gt;&lt;tt&gt;width&lt;/tt&gt;&lt;/b&gt;&lt;/i&gt;.&lt;/p&gt;  &lt;p&gt;For example:&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;scanf("%lOd",&amp;amp;i)&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;would use at most the first ten digits typed as the new value for &lt;b&gt;&lt;tt&gt;i&lt;/tt&gt;&lt;/b&gt;.&lt;/p&gt;  &lt;p&gt;There is one main problem with &lt;tt&gt;&lt;b&gt;scanf&lt;/b&gt;&lt;/tt&gt; function which can make it unreliable in certain cases. The reason being is that &lt;tt&gt;&lt;b&gt;scanf&lt;/b&gt;&lt;/tt&gt; tends to ignore white spaces, i.e. the space character. If you require your input to contain spaces this can cause a problem. Therefore for &lt;i&gt;string&lt;/i&gt; data input the function &lt;b&gt;&lt;tt&gt;getstr()&lt;/tt&gt;&lt;/b&gt; may well be more reliable as it records spaces in the input text and treats them as an ordinary characters.&lt;/p&gt;  &lt;hr /&gt;  &lt;h3&gt;Custom Libraries&lt;/h3&gt;  If you think &lt;b&gt;&lt;tt&gt;printf&lt;/tt&gt;&lt;/b&gt; and &lt;tt&gt;&lt;b&gt;scanf&lt;/b&gt;&lt;/tt&gt; don't seem enough to do the sort of job that any modern programmer expects to do, you would be right. In the early days being able to print a line at a time was fine but today we expect to be able to print anywhere on the screen at any time.   &lt;p&gt;The point is that as far as standard C goes simple I/O devices are &lt;i&gt;stream-oriented&lt;/i&gt; - that is you send or get a stream of characters without any notion of being able to move the current position in the &lt;i&gt;stream&lt;/i&gt;. If you want to move backwards and forwards through the data then you need to use a &lt;i&gt;direct access file&lt;/i&gt;. In more simple terms, C doesn't have a Tab(X,Y) or Locate(X,Y) function or command which moves the cursor to the specified location! How are you ever going to write your latest block buster game, let alone build your sophisticated input screens?&lt;/p&gt;  &lt;p&gt;Well you don't have to worry too much because although C may not define them as standard, all C implementations come with an extensive graphics/text function library that allows you to do all of this and more. Such a library isn't standard, however the principles are always the same. The Borland and Microsoft offerings are usually considered as the two facto standards.&lt;/p&gt;  &lt;hr /&gt;   &lt;h3&gt;Summing It Up&lt;/h3&gt;  Now that we have arithmetic, a way of reading values in and a way of displaying them, it's possible to write a slightly more interesting program than "Hello World". Not much more interesting, it's true, but what do you expect with two instructions and some arithmetic?   &lt;p&gt;Let's write a program that adds two numbers together and prints the result. (I told you it wasn't that much more interesting!) Of course, if you want to work out something else like Fahrenheit to centigrade, inches to centimetres or the size of your bank balance, then that's up to you - the principle is the same.&lt;/p&gt;  &lt;p&gt;The program is a bit more complicated than you might expect, but only because of the need to let the user know what is happening:&lt;/p&gt;    &lt;pre&gt;&lt;kbd&gt;&lt;br /&gt;&lt;b&gt;#include &lt;stdio.h&gt;&lt;br /&gt;main()&lt;br /&gt;{&lt;br /&gt; int a,b,c;&lt;br /&gt; printf("\nThe first number is ");&lt;br /&gt; scanf("%d",&amp;amp;a);&lt;br /&gt; printf("The second number is ");&lt;br /&gt; scanf("%d",&amp;amp;b);&lt;br /&gt; c=a+b;&lt;br /&gt; printf("The answer is %d \n",c);&lt;br /&gt;}&lt;br /&gt;&lt;/stdio.h&gt;&lt;/b&gt; &lt;/kbd&gt;&lt;/pre&gt;  &lt;p&gt;[&lt;a href="http://www.le.ac.uk/cc/tutorials/c/programs/scanf.c"&gt;program&lt;/a&gt;]&lt;/p&gt;  &lt;p&gt;The first instruction declares three integer variables: &lt;b&gt;&lt;tt&gt;a&lt;/tt&gt;&lt;/b&gt;, &lt;b&gt;&lt;tt&gt;b&lt;/tt&gt;&lt;/b&gt; and &lt;b&gt;&lt;tt&gt;c&lt;/tt&gt;&lt;/b&gt;. The first two &lt;b&gt;&lt;tt&gt;printf&lt;/tt&gt;&lt;/b&gt; statements simply display message on the screen asking the user for the values. The &lt;tt&gt;&lt;b&gt;scanf&lt;/b&gt;&lt;/tt&gt; functions then read in the values from the keyboard into &lt;b&gt;&lt;tt&gt;a&lt;/tt&gt;&lt;/b&gt; and &lt;b&gt;&lt;tt&gt;b&lt;/tt&gt;&lt;/b&gt;. These are added together and the result in &lt;b&gt;&lt;tt&gt;c&lt;/tt&gt;&lt;/b&gt; is displayed on the screen with a suitable message. Notice the way that you can include a message in the &lt;tt&gt;&lt;b&gt;printf&lt;/b&gt;&lt;/tt&gt; statement along with the value.&lt;/p&gt;  &lt;p&gt;Type the program in, compile it and link it and the result should be your first interactive program. Try changing it so that it works out something a little more adventurous. Try changing the messages as well. All you have to remember is that you cannot store values or work out results greater than the range of an integer variable or with a fractional part.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5670527689344996196-8061669821384504061?l=rao-programmingofc.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://rao-programmingofc.blogspot.com/feeds/8061669821384504061/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5670527689344996196&amp;postID=8061669821384504061' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5670527689344996196/posts/default/8061669821384504061'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5670527689344996196/posts/default/8061669821384504061'/><link rel='alternate' type='text/html' href='http://rao-programmingofc.blogspot.com/2008/02/input-and-output-functions.html' title='Input and Output Functions'/><author><name>Ramjaan</name><uri>http://www.blogger.com/profile/07862712225011522143</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='http://1.bp.blogspot.com/_uyQpXxI_GeY/SQy7Z8dn6II/AAAAAAAAACI/94M3auWVt4A/S220/images.jpeg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5670527689344996196.post-2974975835329526125</id><published>2008-02-08T02:25:00.000-08:00</published><updated>2008-02-08T02:26:58.439-08:00</updated><title type='text'>Data Types</title><content type='html'>&lt;h1&gt;                            Data Types&lt;/h1&gt;&lt;br /&gt;&lt;h2&gt;Objectives&lt;/h2&gt;  &lt;p&gt;Having read this section you should be able to:&lt;/p&gt;  &lt;ol&gt;&lt;li&gt;declare (name) a local variable as being one of C's five data types&lt;/li&gt;&lt;li&gt;initialise local variables&lt;/li&gt;&lt;li&gt;perform simple arithmetic using local variables&lt;/li&gt;&lt;/ol&gt;  &lt;hr /&gt; &lt;p&gt;Now we have to start looking into the details of the C language. How easy you find the rest of this section will depend on whether you have ever programmed before - no matter what the language was. There are a great many ideas common to programming in any language and C is no exception to this rule.&lt;/p&gt;  &lt;p&gt;So if you haven't programmed before, you need to take the rest of this section slowly and keep going over it until it makes sense. If, on the other hand, you have programmed before you'll be wondering what all the fuss is about It's a lot like being able to ride a bike!&lt;/p&gt;  &lt;p&gt;The first thing you need to know is that you can create &lt;i&gt;variables&lt;/i&gt; to store &lt;i&gt;values&lt;/i&gt; in. A variable is just a named area of storage that can hold a single value (numeric or character). C is very fussy about how you create variables and what you store in them. It demands that you declare the name of each variable that you are going to use and its &lt;i&gt;type&lt;/i&gt;, or &lt;i&gt;class&lt;/i&gt;, before you actually try to do anything with it.&lt;/p&gt;  &lt;p&gt;In this section we are only going to be discussing &lt;i&gt;local&lt;/i&gt; variables. These are variables that are used within the current program unit (or function) in a later section we will looking at &lt;i&gt;global&lt;/i&gt; variables - variables that are available to all the program's functions.&lt;/p&gt;  &lt;hr /&gt; &lt;p&gt;There are five basic data types associated with variables:&lt;/p&gt;    &lt;ul&gt;&lt;li&gt;&lt;b&gt;int&lt;/b&gt; - integer: a whole number.&lt;/li&gt;&lt;li&gt;&lt;b&gt;float&lt;/b&gt; - floating point value: ie a number with a fractional part.&lt;/li&gt;&lt;li&gt;&lt;b&gt;double&lt;/b&gt; - a double-precision floating point value.&lt;/li&gt;&lt;li&gt;&lt;b&gt;char&lt;/b&gt; - a single character.&lt;/li&gt;&lt;li&gt;&lt;b&gt;void&lt;/b&gt; - valueless special purpose type which we will examine closely in later sections.&lt;/li&gt;&lt;/ul&gt;  &lt;p&gt;One of the confusing things about the C language is that the range of values and the amount of storage that each of these types takes is not defined. This is because in each case the 'natural' choice is made for each type of machine. You can call &lt;i&gt;variables&lt;/i&gt; what you like, although it helps if you give them sensible names that give you a hint of what they're being used for - names like &lt;tt&gt;sum&lt;/tt&gt;, &lt;tt&gt;total&lt;/tt&gt;, &lt;tt&gt;average&lt;/tt&gt; and so on. If you are translating a formula then use variable names that reflect the elements used in the formula. For example, &lt;tt&gt;2&lt;span style="font-size: 12pt; font-family: Symbol;"&gt;p&lt;/span&gt;r&lt;/tt&gt; (that should read as "2 pi r" but that depends upon how your browser has been set-up) would give local variables names of &lt;b&gt;&lt;tt&gt;pi&lt;/tt&gt;&lt;/b&gt; and &lt;b&gt;&lt;tt&gt;r&lt;/tt&gt;&lt;/b&gt;. Remember, C programmers tend to prefer short names!&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Note:&lt;/b&gt; &lt;i&gt;all C's variables must begin with a letter or a "_" (underscore) character&lt;/i&gt;.&lt;/p&gt;  &lt;hr /&gt;  &lt;a name="IN"&gt;&lt;h3&gt;Integer Number Variables&lt;/h3&gt;&lt;/a&gt;  The first type of variable we need to know about is of class type &lt;b&gt;&lt;tt&gt;int&lt;/tt&gt;&lt;/b&gt; - short for integer. An &lt;b&gt;&lt;tt&gt;int&lt;/tt&gt;&lt;/b&gt; variable can store a value in the range -32768 to +32767. You can think of it as a largish positive or negative whole number: no fractional part is allowed. To declare an &lt;b&gt;&lt;tt&gt;int&lt;/tt&gt;&lt;/b&gt; you use the instruction:  &lt;p&gt;&lt;b&gt;&lt;tt&gt;int &lt;i&gt;variable name&lt;/i&gt;;&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;For example:&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;int a;&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;declares that you want to create an &lt;b&gt;&lt;tt&gt;int&lt;/tt&gt;&lt;/b&gt; variable called &lt;b&gt;&lt;tt&gt;a&lt;/tt&gt;&lt;/b&gt;.&lt;/p&gt;  &lt;p&gt;To assign a value to our integer variable we would use the following C statement:&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;a=10;&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;The C programming language uses the "=" character for &lt;i&gt;assignment&lt;/i&gt;. A statement of the form &lt;b&gt;&lt;tt&gt;a=10;&lt;/tt&gt;&lt;/b&gt; should be interpreted as &lt;i&gt;take the numerical value 10 and store it in a memory location associated with the integer variable a&lt;/i&gt;. The "=" character should not be seen as an equality otherwise writing statements of the form:&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;a=a+10;&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;will get mathematicians blowing fuses! This statement should be interpreted as &lt;i&gt;take the current value stored in a memory location associated with the integer variable a; add the numerical value 10 to it and then replace this value in the memory location associated with a&lt;/i&gt;.&lt;/p&gt;  &lt;hr /&gt;  &lt;a name="DN"&gt;&lt;h3&gt;Decimal Number Variables&lt;/h3&gt;&lt;/a&gt;  As described above, an integer variable has no fractional part. Integer variables tend to be used for counting, whereas &lt;i&gt;real&lt;/i&gt; numbers are used in arithmetic. C uses one of two keywords to declare a variable that is to be associated with a decimal number: &lt;b&gt;&lt;tt&gt;float&lt;/tt&gt;&lt;/b&gt; and &lt;b&gt;&lt;tt&gt;double&lt;/tt&gt;&lt;/b&gt;. They are each offer a different level of precision as outlined below.   &lt;dl&gt;&lt;dt&gt;&lt;b&gt;float&lt;/b&gt;&lt;/dt&gt;&lt;dd&gt;A float, or floating point, number has about seven digits of precision and a range of about 1.E-36 to 1.E+36. A float takes four bytes to store.&lt;/dd&gt;&lt;dt&gt;&lt;b&gt;double&lt;/b&gt;&lt;/dt&gt;&lt;dd&gt;A double, or double precision, number has about 13 digits of precision and a range of about 1.E-303 to 1.E+303. A double takes eight bytes to store.&lt;/dd&gt;&lt;/dl&gt;  &lt;p&gt;For example:&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;float total;&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;double sum;&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;To assign a numerical value to our floating point and double precision variables we would use the following C statement:&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;total=0.0;&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;sum=12.50;&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;hr /&gt;   &lt;a name="CV"&gt;&lt;h3&gt;Character Variables&lt;/h3&gt;&lt;/a&gt;  C only has a concept of numbers and characters. It very often comes as a surprise to some programmers who learnt a beginner's language such as BASIC that C has no understanding of &lt;i&gt;strings&lt;/i&gt; but a string is only an &lt;i&gt;array&lt;/i&gt; of characters and C does have a concept of arrays which we shall be meeting later in this course.   &lt;p&gt;To declare a variable of type character we use the keyword &lt;tt&gt;&lt;b&gt;char&lt;/b&gt;&lt;/tt&gt;. - A single character stored in one byte.&lt;/p&gt;  &lt;p&gt;For example:&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;char c;&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;To assign, or store, a character value in a &lt;tt&gt;&lt;b&gt;char&lt;/b&gt;&lt;/tt&gt; data type is easy - a character variable is just a symbol enclosed by single quotes. For example, if &lt;b&gt;&lt;tt&gt;c&lt;/tt&gt;&lt;/b&gt; is a &lt;tt&gt;&lt;b&gt;char&lt;/b&gt;&lt;/tt&gt; variable you can store the letter &lt;b&gt;A&lt;/b&gt; in it using the following C statement:&lt;/p&gt;  &lt;p&gt;&lt;tt&gt;&lt;b&gt;&lt;kbd&gt;c='A'&lt;/kbd&gt;&lt;/b&gt;&lt;/tt&gt;&lt;/p&gt;  &lt;p&gt;Notice that you can only store a single character in a &lt;b&gt;&lt;tt&gt;&lt;kbd&gt;char&lt;/kbd&gt;&lt;/tt&gt;&lt;/b&gt; variable. Later we will be discussing using character strings, which has a very real potential for confusion because a string constant is written between double quotes. But for the moment remember that a &lt;tt&gt;&lt;b&gt;char&lt;/b&gt;&lt;/tt&gt; variable is &lt;b&gt;'A'&lt;/b&gt; and not &lt;b&gt;"A"&lt;/b&gt;.&lt;/p&gt;  &lt;hr /&gt;   &lt;a name="AS"&gt;&lt;h3&gt;Assignment Statement&lt;/h3&gt;&lt;/a&gt;  Once you've declared a variable you can use it, but not until it has been declared - attempts to use a variable that has not been defined will cause a compiler error. Using a variable means storing something in it. You can store a value in a variable using:  &lt;p&gt;&lt;b&gt;&lt;tt&gt;name = value;&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;For example:&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;a=10;&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;stores the value &lt;b&gt;&lt;tt&gt;10&lt;/tt&gt;&lt;/b&gt; in the &lt;b&gt;&lt;tt&gt;int&lt;/tt&gt;&lt;/b&gt; variable &lt;b&gt;&lt;tt&gt;a&lt;/tt&gt;&lt;/b&gt;. What could be simpler? Not much, but it isn't actually very useful! Who wants to store a known value like 10 in a variable so you can use it later? It is 10, always was 10 and always will be 10. What makes variables useful is that you can use them to store the result of some arithmetic.&lt;/p&gt;  &lt;p&gt;Consider four very simple mathematical operations: add, subtract, multiply and divide. Let us see how C would use these operations on two float variables &lt;tt&gt;&lt;b&gt;a&lt;/b&gt;&lt;/tt&gt; and &lt;tt&gt;&lt;b&gt;b&lt;/b&gt;&lt;/tt&gt;.&lt;/p&gt;  &lt;dl&gt;&lt;dt&gt;add&lt;/dt&gt;&lt;dd&gt;&lt;tt&gt;&lt;b&gt;a+b&lt;/b&gt;&lt;/tt&gt;&lt;/dd&gt;&lt;dt&gt;subtract&lt;/dt&gt;&lt;dd&gt;&lt;tt&gt;&lt;b&gt;a-b&lt;/b&gt;&lt;/tt&gt;&lt;/dd&gt;&lt;dt&gt;multiply&lt;/dt&gt;&lt;dd&gt;&lt;tt&gt;&lt;b&gt;a*b&lt;/b&gt;&lt;/tt&gt;&lt;/dd&gt;&lt;dt&gt;divide&lt;/dt&gt;&lt;dd&gt;&lt;tt&gt;&lt;b&gt;a/b&lt;/b&gt;&lt;/tt&gt;&lt;/dd&gt;&lt;/dl&gt;  Note that we have used the following characters from C's &lt;a href="http://www.le.ac.uk/cc/tutorials/c/ccccchar.html"&gt;character set&lt;/a&gt;:   &lt;pre&gt;&lt;b&gt;&lt;kbd&gt;+     for add&lt;br /&gt;-     for subtract&lt;br /&gt;*     for multiply&lt;br /&gt;/     for divide&lt;/kbd&gt;&lt;/b&gt; &lt;/pre&gt;  BE CAREFUL WITH ARITHMETIC!!! What is the answer to this simple calculation?   &lt;p&gt;&lt;tt&gt;&lt;b&gt;a=10/3&lt;/b&gt;&lt;/tt&gt;&lt;/p&gt;  &lt;p&gt;The answer depends upon how &lt;tt&gt;&lt;b&gt;a&lt;/b&gt;&lt;/tt&gt; was declared. If it was declared as type &lt;tt&gt;&lt;b&gt;int&lt;/b&gt;&lt;/tt&gt; the answer will be 3; if &lt;tt&gt;&lt;b&gt;a&lt;/b&gt;&lt;/tt&gt; is of type &lt;tt&gt;&lt;b&gt;float&lt;/b&gt;&lt;/tt&gt; then the answer will be 3.333. It is left as an exercise to the reader to find out the answer for &lt;tt&gt;&lt;b&gt;a&lt;/b&gt;&lt;/tt&gt; of type &lt;tt&gt;&lt;b&gt;char&lt;/b&gt;&lt;/tt&gt;.&lt;/p&gt;  &lt;p&gt;Two points to note from the above calculation:&lt;/p&gt;  &lt;ol&gt;&lt;li&gt;C ignores fractions when doing integer division!&lt;/li&gt;&lt;li&gt;when doing &lt;tt&gt;&lt;b&gt;float&lt;/b&gt;&lt;/tt&gt; calculations integers will be converted into &lt;tt&gt;&lt;b&gt;float&lt;/b&gt;&lt;/tt&gt;. We will see later how C handles type conversions.&lt;/li&gt;&lt;/ol&gt;  &lt;hr /&gt;  &lt;a name="AO"&gt;&lt;h3&gt;Arithmetic Ordering&lt;/h3&gt;&lt;/a&gt;  Whilst we are dealing with arithmetic we want to remind you about something that everyone learns at junior school but then we forget it. Consider the following calculation:   &lt;p&gt;&lt;tt&gt;&lt;b&gt;a=10.0 + 2.0 * 5.0 - 6.0 / 2.0&lt;/b&gt;&lt;/tt&gt;&lt;/p&gt;  &lt;p&gt;What is the answer? If you think its 27 go to the bottom of the class! Perhaps you got that answer by following each instruction as if it was being typed into a calculator. A computer doesn't work like that and it has its own set of rules when performing an arithmetic calculation. All mathematical operations form a hierarchy which is shown &lt;a href="http://www.le.ac.uk/cc/tutorials/c/ccccchar.html#arithmetic"&gt;here&lt;/a&gt;. In the above calculation the multiplication and division parts will be evaluated first and then the addition and subtraction parts. This gives an answer of 17.&lt;/p&gt;  &lt;p&gt;&lt;i&gt;&lt;b&gt;Note:&lt;/b&gt; To avoid confusion use brackets&lt;/i&gt;. The following are two different calculations:&lt;/p&gt;  &lt;p&gt;&lt;tt&gt;&lt;b&gt;a=10.0 + (2.0 * 5.0) - (6.0 / 2.0)&lt;/b&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;b&gt;a=(10.0 + 2.0) * (5.0 - 6.0) / 2.0&lt;/b&gt;&lt;/tt&gt;&lt;/p&gt;  &lt;p&gt;You can freely mix &lt;b&gt;&lt;tt&gt;int&lt;/tt&gt;&lt;/b&gt;, &lt;b&gt;&lt;tt&gt;float&lt;/tt&gt;&lt;/b&gt; and &lt;b&gt;&lt;tt&gt;double&lt;/tt&gt;&lt;/b&gt; variables in expressions. In nearly all cases the lower precision values are converted to the highest precision values used in the expression. For example, the expression &lt;b&gt;&lt;tt&gt;f*i&lt;/tt&gt;&lt;/b&gt;, where &lt;b&gt;&lt;tt&gt;f&lt;/tt&gt;&lt;/b&gt; is a &lt;b&gt;&lt;tt&gt;float&lt;/tt&gt;&lt;/b&gt; and &lt;b&gt;&lt;tt&gt;i&lt;/tt&gt;&lt;/b&gt; is an &lt;b&gt;&lt;tt&gt;int&lt;/tt&gt;&lt;/b&gt;, is evaluated by converting the &lt;b&gt;&lt;tt&gt;int&lt;/tt&gt;&lt;/b&gt; to a &lt;b&gt;&lt;tt&gt;float&lt;/tt&gt;&lt;/b&gt; and then multiplying. The final result is, of course, a &lt;b&gt;&lt;tt&gt;float&lt;/tt&gt;&lt;/b&gt; but this may be assigned to another data type and the conversion will be made automatically. If you assign to a lower precision type then the value is truncated and not rounded. In other words, in nearly all cases you can ignore the problems of converting between types.&lt;/p&gt;  &lt;p&gt;This is very reasonable but more surprising is the fact that the data type &lt;tt&gt;&lt;b&gt;char&lt;/b&gt;&lt;/tt&gt; can also be freely mixed with &lt;b&gt;&lt;tt&gt;ints&lt;/tt&gt;&lt;/b&gt;, &lt;b&gt;&lt;tt&gt;floats&lt;/tt&gt;&lt;/b&gt; and &lt;b&gt;&lt;tt&gt;doubles&lt;/tt&gt;&lt;/b&gt;. This will shock any programmer who has used another language, as it's another example of C getting us closer than is customary to the way the machine works. A character is represented as an &lt;a href="http://www.le.ac.uk/cc/glossary/ccgla.html#33"&gt;ASCII&lt;/a&gt; or some other code in the range O to 255, and if you want you can use this integer code value in arithmetic. Another way of thinking about this is that a &lt;b&gt;&lt;tt&gt;char&lt;/tt&gt;&lt;/b&gt; variable is just a single-byte integer variable that can hold a number in the range O to 255, which can optionally be interpreted as a character. Notice, however, that C gives you access to memory in the smallest chunks your machine works with, i.e. one byte at a time, with no overheads.&lt;/p&gt;  &lt;hr /&gt;   &lt;a name="SD"&gt;&lt;h3&gt;Something To Declare&lt;/h3&gt;&lt;/a&gt;  Before you can use a variable you have to declare it. As we have seen above, to do this you state its &lt;i&gt;type&lt;/i&gt; and then give its &lt;i&gt;name&lt;/i&gt;. For example, &lt;b&gt;&lt;tt&gt;int i;&lt;/tt&gt;&lt;/b&gt; declares an integer variable. You can declare any number of variables of the same type with a single statement. For example:  &lt;p&gt;&lt;b&gt;&lt;tt&gt;int a, b, c;&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;declares three integers: &lt;b&gt;&lt;tt&gt;a&lt;/tt&gt;&lt;/b&gt;, &lt;b&gt;&lt;tt&gt;b&lt;/tt&gt;&lt;/b&gt; and &lt;b&gt;&lt;tt&gt;c&lt;/tt&gt;&lt;/b&gt;. You have to declare all the variables that you want to use at the start of the program. Later you will discover that exactly where you declare a variable makes a difference, but for now you should put variable declarations after the opening curly bracket of the &lt;b&gt;&lt;tt&gt;main&lt;/tt&gt;&lt;/b&gt; program.&lt;/p&gt;    &lt;p&gt;Here is an example program that includes some of the concepts outlined above. It includes a slightly more advanced use of the &lt;b&gt;&lt;tt&gt;printf&lt;/tt&gt;&lt;/b&gt; function which will covered in detail in the next part of this course:&lt;/p&gt;  &lt;pre&gt;/*&lt;br /&gt;/*&lt;br /&gt;   Program#int.c&lt;br /&gt;&lt;br /&gt;   Another simple program&lt;br /&gt;   using int and printf&lt;br /&gt;*/&lt;br /&gt;&lt;br /&gt;#include &lt;stdio.h&gt;&lt;br /&gt;&lt;br /&gt;main()&lt;br /&gt;{&lt;br /&gt;   int a,b,average;&lt;br /&gt;   a=10;&lt;br /&gt;   b=6;&lt;br /&gt;   average = ( a+b ) / 2 ;&lt;br /&gt;   printf("Here ");&lt;br /&gt;   printf("is ");&lt;br /&gt;   printf("the ");&lt;br /&gt;   printf("answer... ");&lt;br /&gt;   printf("\n");&lt;br /&gt;   printf("%d.",average);&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;  [&lt;a href="http://www.le.ac.uk/cc/tutorials/c/programs/int.c"&gt;program&lt;/a&gt;]   &lt;hr /&gt; &lt;h3&gt;More On Initialising Variables&lt;/h3&gt;  You can assign an initial value to a variable when you declare it. For example:  &lt;p&gt;&lt;b&gt;&lt;tt&gt;int i=1;&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;sets the &lt;b&gt;&lt;tt&gt;int&lt;/tt&gt;&lt;/b&gt; variable to one as soon as it's created. This is just the same as:&lt;/p&gt;    &lt;pre&gt;&lt;b&gt;int i;&lt;br /&gt;i=l;&lt;br /&gt;&lt;/b&gt; &lt;/pre&gt;  but the compiler may be able to speed up the operation if you initialise the variable as part of its declaration. Don't assume that an uninitialised variable has a sensible value stored in it. Some C compilers store 0 in newly created numeric variables but nothing in the C language compels them to do so.   &lt;hr /&gt; &lt;h3&gt;Summary&lt;/h3&gt;  Variable names:  &lt;ul&gt;&lt;li&gt;should be lowercase for local variables&lt;/li&gt;&lt;li&gt;should be UPPERCASE for symbolic constants (to be discussed later)&lt;/li&gt;&lt;li&gt;only the first 31 characters of a variables name are significant&lt;/li&gt;&lt;li&gt;must begin with a letter or _ (under score) character&lt;/li&gt;&lt;/ul&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5670527689344996196-2974975835329526125?l=rao-programmingofc.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://rao-programmingofc.blogspot.com/feeds/2974975835329526125/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5670527689344996196&amp;postID=2974975835329526125' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5670527689344996196/posts/default/2974975835329526125'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5670527689344996196/posts/default/2974975835329526125'/><link rel='alternate' type='text/html' href='http://rao-programmingofc.blogspot.com/2008/02/data-types.html' title='Data Types'/><author><name>Ramjaan</name><uri>http://www.blogger.com/profile/07862712225011522143</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='http://1.bp.blogspot.com/_uyQpXxI_GeY/SQy7Z8dn6II/AAAAAAAAACI/94M3auWVt4A/S220/images.jpeg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5670527689344996196.post-4364402590224902684</id><published>2008-02-08T02:21:00.000-08:00</published><updated>2008-02-08T02:23:28.758-08:00</updated><title type='text'>Your First Program</title><content type='html'>&lt;h1&gt;                 Your First Program&lt;/h1&gt;&lt;br /&gt;&lt;h2&gt;Objectives&lt;/h2&gt;  &lt;p&gt;Having read this section you should have an understanding of:&lt;/p&gt;  &lt;ol&gt;&lt;li&gt;a pre-processor directive that must be present in all your C programs.&lt;/li&gt;&lt;li&gt;a simple C function used to write information to your screen.&lt;/li&gt;&lt;li&gt;how to add comments to your programs&lt;/li&gt;&lt;/ol&gt;  &lt;hr /&gt; &lt;p&gt;Now that you've seen the compiler in action it's time for you to write your very own first C program. You can probably guess what it's going to be - the program that everyone writes just to check they understand the very, very, very basics of what is going on in a new language.&lt;/p&gt;  &lt;p&gt;Yes - it's the ubiquitous &lt;b&gt;"Hello World"&lt;/b&gt; program. All your first program is going to do is print the message "&lt;b&gt;&lt;kbd&gt;Hello World&lt;/kbd&gt;&lt;/b&gt;" on the screen.&lt;/p&gt;  &lt;hr /&gt; &lt;p&gt;The program is a short one, to say the least. Here it is:&lt;/p&gt;    &lt;pre&gt;&lt;b&gt;&lt;kbd&gt;#include &lt;stdio.h&gt;&lt;br /&gt;main()&lt;br /&gt;{&lt;br /&gt;   printf("Hello World\n");&lt;br /&gt;}&lt;br /&gt;&lt;/kbd&gt;&lt;/b&gt; &lt;/pre&gt;  [&lt;a href="http://www.le.ac.uk/cc/tutorials/c/programs/hello.c"&gt;program&lt;/a&gt;]  &lt;p&gt;The first line is the standard start for all C programs - &lt;b&gt;&lt;tt&gt;main()&lt;/tt&gt;&lt;/b&gt;. After this comes the program's only instruction enclosed in curly brackets &lt;b&gt;&lt;tt&gt;{}&lt;/tt&gt;&lt;/b&gt;. The curly brackets mark the start and end of the list of instructions that make up the program - in this case just one instruction.&lt;/p&gt;  &lt;p&gt;Notice the semicolon marking the end of the instruction. You might as well get into the habit of ending every C instruction with a semicolon - it will save you a lot of trouble! Also notice that the semicolon marks the end of an instruction - it isn't a separator as is the custom in other languages.&lt;/p&gt;  &lt;p&gt;If you're puzzled about why the curly brackets are on separate lines I'd better tell you that it's just a layout convention to help you spot matching brackets. C is very unfussy about the way you lay it out. For example, you could enter the &lt;b&gt;Hello World&lt;/b&gt; program as:&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;&lt;kbd&gt;main(){printf("Hello World\n");}&lt;/kbd&gt;&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;but this is unusual.&lt;/p&gt;  &lt;hr /&gt; &lt;p&gt;The &lt;tt&gt;&lt;b&gt;&lt;kbd&gt;printf&lt;/kbd&gt;&lt;/b&gt;&lt;/tt&gt; function does what its name suggest it does: it prints, on the screen, whatever you tell it to. The &lt;tt&gt;&lt;b&gt;&lt;kbd&gt;"\n"&lt;/kbd&gt;&lt;/b&gt;&lt;/tt&gt; is a special symbols that forces a new line on the screen.&lt;/p&gt;  &lt;p&gt;OK, that's enough explanation of our first program! Type it in and save it as &lt;b&gt;&lt;tt&gt;Hello.c&lt;/tt&gt;&lt;/b&gt;. Then use the compiler to compile it, then the linker to link it and finally run it. The output is as follows:&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;Hello World&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;hr /&gt; &lt;a name="Commenting Programs"&gt;&lt;h3&gt;Add Comments to a Program&lt;/h3&gt;&lt;/a&gt;  A &lt;b&gt;comment&lt;/b&gt; is a note to yourself (or others) that you put into your source code. All comments are ignored by the compiler. They exist solely for your benefit. Comments are used primarily to document the meaning and purpose of your source code, so that you can remember later how it functions and how to use it. You can also use a comment to temporarily remove a line of code. Simply surround the line(s) with the comment symbols.  &lt;p&gt;In C, the start of a comment is signalled by the &lt;b&gt;&lt;tt&gt;/*&lt;/tt&gt;&lt;/b&gt; character pair. A comment is ended by &lt;tt&gt;&lt;b&gt;*/&lt;/b&gt;&lt;/tt&gt;. For example, this is a syntactically correct C comment:&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;/* This is a comment. */&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Comments can extend over several lines and can go anywhere except in the middle of any C keyword, function name or variable name. In C you can't have one comment within another comment. That is comments may not be nested. Lets now look at our first program one last time but this time with comments:&lt;/p&gt;    &lt;pre&gt;&lt;b&gt;&lt;kbd&gt;main() /* main function heading */&lt;br /&gt;{&lt;br /&gt;   printf("\n Hello, World! \n");  /* Display message on */&lt;br /&gt;}                                   /* the screen */&lt;/kbd&gt;&lt;/b&gt;&lt;br /&gt;&lt;/pre&gt;  This program is not large enough to warrant comment statements but the principle is still the same&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5670527689344996196-4364402590224902684?l=rao-programmingofc.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://rao-programmingofc.blogspot.com/feeds/4364402590224902684/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5670527689344996196&amp;postID=4364402590224902684' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5670527689344996196/posts/default/4364402590224902684'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5670527689344996196/posts/default/4364402590224902684'/><link rel='alternate' type='text/html' href='http://rao-programmingofc.blogspot.com/2008/02/your-first-program.html' title='Your First Program'/><author><name>Ramjaan</name><uri>http://www.blogger.com/profile/07862712225011522143</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='http://1.bp.blogspot.com/_uyQpXxI_GeY/SQy7Z8dn6II/AAAAAAAAACI/94M3auWVt4A/S220/images.jpeg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5670527689344996196.post-4744693295904797439</id><published>2008-02-08T02:20:00.000-08:00</published><updated>2008-02-08T02:21:49.979-08:00</updated><title type='text'>Structure of C Programs</title><content type='html'>&lt;h1&gt;             Structure of C Programs&lt;/h1&gt;&lt;br /&gt;&lt;h2&gt;Objectives&lt;/h2&gt;  &lt;p&gt;Having completed this section you should know about:&lt;/p&gt;  &lt;ol&gt;&lt;li&gt;C's character set&lt;/li&gt;&lt;li&gt;C's keywords&lt;/li&gt;&lt;li&gt;the general structure of a C program&lt;/li&gt;&lt;li&gt;that all C statement must end in a ;&lt;/li&gt;&lt;li&gt;that C is a free format language&lt;/li&gt;&lt;li&gt;all C programs us header files that contain standard library functions.&lt;/li&gt;&lt;/ol&gt;  &lt;hr /&gt;  &lt;a name="CS"&gt;&lt;h3&gt;C's Character Set&lt;/h3&gt;&lt;/a&gt;  C does not use, nor requires the use of, every character found on a modern computer keyboard. The only characters required by the C Programming Language are as follows:  &lt;ul&gt;&lt;li&gt;&lt;b&gt;A - Z&lt;/b&gt;&lt;/li&gt;&lt;li&gt;&lt;b&gt;a -z&lt;/b&gt;&lt;/li&gt;&lt;li&gt;&lt;b&gt;0 - 9&lt;/b&gt;&lt;/li&gt;&lt;li&gt;&lt;b&gt;space . , : ; ' $ "&lt;/b&gt;&lt;/li&gt;&lt;li&gt;&lt;b&gt;# % &amp;amp; ! _ {} [] () &lt; &gt; |&lt;/b&gt;&lt;/li&gt;&lt;li&gt;&lt;b&gt;+ - / * =&lt;/b&gt;&lt;/li&gt;&lt;/ul&gt;  &lt;p&gt;The use of most of this set of characters will be discussed throughout the course.&lt;/p&gt;  &lt;hr /&gt;  &lt;a name="FORM"&gt;&lt;h3&gt;The form of a C Program&lt;/h3&gt;&lt;/a&gt;  All C programs will consist of at least one function, but it is usual (when your experience grows) to write a C program that comprises several functions. The only function that has to be present is the function called &lt;b&gt;&lt;tt&gt;main&lt;/tt&gt;&lt;/b&gt;. For more advanced programs the &lt;b&gt;&lt;tt&gt;main&lt;/tt&gt;&lt;/b&gt; function will act as a controlling function calling other functions in their turn to do the dirty work! The &lt;b&gt;&lt;tt&gt;main&lt;/tt&gt;&lt;/b&gt; function is the first function that is called when your program executes.   &lt;p&gt;C makes use of only 32 &lt;a href="http://www.le.ac.uk/cc/tutorials/c/cccckey.html"&gt;keywords&lt;/a&gt; which combine with the formal syntax to the form the C programming language. Note that all keywords are written in lower case - C, like UNIX, uses upper and lowercase text to mean different things. If you are not sure what to use then always use lowercase text in writing your C programs. A keyword may not be used for any other purposes. For example, you cannot have a variable called &lt;b&gt;&lt;tt&gt;auto&lt;/tt&gt;&lt;/b&gt;.&lt;/p&gt;  &lt;hr /&gt;  &lt;a name="LAYOUT"&gt;&lt;h3&gt;The layout of C Programs&lt;/h3&gt;&lt;/a&gt;  The general form of a C program is as follows (don't worry about what everything means at the moment - things will be explained later):   &lt;pre&gt;pre-processor directives&lt;br /&gt;global declarations&lt;br /&gt;main()&lt;br /&gt;{&lt;br /&gt;  local variables to function main ;&lt;br /&gt;  statements associated with function main ;&lt;br /&gt;}&lt;br /&gt;f1()&lt;br /&gt;{&lt;br /&gt;  local variables to function 1 ;&lt;br /&gt;  statements associated with function 1 ;&lt;br /&gt;}&lt;br /&gt;f2()&lt;br /&gt;{&lt;br /&gt;  local variables to function f2 ;&lt;br /&gt;  statements associated with function 2 ;&lt;br /&gt;}&lt;br /&gt;.&lt;br /&gt;.&lt;br /&gt;.&lt;br /&gt;etc&lt;br /&gt;&lt;/pre&gt;  &lt;p&gt;Note the use of the bracket set () and {}. () are used in conjunction with function names whereas {} are used as to delimit the C statements that are associated with that function. Also note the semicolon - yes it is there, but you might have missed it! a semicolon (;) is used to terminate C statements. C is a free format language and long statements can be continued, without truncation, onto the next line. The semicolon informs the C compiler that the end of the statement has been reached. Free format also means that you can add as many spaces as you like to improve the look of your programs.&lt;/p&gt;  &lt;p&gt;A very common mistake made by everyone, who is new to the C programming language, is to miss off the semicolon. The C compiler will concatenate the various lines of the program together and then tries to understand them - which it will not be able to do. The error message produced by the compiler will relate to a line of you program which could be some distance from the initial mistake.&lt;/p&gt;  &lt;hr /&gt;  &lt;a name="PP"&gt;&lt;h3&gt;Pre-processor Directives&lt;/h3&gt;&lt;/a&gt;  C is a small language but provides the programmer with all the tools to be able to write powerful programs. Some people don't like C because it is too primitive! Look again at the set of &lt;a href="http://www.le.ac.uk/cc/tutorials/c/cccckey.html"&gt;keywords&lt;/a&gt; that comprises the C language and see if you can find a command that allows you to print to the computer's screen the result of, say, a simple calculation. Don't look too hard because it doesn't exist.   &lt;p&gt;It would be very tedious, for all of us, if every time we wanted to communicate with the computer we all had to write our own output functions. Fortunately, we do not have to. C uses libraries of standard functions which are included when we build our programs. For the novice C programmer one of the many questions always asked &lt;i&gt;is does a function already exist for what I want to do?&lt;/i&gt; Only experience will help here but we do include a function listing as part of this course.&lt;/p&gt;  &lt;p&gt;All programs you will write will need to communicate to the outside world - I don't think I can think of a program that doesn't need to tell someone an answer. So all our C programs will need at least one of C's standard libraries which deals with standard inputting and outputting of data. This library is called &lt;b&gt;&lt;tt&gt;stdin.h&lt;/tt&gt;&lt;/b&gt; and it is declared in our programs before the &lt;b&gt;&lt;tt&gt;main&lt;/tt&gt;&lt;/b&gt; function. The &lt;tt&gt;.h&lt;/tt&gt; extension indicates that this is a header file.&lt;/p&gt;  &lt;p&gt;I have already mentioned that C is a free format language and that you can layout your programs how you want to using as much white space as you like. The only exception are statements associated with the pre-processor.&lt;/p&gt;  &lt;p&gt;All pre-processor directives begin with a # and the must start in the first column. The commonest directive to all C programs is:&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;#include &lt;stdio.h&gt;&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Note the use of the angle brackets (&lt;&gt;) around the header's name. These indicate that the header file is to be looked for on the system disk which stores the rest of the C program application. Some text books will show the above statement as follows:&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;tt&gt;#include "stdio.h"&lt;/tt&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;The double quotes indicate that the current working directory should be searched for the required header file. This will be true when you write your own header files but the standard header files should always have the angle brackets around them.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;NOTE:&lt;/b&gt; just to keep you on your toes - pre-processor statements, such as &lt;b&gt;&lt;tt&gt;include&lt;/tt&gt;&lt;/b&gt;, DO NOT use semi-colons as delimiters! But don't forget the # must be in the first column.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5670527689344996196-4744693295904797439?l=rao-programmingofc.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://rao-programmingofc.blogspot.com/feeds/4744693295904797439/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5670527689344996196&amp;postID=4744693295904797439' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5670527689344996196/posts/default/4744693295904797439'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5670527689344996196/posts/default/4744693295904797439'/><link rel='alternate' type='text/html' href='http://rao-programmingofc.blogspot.com/2008/02/structure-of-c-programs.html' title='Structure of C Programs'/><author><name>Ramjaan</name><uri>http://www.blogger.com/profile/07862712225011522143</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='http://1.bp.blogspot.com/_uyQpXxI_GeY/SQy7Z8dn6II/AAAAAAAAACI/94M3auWVt4A/S220/images.jpeg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5670527689344996196.post-2284189578744921713</id><published>2008-02-08T02:17:00.000-08:00</published><updated>2008-02-08T02:20:34.528-08:00</updated><title type='text'>Running C Programs</title><content type='html'>&lt;h1&gt;                Running C Programs&lt;/h1&gt;&lt;h1&gt; &lt;/h1&gt;&lt;h2&gt;Objectives&lt;/h2&gt;  &lt;p&gt;Having read this section you should be able to:&lt;/p&gt;  &lt;ol&gt;&lt;li&gt;Edit, link and run your C programs&lt;/li&gt;&lt;/ol&gt;  &lt;hr /&gt; &lt;p&gt;This section is primarily aimed at the beginner who as no or little experience of using compiled languages. We cover the various stages of program development. The basic principles of this section will apply to what ever C compiler you choose to use, the stages are &lt;i&gt;nearly&lt;/i&gt; always the same&lt;/p&gt;  &lt;hr /&gt; &lt;h3&gt;The Edit-Compile-Link-Execute Process&lt;/h3&gt;  &lt;p&gt;Developing a program in a compiled language such as C requires at least four steps:&lt;/p&gt;  &lt;ol&gt;&lt;li&gt;&lt;b&gt;editing&lt;/b&gt; (or writing) the program&lt;/li&gt;&lt;li&gt;&lt;b&gt;compiling&lt;/b&gt; it&lt;/li&gt;&lt;li&gt;&lt;b&gt;linking&lt;/b&gt; it&lt;/li&gt;&lt;li&gt;&lt;b&gt;executing&lt;/b&gt; it&lt;/li&gt;&lt;/ol&gt;  &lt;p&gt;We will now cover each step separately.&lt;/p&gt;  &lt;hr /&gt; &lt;h3&gt;Editing&lt;/h3&gt;  &lt;p&gt;You write a computer program with words and symbols that are understandable to human beings. This is the &lt;i&gt;editing&lt;/i&gt; part of the development cycle. You type the program directly into a window on the screen and save the resulting text as a separate file. This is often referred to as the &lt;i&gt;source file&lt;/i&gt; (you can read it with the &lt;tt&gt;&lt;b&gt;TYPE&lt;/b&gt;&lt;/tt&gt; command in &lt;tt&gt;&lt;b&gt;DOS&lt;/b&gt;&lt;/tt&gt; or the &lt;tt&gt;&lt;b&gt;cat&lt;/b&gt;&lt;/tt&gt; command in &lt;tt&gt;&lt;b&gt;unix&lt;/b&gt;&lt;/tt&gt;). The custom is that the text of a C program is stored in a file with the extension .c for C programming language&lt;/p&gt;  &lt;hr /&gt; &lt;h3&gt;Compiling&lt;/h3&gt;  &lt;p&gt;You cannot directly execute the source file. To run on any computer system, the source file must be translated into binary numbers understandable to the computer's Central Processing Unit (for example, the 80*87 microprocessor). This process produces an intermediate object file - with the extension &lt;tt&gt;.obj&lt;/tt&gt;, the &lt;tt&gt;.obj&lt;/tt&gt; stands for Object.&lt;/p&gt;  &lt;hr /&gt; &lt;h3&gt;Linking&lt;/h3&gt;  &lt;p&gt;The first question that comes to most peoples minds is &lt;i&gt;Why is linking necessary?&lt;/i&gt; The main reason is that many compiled languages come with library routines which can be added to your program. Theses routines are written by the manufacturer of the compiler to perform a variety of tasks, from input/output to complicated mathematical functions. In the case of C the standard input and output functions are contained in a library (&lt;tt&gt;stdio.h&lt;/tt&gt;) so even the most basic program will require a library function. After linking the file extension is &lt;tt&gt;.exe&lt;/tt&gt; which are executable files.&lt;/p&gt;   &lt;hr /&gt; &lt;h3&gt;Executable files&lt;/h3&gt;  &lt;p&gt;Thus the text editor produces &lt;tt&gt;.c&lt;/tt&gt; source files, which go to the compiler, which produces &lt;tt&gt;.obj&lt;/tt&gt; object files, which go to the linker, which produces &lt;tt&gt;.exe&lt;/tt&gt; executable file. You can then run &lt;tt&gt;.exe&lt;/tt&gt; files as you can other applications, simply by typing their names at the &lt;tt&gt;&lt;b&gt;DOS&lt;/b&gt;&lt;/tt&gt; prompt or run using windows menu.&lt;/p&gt;  &lt;hr /&gt; &lt;a name="Personal Computer"&gt;&lt;h3&gt;Using Microsoft C&lt;/h3&gt;&lt;/a&gt;  &lt;dl&gt;&lt;dt&gt;&lt;b&gt;Edit stage:&lt;/b&gt;&lt;/dt&gt;&lt;dd&gt;Type program in using one of the Microsoft Windows editing packages.&lt;/dd&gt;&lt;dt&gt;&lt;b&gt;Compile and link:&lt;/b&gt;&lt;/dt&gt;&lt;dd&gt;Select &lt;b&gt;Building&lt;/b&gt; from &lt;b&gt;Make&lt;/b&gt; menu. &lt;b&gt;Building&lt;/b&gt; option allows you to both &lt;b&gt;compile&lt;/b&gt; and &lt;b&gt;link&lt;/b&gt; in the same option.&lt;/dd&gt;&lt;dt&gt;&lt;b&gt;Execute:&lt;/b&gt;&lt;/dt&gt;&lt;dd&gt;Use the &lt;b&gt;Run&lt;/b&gt; menu and select &lt;b&gt;Go&lt;/b&gt; option.&lt;/dd&gt;&lt;dt&gt;&lt;b&gt;Errors:&lt;/b&gt;&lt;/dt&gt;&lt;dd&gt;First error highlighted. Use &lt;b&gt;Next Error&lt;/b&gt; from &lt;b&gt;Search&lt;/b&gt; menu for further errors if applicable.&lt;/dd&gt;&lt;/dl&gt;  &lt;p&gt;If you get an error message, or you find that the program doesn't work when you finally run it (at least not in the way you anticipated) you will have to go back to the source file - the &lt;tt&gt;.c&lt;/tt&gt; file - to make changes and go through the whole development process again!&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5670527689344996196-2284189578744921713?l=rao-programmingofc.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://rao-programmingofc.blogspot.com/feeds/2284189578744921713/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5670527689344996196&amp;postID=2284189578744921713' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5670527689344996196/posts/default/2284189578744921713'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5670527689344996196/posts/default/2284189578744921713'/><link rel='alternate' type='text/html' href='http://rao-programmingofc.blogspot.com/2008/02/running-c-programs.html' title='Running C Programs'/><author><name>Ramjaan</name><uri>http://www.blogger.com/profile/07862712225011522143</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='http://1.bp.blogspot.com/_uyQpXxI_GeY/SQy7Z8dn6II/AAAAAAAAACI/94M3auWVt4A/S220/images.jpeg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5670527689344996196.post-621208467009472506</id><published>2008-02-08T01:49:00.000-08:00</published><updated>2008-02-08T02:37:28.564-08:00</updated><title type='text'>C introduction</title><content type='html'>&lt;b&gt;So you want to learn C?&lt;/b&gt; We hope to provide you with an easy step by step guide to programming in C. The course is split up into several sections, or lessons, which include C example programs for you to demonstrate what has been taught. Although the ordering of the sections does not have to be strictly followed, the sections become progressively more involved and assume background knowledge attained from previous sections. Good Luck!&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;                                                     &lt;span style="font-weight: bold;"&gt;1.OVERVIEW OF C.&lt;br /&gt;&lt;br /&gt;1.why we use C.:-&lt;/span&gt;C has been used successfully for every type of programming problem imaginable from operating systems to spreadsheets to expert systems - and efficient compilers are available for machines ranging in power from the Apple Macintosh to the Cray supercomputers. The largest measure of C's success seems to be based on purely practical considerations:&lt;br /&gt;&lt;br /&gt;   * the portability of the compiler;&lt;br /&gt;   * the standard library concept;&lt;br /&gt;   * a powerful and varied repertoire of operators;&lt;br /&gt;   * an elegant syntax;&lt;br /&gt;   * ready access to the hardware when needed;&lt;br /&gt;   * and the ease with which applications can be optimised by hand-coding isolated procedures&lt;br /&gt;&lt;br /&gt;C is often called a "Middle Level" programming language. This is not a reflection on its lack of programming power but more a reflection on its capability to access the system's low level functions. Most high-level languages (e.g. Fortran) provides everything the programmer might want to do already built into the language. A low level language (e.g. assembler) provides nothing other than access to the machines basic instruction set. A middle level language, such as C, probably doesn't supply all the constructs found in high-languages - but it provides you with all the building blocks that you will need to produce the results you want!&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;2.USES OF C&lt;/span&gt;&lt;br /&gt;C was initially used for system development work, in particular the programs that make-up the operating system. Why use C? Mainly because it produces code that runs nearly as fast as code written in assembly language. Some examples of the use of C might be:&lt;br /&gt;&lt;br /&gt;   * Operating Systems&lt;br /&gt;   * Language Compilers&lt;br /&gt;   * Assemblers&lt;br /&gt;   * Text Editors&lt;br /&gt;   * Print Spoolers&lt;br /&gt;   * Network Drivers&lt;br /&gt;   * Modern Programs&lt;br /&gt;   * Data Bases&lt;br /&gt;   * Language Interpreters&lt;br /&gt;   * Utilities&lt;br /&gt;&lt;br /&gt;In recent years C has been used as a general-purpose language because of its popularity with programmers. It is not the world's easiest language to learn and you will certainly benefit if you are not learning C as your first programming language! C is trendy (I nearly said sexy) - many well established programmers are switching to C for all sorts of reasons, but mainly because of the portability that writing standard C programs can offer.&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;&lt;br /&gt;&lt;br /&gt;3.A Brief History of C&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;C is a general-purpose language which has been closely associated with the UNIX operating system for which it was developed - since the system and most of the programs that run it are written in C.&lt;br /&gt;&lt;br /&gt;Many of the important ideas of C stem from the language BCPL, developed by Martin Richards. The influence of BCPL on C proceeded indirectly through the language B, which was written by Ken Thompson in 1970 at Bell Labs, for the first UNIX system on a DEC PDP-7. BCPL and B are "type less" languages whereas C provides a variety of data types.&lt;br /&gt;&lt;br /&gt;In 1972 Dennis Ritchie at Bell Labs writes C and in 1978 the publication of The C Programming Language by Kernighan &amp;amp; Ritchie caused a revolution in the computing world.&lt;br /&gt;&lt;br /&gt;In 1983, the American National Standards Institute (ANSI) established a committee to provide a modern, comprehensive definition of C. The resulting definition, the ANSI standard, or "ANSI C", was completed late 1988.&lt;br /&gt;&lt;br /&gt;A Rough Guide to Programming Languages is available on-line for those of you that are interested.&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;&lt;br /&gt;&lt;br /&gt;4.C for Personal Computers&lt;/span&gt;&lt;br /&gt;With regards to personal computers Microsoft C for IBM (or clones) PC's. and Borlands C are seen to be the two most commonly used systems. However, the latest version of Microsoft C is now considered to be the most powerful and efficient C compiler for personal computers.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;We hope we have now managed to convince you to continue with this online C course and hopefully in time become a confident C programmer.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5670527689344996196-621208467009472506?l=rao-programmingofc.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://rao-programmingofc.blogspot.com/feeds/621208467009472506/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5670527689344996196&amp;postID=621208467009472506' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5670527689344996196/posts/default/621208467009472506'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5670527689344996196/posts/default/621208467009472506'/><link rel='alternate' type='text/html' href='http://rao-programmingofc.blogspot.com/2008/02/c-introduction.html' title='C introduction'/><author><name>Ramjaan</name><uri>http://www.blogger.com/profile/07862712225011522143</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='http://1.bp.blogspot.com/_uyQpXxI_GeY/SQy7Z8dn6II/AAAAAAAAACI/94M3auWVt4A/S220/images.jpeg'/></author><thr:total>0</thr:total></entry></feed>
