tag:blogger.com,1999:blog-42560309484138029312024-03-12T19:48:35.162-07:00Learn C by example in 5 hoursLearn C by example in 5 hours,c tutorial,learn c online,learn c video,learn programming,learn objective c,learn c tutorial,learn c language,learn c books,C tutorial on-lineRimawihttp://www.blogger.com/profile/10816234485402195660noreply@blogger.comBlogger1125tag:blogger.com,1999:blog-4256030948413802931.post-5929753161191874372011-05-17T02:26:00.000-07:002011-10-24T04:19:27.165-07:00Learn C by example in 5 hours<div dir="ltr" style="text-align: left;" trbidi="on"><div style="text-align: center;"><br />
</div><h1 style="text-align: center;"><a href="http://learncin5hours.blogspot.com/2011/05/learn-c-by-example-in-5-hours.html">Learn C by example in 5 hours,C tutorial on-line.</a></h1><div><br />
</div><hr /><span class="Apple-style-span" style="line-height: 18px;"><span class="Apple-style-span" style="font-family: inherit;">The tutorials in this site are aimed primarily at beginning level programmers, including those who have little to no prior programming experience. Intermediate level programmers will probably also find plenty of tips and </span></span><br />
<span class="Apple-style-span" style="line-height: 18px;"><span class="Apple-style-span" style="font-family: inherit;">tricks that may be of use in improving their programming skills.</span></span><br />
<span class="Apple-style-span" style="line-height: 18px;"><span class="Apple-style-span" style="font-family: inherit;"><br />
</span></span><br />
Have you always wanted to master a programming language. <span class="Apple-style-span" style="line-height: 18px;"><span class="Apple-style-span" style="font-family: inherit;">The C programming language is a popular and widely used programming language for creating <b>computer programs</b>. Programmers around the world embrace C because it gives maximum control and efficiency to the programmer.</span></span><br />
<span class="Apple-style-span" style="font-family: inherit;"><span class="Apple-style-span" style="line-height: 18px;">If you are a programmer, or if you are interested in becoming a programmer, there are a couple of benefits you gain from learning C,</span>Well atleast the basics that will help you to build more powerful programs.You say I can't show you C in 5 hours.</span><br />
<span class="Apple-style-span" style="font-family: inherit;"><br />
</span><br />
<span class="Apple-style-span" style="font-family: inherit;"><br />
</span><br />
<span class="Apple-style-span" style="font-family: inherit;"></span><br />
<div class="separator" style="clear: both; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-align: center;"><span class="Apple-style-span" style="font-family: inherit;"><a href="http://1.bp.blogspot.com/-puYb9sw3iJQ/TdI9jkCYz3I/AAAAAAAAAJw/hlpxkbMXC1k/s1600/Learn+C+.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="194" src="http://1.bp.blogspot.com/-puYb9sw3iJQ/TdI9jkCYz3I/AAAAAAAAAJw/hlpxkbMXC1k/s320/Learn+C+.gif" style="cursor: move;" width="320" /></a></span></div><div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-align: center;"><span class="Apple-style-span" style="font-family: inherit;"><span style="font-size: large;"><span style="font-size: large;"><span style="font-size: large;"><span style="font-size: large;"><span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: 13px; font-weight: bold;">This animation shows the execution of a simple C program</span></span></span></span></span></span></div><div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"></div><br />
<div style="text-align: center;"><span class="Apple-style-span" style="color: #0000ee; font-size: 32px; font-weight: bold;"><a href="http://learncin5hours.blogspot.com/">Learn C by example in 5 hours</a></span></div><hr />A simple hello program.<br />
<span style="font-weight: bold;"><span class="Apple-style-span" style="font-weight: normal;">demonstrates the const function in all c programs--the main() function.</span></span><br />
<span class="Apple-style-span" style="color: #0000ee; font-size: 32px; font-weight: bold;"><a href="http://learncin5hours.blogspot.com/">Learn C by example in 5 hours</a></span><span style="font-weight: bold;"><span class="Apple-style-span" style="font-weight: normal;"><br />
</span><span class="Apple-style-span" style="color: blue; font-size: large;">Example 1</span></span><br />
<span style="font-weight: bold;"><span class="Apple-style-span" style="color: blue;"><br />
</span> main()<br />
{<br />
puts("hello world guess who is writing a c program");<br />
return(0);<br />
}<br />
</span><br />
<span style="font-weight: bold;"><br />
</span><br />
That's it. In all c programs there is a main function which is followed by a { and closed by a } after a return()function.It doesn't have to be return(0) but that depends upon the type of c compiler you have. Check your compiler before you start your programming.<br />
<br />
<span style="font-weight: bold;"><span class="Apple-style-span" style="font-weight: normal;"> You saw above that puts function is used to put a whole sentence on the screen; but are there functions that will put characters on the screen/take characters: Yes and next is a table of what they are and what they do. Read them and the examples that follow.</span></span><br />
<br />
<br />
<table border="1" style="font-weight: bold;"><tbody>
<tr><th align="left">getchar()</th> <th>Gets a single character from the input/keyboard.</th></tr>
<tr> </tr>
</tbody></table><table border="1" style="font-weight: bold;"><tbody>
<tr><th align="left">putchar()</th> <th>Puts a single character on the screen.</th></tr>
<tr> </tr>
</tbody></table><br />
<br />
The printf function is a function used to print the output to the screen.printf() needs to know if the output is an integer,real,etc<br />
<span class="Apple-style-span" style="color: #0000ee; font-size: 32px; font-weight: bold;"><a href="http://learncin5hours.blogspot.com/">Learn C by example in 5 hours</a></span><br />
<b style="font-weight: bold;"><span class="Apple-style-span" style="font-size: large;"><span class="Apple-style-span" style="color: blue;">Example 2</span> </span></b><br />
<b style="font-weight: bold;"><span class="Apple-style-span" style="font-size: large;"><br />
</span></b><br />
<b style="font-weight: bold;">main()<br />
{<br />
printf(hello);<br />
}</b><br />
<span style="font-weight: bold;"><br />
<span class="Apple-style-span" style="font-weight: normal;"> Assuming hello was defined earlier say by #define hello "Hello!" the output is Hello!. But if the output is an integer then %d has to be attatched to the printf statement.<br />
</span></span><br />
This above can be shown as printf("I am %d years old",12) which will result in the following result:I am 12 years old<br />
<br />
The %d tells that an integer is to be placed here.<br />
<br />
Now we will look into a function called scanf().This lets you input from the kewyboard and for that input to be taken by the program and processed.Once again it is important to tell scanf() what type of data is being scanned.<br />
<br />
<span style="font-weight: bold;"><span class="Apple-style-span" style="font-weight: normal;"> Here is an example of a program that demonstrates both scanf and printf in unison.</span></span><br />
<span class="Apple-style-span" style="color: #0000ee; font-size: 32px; font-weight: bold;"><a href="http://learncin5hours.blogspot.com/">Learn C by example in 5 hours</a></span><br />
<span class="Apple-style-span"></span><br />
<span class="Apple-style-span" style="color: blue; font-size: large; font-weight: bold;">Example 3 </span><br />
<span style="font-weight: bold;"><br />
</span><br />
<b><b> main() {<br />
int count;<br />
puts("Please enter a number: ");<br />
scanf("%d", &count);<br />
printf("The number is %d",count);<br />
}<br />
</b></b><br />
<b><b><br />
</b></b><br />
<hr />That concludes the first hour of your tutorial.Now this is a list of data type identifiers.<br />
<br />
<br />
%f=float %c=char %s =s tring %e=inputs number in scientific notation.<br />
<br />
<br />
As you saw in the first hour of our tutorial c is a language in which you program using functions. Functions are usually identified by the following characteristic:>> functionname() In c the main() function is essential. Think of it as a constant function for all your programs and all other functions can be accessed from the main().Before I show you how we do that let us have an example where we want to pause a program before the screen is changed. This would involve the foll- owing procedure:>> write a main function then use puts function to put statements on the screen like we did in section 1 above and then before the next set of puts statements declare a pause.<br />
<br />
This is how it is done:<br />
<span class="Apple-style-span" style="color: #0000ee; font-size: 32px; font-weight: bold;"><a href="http://learncin5hours.blogspot.com/">Learn C by example in 5 hours</a></span><br />
<span class="Apple-style-span" style="color: blue; font-size: large;"><b>Example 4 </b></span><br />
<span class="Apple-style-span" style="color: blue;"><br />
</span><b> main()</b><br />
<b> {</b><br />
<b> puts("hello there");</b><br />
<b> puts("what is your name?")</b><br />
<b> pause()</b><br />
<b> puts("It is nice to meet you")</b><br />
<b> }</b><br />
<b> pause();</b><br />
<b> {</b><br />
<b> int move_on;</b><br />
<b> printf("press entere to continue");</b><br />
<b> move_on=getchar();</b><br />
<b> return(0);</b><br />
<b> }</b><br />
<br />
This above will pause until a key is pressed on the keyboard. Granted that the above program makes no sense from a practical point of view but I want to show is the use of another function inside the main function.<br />
<br />
C has many functions that comes with it. See your compiler manual to see what you have.Now we are going to look at conditions in c programming:>> the if command and do command.<br />
<br />
Here is an example of th if command:<br />
<span class="Apple-style-span" style="color: #0000ee; font-size: 32px; font-weight: bold;"><a href="http://learncin5hours.blogspot.com/">Learn C by example in 5 hours</a></span><br />
<span class="Apple-style-span" style="color: blue; font-size: large;"><b>Example 5 </b></span><br />
<span class="Apple-style-span" style="color: blue;"><br />
</span><b> main()</b><br />
<b> {</b><br />
<b> float cost,tax,luxury,total;</b><br />
<b> luxury=0.0;</b><br />
<b> printf("Enter the cost of the item: ");</b><br />
<b> scanf("%f", &cost);</b><br />
<b> tax=cost*0.06;</b><br />
<b> if(cost>40000.0)</b><br />
<b> luxury=cost*0.005;</b><br />
<b> total=cost+tax+luxury;</b><br />
<b> printf("the total cost is %0.2f",total);</b><br />
<b> }</b><br />
<br />
This is a simple example of one if statement. Another If statement is the if -else statement. This can be shown as this <br />
<span class="Apple-style-span" style="color: #0000ee; font-size: 32px; font-weight: bold;"><a href="http://learncin5hours.blogspot.com/">Learn C by example in 5 hours</a></span><br />
<span class="Apple-style-span" style="color: blue; font-size: large;"><b>Example 6 </b></span><br />
<span class="Apple-style-span" style="color: blue;"><br />
</span><b> if(cost >40000)</b><br />
<b> {</b><br />
<b> luxury=cost*0.005;</b><br />
<b> printf("The luxury tax is %.2f",luxury);</b><br />
<b> }</b><br />
<b> else</b><br />
<b> {</b><br />
<b> puts("There is no luxury tax for the items");</b><br />
<b> luxury=0.0;</b><br />
<b> }</b><br />
<br />
Now the format a do statement is as follows:<br />
<br />
<b> do</b><br />
<b> {</b><br />
<b> instruction;</b><br />
<b> instruction</b><br />
<b> }</b><br />
<b> while(condition);</b><br />
<br />
The format for a FOR statement is as follows:<br />
<br />
<b> for(initial=value;condition;increment)</b><br />
<b> instruction;</b><br />
<br />
Now for an example:<br />
<span class="Apple-style-span" style="color: #0000ee; font-size: 32px; font-weight: bold;"><a href="http://learncin5hours.blogspot.com/">Learn C by example in 5 hours</a></span><br />
<span class="Apple-style-span" style="color: blue; font-size: large;"><b>Example 7 </b></span><br />
<b><br />
</b><br />
<span class="Apple-style-span" style="color: blue; font-size: large;"><b></b></span><b> main()</b><br />
<b> {</b><br />
<b> int row,column;</b><br />
<b> puts("\t\tMY Handy multipication table");</b><br />
<b> for(row=1;tow<=10;row++)</b><br />
<b> {</b><br />
<b> for(column=1;column<=10;column++)</b><br />
<b> printf("%6d", row*column);</b><br />
<b> putchar('\n');</b><br />
<b> }</b><br />
<b> }</b><br />
<br />
The output is a multipication table of 10x10 size.<br />
<span class="Apple-style-span" style="color: #0000ee; font-size: 32px; font-weight: bold;"><a href="http://learncin5hours.blogspot.com/">Learn C by example in 5 hours</a></span><br />
<span class="Apple-style-span" style="background-color: white;"><span class="Apple-style-span" style="color: blue; font-size: large;"><b>Example 8 </b></span></span><br />
<span class="Apple-style-span" style="background-color: white;"><span class="Apple-style-span" style="color: blue;"><br />
</span></span><b> main()</b><br />
<b> {</b><br />
<b> int temp;</b><br />
<b> float celsius;</b><br />
<b> char repeat;</b><br />
<b> do</b><br />
<b> {</b><br />
<b> printf("Input a temperature:");</b><br />
<b> scanf("%d", &temp);</b><br />
<b> celsius=(5.0/9.0)*(temp-32);</b><br />
<b> printf(%d degrees F is %6.2f degrees celsius\n",temp, celsius);</b><br />
<b> printf(("do you have another temperature?");</b><br />
<b> repeat=getchar();</b><br />
<b> putchar('\n');</b><br />
<b> }</b><br />
<b> while(repeat=='y'|| repeat=='y');</b><br />
<b> }</b><br />
<br />
This shows you to how to use the do command for conditional programming in c.<br />
<br />
<br />
<br />
<hr />Now we are in our 3rd hour. Now we will concentrate on arrays:<br />
<br />
What is a flag?<br />
<br />
A flag is an algorithm that informs the program that a certain condition has occured.<br />
<span class="Apple-style-span" style="color: #0000ee; font-size: 32px; font-weight: bold;"><a href="http://learncin5hours.blogspot.com/">Learn C by example in 5 hours</a></span><br />
<span class="Apple-style-span" style="color: blue; font-size: large;"><b>Example 9 </b></span><br />
<div><span class="Apple-style-span" style="color: blue; font-size: large;"><b><br />
</b></span><b> main()</b><br />
<b> {</b><br />
<b> int temp;</b><br />
<b> float celsius;</b><br />
<b> char repeat;</b><br />
<b> char flag;</b><br />
<b> do</b><br />
<b> {</b><br />
<b> flag='n";</b><br />
<b> do</b><br />
<b> {</b><br />
<b> if(flag=='n')</b><br />
<b> printf("Input a valid temperature :");</b><br />
<b> else</b><br />
<b> printf("input a valid temperature,stupid:");</b><br />
<b> scanf("%d",&temp);</b><br />
<b> flag='y';</b><br />
<b> }</b><br />
<b> while (temp<0||temp >100);</b><br />
<b> celsius=(5.0/9.0)*(temp-32);</b><br />
<b> printf("%d degrees F is %6.2f degrees celsius\n",temp,celsius);</b><br />
<b> printf("Do you have another temperature?");</b><br />
<b> repeat=getchar();</b><br />
<b> putchar('\n');</b><br />
<b> }</b><br />
<b> while (repeat=='y' || repeat=='Y");</b><br />
<b> }</b><br />
<br />
That was an example of how flags work.<br />
<br />
What is the break command?<br />
<br />
<br />
The break command ends the loop in which it is placed just as if the while condition, or the condition in a for loop becomes false. <br />
<br />
<b style="font-weight: bold;"><span style="font-size: large;">How to declare an array?</span></b><span style="font-size: large; font-weight: bold;"><br />
</span><br />
<span style="font-size: large;"><span style="font-size: small;"> An array can be defined as follows:<br />
<br />
<b> int temp[5]={45,56,12,98,12};</b><br />
<br />
This would mean the following:<br />
<br />
<b> temp[0]=45....temp[4]=12</b><br />
<br />
This was a single dimension array with 5 elements of the integer type.If you wanted to depict float variables just use float temp instead of int temp.<br />
</span></span><br />
<span style="font-size: large;"><span style="font-size: small;"> Let us now see an example of using an array for two tasks.<br />
</span></span><br />
<span style="font-size: large; font-weight: bold;"><span style="font-size: small;"> main()<br />
{<br />
int temps[31];<br />
int index,total;<br />
float average,celsius;<br />
total=0.0;<br />
for(index=0;index<31;index++)<br />
{<br />
printf("enter temperature #%d:",index);<br />
scanf("%d",&temps[index]);<br />
}<br />
for(index=0;index<31;index++)<br />
total+=temps[index];<br />
average=total/31.0<br />
printf("average is:%f\n\n", average);<br />
puts9"fahrenheit\tcelsius\n");<br />
for(index=0;index<31;index++)<br />
{<br />
celsius=(5.0/9.0)*(temps[index]-32);<br />
printf("%d\t\t%6.2f\n",temps[index],celsius);<br />
}<br />
}<br />
<br />
</span></span><br />
<span style="font-size: large;"><span class="Apple-style-span" style="font-size: small;"> Now I am going to show you how to pass an array. When you pass an array you are actually passing the address of the array.</span><br />
<span class="Apple-style-span" style="color: #0000ee; font-size: 32px; font-weight: bold;"><a href="http://learncin5hours.blogspot.com/">Learn C by example in 5 hours</a></span><br />
<span style="font-weight: bold;"><span class="Apple-style-span" style="color: blue;">Example 10</span> </span></span><br />
<span style="font-size: large;"><span style="font-weight: bold;"><br />
<span style="font-size: small;"> #define count 31<br />
main()<br />
{<br />
int temps[count];<br />
int index;<br />
float celsius;<br />
for(index=0; index< count;index++)<br />
{<br />
celsius=(5.0/9.0)*(heat[index]-32);<br />
printf("%d\t\t%6.2f\n",heat[index],celsius);<br />
}<br />
}<br />
<br />
</span></span></span><br />
<hr style="font-weight: bold;" /><span style="font-size: large;"><span style="font-size: small;"><span style="font-size: medium;"><span style="font-size: small;"> <br />
Now we are in the fourth hour of our tutorial.We are now going to look at 1)comparing strings 2)determining string lengths. 3) combining strings 4)structures.</span></span></span></span><br />
<div><span style="font-size: large;"><span style="font-size: small;"><span style="font-size: medium;"><span style="font-size: small;"><br />
<span style="font-size: large; font-weight: bold;"> Comparing 2 strings:</span></span></span></span></span><br />
<div><span style="font-size: large;"><span style="font-size: small;"><span style="font-size: medium;"><span style="font-size: small;"><span style="font-size: large;"><span style="font-size: small;"> In c it is not possible to directly compare two strings so a statement like if (string1==string2) is not valid.</span></span><br />
<span style="font-size: large;"><span style="font-size: small;"> Most c libraries contain a function called the strcmp().This is used to compare two strings in the following manner.<br />
<br />
<b> if(strcmp(name1,name2)==0)</b><br />
<b> puts("The names are the same");</b><br />
<b> else</b><br />
<b> puts("The names are not the same.");</b><br />
</span></span><br />
<span style="font-size: large; font-weight: bold;"><span style="font-size: small;"> <span style="font-size: large;">Determining string length.</span></span></span></span></span></span></span></div><div><span style="font-size: large;"><span style="font-size: small;"><span style="font-size: medium;"><span style="font-size: small;"><span style="font-size: large;"><span style="font-size: small;"><span style="font-size: large;"> <span style="font-size: small;">This is done using the strlen() function.</span></span></span></span><br />
<span style="font-size: large;"><span style="font-size: small;"><span style="font-size: large;"><span style="font-size: small;">a simple programming bit showing this function looks like this:<br />
<br />
</span></span></span></span><br />
<span style="font-size: large; font-weight: bold;"><span style="font-size: small;"><span style="font-size: large;"><span style="font-size: small;"> gets(name);<br />
count=strlen(name);<br />
printf("the string %s has %d characters",name,count);<br />
<br />
</span></span></span></span><br />
<span style="font-size: large; font-weight: bold;"><span style="font-size: small;"><span style="font-size: large;"><span style="font-size: small;"><span style="font-size: large;">Combining strings:</span></span></span></span></span></span></span></span></span></div><div><span style="font-size: large;"><span style="font-size: large;"><span style="font-size: large;"><span style="font-size: large;"><span class="Apple-style-span" style="font-size: small;">We use the function strcpy() an example follows:</span><br />
<span class="Apple-style-span" style="color: #0000ee; font-size: 32px; font-weight: bold;"><a href="http://learncin5hours.blogspot.com/">Learn C by example in 5 hours</a></span><br />
<span class="Apple-style-span" style="color: blue;"><b> Example 11</b></span> <br />
</span></span></span><br />
<span style="font-size: large; font-weight: bold;"><span style="font-size: small;"><span style="font-size: large;"><span style="font-size: small;"><span style="font-size: large;"><span style="font-size: small;"> strcpy(name,"Adam");<br />
strcpy(name1,"and eve");<br />
strcat(name,name1);<br />
puts(name);<br />
<br />
</span></span></span></span></span></span><br />
<span style="font-size: large;"><span style="font-size: small;"><span style="font-size: large;"><span style="font-size: small;"><span style="font-size: large;"><span style="font-size: small;"> The assumption being that adam and eve are two values of the variables name1 and name2. The end result is the combination of the 2 names.<br />
</span></span></span></span></span></span><br />
<span style="font-size: large; font-weight: bold;"><span style="font-size: small;"><span style="font-size: large;"><span style="font-size: small;"><span style="font-size: large;"><span style="font-size: small;"> <span style="font-size: large;">What are structures?<br />
</span></span></span></span></span></span></span><br />
<span style="font-size: small;"><span style="font-size: large;"><span style="font-size: small;"><span style="font-size: large;"><span style="font-size: small;"><span style="font-size: large;"><span style="font-size: small;"> A structure variable is a collection of other variables comprising different types.<br />
</span></span></span></span></span></span></span><br />
<span style="font-size: large; font-weight: bold;"><span style="font-size: small;"><span style="font-size: large;"><span style="font-size: small;"><span style="font-size: large;"><span style="font-size: small;"><span style="font-size: large;">What are pointers?<br />
</span></span></span></span></span></span></span><br />
<span style="font-size: large;"><span style="font-size: large;"><span style="font-size: large;"><span class="Apple-style-span" style="font-size: small;">Ponters are variables which refer to the memory locations of other variables.</span><br />
<span class="Apple-style-span" style="font-size: small;"> This is how a structure is defined.</span><br />
<span class="Apple-style-span" style="color: #0000ee; font-size: 32px; font-weight: bold;"><a href="http://learncin5hours.blogspot.com/">Learn C by example in 5 hours</a></span><br />
<span class="Apple-style-span" style="color: blue;"><b>Example 12</b></span> </span></span></span></span><br />
<span style="font-size: large;"><span style="font-size: large;"><span style="font-size: large;"><span style="font-size: large;"></span></span></span><br />
<span style="font-size: large; font-weight: bold;"><span style="font-size: small;"><span style="font-size: large;"><span style="font-size: small;"><span style="font-size: large;"><span style="font-size: small;">struct cd<br />
{<br />
char name[20];<br />
char description[40];<br />
char category[12];<br />
float cost;<br />
int number;<br />
};<br />
main()<br />
</span></span></span></span></span></span><br />
<span style="font-size: large;"><span style="font-size: small;"><span style="font-size: large;"><span style="font-size: small;"><span style="font-size: large;"><span style="font-size: small;"> Notice how the main function comes after the definition of the structure. In the example above the cd was a cd disk and I was writing the definition of a cd collection program.<br />
</span></span></span></span></span></span><br />
<span style="font-size: large; font-weight: bold;"><span style="font-size: small;"><span style="font-size: large;"><span style="font-size: small;"><span style="font-size: large;"><span style="font-size: small;"> </span></span></span></span></span></span><br />
</span><br />
<hr style="font-size: medium; font-weight: bold;" /><span style="font-size: large;"><span style="font-size: large;"><span style="font-size: small;"><span style="font-size: large;"><span style="font-size: small;"><span style="font-size: large;"><span style="font-size: small;"> Now in the fifth hour I will show you how to output your data onto a disk.After all what is the use of the program if you can't save output to a disk.<br />
<br />
Inorder to do this we have to use a pointer. The pointer in this case is FILE. The syntax to declare a file is :FILE*file_ponter;<br />
<br />
The link between your program, the file and the computer is established with the fopen() function using the syntax shown below: <br />
<b> pointer=fopen("FILENAME","mode");</b><br />
<br />
<br />
For example to create a file by the name cd.dat we do the following:<br />
<br />
<b> FILE*cdfile;</b><br />
<b> cdfile=fopen("CD>DAT","w");</b></span></span></span></span></span></span></span></div><div><span style="font-size: large;"><span style="font-size: small;"><span style="font-size: medium;"><span style="font-size: small;"><span style="font-size: large;"><span style="font-size: small;"><span style="font-size: large;"><span style="font-size: small;"><span style="font-size: large;"><span style="font-size: small;"><b><br />
</b> If you will be reading from the file above use "r" instead "w" in the <br />
second sentence.<br />
<br />
In order to rpint information use the following command:<br />
<b> FILE*cdfile;</b><br />
<b> cdfile=fopen("PRN","w");</b><br />
A file is closed by using the fclose() command.Next we will look at an exam ple of reading from a file.<br />
<span class="Apple-style-span" style="color: #0000ee; font-size: 32px; font-weight: bold;"><a href="http://learncin5hours.blogspot.com/">Learn C by example in 5 hours</a></span><br />
<span style="font-size: large; font-weight: bold;"><span class="Apple-style-span" style="color: blue;">Example 13</span> </span></span></span></span></span></span></span></span></span></span></span><br />
<span style="font-size: large;"><span style="font-size: large;"><span style="font-size: large;"><span style="font-size: large;"><span style="font-size: large;"> </span><b><br />
</b> <span style="font-size: large;"><b style="font-size: medium;"> #include "stdio.h"</b><br />
<b style="font-size: medium;"> main()</b><br />
<b style="font-size: medium;"> {</b><br />
<b style="font-size: medium;"> FILE*fp;</b><br />
<b style="font-size: medium;"> int letter;</b><br />
<b style="font-size: medium;"> if((fp=fopen("MYFILE","r"))==NULL)</b><br />
<b style="font-size: medium;"> {</b><br />
<b style="font-size: medium;"> puts("Cannot oepn the file");</b><br />
<b style="font-size: medium;"> exit();</b><br />
<b style="font-size: medium;"> }</b><br />
<b style="font-size: medium;"> while((letter=fgetc(fp)) !=eof)</b><br />
<b style="font-size: medium;"> printf("%c",letter);</b><br />
<b style="font-size: medium;"> fclose(fp);</b><br />
<b style="font-size: medium;"> }</b><br />
<br />
<span class="Apple-style-span" style="font-size: small;"> The eof statement means end of file and this is included in the stdio.h header file which was declared at the start of the example. The stdio.h header file is one of many that comes with your compiler. So check your compiler specifics for other header files which will help perform other functions.</span><br />
</span></span></span></span></span><br />
<span style="font-size: large;"><span style="font-size: large;"><span style="font-size: large;"><span style="font-size: large;"> <span style="font-size: large;"><span style="font-size: small;">In this Site you </span></span></span></span></span></span>Learn C in 5 hours Now you should be in a position to write simple programs in C and know every most common thing in C.<br />
<span style="font-size: large;"><span style="font-size: small;"><span style="font-size: medium;"><span style="font-size: small;"><span style="font-size: large;"><span style="font-size: small;"><span style="font-size: large;"><span style="font-size: small;"><span style="font-size: large;"><span style="font-size: small;"><span style="font-size: large;"><span style="font-size: small;"><br />
</span></span></span></span></span></span></span></span></span></span></span></span><br />
<span style="font-size: large;"><span style="font-size: small;"><span style="font-size: medium;"><span style="font-size: small;"><span style="font-size: large;"><span style="font-size: small;"><span style="font-size: large;"><span style="font-size: small;"><span style="font-size: large;"><span style="font-size: small;"><span style="font-size: large;"><span style="font-size: small;"><br />
</span></span></span></span></span></span></span></span></span></span></span></span><br />
<div style="text-align: center;"><span style="font-size: large;"><span style="font-size: small;"><span style="font-size: medium;"><span style="font-size: small;"><span style="font-size: large;"><span style="font-size: small;"><span style="font-size: large;"><span style="font-size: small;"><span style="font-size: large;"><span style="font-size: small;"><span style="font-size: large;"><span style="font-size: small;"><span class="Apple-style-span" style="-webkit-text-decorations-in-effect: underline; color: #0000ee; font-size: 32px; font-weight: bold;"><a href="http://learncin5hours.blogspot.com/">Learn C by example in 5 hours</a>,<a href="http://learncin5hours.blogspot.com/">C tutorial on-line</a>.</span></span></span></span></span></span></span></span></span></span></span></span></span></div></div></div></div></div>Rimawihttp://www.blogger.com/profile/10816234485402195660noreply@blogger.com0