tag:blogger.com,1999:blog-52389345733158091112024-03-13T14:39:11.292-07:00C-Language Overview - Tutorial & Theory for BeginnersC language tutorial for beginners, Nested Loop, pseudo code. programming theory, Flowchart and language overviewAdminhttp://www.blogger.com/profile/17184499275006958237noreply@blogger.comBlogger57125tag:blogger.com,1999:blog-5238934573315809111.post-55141731392606578862012-04-06T01:32:00.003-07:002012-04-06T01:32:59.402-07:00Main Function in C<div dir="ltr" style="text-align: left;" trbidi="on"><span style="font-size: large;">There must be at least one <b>function</b> in any <b>C source code</b>. This is called as <b>Main() </b>and is a mandetory <b>requirement of C language</b>. This is the entry point of any <b>C program</b>. From main() function the code execution flows as per the programmer’s chosen custom functions. There may or may not be other <b>functions</b> written by user in a <b>program</b>.</span></div>Adminhttp://www.blogger.com/profile/17184499275006958237noreply@blogger.comtag:blogger.com,1999:blog-5238934573315809111.post-3446886669411327892012-04-06T01:32:00.000-07:002012-04-06T01:32:00.868-07:00Basic C Structure<div dir="ltr" style="text-align: left;" trbidi="on"><br />
<span style="font-size: large;">A sample C Program template can be as simple as below. The famous “Hello World”</span><br />
<span style="font-size: large;">is the famous program among the people interested to learn this language.</span><br />
<br />
<span style="font-size: large;">#include<stdio.h></span><br />
<span style="font-size: large;">int</span><br />
<span style="font-size: large;">main() </span><br />
<span style="font-size: large;">{ </span><br />
<span style="font-size: large;"> –other statements</span><br />
<span style="font-size: large;">}</span><br />
</div>Adminhttp://www.blogger.com/profile/17184499275006958237noreply@blogger.comtag:blogger.com,1999:blog-5238934573315809111.post-36794750791550099022012-01-17T06:56:00.004-08:002012-01-17T06:56:38.855-08:00String standard functions<div dir="ltr" style="text-align: left;" trbidi="on"><br />
<table border="1" cellpadding="0" cellspacing="0" class="MsoNormalTable" style="border-collapse: collapse; border: none; mso-padding-alt: 0in 5.4pt 0in 5.4pt;"><tbody>
<tr style="height: 12.5pt; mso-yfti-firstrow: yes; mso-yfti-irow: 0;"> <td style="border: none; height: 12.5pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top"> <div class="Default"><b><span style="font-size: 10.0pt;">Functions </span></b><span style="font-size: 10.0pt;"><o:p></o:p></span></div></td> <td style="border: none; height: 12.5pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top"> <div class="Default"><b><span style="font-size: 10.0pt;">Description </span></b><span style="font-size: 10.0pt;"><o:p></o:p></span></div></td> </tr>
<tr style="height: 12.5pt; mso-yfti-irow: 1;"> <td style="border: none; height: 12.5pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top"> <div class="Default"><span style="font-size: 10.0pt;">Strlen() <o:p></o:p></span></div></td> <td style="border: none; height: 12.5pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top"> <div class="Default"><span style="font-size: 10.0pt;">Determines the length of a string <o:p></o:p></span></div></td> </tr>
<tr style="height: 12.5pt; mso-yfti-irow: 2;"> <td style="border: none; height: 12.5pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top"> <div class="Default"><span style="font-size: 10.0pt;">Strcpy() <o:p></o:p></span></div></td> <td style="border: none; height: 12.5pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top"> <div class="Default"><span style="font-size: 10.0pt;">Copies a string from source to destination <o:p></o:p></span></div></td> </tr>
<tr style="height: 12.5pt; mso-yfti-irow: 3;"> <td style="border: none; height: 12.5pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top"> <div class="Default"><span style="font-size: 10.0pt;">Strncpy() <o:p></o:p></span></div></td> <td style="border: none; height: 12.5pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top"> <div class="Default"><span style="font-size: 10.0pt;">Copies characters of a string to another string up to the specified length <o:p></o:p></span></div></td> </tr>
<tr style="height: 12.5pt; mso-yfti-irow: 4;"> <td style="border: none; height: 12.5pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top"> <div class="Default"><span style="font-size: 10.0pt;">Stricmp() <o:p></o:p></span></div></td> <td style="border: none; height: 12.5pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top"> <div class="Default"><span style="font-size: 10.0pt;">Compares characters of two strings <o:p></o:p></span></div></td> </tr>
<tr style="height: 12.55pt; mso-yfti-irow: 5;"> <td style="border: none; height: 12.55pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top"> <div class="Default"><span style="font-size: 10.0pt;">Strcmp() <o:p></o:p></span></div></td> <td style="border: none; height: 12.55pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top"> <div class="Default"><span style="font-size: 10.0pt;">Compares characters of two strings up to the specified length <o:p></o:p></span></div></td> </tr>
<tr style="height: 12.5pt; mso-yfti-irow: 6;"> <td style="border: none; height: 12.5pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top"> <div class="Default"><span style="font-size: 10.0pt;">Strncmp() <o:p></o:p></span></div></td> <td style="border: none; height: 12.5pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top"> <div class="Default"><span style="font-size: 10.0pt;">Compares characters of two strings up to the specified length <o:p></o:p></span></div></td> </tr>
<tr style="height: 12.5pt; mso-yfti-irow: 7;"> <td style="border: none; height: 12.5pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top"> <div class="Default"><span style="font-size: 10.0pt;">Strnicmp() <o:p></o:p></span></div></td> <td style="border: none; height: 12.5pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top"> <div class="Default"><span style="font-size: 10.0pt;">Compares characters of two strings up to the specified length <o:p></o:p></span></div></td> </tr>
<tr style="height: 12.5pt; mso-yfti-irow: 8;"> <td style="border: none; height: 12.5pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top"> <div class="Default"><span style="font-size: 10.0pt;">Strlwr() <o:p></o:p></span></div></td> <td style="border: none; height: 12.5pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top"> <div class="Default"><span style="font-size: 10.0pt;">Converts uppercase characters of a string to lower case <o:p></o:p></span></div></td> </tr>
<tr style="height: 12.4pt; mso-yfti-irow: 9;"> <td style="border: none; height: 12.4pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top"> <div class="Default"><span style="font-size: 10.0pt;">Strupr() <o:p></o:p></span></div></td> <td style="border: none; height: 12.4pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top"> <div class="Default"><span style="font-size: 10.0pt;">Converts lowercase characters of a string to upper case <o:p></o:p></span></div></td> </tr>
<tr style="height: 12.5pt; mso-yfti-irow: 10;"> <td style="border: none; height: 12.5pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top"> <div class="Default"><span style="font-size: 10.0pt;">Strdup() <o:p></o:p></span></div></td> <td style="border: none; height: 12.5pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top"> <div class="Default"><span style="font-size: 10.0pt;">Duplicates a string <o:p></o:p></span></div></td> </tr>
<tr style="height: 12.5pt; mso-yfti-irow: 11;"> <td style="border: none; height: 12.5pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top"> <div class="Default"><span style="font-size: 10.0pt;">Strchr() <o:p></o:p></span></div></td> <td style="border: none; height: 12.5pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top"> <div class="Default"><span style="font-size: 10.0pt;">Determines the first occurrence of a given character in a string <o:p></o:p></span></div></td> </tr>
<tr style="height: 12.5pt; mso-yfti-irow: 12;"> <td style="border: none; height: 12.5pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top"> <div class="Default"><span style="font-size: 10.0pt;">Strrchr() <o:p></o:p></span></div></td> <td style="border: none; height: 12.5pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top"> <div class="Default"><span style="font-size: 10.0pt;">Determines the last occurrence of a given character in a string <o:p></o:p></span></div></td> </tr>
<tr style="height: 12.5pt; mso-yfti-irow: 13;"> <td style="border: none; height: 12.5pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top"> <div class="Default"><span style="font-size: 10.0pt;">Strstr() <o:p></o:p></span></div></td> <td style="border: none; height: 12.5pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top"> <div class="Default"><span style="font-size: 10.0pt;">Determines the first occurrence of a given string in another string <o:p></o:p></span></div></td> </tr>
<tr style="height: 12.5pt; mso-yfti-irow: 14;"> <td style="border: none; height: 12.5pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top"> <div class="Default"><span style="font-size: 10.0pt;">Strcat() <o:p></o:p></span></div></td> <td style="border: none; height: 12.5pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top"> <div class="Default"><span style="font-size: 10.0pt;">Appends source string to destination string <o:p></o:p></span></div></td> </tr>
<tr style="height: 12.5pt; mso-yfti-irow: 15;"> <td style="border: none; height: 12.5pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top"> <div class="Default"><span style="font-size: 10.0pt;">Strrev() <o:p></o:p></span></div></td> <td style="border: none; height: 12.5pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top"> <div class="Default"><span style="font-size: 10.0pt;">Reverses all characters of a string <o:p></o:p></span></div></td> </tr>
<tr style="height: 12.5pt; mso-yfti-irow: 16;"> <td style="border: none; height: 12.5pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top"> <div class="Default"><span style="font-size: 10.0pt;">Strset() <o:p></o:p></span></div></td> <td style="border: none; height: 12.5pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top"> <div class="Default"><span style="font-size: 10.0pt;">Sets all characters of a string with a given argument or symbol <o:p></o:p></span></div></td> </tr>
<tr style="height: 12.5pt; mso-yfti-irow: 17;"> <td style="border: none; height: 12.5pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top"> <div class="Default"><span style="font-size: 10.0pt;">Strspn() <o:p></o:p></span></div></td> <td style="border: none; height: 12.5pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top"> <div class="Default"><span style="font-size: 10.0pt;">Finds up to what length two strings are identical <o:p></o:p></span></div></td> </tr>
<tr style="height: 24.5pt; mso-yfti-irow: 18; mso-yfti-lastrow: yes;"> <td style="border: none; height: 24.5pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top"> <div class="Default"><span style="font-size: 10.0pt;">Strpbrk() <o:p></o:p></span></div></td> <td style="border: none; height: 24.5pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top"> <div class="Default"><span style="font-size: 10.0pt;">Searches the first occurrence of the character in a given string and then displays the string starting from that character <o:p></o:p></span></div></td> </tr>
</tbody></table></div>Adminhttp://www.blogger.com/profile/17184499275006958237noreply@blogger.comtag:blogger.com,1999:blog-5238934573315809111.post-50505232897706292502012-01-17T06:56:00.002-08:002012-01-17T06:56:19.223-08:00sprintf()<div dir="ltr" style="text-align: left;" trbidi="on"><br />
<div class="Default"><span style="color: windowtext;"><span style="font-size: large;">This function is similar to the printf() function except for a small difference between them. The printf() function sends the output to the screen whereas the sprint() function writes the values of any data type to an array of characters.</span> <o:p></o:p></span></div></div>Adminhttp://www.blogger.com/profile/17184499275006958237noreply@blogger.comtag:blogger.com,1999:blog-5238934573315809111.post-60574195856860719392012-01-17T06:56:00.000-08:002012-01-17T06:56:00.405-08:00sscanf()<div dir="ltr" style="text-align: left;" trbidi="on"><br />
<div class="Default"><span style="color: windowtext;"><span style="font-size: large;">This function allows reading characters from a character array and writes them to another array. This function is similar to scanf(), but instead of reading from standard input it reads data from an array. </span><o:p></o:p></span></div></div>Adminhttp://www.blogger.com/profile/17184499275006958237noreply@blogger.comtag:blogger.com,1999:blog-5238934573315809111.post-78822840724120925162012-01-17T06:52:00.001-08:002012-01-17T06:53:57.719-08:00Add Comments in C<div dir="ltr" style="text-align: left;" trbidi="on"><span style="font-size: large;"><br />
</span><br />
<span style="font-size: large;">Load the file comments.c and observe it on your monitor for an example of how comments</span><br />
<span style="font-size: large;">can be added to a C program.</span><br />
<span style="font-size: large;"><br />
</span><br />
<span style="font-size: large;"><br />
</span><br />
<span style="font-size: large;">/* This is a comment ignored by the compiler */</span><br />
<span style="font-size: large;">main( ) /* This is another comment ignored by the compiler */</span><br />
<span style="font-size: large;">{</span><br />
<span style="font-size: large;">printf("We are looking at how comments are "); /* A comment is</span><br />
<span style="font-size: large;">allowed to be</span><br />
<span style="font-size: large;">continued on</span><br />
<span style="font-size: large;">another line */</span><br />
<span style="font-size: large;">2-3 C Tutorial Getting started in C</span><br />
<span style="font-size: large;">printf("used in C.\n");</span><br />
<span style="font-size: large;">}</span><br />
<span style="font-size: large;">/* One more comment for effect */</span><br />
<span style="font-size: large;"><br />
</span><br />
<span style="font-size: large;"><br />
</span><br />
<span style="font-size: large;"><br />
</span><br />
<span style="font-size: large;">Comments are added to make a program more readable to you but the compiler must ignore the comments. The slash star combination is used inC for comment delimiters. They are illustrated in the program at hand. Please note that the program does not illustrate good commenting practice, but is intended to illustrate where comments can go in a program. It is a very sloppy looking program. The first slash star combination introduces the first comment and the star at the end of the first line terminates this comment. Note that this comment is prior to the beginning of the program illustrating that a comment can precede the program itself. Good programming practice would include a comment prior to the program with a short introductory description of the program. The next comment is after the "main( )" program entry point and prior to the opening brace for the program code itself. The third comment starts after the first executable statement and continue for four lines. This is perfectly legal because a comment can continue for as many lines as desired until it is terminated. Note carefully that if anything were included in the blank spaces to the left of the three</span><br />
<span style="font-size: large;">continuation lines of the comment, it would be part of the comment and would not be compiled. The last comment is located following the completion of the program, illustrating that comments can go nearly anywhere in a C program. Experiment with this program be adding comments in other places to see what will happen. Comment out one of the printf statements by putting comment delimiters both before and after it and see that it does not get printed out.</span><br />
<span style="font-size: large;">Comments are very important in any programming language because you will soon forget what you did and why you did it. It will be much easier to modify or fix a well commented program</span><br />
<span style="font-size: large;">a year from now than one with few or no comments. You will very quickly develop your own personal style of commenting.</span><br />
<span style="font-size: large;">Somecompilers allow you to "nest" comments which can be very handy if you need to "comment out" a section of code during debugging. Check your compiler documentation for the availability of this feature with your particular compiler. Compile and run comments.c at this time.</span><br />
<span style="font-size: large;"><br />
</span><br />
<br />
</div>Adminhttp://www.blogger.com/profile/17184499275006958237noreply@blogger.comtag:blogger.com,1999:blog-5238934573315809111.post-74102097395674060122012-01-17T06:51:00.000-08:002012-01-17T06:51:41.108-08:00To Print Some Numbers<div dir="ltr" style="text-align: left;" trbidi="on"><br />
<span style="font-size: large;">Load the file named oneint.c and display it on the monitor for our first example of how to</span><br />
<span style="font-size: large;">work with data in a C program.</span><br />
<span style="font-size: large;"><br />
</span><br />
<br />
<div class="MsoNormal"><span style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; font-family: 'Courier New';">main</span><b><span style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; color: navy; font-family: 'Courier New';">(</span></b><span style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; font-family: 'Courier New';"> </span><b><span style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; color: navy; font-family: 'Courier New';">)</span></b><span style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; font-family: 'Courier New';"><o:p></o:p></span></div><div class="MsoNormal"><b><span style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; color: navy; font-family: 'Courier New';">{</span></b><span style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; font-family: 'Courier New';"><o:p></o:p></span></div><div class="MsoNormal"><span style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; color: #8000ff; font-family: 'Courier New';">int</span><span style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; font-family: 'Courier New';"> index</span><b><span style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; color: navy; font-family: 'Courier New';">;</span></b><span style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; font-family: 'Courier New';"><o:p></o:p></span></div><div class="MsoNormal"><span style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; font-family: 'Courier New';">index </span><b><span style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; color: navy; font-family: 'Courier New';">=</span></b><span style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; font-family: 'Courier New';"> </span><span style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; color: #ff8000; font-family: 'Courier New';">13</span><b><span style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; color: navy; font-family: 'Courier New';">;</span></b><span style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; font-family: 'Courier New';"><o:p></o:p></span></div><div class="MsoNormal"><span style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; font-family: 'Courier New';">printf</span><b><span style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; color: navy; font-family: 'Courier New';">(</span></b><span style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; color: grey; font-family: 'Courier New';">"The value of the index is %d\n"</span><b><span style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; color: navy; font-family: 'Courier New';">,</span></b><span style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; font-family: 'Courier New';">index</span><b><span style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; color: navy; font-family: 'Courier New';">);</span></b><span style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; font-family: 'Courier New';"><o:p></o:p></span></div><div class="MsoNormal"><span style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; font-family: 'Courier New';">index </span><b><span style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; color: navy; font-family: 'Courier New';">=</span></b><span style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; font-family: 'Courier New';"> </span><span style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; color: #ff8000; font-family: 'Courier New';">27</span><b><span style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; color: navy; font-family: 'Courier New';">;</span></b><span style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; font-family: 'Courier New';"><o:p></o:p></span></div><div class="MsoNormal"><span style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; font-family: 'Courier New';">printf</span><b><span style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; color: navy; font-family: 'Courier New';">(</span></b><span style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; color: grey; font-family: 'Courier New';">"The valve of the index = %d\n"</span><b><span style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; color: navy; font-family: 'Courier New';">,</span></b><span style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; font-family: 'Courier New';">index</span><b><span style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; color: navy; font-family: 'Courier New';">);</span></b><span style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; font-family: 'Courier New';"><o:p></o:p></span></div><div class="MsoNormal"><span style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; font-family: 'Courier New';">index </span><b><span style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; color: navy; font-family: 'Courier New';">=</span></b><span style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; font-family: 'Courier New';"> </span><span style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; color: #ff8000; font-family: 'Courier New';">10</span><b><span style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; color: navy; font-family: 'Courier New';">;</span></b><span style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; font-family: 'Courier New';"><o:p></o:p></span></div><div class="MsoNormal"><span style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; font-family: 'Courier New';">printf</span><b><span style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; color: navy; font-family: 'Courier New';">(</span></b><span style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; color: grey; font-family: 'Courier New';">"The value of the index = %d\n"</span><b><span style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; color: navy; font-family: 'Courier New';">,</span></b><span style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; font-family: 'Courier New';">index</span><b><span style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; color: navy; font-family: 'Courier New';">);</span></b><span style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; font-family: 'Courier New';"><o:p></o:p></span></div><div class="MsoNormal"><b><span style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; color: navy; font-family: 'Courier New';">}</span></b></div><div class="MsoNormal"><b><span style="background: white; color: navy; font-family: "Courier New"; font-size: 10.0pt; mso-highlight: white;"><br />
</span></b></div><br />
<span style="font-size: large;"><br />
</span><br />
<br />
<span style="font-size: large;">The entry point "main" should be clear to you by now as well as the beginning brace. The first</span><br />
<span style="font-size: large;">new thing we encounter is the line containing "int index;", which is used to define an integer</span><br />
<span style="font-size: large;">variable named "index". The "int" is a reserved word in C, and can therefore not be used for</span><br />
<span style="font-size: large;">anything else. It defines a variable that can have a value from -32768 to 32767 on most MS-DOS</span><br />
<span style="font-size: large;">microcomputer implementations of C. It defines a variable with a value from -2147483648 to</span><br />
<span style="font-size: large;">2147483647 in HiTech C. Consult your compiler users manual for the exact definition for your</span><br />
<span style="font-size: large;">compiler. The variable name, "index", can be any name that follows the rules for an identifier and is not one of the reserved words for C. Consult your manual for an exact definition of an</span><br />
<span style="font-size: large;">identifier for your compiler. In HiTech C, the construction of identifier names is the same as</span><br />
<span style="font-size: large;">in UNIX, however 31 characters and both cases are significant. The compiler prepends an</span><br />
<span style="font-size: large;">underscore to external references in the assembler pass. The final character on the line, the</span><br />
<span style="font-size: large;">semi-colon, is the statement terminator used in C.</span><br />
<span style="font-size: large;">We will see in a later chapter that additional integers could also be defined on the same line,</span><br />
<span style="font-size: large;">but we will not complicate the present situation.</span><br />
<span style="font-size: large;">Observing the main body of the program, you will notice that there are three statements that</span><br />
<span style="font-size: large;">assign a value to the variable "index", but only one at a time. The first one assigns the value of</span><br />
<span style="font-size: large;">13 to "index", and its value is printed out. (We will see how shortly.) Later, the value 27 is</span><br />
<span style="font-size: large;">assigned to "index", and finally 10 is assigned to it, each value being printed out. It should be</span><br />
<span style="font-size: large;">intuitively clear that "index" is indeed a variable and can store many different values. Please</span><br />
<span style="font-size: large;">note that many times the words "printed out" are used to mean "displayed on the monitor". You</span><br />
<span style="font-size: large;">will find that in many cases experienced programmers take this liberty, probably due to the</span><br />
<span style="font-size: large;">"printf" function being used for monitor display.</span><br />
<br />
</div>Adminhttp://www.blogger.com/profile/17184499275006958237noreply@blogger.comtag:blogger.com,1999:blog-5238934573315809111.post-32690768595910162132012-01-17T06:41:00.001-08:002012-01-17T06:41:47.425-08:00First C Program<div dir="ltr" style="text-align: left;" trbidi="on"><br />
<span style="font-size: large;">You are looking at the simplest possible C program. There is no way to simplify this program,</span><br />
<span style="font-size: large;">or to leave anything out. Unfortunately, the program doesn’t do anything.</span><br />
<span style="font-size: large;">main()</span><br />
<span style="font-size: large;">{}</span><br />
<span style="font-size: large;">The word "main" is very important, and must appear once, and only once, in every C program.</span><br />
<span style="font-size: large;">This is the point where execution is begun when the program is run. We will see later that this</span><br />
<span style="font-size: large;">does not have to be the first statement in the program, but it must exist as the entry point.</span><br />
<span style="font-size: large;">Following the "main" program name is a pair of parentheses, which are an indication to the</span><br />
<span style="font-size: large;">compiler that this is a function. We will cover exactly what a function is in due time. For now,</span><br />
<span style="font-size: large;">I suggest that you simply include the pair of parentheses.</span><br />
<span style="font-size: large;">The two curly brackets { }, properly called braces, are used to define the limits of the program</span><br />
<span style="font-size: large;">itself. The actual program statements go between the two braces and in this case, there are no</span><br />
<span style="font-size: large;">statements because the program does absolutely nothing. Youcan compile and run this program,</span><br />
<span style="font-size: large;">but since it has no executable statements, it does nothing. Keep in mind however, that it is a</span><br />
<span style="font-size: large;">valid C program.</span><br />
</div>Adminhttp://www.blogger.com/profile/17184499275006958237noreply@blogger.comtag:blogger.com,1999:blog-5238934573315809111.post-65694016770897556512012-01-17T06:40:00.001-08:002012-01-17T06:40:51.604-08:00List.xrel file<div dir="ltr" style="text-align: left;" trbidi="on"><br />
<br />
<span style="font-size: large;">This file will list the source files for you with line numbers and filename. To use it, simply type</span><br />
<span style="font-size: large;">"LIST" followed by the appropriate filename. Type list firstex.c now for an example.</span><br />
<span style="font-size: large;">The C source code is given later in Chapter 14 along with a brief description of its operation.</span><br />
<span style="font-size: large;">Applix 1616 users always have the inbuilt edit comand available to them, so this program</span><br />
<span style="font-size: large;">isn’t really essential.</span><br />
<br />
</div>Adminhttp://www.blogger.com/profile/17184499275006958237noreply@blogger.comtag:blogger.com,1999:blog-5238934573315809111.post-5927064378516998512012-01-17T06:38:00.001-08:002012-01-17T06:38:57.099-08:00What Is An Identifier<div dir="ltr" style="text-align: left;" trbidi="on"><br />
<span style="font-size: large;">Before you can do anything in any language, you must at least know how you name an identifier.</span><br />
<span style="font-size: large;">An indentifier is used for any variable, function, data definition, etc. In the programming language</span><br />
<span style="font-size: large;">C, an identifier is a combination of alphanumeric characters, the first being a letter of the</span><br />
<span style="font-size: large;">alphabet or an underline, and the remaining being any letter of the alphabet, any numeric digit,</span><br />
<span style="font-size: large;">or the underline. Two rules must be kept in mind when naming identifiers.</span><br />
<span style="font-size: large;">1. The case of alphabetic characters is significant. Using "INDEX" for a variable is not</span><br />
<span style="font-size: large;">the same as using "index" and neither of them is the same as using "InDex" for a variable. All</span><br />
<span style="font-size: large;">three refer to different variables.</span><br />
<span style="font-size: large;">2. As C is defined, up to eight significant characters can be used and will be considered</span><br />
<span style="font-size: large;">significant. If more than eight are used, they may be ignored by the compiler. This may or may</span><br />
<span style="font-size: large;">not be true of your compiler. You should check your reference manual to find out how many</span><br />
<span style="font-size: large;">characters are significant for your compiler. The HiTech C compiler used with the Applix 1616</span><br />
<span style="font-size: large;">allows 31 significant characters, and prepends an underscore (_)</span><br />
<span style="font-size: large;">It should be pointed out that some C compilers allow use of a dollar sign in an identifier name,</span><br />
<span style="font-size: large;">but since it is not universal, it will not be used anywhere in this tutorial. Check your documentation</span><br />
<span style="font-size: large;">to see if it is permissible for your particular compiler.</span><br />
</div>Adminhttp://www.blogger.com/profile/17184499275006958237noreply@blogger.comtag:blogger.com,1999:blog-5238934573315809111.post-75006953339021245602012-01-16T06:13:00.000-08:002012-01-16T06:13:56.785-08:00Difference between return 0 and exit(0)<div dir="ltr" style="text-align: left;" trbidi="on"><div style="color: black;"></div><div style="color: black;"><span style="font-size: large;">return 0 is used when a funtion has return type of int n we do not return any variable , where as exit(0) is used to terminate the program abruptly, it can take values in miliseconds.</span></div><div style="color: black;"><span style="font-size: large;"><br />
</span></div><div style="color: black;"><span style="font-size: large;"><b>return:</b> control return to a function.</span></div><div style="color: black;"><span style="font-size: large;"><b>exit():</b>Use to terminate a process.</span></div></div>Adminhttp://www.blogger.com/profile/17184499275006958237noreply@blogger.comtag:blogger.com,1999:blog-5238934573315809111.post-32066280770316435562011-08-18T13:26:00.000-07:002011-08-18T13:26:23.961-07:00File Handling in C Language<div dir="ltr" style="text-align: left;" trbidi="on"><span class="Apple-style-span" style="background-color: white; color: #222222; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 20px;"></span><br />
<div style="line-height: 19px; margin-bottom: 1em; margin-top: 0.5em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">In this section, we will discuss about files which are very important for storing information permanently. We store information in files for many purposes, like data processing by our programs.</div><h1 style="color: #333333; font-family: Verdana, Geneva, sans-serif; font-size: 18px; font-style: normal; line-height: 24px; margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 20px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">What is a File?</h1><div style="line-height: 19px; margin-bottom: 1em; margin-top: 0.5em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">Abstractly, a file is a collection of bytes stored on a secondary storage device, which is generally a disk of some kind. The collection of bytes may be interpreted, for example, as characters, words, lines, paragraphs and pages from a textual document; fields and records belonging to a database; or pixels from a graphical image. The meaning attached to a particular file is determined entirely by the data structures and operations used by a program to process the file. It is conceivable (and it sometimes happens) that a graphics file will be read and displayed by a program designed to process textual data. The result is that no meaningful output occurs (probably) and this is to be expected. A file is simply a machine decipherable storage media where programs and data are stored for machine usage.</div><div style="line-height: 19px; margin-bottom: 1em; margin-top: 0.5em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">Essentially there are two kinds of files that programmers deal with text files and binary files. These two classes of files will be discussed in the following sections.</div><h2 style="color: #333333; font-family: Verdana, Geneva, sans-serif; font-size: 1.25em; font-style: normal; margin-bottom: 0em; margin-left: 0px; margin-right: 0px; margin-top: 0.5em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">ASCII Text files</h2><div style="line-height: 19px; margin-bottom: 1em; margin-top: 0.5em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">A text file can be a stream of characters that a computer can process sequentially. It is not only processed sequentially but only in forward direction. For this reason a text file is usually opened for only one kind of operation (reading, writing, or appending) at any given time.</div><div style="line-height: 19px; margin-bottom: 1em; margin-top: 0.5em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">Similarly, since text files only process characters, they can only read or write data one character at a time. (In C Programming Language, Functions are provided that deal with lines of text, but these still essentially process data one character at a time.) A text stream in C is a special kind of file. Depending on the requirements of the operating system, newline characters may be converted to or from carriage-return/linefeed combinations depending on whether data is being written to, or read from, the file. Other character conversions may also occur to satisfy the storage requirements of the operating system. These translations occur transparently and they occur because the programmer has signalled the intention to process a text file.</div><h2 style="color: #333333; font-family: Verdana, Geneva, sans-serif; font-size: 1.25em; font-style: normal; margin-bottom: 0em; margin-left: 0px; margin-right: 0px; margin-top: 0.5em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">Binary files</h2><div style="line-height: 19px; margin-bottom: 1em; margin-top: 0.5em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">A binary file is no different to a text file. It is a collection of bytes. In C Programming Language a byte and a character are equivalent. Hence a binary file is also referred to as a character stream, but there are two essential differences.</div><ol style="margin-bottom: 0px; margin-left: 25px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><li style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">No special processing of the data occurs and each byte of data is transferred to or from the disk unprocessed.</li>
<li style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">C Programming Language places no constructs on the file, and it may be read from, or written to, in any manner chosen by the programmer.</li>
</ol><div style="line-height: 19px; margin-bottom: 1em; margin-top: 0.5em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">Binary files can be either processed sequentially or, depending on the needs of the application, they can be processed using random access techniques. In C Programming Language, processing a file using random access techniques involves moving the current file position to an appropriate place in the file before reading or writing data. This indicates a second characteristic of binary files.<br />
They a generally processed using read and write operations simultaneously.</div><div style="line-height: 19px; margin-bottom: 1em; margin-top: 0.5em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">For example, a database file will be created and processed as a binary file. A record update operation will involve locating the appropriate record, reading the record into memory, modifying it in some way, and finally writing the record back to disk at its appropriate location in the file. These kinds of operations are common to many binary files, but are rarely found in applications that process text files.</div><div style="line-height: 19px; margin-bottom: 1em; margin-top: 0.5em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><br />
</div><span class="Apple-style-span" style="background-color: white; color: #222222; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 20px;"></span><br />
<h2 style="color: #333333; font-family: Verdana, Geneva, sans-serif; font-size: 1.25em; font-style: normal; margin-bottom: 0em; margin-left: 0px; margin-right: 0px; margin-top: 0.5em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span class="Apple-style-span" style="background-color: white; color: #222222; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 20px;">Creating a file and output some data</span></h2><div style="line-height: 19px; margin-bottom: 1em; margin-top: 0.5em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span class="Apple-style-span" style="background-color: white; color: #222222; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 20px;">In order to create files we have to learn about File I/O i.e. how to write data into a file and how to read data from a file. We will start this section with an example of writing data to a file. We begin as before with the include statement for stdio.h, then define some variables for use in the example including a rather strange looking new type.</span></div><pre style="border-bottom-color: rgb(216, 216, 216); border-bottom-style: solid; border-bottom-width: 1px; border-left-color: rgb(216, 216, 216); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(216, 216, 216); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(216, 216, 216); border-top-style: solid; border-top-width: 1px; color: seagreen; display: block; font-family: Monaco, 'Andale Mono', 'Courier New', Courier, mono; font-size: 0.9em; font-style: normal; font-variant: normal; height: auto; line-height: 1.3em; margin-bottom: 2px; margin-top: 2px; max-height: 200px; overflow-x: auto; overflow-y: auto; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px;"><span class="Apple-style-span" style="background-color: white; color: #222222; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 20px;">/* Program to create a file and write some data the file */
#include <stdio.h>
#include <stdio.h>
main( )
{
FILE *fp;
char stuff[25];
int index;
fp = fopen("TENLINES.TXT","w"); /* open for writing */
strcpy(stuff,"This is an example line.");
for (index = 1; index <= 10; index++)
fprintf(fp,"%s Line number %d\n", stuff, index);
fclose(fp); /* close the file before ending program */
}</span></pre><div style="line-height: 19px; margin-bottom: 1em; margin-top: 0.5em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span class="Apple-style-span" style="background-color: white; color: #222222; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 20px;">The type FILE is used for a file variable and is defined in the stdio.h file. It is used to define a file pointer for use in file operations. Before we can write to a file, we must open it. What this really means is that we must tell the system that we want to write to a file and what the file name is. We do this with the fopen() function illustrated in the first line of the program. The file pointer, fp in our case, points to the file and two arguments are required in the parentheses, the file name first, followed by the file type.</span></div><div style="line-height: 19px; margin-bottom: 1em; margin-top: 0.5em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span class="Apple-style-span" style="background-color: white; color: #222222; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 20px;">The file name is any valid DOS file name, and can be expressed in upper or lower case letters, or even mixed if you so desire. It is enclosed in double quotes. For this example we have chosen the name TENLINES.TXT. This file should not exist on your disk at this time. If you have a file with this name, you should change its name or move it because when we execute this program, its contents will be erased. If you don’t have a file by this name, that is good because we will create one and put some data into it. You are permitted to include a directory with the file name.The directory must, of course, be a valid directory otherwise an error will occur. Also, because of the way C handles literal strings, the directory separation character ‘\’ must be written twice. For example, if the file is to be stored in the \PROJECTS sub directory then the file name should be entered as “\\PROJECTS\\TENLINES.TXT”. The second parameter is the file attribute and can be any of three letters, r, w, or a, and must be lower case.</span></div><h3 style="color: #333333; font-family: Verdana, Geneva, sans-serif; font-size: 1.25em; font-style: normal; margin-bottom: 0.75em; margin-left: 0px; margin-right: 0px; margin-top: 1.3em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0.5em;"><span class="Apple-style-span" style="background-color: white; color: #222222; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 20px;">Reading (r)</span></h3><div style="line-height: 19px; margin-bottom: 1em; margin-top: 0.5em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span class="Apple-style-span" style="background-color: white; color: #222222; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 20px;">When an r is used, the file is opened for reading, a w is used to indicate a file to be used for writing, and an a indicates that you desire to append additional data to the data already in an existing file. Most C compilers have other file attributes available; check your Reference Manual for details. Using the r indicates that the file is assumed to be a text file. Opening a file for reading requires that the file already exist. If it does not exist, the file pointer will be set to NULL and can be checked by the program.</span></div><div style="line-height: 19px; margin-bottom: 1em; margin-top: 0.5em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span class="Apple-style-span" style="background-color: white; color: #222222; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 20px;">Here is a small program that reads a file and display its contents on screen.</span></div><pre style="border-bottom-color: rgb(216, 216, 216); border-bottom-style: solid; border-bottom-width: 1px; border-left-color: rgb(216, 216, 216); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(216, 216, 216); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(216, 216, 216); border-top-style: solid; border-top-width: 1px; color: seagreen; display: block; font-family: Monaco, 'Andale Mono', 'Courier New', Courier, mono; font-size: 0.9em; font-style: normal; font-variant: normal; height: auto; line-height: 1.3em; margin-bottom: 2px; margin-top: 2px; max-height: 200px; overflow-x: auto; overflow-y: auto; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px;"><span class="Apple-style-span" style="background-color: white; color: #222222; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 20px;">/* Program to display the contents of a file on screen */
#include <stdio.h>
void main()
{
FILE *fopen(), *fp;
int c;
fp = fopen("prog.c","r");
c = getc(fp) ;
while (c!= EOF)
{
putchar(c);
c = getc(fp);
}
fclose(fp);
}</span></pre><h3 style="color: #333333; font-family: Verdana, Geneva, sans-serif; font-size: 1.25em; font-style: normal; margin-bottom: 0.75em; margin-left: 0px; margin-right: 0px; margin-top: 1.3em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0.5em;"><span class="Apple-style-span" style="background-color: white; color: #222222; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 20px;">Writing (w)</span></h3><div style="line-height: 19px; margin-bottom: 1em; margin-top: 0.5em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span class="Apple-style-span" style="background-color: white; color: #222222; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 20px;">When a file is opened for writing, it will be created if it does not already exist and it will be reset if it does, resulting in the deletion of any data already there. Using the w indicates that the file is assumed to be a text file.</span></div><div style="line-height: 19px; margin-bottom: 1em; margin-top: 0.5em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span class="Apple-style-span" style="background-color: white; color: #222222; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 20px;">Here is the program to create a file and write some data into the file.</span></div><pre style="border-bottom-color: rgb(216, 216, 216); border-bottom-style: solid; border-bottom-width: 1px; border-left-color: rgb(216, 216, 216); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(216, 216, 216); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(216, 216, 216); border-top-style: solid; border-top-width: 1px; color: seagreen; display: block; font-family: Monaco, 'Andale Mono', 'Courier New', Courier, mono; font-size: 0.9em; font-style: normal; font-variant: normal; height: auto; line-height: 1.3em; margin-bottom: 2px; margin-top: 2px; max-height: 200px; overflow-x: auto; overflow-y: auto; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px;"><span class="Apple-style-span" style="background-color: white; color: #222222; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 20px;">#include <stdio.h>
int main()
{
FILE *fp;
file = fopen("file.txt","w");
/*Create a file and add text*/
fprintf(fp,"%s","This is just an example :)"); /*writes data to the file*/
fclose(fp); /*done!*/
return 0;
}</span></pre><h3 style="color: #333333; font-family: Verdana, Geneva, sans-serif; font-size: 1.25em; font-style: normal; margin-bottom: 0.75em; margin-left: 0px; margin-right: 0px; margin-top: 1.3em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0.5em;"><span class="Apple-style-span" style="background-color: white; color: #222222; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 20px;">Appending (a)</span></h3><div style="line-height: 19px; margin-bottom: 1em; margin-top: 0.5em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span class="Apple-style-span" style="background-color: white; color: #222222; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 20px;">When a file is opened for appending, it will be created if it does not already exist and it will be initially empty. If it does exist, the data input point will be positioned at the end of the present data so that any new data will be added to any data that already exists in the file. Using the a indicates that the file is assumed to be a text file.</span></div><div style="line-height: 19px; margin-bottom: 1em; margin-top: 0.5em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span class="Apple-style-span" style="background-color: white; color: #222222; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 20px;">Here is a program that will add text to a file which already exists and there is some text in the file.</span></div><pre style="border-bottom-color: rgb(216, 216, 216); border-bottom-style: solid; border-bottom-width: 1px; border-left-color: rgb(216, 216, 216); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(216, 216, 216); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(216, 216, 216); border-top-style: solid; border-top-width: 1px; color: seagreen; display: block; font-family: Monaco, 'Andale Mono', 'Courier New', Courier, mono; font-size: 0.9em; font-style: normal; font-variant: normal; height: auto; line-height: 1.3em; margin-bottom: 2px; margin-top: 2px; max-height: 200px; overflow-x: auto; overflow-y: auto; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px;"><span class="Apple-style-span" style="background-color: white; color: #222222; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 20px;">#include <stdio.h>
int main()
{
FILE *fp
file = fopen("file.txt","a");
fprintf(fp,"%s","This is just an example :)"); /*append some text*/
fclose(fp);
return 0;
}</span></pre><h2 style="color: #333333; font-family: Verdana, Geneva, sans-serif; font-size: 1.25em; font-style: normal; margin-bottom: 0em; margin-left: 0px; margin-right: 0px; margin-top: 0.5em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span class="Apple-style-span" style="background-color: white; color: #222222; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 20px;">Outputting to the file</span></h2><div style="line-height: 19px; margin-bottom: 1em; margin-top: 0.5em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span class="Apple-style-span" style="background-color: white; color: #222222; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 20px;">The job of actually outputting to the file is nearly identical to the outputting we have already done to the standard output device. The only real differences are the new function names and the addition of the file pointer as one of the function arguments. In the example program, fprintf replaces our familiar printf function name, and the file pointer defined earlier is the first argument within the parentheses. The remainder of the statement looks like, and in fact is identical to, the printf statement.</span></div><h2 style="color: #333333; font-family: Verdana, Geneva, sans-serif; font-size: 1.25em; font-style: normal; margin-bottom: 0em; margin-left: 0px; margin-right: 0px; margin-top: 0.5em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span class="Apple-style-span" style="background-color: white; color: #222222; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 20px;">Closing a file</span></h2><div style="line-height: 19px; margin-bottom: 1em; margin-top: 0.5em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span class="Apple-style-span" style="background-color: white; color: #222222; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 20px;">To close a file you simply use the function fclose with the file pointer in the parentheses. Actually, in this simple program, it is not necessary to close the file because the system will close all open files before returning to DOS, but it is good programming practice for you to close all files in spite of the fact that they will be closed automatically, because that would act as a reminder to you of what files are open at the end of each program.</span></div><div style="line-height: 19px; margin-bottom: 1em; margin-top: 0.5em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span class="Apple-style-span" style="background-color: white; color: #222222; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 20px;">You can open a file for writing, close it, and reopen it for reading, then close it, and open it again for appending, etc. Each time you open it, you could use the same file pointer, or you could use a different one. The file pointer is simply a tool that you use to point to a file and you decide what file it will point to. Compile and run this program. When you run it, you will not get any output to the monitor because it doesn’t generate any. After running it, look at your directory for a file named TENLINES.TXT and type it; that is where your output will be. Compare the output with that specified in the program; they should agree! Do not erase the file named TENLINES.TXT yet; we will use it in</span><br />
<span class="Apple-style-span" style="background-color: white; color: #222222; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 20px;">some of the other examples in this section.</span></div><div style="line-height: 19px; margin-bottom: 1em; margin-top: 0.5em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span class="Apple-style-span" style="background-color: white; color: #222222; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 20px;">Reading from a text file</span></div><span class="Apple-style-span" style="background-color: white; color: seagreen; font-family: Monaco, 'Andale Mono', 'Courier New', Courier, mono; font-size: 12px; line-height: 15px; white-space: pre;">#include <stdio.h></span><span class="Apple-style-span" style="background-color: white; color: #222222; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 20px;"><div style="line-height: 19px; margin-bottom: 1em; margin-top: 0.5em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">Now for our first program that reads from a file. This program begins with the familiar include, some data definitions, and the file opening statement which should require no explanation except for the fact that an r is used here because we want to read it.</div><pre style="border-bottom-color: rgb(216, 216, 216); border-bottom-style: solid; border-bottom-width: 1px; border-left-color: rgb(216, 216, 216); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(216, 216, 216); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(216, 216, 216); border-top-style: solid; border-top-width: 1px; color: seagreen; display: block; font-family: Monaco, 'Andale Mono', 'Courier New', Courier, mono; font-size: 0.9em; font-style: normal; font-variant: normal; height: auto; line-height: 1.3em; margin-bottom: 2px; margin-top: 2px; max-height: 200px; overflow-x: auto; overflow-y: auto; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px;">main( )
{
FILE *fp;
char c;
funny = fopen("TENLINES.TXT", "r");
if (fp == NULL)
printf("File doesn't exist\n");
else {
do {
c = getc(fp); /* get one character from the file
*/
putchar(c); /* display it on the monitor
*/
} while (c != EOF); /* repeat until EOF (end of file)
*/
}
fclose(fp);
}</pre><div style="line-height: 19px; margin-bottom: 1em; margin-top: 0.5em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">In this program we check to see that the file exists, and if it does, we execute the main body of the program. If it doesn’t, we print a message and quit. If the file does not exist, the system will set the pointer equal to NULL which we can test. The main body of the program is one do while loop in which a single character is read from the file and output to the monitor until an EOF (end of file) is detected from the input file. The file is then closed and the program is terminated. At this point, we have the potential for one of the most common and most perplexing problems of programming in C. The variable returned from the getc function is a character, so we can use a char variable for this purpose. There is a problem that could develop here if we happened to use an unsigned char however, because C usually returns a minus one for an EOF – which an unsigned char type variable is not<br />
capable of containing. An unsigned char type variable can only have the values of zero to 255, so it will return a 255 for a minus one in C. This is a very frustrating problem to try to find. The program can never find the EOF and will therefore never terminate the loop. This is easy to prevent: always have a char or int type variable for use in returning an EOF. There is another problem with this program but we will worry about it when we get to the next program and solve it with the one following that.</div><div style="line-height: 19px; margin-bottom: 1em; margin-top: 0.5em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">After you compile and run this program and are satisfied with the results, it would be a good exercise to change the name of TENLINES.TXT and run the program again to see that the NULL test actually works as stated. Be sure to change the name back because we are still not finished with TENLINES.TXT.</div></span></div>Adminhttp://www.blogger.com/profile/17184499275006958237noreply@blogger.comtag:blogger.com,1999:blog-5238934573315809111.post-5399153541843251692011-08-18T13:23:00.001-07:002011-08-18T13:24:20.289-07:00Array in C Language<div dir="ltr" style="text-align: left;" trbidi="on"><br />
<span class="Apple-style-span" style="background-color: white; color: #222222; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 20px;"></span><br />
<h2 style="color: #333333; font-family: Verdana, Geneva, sans-serif; font-size: 1.25em; font-style: normal; margin-bottom: 0em; margin-left: 0px; margin-right: 0px; margin-top: 0.5em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">What is an Array in C Language?</h2><div style="line-height: 19px; margin-bottom: 1em; margin-top: 0.5em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">An array in C Programing Language can be defined as number of memory locations, each of which can store the same data type and which can be references through the same variable name.</div><div style="line-height: 19px; margin-bottom: 1em; margin-top: 0.5em; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">An array is a collective name given to a group of similar quantities. These similar quantities could be percentage marks of 100 students, number of chairs in home, or salaries of 300 employees or ages of 25 students. Thus an array is a collection of similar elements. These similar elements could be all integers or all floats or all characters etc. Usually, the array of characters is called a “string”, where as an array of integers or floats is called simply an array. All elements of any given array must be of the same type i.e we can’t have an array of 10 numbers, of which 5 are ints and 5 are floats.</div><div id="quoteright" style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">Arrays and pointers have a special relationship as arrays use pointers to reference memory locations.</div><div id="quoteright" style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><br />
</div><br />
<br />
Declaration of an Array<br />
<br />
Arrays must be declared before they can be used in the program. Standard array declaration is as<br />
<br />
type variable_name[lengthofarray];<br />
<br />
Here type specifies the variable type of the element which is going to be stored in the array. In C programmin language we can declare the array of any basic standard type which C language supports. For example<br />
<br />
double height[10];<br />
float width[20];<br />
int min[9];<br />
char name[20];<br />
<br />
In C Language, arrays starts at position 0. The elements of the array occupy adjacent locations in memory. C Language treats the name of the array as if it were a pointer to the first element This is important in understanding how to do arithmetic with arrays. Any item in the array can be accessed through its index, and it can be accesed any where from with in the program. So<br />
<br />
m=height[0];<br />
<br />
variable m will have the value of first item of array height.<br />
<br />
The program below will declare an array of five integers and print all the elements of the array.<br />
<br />
int myArray [5] = {1,2,3,4,5};<br />
/* To print all the elements of the array<br />
for (int i=0;i<5;i++){<br />
printf("%d", myArray[i]);<br />
}<br />
<br />
Initializing Arrays<br />
<br />
Initializing of array is very simple in c programming. The initializing values are enclosed within the curly braces in the declaration and placed following an equal sign after the array name. Here is an example which declares and initializes an array of five elements of type int. Array can also be initialized after declaration. Look at the following C code which demonstrate the declaration and initialization of an array.<br />
<br />
int myArray[5] = {1, 2, 3, 4, 5}; //declare and initialize the array in one statement<br />
int studentAge[4];<br />
studentAge[0]=14;<br />
studentAge[1]=13;<br />
studentAge[2]=15;<br />
studentAge[3]=16;<br />
<br />
Performing operations on Arrays<br />
<br />
Here is a program that will demonstrate the simple operations of the array.<br />
<br />
#include <stdio.h><br />
void oneWay(void);<br />
void anotherWay(void);<br />
int main(void) {<br />
printf("\noneWay:\n");<br />
oneWay();<br />
printf("\nantherWay:\n");<br />
anotherWay();<br />
}<br />
<br />
/*Array initialized with aggregate */<br />
void oneWay(void) {<br />
int vect[10] = {1,2,3,4,5,6,7,8,9,0};<br />
int i;<br />
for (i=0; i<10; i++){<br />
printf("i = %2d vect[i] = %2d\n", i, vect[i]);<br />
}<br />
}<br />
<br />
/*Array initialized with loop */<br />
void anotherWay(void) {<br />
int vect[10];<br />
int i;<br />
for (i=0; i<10; i++)<br />
vect[i] = i+1;<br />
for (i=0; i<10; i++)<br />
printf("i = %2d vect[i] = %2d\n", i, vect[i]);<br />
}<br />
<br />
/* The output of this program is<br />
oneWay:<br />
i = 0 vect[i] = 1<br />
i = 1 vect[i] = 2<br />
i = 2 vect[i] = 3<br />
i = 3 vect[i] = 4<br />
i = 4 vect[i] = 5<br />
i = 5 vect[i] = 6<br />
i = 6 vect[i] = 7<br />
i = 7 vect[i] = 8<br />
i = 8 vect[i] = 9<br />
i = 9 vect[i] = 0<br />
<br />
antherWay:<br />
i = 0 vect[i] = 1<br />
i = 1 vect[i] = 2<br />
i = 2 vect[i] = 3<br />
i = 3 vect[i] = 4<br />
i = 4 vect[i] = 5<br />
i = 5 vect[i] = 6<br />
i = 6 vect[i] = 7<br />
i = 7 vect[i] = 8<br />
i = 8 vect[i] = 9<br />
i = 9 vect[i] = 10<br />
*/<br />
<br />
Here is a more complex program that will demonstrate how to read, write and traverse the integer arrays<br />
<br />
#include <stdio.h><br />
void intSwap(int *x, int *y);<br />
int getIntArray(int a[], int nmax, int sentinel);<br />
void printIntArray(int a[], int n);<br />
void reverseIntArray(int a[], int n);<br />
<br />
int main(void) {<br />
int x[10];<br />
int hmny;<br />
<br />
hmny = getIntArray(x, 10, 0);<br />
printf("The array was: \n");<br />
printIntArray(x,hmny);<br />
reverseIntArray(x,hmny);<br />
printf("after reverse it is:\n");<br />
printIntArray(x,hmny);<br />
}<br />
<br />
void intSwap(int *x, int *y)<br />
/* It swaps the content of x and y */<br />
{<br />
int temp = *x;<br />
*x = *y;<br />
*y = temp;<br />
}<br />
<br />
/* n is the number of elements in the array a.<br />
* These values are printed out, five per line. */<br />
void printIntArray(int a[], int n){<br />
int i;<br />
for (i=0; i<n; ){<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span> printf("\t%d ", a[i++]);<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span> if (i%5==0)<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span> printf("\n");<br />
}<br />
printf("\n");<br />
}<br />
<br />
/* It reads up to nmax integers and stores then in a; sentinel<br />
* terminates input. */<br />
int getIntArray(int a[], int nmax, int sentinel)<br />
{<br />
int n = 0;<br />
int temp;<br />
<br />
do {<br />
printf("Enter integer [%d to terminate] : ", sentinel);<br />
scanf("%d", &temp);<br />
if (temp==sentinel) break;<br />
if (n==nmax)<br />
printf("array is full\n");<br />
else<br />
a[n++] = temp;<br />
}while (1);<br />
return n;<br />
}<br />
<br />
/* It reverse the order of the first n elements of array */<br />
void reverseIntArray(int a[], int n)<br />
{<br />
int i;<br />
for(i=0;i<n/2;i++){<br />
intSwap(&a[i],&a[n-i-1]);<br />
}<br />
}<br />
<br />
Copy one array into another<br />
<br />
There is no such statement in C language which can directly copy an array into another array. So we have to copy each item seperately into another array.<br />
<br />
#include <stdio.h><br />
int main()<br />
{<br />
int iMarks[4];<br />
short newMarks[4];<br />
iMarks[0]=78;<br />
iMarks[1]=64;<br />
iMarks[2]=66;<br />
iMarks[3]=74;<br />
for(i=0; i<4; i++)<br />
newMarks[i]=iMarks[i];<br />
for(j=0; j<4; j++)<br />
printf("%d\n", newMarks[j]);<br />
return 0;<br />
}<br />
<br />
To summarize, arrays are provides a simple mechanism where more than one elements of same type are to be used. We can maintain, manipulate and store multiple elements of same type in one array variable and access them through index.<br />
<br />
Multidimensional Arrays<br />
<br />
In C Language one can have arrays of any dimensions. To understand the concept<br />
of multidimensional arrays let us consider the following 4 X 5 matrix<br />
<br />
Row number (i)<br />
<br />
Column numbers (j)<br />
<br />
0<br />
<br />
11<br />
<br />
3<br />
<br />
5<br />
<br />
-9<br />
<br />
-6<br />
<br />
1<br />
<br />
5<br />
<br />
6<br />
<br />
-8<br />
<br />
7<br />
<br />
24<br />
<br />
2<br />
<br />
-8<br />
<br />
9<br />
<br />
2<br />
<br />
12<br />
<br />
45<br />
<br />
3<br />
<br />
10<br />
<br />
13<br />
<br />
-10<br />
<br />
4<br />
<br />
5<br />
<br />
Let us assume the name of matrix is x<br />
<br />
To access a particular element from the array we have to use two subscripts on for row number and other for column number the notation is of the form X [i] [j] where i stands for row subscripts and j stands for column subscripts. Thus X [0] [0] refers to 10, X [2] [1] refers to 16 and so on In short multi dimensional arrays are defined more or less in the same manner as single dimensional arrays, except that for subscripts you require two squire two square brackets. We will restrict our decision to two dimensional arrays.<br />
<br />
Below given are some typical two-dimensional array definitions<br />
<br />
float table [50] [50];<br />
char line [24] [40];<br />
<br />
The first example defines tables as a floating point array having 50 rows and 50 columns. the number of elements will be 2500 (50 X50).<br />
<br />
The second declaration example establishes an array line of type character with 24 rows and 40 columns. The number of elements will be (24 X 40) 1920 consider the following two dimensional array definition int values [3] [4] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10. 11, 12, };<br />
<br />
Values [0] [0] = 1 Values [0] [1] = 2 Values [0] [2] = 3 Values [0] [3] = 4<br />
Values [1] [0] = 5 Values [1] [1] = 6 Values [1] [2] = 7 Values [1] [3] = 8<br />
Values [2] [0] = 9 Values [2] [1] = 10 Values [2] [2] = 11 Values [2] [3] = 12<br />
<br />
Here the first subscript stands for the row number and second one for column number. First subscript ranges from 0 to 2 and there are altogether 3 rows second one ranges from 0 to 3 and there are altogether 4 columns.<br />
<br />
Alternatively the above definition can be defined and initialised as<br />
<br />
int values [3] [4] = {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>{<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>1, 2, 3, 4<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>{<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>5, 6, 7, 8<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>{<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>9, 10, 11, 12<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
};<br />
<br />
Here the values in first pair of braces are initialised to elements of first row, the values of second pair of inner braces are assigned to second row and so on. Note that outer pair of curly braces is required. If there are two few values within a pair of braces the remaining elements will be assigned as zeros.<br />
<br />
Here is a sample program that stores roll numbers and marks obtained by a student side by side in matrix<br />
<br />
main ( )<br />
{<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>int stud [4] [2];<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>int i, j;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>for (i =0; i < =3; i ++)<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>{<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>printf ("\n Enter roll no. and marks");<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>scanf ("%d%d", &stud [i] [0], &stud [i] [1] );<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>for (i = 0; i < = 3; i ++)<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>printf ("\n %d %d", stud [i] [0], stud [i] [1]);<br />
}<br />
<br />
The above example illustrates how a two dimensional array can be read and how the values stored in the array can be displayed on screen.<br />
<br />
</div>Adminhttp://www.blogger.com/profile/17184499275006958237noreply@blogger.comtag:blogger.com,1999:blog-5238934573315809111.post-69017177238428369072011-05-08T11:04:00.001-07:002011-05-08T11:04:34.807-07:00What is a dangling pointerA dangling pointer arises when you use the address of an object after its lifetime is over. This<br />may occur in situations like returning addresses of the automatic variables from a function or<br />using the address of the memory block after it is freed.Adminhttp://www.blogger.com/profile/17184499275006958237noreply@blogger.comtag:blogger.com,1999:blog-5238934573315809111.post-13420495959252965892011-05-08T11:01:00.003-07:002011-05-08T11:01:48.386-07:00How to write a C program<span class="Apple-style-span" style="font-family: Arial, Helvetica; font-size: small; "><p>We specified the algorithm using English statements. However, these statements are sufficiently ‘computer-oriented’ for a computer program to be written directly from them. Before we do this, let us see how we expect the program to work from the user’s point of view.</p><p><span><span>First, the program will type the request for the length of a side; we say the program <em><span>prompts</span></em> the user to supply data. The screen display might look like this:</span></span></p><p><span><span>Enter length of side:</span></span></p><p><span><span>The computer will then wait for the user to type the length. Suppose the user types 12. The display will look like this:</span></span></p><p><span><span>Enter length of side: 12</span></span></p><p><span><span>The program will then accept (we say <em><span>read</span></em>) the number typed, calculate the area and print the result. The display may look like this:</span></span></p><p><span><span>Enter length of side: 12</span></span></p><p><span><span>Area of square is 144</span></span></p><p><span><span>Here we have specified what the <em><span>output</span></em> of the program should look like. For instance, there is a blank line between the prompt line and the line that gives the answer; we have also specified the exact form of the answer. This is a simple example of <em><span>output design</span></em>. This is necessary since the programmer cannot write the program unless he knows the precise output required.</span></span></p><p><span><span>In order to write the computer program from the algorithm, a suitable <em><span>programming language</span></em> must be chosen. We can think of a <em><span>program</span></em> as a set of instructions, <em><span>written in a programming language</span></em>, which, when executed, will produce a solution to a given problem or perform some specified task.</span></span></p><p><span><span>The major difference between an algorithm and a program is that an algorithm can be written using informal language without having to follow any special rules (though some <em><span>conventions</span></em> are usually followed) whereas a program is written in a programming language and <em><span>must</span></em> follow all the rules (the <em><span>syntax</span></em> rules) of the language. (Similarly, if we wish to write correct English, we must follow the syntax rules of the English language).</span></span></p><p><span><span>In this series, we will be showing you how to write programs in C, the programming language developed by Ken Thompson and Dennis Ritchie of Bell Laboratories, and one of the most popular and widely used today. </span></span><span><span>A program written in a high-level language such as C is usually referred to as a <em>source program</em> or <em>source code</em>.</span></span></p><p><span><span>The C program which requests the user to enter the length of a side and prints the area of the square is shown here:</span></span></p><p><code>#include <stdio.h><br /> main() { <br /> int a, s;<br /> printf("Enter length of side: ");<br /> scanf("%d", &s); //store length in s<br /> a = s * s; //calculate area; store in a<br /> printf("\nArea of square is %d\n", a);<br />}</stdio.h></code></p><p><span><span>It is not too important that you understand everything about this program at this time. But you can observe that a C program has something (a function) called <em>main</em> followed by opening and closing brackets. Between the left brace </span></span><span>{</span><span><span><span> </span>and the right brace </span></span><span>} we have what is called the <em>body</em> of the function. </span><span><span>The statement</span></span></p><p><code>int a, s;</code></p><p><span><span>is called a <em>declaration</em>. The parts of a line after double slashes ( // )are <em>comments</em> which help to explain the program but have no effect when the program is run. The asterisk ( * ) is used to denote multiplication.</span></span></p><p><span><span>All of these terms will be explained in detail in due course.</span></span></p></span>Adminhttp://www.blogger.com/profile/17184499275006958237noreply@blogger.comtag:blogger.com,1999:blog-5238934573315809111.post-4388028724803283902011-05-08T11:01:00.001-07:002011-05-08T11:01:10.125-07:00Develop the Algorithm<span class="Apple-style-span" style="font-family: Arial, Helvetica; font-size: small; "><h2>Develop the Algorithm</h2><div class="KonaBody" style="word-wrap: break-word; "><p><span>Using the notion of an algorithm and the concept of a variable, we develop the following algorithm for calculating the area of a square, given one side:</span></p><p></p><p><em><span>Algorithm for calculating area of square, given one side</span></em></p><p></p><p><span>(1)<span> </span>Ask the user for the length of a side</span></p><p><span><span>(2)<span> </span>Store the value in the box </span><span>s</span></span></p><p><span><span>(3)<span> </span>Calculate the area of the square (</span><span>s</span><span> </span></span><span><span>times</span></span><span><span> </span><span>s</span><span>)</span></span></p><p><span><span>(4)<span> </span>Store the area in the box </span><span>a</span></span></p><p><span><span>(5)<span> </span>Print the value in box </span><span>a</span><span>, appropriately labelled</span></span></p><p><span>(6)<span> </span>Stop</span></p><p></p><p><span>When an algorithm is developed, it must be checked to make sure that it is doing its intended job correctly. We can test an algorithm by ‘playing computer,' that is, we execute the instructions by hand, using appropriate data values. This process is called <em>dry running</em> or <em>desk checking</em> the algorithm. It is used to pinpoint any errors in logic before the computer program is actually written. We should <em>never</em> start to write programming code unless we are confident that the algorithm is correct. Here, the algorithm is fairly simple, so it is easy to check that it is indeed correct.</span></p></div></span>Adminhttp://www.blogger.com/profile/17184499275006958237noreply@blogger.comtag:blogger.com,1999:blog-5238934573315809111.post-16623982727136441832011-05-08T11:00:00.000-07:002011-05-08T11:01:02.268-07:00Data and Variables<span class="Apple-style-span" style="font-family: Arial, Helvetica; font-size: small; "><div class="KonaBody" style="word-wrap: break-word; "><p><span>All computer programs, except the most trivial, are written to operate on <em>data</em>. For example:</span></p><p></p><p><span>•<span> </span>the data for an action game might be keys pressed or the position of the cursor when the mouse is clicked;</span></p><p><span>•<span> </span>the data for a word processing program are the keys pressed while you are typing a letter;</span></p><p><span>•<span> </span>the data for an accounting program would include, among other things, expenses and income;</span></p><p><span>•<span> </span>the data for a program that teaches Spanish could be an English word that you type in response to a question.</span></p><p></p><p><span>For a program to be run, it must be stored in the computer’s memory. When data is supplied to a program, that data is also stored in memory. Thus we think of memory as a place for holding programs and data. One of the nice things about programming in a high-level language (like C or Java) is that you don’t have to worry about <em>which</em> memory locations are used to store your data. But how do we refer to an item of data, given that there may be many data items in memory?</span></p><p><span><span>Think of memory as a set of boxes (or storage locations). Each box can hold one item of data, for example, one number. We can give a name to a box, and we will be able to refer to that box by the given name. In our example, we will need two boxes, one to hold the side of the square and one to hold the area. We will call these boxes </span><span>s</span><span> and </span><span>a</span><span>, respectively.</span></span></p><p></p><p><span><span>If we wish, we can change the value in a box at any time; since the values can vary, </span><span>s</span><span> and </span><span>a</span><span> are called variable names, or simply <em>variables. </em>Thus a variable is a name associated with a particular memory location or, if you wish, it is a <em>label</em> for the memory location. We can speak of giving a variable a value, or setting a variable to a specific value, such as </span></span><span>1, for instance. Important points to remember are:</span></p><p><span>•<span> </span>a box can hold only one value at a time; if we put in a new value, the old one is lost;</span></p><p><span>•<span> </span>we must not assume that a box contains <em>any</em> value unless we specifically store a value in the box. In particular, we must not assume that the box contains </span><span>0</span><span>.</span></p><p></p><p><span>Variables are a common feature of computer programs. It is very difficult to imagine what programming would be like without them. In everyday life, we often use variables. For example, we speak of an ‘address’. Here, ‘address’ is a variable whose value depends on the person under consideration. Other common variables are telephone number, name of school, subject, size of population, type of car, television model, etc. (What are some possible values of these variables?)</span></p><p><span>Now that we know a little bit about variables, we are ready to develop the algorithm for calculating the area of a square.</span></p></div></span>Adminhttp://www.blogger.com/profile/17184499275006958237noreply@blogger.comtag:blogger.com,1999:blog-5238934573315809111.post-81612389511225213092011-05-08T10:59:00.001-07:002011-05-08T10:59:55.265-07:00Write an algorithm to solve the problem<span class="Apple-style-span" style="font-family: Arial, Helvetica; font-size: small; "><p>An <em>algorithm</em> is a set of instructions which, if faithfully followed, will produce a solution to a given problem or perform some specified task. When an instruction is followed, we say it is <em>executed</em>. We can speak of an algorithm for finding a word in a dictionary, for changing a punctured tyre or for playing a video game.</p><p>For any problem, there will normally be more than one algorithm to solve it. Each algorithm will have its own advantages and disadvantages. When we are searching for a word in the dictionary, one method would be to start at the beginning and look at each word in turn. A second method would be to start at the end and search backwards. Here, an advantage of the first method is that it would find a word faster if it were at the beginning, while the second method would be faster if the word were towards the end.</p><p>Another method for searching for the word would be one which used the fact that the words in a dictionary are in alphabetical order—this is the method we all use when looking up a word in a dictionary. In any situation, a programmer would usually have a choice of algorithms, and it is one of her more important jobs to decide which algorithm is the best, and why this is so.</p><p>In our example, we must write the instructions in our algorithm in such a way that they can be easily converted into a form which the computer can follow. Computer instructions fall into three main categories:</p><p>(1)<span> </span><em>Input</em> instructions, used for supplying data from the ‘outside world’ to a program; this is usually done via the keyboard or a file.</p><p>(2)<span> </span><em>Processing</em> instructions, used for manipulating data inside the computer. These instructions allow us to add, subtract, multiply and divide; they also allow us to compare two values, and act according to the result of the comparison. Also, we can move data from one location in the computer’s memory to another location.</p><p>(3)<span> </span><em>Output</em> instructions, used for getting information out of the computer to the outside world.</p><span><br /></span></span>Adminhttp://www.blogger.com/profile/17184499275006958237noreply@blogger.comtag:blogger.com,1999:blog-5238934573315809111.post-88777687105549438752010-12-12T01:49:00.000-08:002010-12-12T03:35:51.874-08:00Conventions (Rules) for creating Variable<span class="Apple-style-span">1. First Character of variable should be an alphabet</span><div><span class="Apple-style-span"><br /></span></div><div><span class="Apple-style-span">2. The name of <a href="http://c-overview.blogspot.com/search/label/VARIABLES"><b><span class="Apple-style-span" >variables</span> </b></a>can be in lower case as well as upper case.</span></div><div><span class="Apple-style-span"><br /></span></div><div><span class="Apple-style-span">3. Two or more variables cannot have same name.</span></div><div><span class="Apple-style-span"><br /></span></div><div><span class="Apple-style-span">4. It Cannot contain any space in between variable name.</span></div><div><span class="Apple-style-span"><br /></span></div><div><span class="Apple-style-span">5. It cannot contain Period Sign ( . )</span></div><div><span class="Apple-style-span"><br /></span></div><div><span class="Apple-style-span">6. We cannot use keywords as variable names i.e. variable name like int, float, if are invalid.</span></div><div><span class="Apple-style-span"><br /></span></div><div><span class="Apple-style-span">7. No special symbol is allowed in variable name.</span></div><div><span class="Apple-style-span"><br /></span></div><div><span class="Apple-style-span">8. A <a href="http://c-overview.blogspot.com/search/label/DECLARE%20VARIABLE">variable name</a> should not be more than 40 characters, but it is safer to declare variable name as short as possible</span></div><div><span class="Apple-style-span"><br /></span></div><div><a href="http://c-overview.blogspot.com/search/label/DECLARE%20VARIABLE"><span class="Apple-style-span">Example</span></a></div><div><span class="Apple-style-span">int a,b,c;</span></div><div><span class="Apple-style-span">OR</span></div><div><span class="Apple-style-span">int a=10, b=20, c;</span></div>Adminhttp://www.blogger.com/profile/17184499275006958237noreply@blogger.comtag:blogger.com,1999:blog-5238934573315809111.post-25989054292807997032010-12-12T01:41:00.000-08:002010-12-12T01:48:45.033-08:00Definition of Variables<div><span class="Apple-style-span" style="font-family: verdana; "><b>Variable</b> is a quantity, which my vary during program execution. Basically variable are name given to the location in the <b>memory of computer</b>, where different constants are stored. If a program works on some variables then we need to<b> <a href="http://c-overview.blogspot.com/search/label/DECLARE%20VARIABLE">declare these variables</a></b> with the help of data types</span></div>Adminhttp://www.blogger.com/profile/17184499275006958237noreply@blogger.comtag:blogger.com,1999:blog-5238934573315809111.post-56836092618260283712010-12-06T11:14:00.000-08:002010-12-06T11:23:21.703-08:00Histoy of C Language<a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_urRtV3T_f1g/TP03IXjNb4I/AAAAAAAAAmM/OuupMlpDCUw/s1600/ritchie.jpeg"><img style="float: left; margin: 0pt 10px 10px 0pt; cursor: pointer; width: 231px; height: 320px;" src="http://2.bp.blogspot.com/_urRtV3T_f1g/TP03IXjNb4I/AAAAAAAAAmM/OuupMlpDCUw/s320/ritchie.jpeg" alt="" id="BLOGGER_PHOTO_ID_5547650932890496898" border="0" /></a><span style="font-weight: bold;font-size:180%;" >Histoy of C Language</span><br /><br />The development of Unix in the C language made it uniquely portable and improvable.<br /><br />The first version of Unix was written in the low-level PDP-7 assembler language. Soon after, a language called TMG was created for the PDP-7 by R. M. McClure. Using TMG to develop a FORTRAN compiler, Ken Thompson instead ended up developing a compiler for a new high-level language he called B, based on the earlier BCPL language developed by Martin Richard. Where it might take several pages of detailed PDP-7 assembly code to accomplish a given task, the same functionality could typically be expressed in a higher level language like B in just a few lines. B was thereafter used for further development of the Unix system, which made the work much faster and more convenient.<br /><br />When the PDP-11 computer arrived at Bell Labs, Dennis Ritchie built on B to create a new language called C which inherited Thompson's taste for concise syntax, and had a powerful mix of high-level functionality and the detailed features required to program an operating system. Most of the components of Unix were eventually rewritten in C, culminating with the kernel itself in 1973. Because of its convenience and power, C went on to become the most popular programming language in the world over the next quarter century.Adminhttp://www.blogger.com/profile/17184499275006958237noreply@blogger.comtag:blogger.com,1999:blog-5238934573315809111.post-78932808038253565072010-12-02T09:01:00.003-08:002010-12-02T09:10:11.887-08:00Definition of Algorithms OR What are Algorithms<span class="Apple-style-span" style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; color: rgb(34, 34, 34); line-height: 18px; "><span class="Apple-style-span" style="font-size: medium; "><b><div><span class="Apple-style-span" style="font-size: x-large;">Definition of Algorithms OR What are Algorithms</span></div><div><span class="Apple-style-span" style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; color: rgb(34, 34, 34); line-height: 18px; "><span class="Apple-style-span" style="font-size: medium; "><b><br /></b></span></span></div>Algorithms</b> are used to solve a problem step by step using english language. It is the part of <b>software designing</b>. An algorithm defines as the step by step procedure or method that can be carried out for solving<b>programming problems</b>. An algorithm tells computer that how to solve problem systematically to get desired output. Mainly we follow following steps to design an algorithm.</span><div><span class="Apple-style-span" style="font-size: medium; "><br /></span></div><div><span class="Apple-style-span" style="font-size: medium; "><b>Step 1 - START</b></span></div><div><span class="Apple-style-span" style="font-size: medium; "><i>It represents beginning of the algorithm.</i></span></div><div><span class="Apple-style-span" style="font-size: medium; "><br /></span></div><div><span class="Apple-style-span" style="font-size: medium; "><b>Step 2 - DECLARE</b></span></div><div><span class="Apple-style-span" style="font-size: medium; "><i>The variables used in algorithm are declared in this step.</i></span></div><div><span class="Apple-style-span" style="font-size: medium; "><br /></span></div><div><span class="Apple-style-span" style="font-size: medium; "><b>Step 3 - INPUT</b></span></div><div><span class="Apple-style-span" style="font-size: medium; "><i>Here we input the values</i></span></div><div><span class="Apple-style-span" style="font-size: medium; "><br /></span></div><div><span class="Apple-style-span" style="font-size: medium; "><b>Step 4 - FORMULA</b></span></div><div><span class="Apple-style-span" style="font-size: medium; "><i>The required result is generated in this step.</i></span></div><div><span class="Apple-style-span" style="font-size: medium; "><br /></span></div><div><span class="Apple-style-span" style="font-size: medium; "><b>Step 5 - OUTPUT</b></span></div><div><span class="Apple-style-span" style="font-size: medium; "><i>It displays the output or result.</i></span></div><div><span class="Apple-style-span" style="font-size: medium; "><br /></span></div><div><span class="Apple-style-span" style="font-size: medium; "><b>Step 6 - STOP</b></span></div><div><span class="Apple-style-span" style="font-size: medium; "><i>It is an end of Algorithm</i></span></div></span>Adminhttp://www.blogger.com/profile/17184499275006958237noreply@blogger.comtag:blogger.com,1999:blog-5238934573315809111.post-21832483415950533692010-12-02T09:01:00.001-08:002010-12-02T09:01:31.361-08:00Advantages of C<span class="Apple-style-span" style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; color: rgb(34, 34, 34); line-height: 18px; "><b><span class="Apple-style-span">C is Portable</span></b><div>This means a program written for one computer may run successfully on other computer also.</div><div><br /></div><div><b><span class="Apple-style-span">C is fast</span></b></div><div>This means that the<b> executable program</b> obtained after compiling and linking runs very fast.<br /><div><br /></div></div><div><b><span class="Apple-style-span">C is compact</span></b></div><div>The statements in<b> C Language</b> are generally short but very powerful.</div><div><br /></div><div><b><span class="Apple-style-span">Simple / Easy</span></b></div><div>The C Language has both the simplicity of <b>High Level Language</b> and speed of<b>Low Level Language</b>. So it is also known as Middle Level Language</div><div><br /></div><div><b><span class="Apple-style-span">C has only 32 Keywords</span></b></div><div><br /></div><div><b><span class="Apple-style-span">C Compiler is easily available</span></b></div><div><br /></div><div>C has ability to extend itself. Users can add their own functions to the C Library</div></span>Adminhttp://www.blogger.com/profile/17184499275006958237noreply@blogger.comtag:blogger.com,1999:blog-5238934573315809111.post-57958481504300437572010-12-02T08:58:00.000-08:002010-12-02T09:01:04.284-08:00Introduction to C Language<p class="MsoNormal" style="line-height:16.85pt"><span class="Apple-style-span"><span class="Apple-style-span" style="font-size: 18px;"><b></b></span></span></p><span class="Apple-style-span"><b><p class="MsoNormal" style="line-height:16.85pt"><span class="Apple-style-span"><span class="Apple-style-span" style="font-size: 18px;"><span class="Apple-style-span" style="font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-weight: normal; font-size: medium; ">The <span style="font-weight: bold; ">C Language</span> is one of the most powerful language. In the beginning due to the different architectures available, the programmer had to learn different languages to create different types of applications. To overcome this problem in 1972, <b>Dennis Ritche</b> at the<b>AT & T Bell Laboratories, USA </b>developed C Language.<br /><br />It is a combination of<b> BCPL</b> and <b>B Language</b>. The problem with <b>Low Level Languages</b> is that they are very difficult for the humans to understand and developed. On the other hand, <b>High Level Languages</b> are easy for the humans to understand but slow in speed.<br /><br />The <b>C Language</b> is high speed and easy to understand, so it is also term as <b>Middle Level Language.</b></span></span></span></p></b></span><p></p>Adminhttp://www.blogger.com/profile/17184499275006958237noreply@blogger.comtag:blogger.com,1999:blog-5238934573315809111.post-15410744889083668432007-08-28T21:36:00.005-07:002010-12-02T09:03:05.422-08:00DECLARE ARRAYS<ol style="margin-top: 0in;" start="1" type="1"><li class="MsoNormal" style="">An array must be declared with type of variable or data type</li><li class="MsoNormal" style="">An array contains same type of data<br /> e.g. – int a [10]</li><li class="MsoNormal" style="">The name of array cannot be same as that of any other variable within the function.</li><li class="MsoNormal" style="">The size of array is specified using sub-script notation (index)</li><li class="MsoNormal" style="">THE sub-script indicates that how many elements are to be allocated to an array.</li><li class="MsoNormal" style="">A sub-script used to declare an array is called dimension</li></ol>Adminhttp://www.blogger.com/profile/17184499275006958237noreply@blogger.com