<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Aditech &#187; C</title>
	<atom:link href="http://blog.aditech.info/category/c/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.aditech.info</link>
	<description>There is something for everyone !!</description>
	<lastBuildDate>Mon, 16 Nov 2009 13:16:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
		<item>
		<title>Saving the output of a C program in a text file(Linux/Unix)</title>
		<link>http://blog.aditech.info/saving-the-output-of-a-c-program-in-a-text-filelinuxunix.html</link>
		<comments>http://blog.aditech.info/saving-the-output-of-a-c-program-in-a-text-filelinuxunix.html#comments</comments>
		<pubDate>Sun, 30 Mar 2008 11:46:17 +0000</pubDate>
		<dc:creator>adithya</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[C in linux/unix]]></category>
		<category><![CDATA[save the output of a c program]]></category>
		<category><![CDATA[Saving the output of a C program in a text file]]></category>
		<category><![CDATA[Saving the output of a C program in a text file(linux/u]]></category>

		<guid isPermaLink="false">http://blog.aditech.info/2008/03/30/saving-the-output-of-a-c-program-in-a-text-filelinuxunix/</guid>
		<description><![CDATA[Consider the following c program #include main() { int i,n; printf("Enter limit\n"); scanf("%d",&#38;n); printf("I will print %d times\n",n); for(i=0;i]]></description>
			<content:encoded><![CDATA[<p>Consider the following c program</p>
<pre lang="C">
#include<stdio.h>
main()
{
int i,n;
printf("Enter limit\n");
scanf("%d",&amp;n);
printf("I will print %d times\n",n);
for(i=0;i<n;i++)
printf("I luv C\n");
}
</pre>
<p>the output will be</p>
<p><code><br />
Enter limit<br />
4<br />
I will print 4 times<br />
I luv C<br />
I luv C<br />
I luv C<br />
I luv C<br />
</code></p>
<p>This is a simple program which will print "I luv C" n times.Now, wouldn't be nice if we saved the output in a text file<br />
then in such case you have to use directed symbol <strong>"&gt;&gt;"</strong><br />
<span id="more-133"></span><br />
<strong>Usage:</strong><br />
<code><br />
./a.out &gt;&gt; filename.txt<br />
</code></p>
<p>let the filename for the above c program be adi.c.In order to direct the output of this program to a text file.Issue commands</p>
<p><code><br />
bash-2.05b$ cc adi.c<br />
bash-2.05b$ ./a.out >> adi.txt<br />
</code></p>
<p><em>where adi.txt is the text file in which you want to save the output</em></p>
<p>When you type <strong>./a.out >> adi.txt</strong> and press enter ,nothing will be displayed.<br />
Now type 5 and press enter.<br />
Open adi.txt file by issuing command vi adi.txt</p>
<p><code><br />
bash-2.05b$vi adi.txt<br />
</code></p>
<p>the following will be it's contents</p>
<p><code><br />
Enter limit<br />
I will print 5 times<br />
I luv C<br />
I luv C<br />
I luv C<br />
I luv C<br />
I luv C<br />
</code></p>
<p>when you direct the output to a text file,2 things are important</p>
<blockquote><p> 1.Only scanning takes place in the terminal.Means no output will be there.You have to remember the sequence in which you should enter values.<br />
2.Your input is not saved in the text file as you can see ,the file contains "Enter limit" but doesn't contain what i inputted.</p></blockquote>
<blockquote><p>1.If the text file already exists,then the output will be appended(added) to the existing contents<br />
2.The text file is saved under the same directory.If you want to store it some other place then give the path<br />
ex:<br />
<code><br />
bash-2.05b$ ./a.out >> \home\adithya\output\adi.txt<br />
</code><br />
will save the output to a text file named adi.txt saved in \home\adithya\output\ directory[/B]</p></blockquote>
<p><strong>Tip:</strong></p>
<p>if you want the contents of the textfile to be of natural output ,then after every scanf() statement issue appropriate printf() statement of the value you scanned.<br />
Ex:if you want to make the directed output file of the above program match exatly with real output then make this change</p>
<p><code><br />
printf("Enter limit\n");<br />
scanf("%d",&amp;n);<br />
printf("%d\n",n);<br />
</code></p>
<p>now the contents of the text file will be like this</p>
<p><code><br />
Enter limit<br />
5<br />
I will print 5 times<br />
I luv C<br />
I luv C<br />
I luv C<br />
I luv C<br />
I luv C<br />
</code><br />
 <img src='http://blog.aditech.info/wp-includes/images/smilies/icon_mrgreen.gif' alt=':mrgreen:' class='wp-smiley' />   <img src='http://blog.aditech.info/wp-includes/images/smilies/icon_mrgreen.gif' alt=':mrgreen:' class='wp-smiley' /> </p>
<h3  class="related_post_title"><hr>You may also like</h3><ul class="related_post"><li><a href="http://blog.aditech.info/dynamic-output-in-c.html" title="Dynamic Output in C">Dynamic Output in C</a></li><li><a href="http://blog.aditech.info/enum-data-type.html" title="enum data type">enum data type</a></li><li><a href="http://blog.aditech.info/wordpress-2_6-a-complete-review-and-guide.html" title="Wordpress 2.6 -A complete review and Guide">Wordpress 2.6 -A complete review and Guide</a></li><li><a href="http://blog.aditech.info/floor-and-ceil.html" title="floor and ceil">floor and ceil</a></li><li><a href="http://blog.aditech.info/advantages-of-running-c-in-linuxunix-1.html" title="Advantages of running c in Linux/Unix-1">Advantages of running c in Linux/Unix-1</a></li><li><a href="http://blog.aditech.info/edit-set.html" title="Edit Set">Edit Set</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://blog.aditech.info/saving-the-output-of-a-c-program-in-a-text-filelinuxunix.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Important instructions for programs using math.h library function</title>
		<link>http://blog.aditech.info/important-instructions-for-programs-using-mathh-library-function.html</link>
		<comments>http://blog.aditech.info/important-instructions-for-programs-using-mathh-library-function.html#comments</comments>
		<pubDate>Sun, 23 Mar 2008 15:14:25 +0000</pubDate>
		<dc:creator>adithya</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[C in linux/unix]]></category>
		<category><![CDATA[Important instructions for programs using math.h librar]]></category>
		<category><![CDATA[math.h in linux]]></category>

		<guid isPermaLink="false">http://blog.aditech.info/2008/03/23/important-instructions-for-programs-using-mathh-library-function/</guid>
		<description><![CDATA[if your program uses math.h header file then sometimes the compilation will lead to errors.In such cases you have to suffix -lm to the compilation file command Usage cc filename.c -lm lm means link mathematical header file.Note that this step is not always required use it only if your program is correct and compilation is [...]]]></description>
			<content:encoded><![CDATA[<p>if your program uses math.h header file then sometimes the compilation will lead to errors.In such cases you have to suffix <strong>-lm</strong> to the compilation file command</p>
<p>Usage<br />
<code><br />
cc <em>filename.c</em> -lm<br />
</code></p>
<p><em>lm</em> means link mathematical header file.Note that this step is not always required use it only if your program is correct and compilation is leading to errors.<br />
and all other procedures are same.<br />
<span id="more-132"></span><br />
ex:<br />
consider the following program</p>
<pre lang="C">
#include<stdio.h>
#include
<math.h> main()
{
float num,sq;
printf("Enter the number\n");
scanf("%f",&amp;num);
sq=sqrt(num);
printf("Sqrt=%f",sq);
}
</pre>
<p>suppose the name of this c file is <strong>adi.c</strong><br />
then sometimes compilation of this file will lead to an error like this</p>
<p><code><br />
bash-2.05b$ cc adi.c<br />
/tmp/cck2Ps76.o(.text+0x42): In function `main':<br />
: undefined reference to `sqrt'<br />
collect2: ld returned 1 exit status<br />
</code></p>
<p>In such case compile like this</p>
<p><code><br />
bash-2.05b$ cc adi.c -lm<br />
</code></p>
<h3  class="related_post_title"><hr>You may also like</h3><ul class="related_post"><li><a href="http://blog.aditech.info/how-to-organize-your-firefox-web-history-just-like-opera.html" title="How to Organize your Firefox Web History just like Opera">How to Organize your Firefox Web History just like Opera</a></li><li><a href="http://blog.aditech.info/seo-for-wordpress-blogs-part-5.html" title="SEO for wordpress blogs-Part 5/5">SEO for wordpress blogs-Part 5/5</a></li><li><a href="http://blog.aditech.info/seo-for-wordpress-blogs-part-2.html" title="SEO for wordpress blogs-Part 2/5">SEO for wordpress blogs-Part 2/5</a></li><li><a href="http://blog.aditech.info/google-can-read-flash.html" title="Google can read Flash">Google can read Flash</a></li><li><a href="http://blog.aditech.info/edit-set.html" title="Edit Set">Edit Set</a></li><li><a href="http://blog.aditech.info/finally-new-theme-for-my-blog.html" title="Finally!! New theme for my blog">Finally!! New theme for my blog</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://blog.aditech.info/important-instructions-for-programs-using-mathh-library-function.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Advantages of running c in Linux/Unix-1</title>
		<link>http://blog.aditech.info/advantages-of-running-c-in-linuxunix-1.html</link>
		<comments>http://blog.aditech.info/advantages-of-running-c-in-linuxunix-1.html#comments</comments>
		<pubDate>Sat, 22 Mar 2008 13:59:28 +0000</pubDate>
		<dc:creator>adithya</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[C in linux/unix]]></category>
		<category><![CDATA[Advantages of running c in Linux/Unix]]></category>
		<category><![CDATA[data overflow in TC]]></category>

		<guid isPermaLink="false">http://blog.aditech.info/2008/03/22/advantages-of-running-c-in-linuxunix-1/</guid>
		<description><![CDATA[C is strongly linked with linux/unix .The kernel of Unix/Linux is written in C.Running C in linux/unit has several advantages .The most important among it is full word length utilization. Consider the following C program #include int main() { int a; float b; printf("Size of int datatype =%d bits\n",(sizeof(a)*8)); printf("Size of float datatype =%d bits\n",(sizeof(b)*8)); [...]]]></description>
			<content:encoded><![CDATA[<p>C is strongly linked with linux/unix .The kernel of Unix/Linux is written in C.Running C in linux/unit has several advantages .The most important among it is full word length utilization.<br />
Consider the following C program</p>
<pre lang="C">
#include<stdio.h>
int main()
{
int a;
float b;
printf("Size of int datatype =%d bits\n",(sizeof(a)*8));
printf("Size of float datatype =%d bits\n",(sizeof(b)*8));
return 1;
}
</pre>
<p><span id="more-131"></span></p>
<blockquote><p>sizeof() returns the amount of bytes that the variable is awarded</p></blockquote>
<p>We want the answer in bits,hence we have to multiplied it by 8 (1 byte=8bits)<br />
now run this c program in turbo c in windows<br />
the output will be</p>
<p><code><br />
Size of int datatype=16 bits<br />
Size of float datatype =32 bits<br />
</code></p>
<p>Even though your computer is 32 or 64 bit computer, Turbo C is utilizing only 16 bits to int which should have been 32 bits(Int is allocated with a space equal to wordlength of the computer)</p>
<p>now run the above c program in linux<br />
the output will be</p>
<p><code>Size of int datatype=32 bits<br />
Size of float datatype =64 bits</code></p>
<p>^^if your processor is 32 bit</p>
<p><code><br />
Size of int datatype=64 bits<br />
Size of float datatype =128 bits<br />
</code><br />
^^if your processor is 64 bit</p>
<p>Ok&#8230;running C program in linux/unix utilizes full word length.what&#8217;s it&#8217;s use for me??  <img src='http://blog.aditech.info/wp-includes/images/smilies/icon_idea.gif' alt=':idea:' class='wp-smiley' /> </p>
<p>The maximum number of singed int datatype(normal int) that can be accomodated if you run c in windows is ((2^15)-1)=<strong>32767</strong><br />
that means if &#8216;a&#8217; is made to store number greater than 32767 it will cause data overflow and you will get garbage values.(Read <a href="http://blog.aditech.info/2008/02/20/how-c-compiler-decides-the-garbage-values-in-case-of-data-overflow/" title="How C Compiler decides the Garbage values in case of data overflow" target="_blank">How C Compiler decides the Garbage values in case of data overflow</a> for more information)</p>
<p>Now ,lets take the scenario in linux/unix</p>
<p>The maximum number of singed int datatype(normal int) that can be accomodated if you run c in linux is ((2^31)-1)=<strong>2147483647</strong> (assuming your comp is 32 bit) <img src='http://blog.aditech.info/wp-includes/images/smilies/icon_eek.gif' alt=':shock:' class='wp-smiley' /> </p>
<p>so the int variable &#8216;a&#8217; can store a maximum value of 2147483647.Compare with this mere 32767.. <img src='http://blog.aditech.info/wp-includes/images/smilies/icon_cool.gif' alt=':cool:' class='wp-smiley' /> </p>
<p>This is for int datatype consider other datatypes such as float,long double,double,long int etc..<br />
Imagine how many big values it can store <img src='http://blog.aditech.info/wp-includes/images/smilies/icon_eek.gif' alt=':shock:' class='wp-smiley' /> </p>
<blockquote><p>So you can play with larger values if you run a C program in linux/unix.<br />
Apart from this the speed of execution will be more than double since it utilizes full available word length.</p></blockquote>
<h3  class="related_post_title"><hr>You may also like</h3><ul class="related_post"><li><a href="http://blog.aditech.info/seo-for-wordpress-blogs-part-5.html" title="SEO for wordpress blogs-Part 5/5">SEO for wordpress blogs-Part 5/5</a></li><li><a href="http://blog.aditech.info/c-in-linuxunix-first-step.html" title="C in linux/unix-First step">C in linux/unix-First step</a></li><li><a href="http://blog.aditech.info/how-to-organize-your-firefox-web-history-just-like-opera.html" title="How to Organize your Firefox Web History just like Opera">How to Organize your Firefox Web History just like Opera</a></li><li><a href="http://blog.aditech.info/save-as-pdf-option-in-your-website-or-blog.html" title="Save as pdf option in your website or blog">Save as pdf option in your website or blog</a></li><li><a href="http://blog.aditech.info/windows-registry-in-depth-part-1.html" title="Windows Registry in depth-Part 1">Windows Registry in depth-Part 1</a></li><li><a href="http://blog.aditech.info/enum-data-type.html" title="enum data type">enum data type</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://blog.aditech.info/advantages-of-running-c-in-linuxunix-1.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>C in linux/unix-First step</title>
		<link>http://blog.aditech.info/c-in-linuxunix-first-step.html</link>
		<comments>http://blog.aditech.info/c-in-linuxunix-first-step.html#comments</comments>
		<pubDate>Sat, 22 Mar 2008 13:26:50 +0000</pubDate>
		<dc:creator>adithya</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[C in linux/unix]]></category>
		<category><![CDATA[running c programs in linux/unix]]></category>

		<guid isPermaLink="false">http://blog.aditech.info/2008/03/22/c-in-linuxunix-first-step/</guid>
		<description><![CDATA[First let&#8217;s learn how to run a c program in linux/unix.Nearly every distro support&#8217;s running c programs. First thing is to type the c program.you can use any editor you want but here i use vi editor which comes with allmost all distro vi editor To create a new c file or to open an [...]]]></description>
			<content:encoded><![CDATA[<p>First let&#8217;s learn how to run a c program in linux/unix.Nearly every distro support&#8217;s running c programs.</p>
<p>First thing is to type the c program.you  can use any editor you want but here i use vi editor which comes with allmost all distro</p>
<h2><span style="font-size: 14pt"><span style="font-size: 18pt"><strong>vi editor</strong></span></span></h2>
<p>To create a new c file or to  open an existing c file,type vi followed by filename.c<br />
ex:<br />
<code>[student@localhost student]$vi adi.c</code></p>
<p>will create a new file adi.c or will open an exisiting file adi.c if it&#8217;s present</p>
<p>now press insert key to edit or modify or add new content<br />
after you complete writing the program,press Esc key and type <strong>:w (colen w) </strong>and press enter to save the file then press  <strong>:q (colen q) </strong>and press enter to exit the vi editor.<br />
<span id="more-130"></span><br />
Next stop:Compilation</p>
<h2><span style="font-size: 14pt"><span style="font-size: 14pt"><strong>Compilation</strong></span></span></h2>
<p>To compile a c file enter cc followed by the filename<br />
ex:<br />
<code>student@localhost student]$cc adi.c</code></p>
<p>if the compilation is successful ,then no message will be displayed.Otherwise a message along with line number where the error is present is displayed.<br />
Next stop:Running the compiled file</p>
<h2><span style="font-size: 14pt"><span style="font-size: 18pt"><strong>Running the compiled file</strong></span></span></h2>
<p>To run the compiled file,enter <strong>./a.out</strong><br />
ex:<br />
<code>[student@localhost student]$./a.out</code></p>
<p>the c program will be running.Just work the same way as you work in windows.<br />
note that irrespective of the file name ,you have to always enter &#8220;./a.out&#8221; to run the c program which you just compiled.</p>
<p>let&#8217;s see an example program to print hi linux<br />
<code>[student@localhost student]$vi first.c</code></p>
<p>press insert and type the following program</p>
<p><code><br />
#include<stdio.h><br />
main()<br />
{<br />
printf("\nHi linux\n");<br />
}<br />
</code><br />
<strong>note that i have given just main() not void main()</strong></p>
<p>now press Esc key and type :w to save the file and :q to exit the vi editor</p>
<p><code>[student@localhost student]$cc first.c<br />
[student@localhost student]$./a.out</code></p>
<p>if you do these steps correctly, then you should get the output like this</p>
<p><code><br />
Hi linux<br />
[student@localhost student]$</code></p>
<blockquote><p> Noteif you get a error like this</p>
<p><code><br />
[student@localhost student]$ cc adi.c<br />
bash: cc: command not found<br />
[student@localhost student]$<br />
</code></p>
<p>means there is no gcc package in your distro and you are unable to run c programs in that  <img src='http://blog.aditech.info/wp-includes/images/smilies/icon_sad.gif' alt=':sad:' class='wp-smiley' /> </p></blockquote>
<h3  class="related_post_title"><hr>You may also like</h3><ul class="related_post"><li><a href="http://blog.aditech.info/saving-the-output-of-a-c-program-in-a-text-filelinuxunix.html" title="Saving the output of a C program in a text file(Linux/Unix)">Saving the output of a C program in a text file(Linux/Unix)</a></li><li><a href="http://blog.aditech.info/enum-data-type.html" title="enum data type">enum data type</a></li><li><a href="http://blog.aditech.info/google-can-read-flash.html" title="Google can read Flash">Google can read Flash</a></li><li><a href="http://blog.aditech.info/how-to-install-oldincompatible-firefox-addon-or-extension.html" title="How to Install Old,Incompatible Firefox Addon or Extension">How to Install Old,Incompatible Firefox Addon or Extension</a></li><li><a href="http://blog.aditech.info/twitter-exposed.html" title="Twitter Exposed">Twitter Exposed</a></li><li><a href="http://blog.aditech.info/ascii-chart.html" title="ASCII Chart">ASCII Chart</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://blog.aditech.info/c-in-linuxunix-first-step.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ASCII Chart</title>
		<link>http://blog.aditech.info/ascii-chart.html</link>
		<comments>http://blog.aditech.info/ascii-chart.html#comments</comments>
		<pubDate>Wed, 20 Feb 2008 12:29:10 +0000</pubDate>
		<dc:creator>adithya</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[Lesser Known Facts]]></category>
		<category><![CDATA[ASCII]]></category>
		<category><![CDATA[ASCII chart]]></category>
		<category><![CDATA[use ASCII characters in C programs]]></category>

		<guid isPermaLink="false">http://blog.aditech.info/2008/02/20/ascii-chart/</guid>
		<description><![CDATA[After seeing this chart if you want to print say square root symbol all you have to do is specify the ASCII value for %c in printf i.e to print square root.. printf(&#8220;%c Hi! %c&#8221;,251,251); and to print heart symbol printf(&#8220;%c Hi! %c&#8221;,3,3); You may also likeChanging CD/DVD/Floppy Drive(s) label and iconTwitter Exposedenum data typeatoi,atol [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center"><a href="http://farm3.static.flickr.com/2392/2085714247_6abe4b8d2b_o.jpg" rel="shadowbox[post-14];player=img;"><img src="http://farm3.static.flickr.com/2392/2085714247_6abe4b8d2b_o.jpg" alt="ASCII chart" height="410" width="495" /></a></p>
<p> After seeing this chart if you want to print say square root symbol all you have to do is specify the ASCII value for %c in printf<br />
i.e to print square root..</p>
<blockquote><p><font color="#0000ff">printf(&#8220;%c Hi! %c&#8221;,251,251);</font></p></blockquote>
<p>and to print heart symbol</p>
<blockquote><p><font color="#0000ff">printf(&#8220;%c Hi! %c&#8221;,3,3);</font></p></blockquote>
<h3  class="related_post_title"><hr>You may also like</h3><ul class="related_post"><li><a href="http://blog.aditech.info/how-to-refresh-or-flush-domain-name-systemdns-cache-in-windows.html" title="How to refresh or flush Domain Name System(DNS) Cache in windows">How to refresh or flush Domain Name System(DNS) Cache in windows</a></li><li><a href="http://blog.aditech.info/random-post-option-in-wordpress.html" title="Random post option in wordpress&#8230;">Random post option in wordpress&#8230;</a></li><li><a href="http://blog.aditech.info/seo-for-wordpress-blogs-part-2.html" title="SEO for wordpress blogs-Part 2/5">SEO for wordpress blogs-Part 2/5</a></li><li><a href="http://blog.aditech.info/how-to-install-oldincompatible-firefox-addon-or-extension.html" title="How to Install Old,Incompatible Firefox Addon or Extension">How to Install Old,Incompatible Firefox Addon or Extension</a></li><li><a href="http://blog.aditech.info/save-as-pdf-option-in-your-website-or-blog.html" title="Save as pdf option in your website or blog">Save as pdf option in your website or blog</a></li><li><a href="http://blog.aditech.info/google-can-read-flash.html" title="Google can read Flash">Google can read Flash</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://blog.aditech.info/ascii-chart.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>atoi,atol and atof functions</title>
		<link>http://blog.aditech.info/atoiatol-and-atof-functions.html</link>
		<comments>http://blog.aditech.info/atoiatol-and-atof-functions.html#comments</comments>
		<pubDate>Wed, 20 Feb 2008 12:18:42 +0000</pubDate>
		<dc:creator>adithya</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[Lesser Known Facts]]></category>
		<category><![CDATA[atof]]></category>
		<category><![CDATA[atoi]]></category>
		<category><![CDATA[atol]]></category>

		<guid isPermaLink="false">http://blog.aditech.info/2008/02/20/atoiatol-and-atof-functions/</guid>
		<description><![CDATA[These functions are available under stdlib.h .These are very unknown functions.Lets see what these do.. atol() atol returns the value in terms of long datatype of the string containing numbers.the syntax is atol(stringvariable) ex: consider the following program #include #include void main() char a[10],b[10]; puts("Enter the value of a"); gets(a); puts("Enter the value of b"); [...]]]></description>
			<content:encoded><![CDATA[<p>These functions are available under stdlib.h .These are very unknown functions.Lets see what these do..</p>
<p><strong>atol()</strong><br />
atol returns the value in terms of long datatype of the string containing numbers.the syntax is</p>
<blockquote><p><font color="#0000ff">atol(stringvariable)</font></p></blockquote>
<p>ex: consider the following program<br />
<span id="more-13"></span></p>
<pre lang="C" line="1">
#include<stdio.h>
#include<stdlib.h>
void main()

char a[10],b[10];
puts("Enter the value of a");
gets(a);
puts("Enter the value of b");
gets(b);
printf("%s+%s=%ld and %s-%s=%ld",a,b,(atol(a)+atol(b)),a,b,(atol(a)-atol(b)));
getch();
}
</pre>
<p>and similarly there is <strong>atof()</strong> which returns the float value of the string containing numbers and <strong>atoi()</strong> which returns integer value.</p>
<p>now to see the difference between the 3 look at this program</p>
<pre lang="C" line="1">
#include<stdio.h>
#include<stdlib.h>
main()
{
char a[]={"2545.965"};
printf("atol=%ld\t atof=%f\t atoi=%d\t\n",atol(a),atof(a),atoi(a));
}
</pre>
<p>the output will be</p>
<blockquote><p><font color="#ff6600">atol=2545                                atof=2545.965000                                    atoi=2545</font></p></blockquote>
<blockquote></blockquote>
<p><em>T</em><em>he output of atoi() and atol() functions will be same but atof() function may give different output depending upon the compiler.That is,in some compiler the output may be like this atof=2545.96498 or something.The above output is extarcted from red hat</em></p>
<blockquote></blockquote>
<p>let&#8217;s look what happens when the string does not contain numeric value.change this statement in the above program</p>
<blockquote><p><font color="#0000ff">char a[]={&#8220;adithya&#8221;};</font></p></blockquote>
<p>now when you run the program the following will be the output</p>
<blockquote><p><font color="#ff9900">atol=0                               atof=0                               atoi=0</font></p></blockquote>
<p>that means the string should contain numeric value<br />
now modify this program as</p>
<blockquote><p><font color="#0000ff">char a[]={&#8220;007adi&#8221;};</font></p></blockquote>
<p>the output in this case(tested in Red hat) will be</p>
<blockquote><p><font color="#0000ff">atol=7                           atof=7.000000                              atoi=7<br />
</font></p></blockquote>
<p>so the functions has taken 007 only not the remaining part<br />
<strong>However note that this depends upon compiler.Some will take ,while some will give output as 0.</strong></p>
<p>Now consider this</p>
<blockquote><p><font color="#0000ff">char a[]={&#8220;adi007&#8243;};</font></p></blockquote>
<p>the output of the program will be</p>
<blockquote><p><font color="#0000ff">atol=0           atof=0.000000           atoi=0</font></p></blockquote>
<p><font color="#000000">I&#8217;m not sure of this but the following is the point that i noticed<br />
now consider this</font></p>
<blockquote><p><font color="#0000ff">char a[]={&#8220;0XA&#8221;};</font></p></blockquote>
<p><font color="#000000">what do you think the output is???<br />
As you can see 0XA is a hexadecimal number(hexadecimal number in C is written by writing 0X(zero X) folowed by hex number).It&#8217;s decimal equivalent is 10.The following is the output i got in Red hat</font></p>
<blockquote><p><font color="Blue">atol=0                        atof=10.000000        atoi=0</font></p></blockquote>
<p><font color="#000000">Now the question is why atol and atoi couldn&#8217;t understand this while atof took the number as hex number?????  <img src="http://www.thinkdigit.com/forum/images/smilies/confused.gif" title="Confused" class="inlineimg" border="0" height="21" width="16" /><img src="http://www.thinkdigit.com/forum/images/smilies/confused.gif" title="Confused" class="inlineimg" border="0" /><img src="http://www.thinkdigit.com/forum/images/smilies/confused.gif" title="Confused" class="inlineimg" border="0" /></font></p>
<h3  class="related_post_title"><hr>You may also like</h3><ul class="related_post"><li><a href="http://blog.aditech.info/create-you-own-custom-firefox-search-plugin-in-seconds.html" title="Create you own custom Firefox search plugin in seconds">Create you own custom Firefox search plugin in seconds</a></li><li><a href="http://blog.aditech.info/advantages-of-running-c-in-linuxunix-1.html" title="Advantages of running c in Linux/Unix-1">Advantages of running c in Linux/Unix-1</a></li><li><a href="http://blog.aditech.info/c-in-linuxunix-first-step.html" title="C in linux/unix-First step">C in linux/unix-First step</a></li><li><a href="http://blog.aditech.info/how-c-compiler-decides-the-garbage-values-in-case-of-data-overflow.html" title="How C Compiler decides the Garbage values in case of data overflow">How C Compiler decides the Garbage values in case of data overflow</a></li><li><a href="http://blog.aditech.info/the-mistake-in-made-with-my-rss-feeds.html" title="The mistake in made with my RSS feeds&#8230;">The mistake in made with my RSS feeds&#8230;</a></li><li><a href="http://blog.aditech.info/edit-set.html" title="Edit Set">Edit Set</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://blog.aditech.info/atoiatol-and-atof-functions.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Saving the output in a text file</title>
		<link>http://blog.aditech.info/saving-the-output-in-a-text-file.html</link>
		<comments>http://blog.aditech.info/saving-the-output-in-a-text-file.html#comments</comments>
		<pubDate>Wed, 20 Feb 2008 08:54:54 +0000</pubDate>
		<dc:creator>adithya</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[Lesser Known Facts]]></category>
		<category><![CDATA[output redirection]]></category>
		<category><![CDATA[Saving the output in a text file]]></category>

		<guid isPermaLink="false">http://blog.aditech.info/2008/02/20/saving-the-output-in-a-text-file/</guid>
		<description><![CDATA[consider the following c program #include&#60;stdio.h&#62; #include&#60;conio.h&#62; main() { int i,n; clrscr(); printf(&#8220;Enter limit\n&#8221;); scanf(&#8220;%d&#8221;,&#38;n); printf(&#8220;I will print %d times\n&#8221;,n); for(i=0;i&#60;n;i++) printf(&#8220;I luv C\n&#8221;); } the output will be Enter limit 4 I will print 4 times I luv C I luv C I luv C I luv C &#160; This is a simple program [...]]]></description>
			<content:encoded><![CDATA[<p>consider the following c program</p>
<blockquote><p><font color="#0000ff">#include&lt;stdio.h&gt;<br />
#include&lt;conio.h&gt;</font><br />
<font color="#0000ff">main()</font><br />
<font color="#0000ff">{</font><br />
<font color="#0000ff">int i,n;</font><br />
<font color="#0000ff">clrscr();</font><br />
<font color="#0000ff">printf(&#8220;Enter limit\n&#8221;);</font><br />
<font color="#0000ff">scanf(&#8220;%d&#8221;,&amp;n);</font><br />
<font color="#0000ff">printf(&#8220;I will print %d times\n&#8221;,n);</font><br />
<font color="#0000ff">for(i=0;i&lt;n;i++)</font><br />
<font color="#0000ff">        printf(&#8220;I luv C\n&#8221;);</font><br />
<font color="#0000ff">}</font></p></blockquote>
<p>the output will be<br />
<span id="more-12"></span></p>
<blockquote><p><font color="#ff6600">Enter limit</font><br />
<font color="#ff6600">4</font><br />
<font color="#ff6600">I will print 4 times</font><br />
<font color="#ff6600">I luv C</font><br />
<font color="#ff6600">I luv C</font><br />
<font color="#ff6600">I luv C</font><br />
<font color="#ff6600">I luv C</font></p></blockquote>
<p style="margin: 5px 20px 20px">&nbsp;</p>
<p>This is a simple program which will print &#8220;I luv C&#8221; n times<br />
Now i want to save the output to a text file without copy pasting the output.<br />
Well,in that case, first compile the saved C file[{ALT} {F9}]<br />
Now go to file menu and click DOS SHELL or DOS PROMPT in Turbo C</p>
<p>You are in DOS now.Lets assume the filename of the above program be some output.c.Then issue command</p>
<p style="margin: 5px 20px 20px">&nbsp;</p>
<blockquote><p><font color="#0000ff">C:\TC\BIN\output.exe &gt;&gt; output.txt</font></p></blockquote>
<p>In general,the command is</p>
<blockquote><p><font color="#0000ff">filename.exe &gt;&gt; textfile.txt</font></p></blockquote>
<p>As soon as you press enter, you will be presented a black screen and you see no message.Now enter 5 and press enter.You will again see DOS prompt.Now open the text file(text file will be in same director(C:\TC\BIN)),the following will be it&#8217;s content</p>
<blockquote><p><font color="#ff6600"> Enter limit<br />
I will print 5 times<br />
I luv C<br />
I luv C<br />
I luv C<br />
I luv C<br />
I luv C</font></p></blockquote>
<blockquote>
<blockquote></blockquote>
</blockquote>
<p>When you direct the output to a text file,2 things are important</p>
<blockquote>
<blockquote><p><font color="Blue"><em>1.Only scanning takes place in DOS.Means no output will be there.You have to remember the sequence in which you should enter values.</em></font><br />
<font color="Blue"><em> 2.Your input is not saved in the text file as you can see ,the file contains &#8220;Enter limit&#8221; but doesn&#8217;t contain what i inputted.</em></font></p></blockquote>
</blockquote>
<p style="margin: 5px 20px 20px">&nbsp;</p>
<p class="smallfont" style="margin-bottom: 2px"> <strong>Note:</strong></p>
<p>1.If the text file already exists,then the output will be appended(added) to the existing contents<br />
2.This is a general procedure assuming that you have saved your c file in C:\TC\BIN\ directory.If you have saved somewhere else say C:\Pro\ then you should first navigate to that directory by using &#8216;cd&#8217; command i.e, cd C:\Pro\ and then the remaining procedures are same<br />
3.The text file is saved under the same directory.If you want to store it some other place then give the path<br />
ex:</p>
<blockquote><p><font color="#0000ff">C:\TC\BIN\output.exe &gt;&gt; C:\output.txt</font></p></blockquote>
<p style="margin: 5px 20px 20px">&nbsp;</p>
<p>will save the output to a text file named output.txt saved in C:\<br />
4.Do not forget to compile the file before saving it&#8217;s output in a text file<br />
5.If you use getch(); at the end ,remember to press a key.<br />
6.In case of above program,blank screen is presented because we have used clrscr();if it&#8217;s not used then you will not see a blank screen.But you see a no prompt.This is the indication that the program is running<br />
7.Save the file before compilation.<br />
8.The compiled file is always having the same name as the saved c file but having extension &#8216;.exe&#8217;</p>
<p><em><strong>Tip:<br />
</strong>if you want the contents of the textfile to be of natural output ,then after every scanf() statement issue appropriate printf() statement of the value you scanned.<br />
Ex:if you want to make the directed output file of the above program match exatly with real output then make this change</em></p>
<blockquote><p><font color="#0000ff">printf(&#8220;Enter limit\n&#8221;);</font><br />
<font color="#0000ff">scanf(&#8220;%d&#8221;,&amp;n);</font><br />
<font color="#0000ff">printf(&#8220;%d\n&#8221;,n);</font></p></blockquote>
<p>now the contents of the text file will be like this</p>
<blockquote><p><font color="#ff6600">Enter limit</font><br />
<font color="#ff6600">5</font><br />
<font color="#ff6600">I will print 5 times</font><br />
<font color="#ff6600">I luv C</font><br />
<font color="#ff6600">I luv C</font><br />
<font color="#ff6600">I luv C</font><br />
<font color="#ff6600">I luv C</font><br />
<font color="#ff6600">I luv C</font></p></blockquote>
<h3  class="related_post_title"><hr>You may also like</h3><ul class="related_post"><li><a href="http://blog.aditech.info/how-c-compiler-decides-the-garbage-values-in-case-of-data-overflow.html" title="How C Compiler decides the Garbage values in case of data overflow">How C Compiler decides the Garbage values in case of data overflow</a></li><li><a href="http://blog.aditech.info/twitter-exposed.html" title="Twitter Exposed">Twitter Exposed</a></li><li><a href="http://blog.aditech.info/wordpress-2_6-a-complete-review-and-guide.html" title="Wordpress 2.6 -A complete review and Guide">Wordpress 2.6 -A complete review and Guide</a></li><li><a href="http://blog.aditech.info/how-to-refresh-or-flush-domain-name-systemdns-cache-in-windows.html" title="How to refresh or flush Domain Name System(DNS) Cache in windows">How to refresh or flush Domain Name System(DNS) Cache in windows</a></li><li><a href="http://blog.aditech.info/enum-data-type.html" title="enum data type">enum data type</a></li><li><a href="http://blog.aditech.info/saving-the-output-of-a-c-program-in-a-text-filelinuxunix.html" title="Saving the output of a C program in a text file(Linux/Unix)">Saving the output of a C program in a text file(Linux/Unix)</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://blog.aditech.info/saving-the-output-in-a-text-file.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How C Compiler decides the Garbage values in case of data overflow</title>
		<link>http://blog.aditech.info/how-c-compiler-decides-the-garbage-values-in-case-of-data-overflow.html</link>
		<comments>http://blog.aditech.info/how-c-compiler-decides-the-garbage-values-in-case-of-data-overflow.html#comments</comments>
		<pubDate>Wed, 20 Feb 2008 08:43:45 +0000</pubDate>
		<dc:creator>adithya</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[Lesser Known Facts]]></category>
		<category><![CDATA[dataoverflow]]></category>
		<category><![CDATA[garbage value]]></category>

		<guid isPermaLink="false">http://blog.aditech.info/2008/02/20/how-c-compiler-decides-the-garbage-values-in-case-of-data-overflow/</guid>
		<description><![CDATA[Garbage value in case of data overflow I did some intense R&#38;D for garbage values and found these facts.After understanding these concepts u can tell what will be the garbage value to be stored in a variable in case of dataoverflow. Here are some of the basic facts required to proceed In case of windows(TC), [...]]]></description>
			<content:encoded><![CDATA[<h1 align="center"><font size="4">Garbage value in case of data overflow</font></h1>
<p>I did some intense R&amp;D for garbage values and found these facts.After understanding these concepts u can tell what will be the garbage value to be stored in a variable in case of dataoverflow.</p>
<p>Here are some of the basic facts required to proceed</p>
<p><font color="#00ffff"><strong><font color="#993300">I</font><font color="#993300">n case of windows(TC),</font></strong></font></p>
<p><font color="#993300"><em> The space allocated for a variable of int datatype is 16 bits.</em></font></p>
<p><font color="#993300"><em> That means the maximum negative value that can be stored by the variable of int datatype is 2^15=-32768.</em></font></p>
<p><font color="#993300"><em> You may ask why 2^15 not 2^16.This is because one bit is reserved for sign(+ or -)</em></font></p>
<p><font color="#993300"><em> simlarly the maximum +ve value that a variable of int datatype can store is 2^15-1=32767.Hang on!!</em></font></p>
<p><font color="#993300"><em> Why -1.Because 0 is also counted in case of +ve values. </em></font></p>
<p><font color="#993300"><em>Now lets come to long datatype.<br />
The space allocated for a variable of long datatype is 32 bits.<br />
Maximum negative value= 2^31=-2147483648<br />
Maximum +ve value= 2^31-1=2147483647<br />
</em></font></p>
<p><font color="#993300"><strong>In case of linux(32 bit),</strong><br />
<em><br />
The space allocated for a variable of int datatype is 32 bits.<br />
Maximum negative value= 2^31=-2147483648<br />
Maximum +ve value= 2^31-1=2147483647</em></font></p>
<p><font color="#993300"><em>Now lets come to long datatype.<br />
The space allocated for a variable of long datatype is 64 bits.<br />
Maximum negative value= 2^63=-9.22337e+18<br />
Maximum +ve value= (2^63)-1=9.22337e+18<br />
</em></font></p>
<p><font color="#993300">Since long in linux can store a huge amount of value.let&#8217;s not consider it.<br />
Let&#8217;s take int and long in case of windows and only long in case of linux</font></p>
<p>That&#8217;s basic stuffs<br />
Now, consider this program</p>
<p style="margin: 5px 20px 20px">&nbsp;</p>
<blockquote><p><font color="#0000ff">#include&lt;stdio.h&gt;</font><br />
<font color="#0000ff">#include&lt;conio.h&gt;</font><br />
<font color="#0000ff">void main()</font><br />
<font color="#0000ff">{</font><br />
<font color="#0000ff">int a;</font><br />
<font color="#0000ff">long int b;</font><br />
<font color="#0000ff">clrscr();</font><br />
<font color="#0000ff">a=45000;</font><br />
<font color="#0000ff">b=4000000000;</font><br />
<font color="#0000ff">printf(&#8220;%d\t%ld\n&#8221;,a,b);</font><br />
<font color="#0000ff">getch();</font><br />
<font color="#0000ff">}</font></p></blockquote>
<p>Since both variables a and b are assigned a value greater than their limits,when you run this program some garbage value will be the output.But, the question is how c decides the garbage value??Can we predict the garbage value without running the program.</p>
<p>Sure,We can.C compiler calculates the garbage value by using the formula<br />
<strong><br />
garbage value=overflow value-reference value.<br />
</strong></p>
<p style="margin: 5px 20px 20px">&nbsp;</p>
<blockquote>
<blockquote><p> <em><font color="blue">Overflow value is nothing but the value stored in the variable.Now what&#8217;s is the reference value??<br />
reference value is the value such that it is greater than the overflow value(neglecting the sign)obtained by the multiplication of the basic reference value by 2 until the value is greater than the overflow value(neglecting the sign) The sign of the reference value is same as that of overflow value Basic reference value is 2^n where n represents the number of bits allocated.</font></em></p>
<p><em><font color="blue">That means Basic reference value in case of int is 2^15=32768 and in case of long,it is  2^31=2147483648<br />
The garbage value thus calculated is checked whether it can fit the variable or not.If not the steps are repeated again.</font></em></p></blockquote>
</blockquote>
<p>Ok thats sounds confusing <img src="http://www.thinkdigit.com/forum/images/smilies/confused.gif" title="Confused" class="inlineimg" border="0" height="21" width="16" /><img src="http://www.thinkdigit.com/forum/images/smilies/confused.gif" title="Confused" class="inlineimg" border="0" /> lets take an example</p>
<p>ex1:</p>
<blockquote>
<blockquote><p><font color="#0000ff">a=45000;</font></p></blockquote>
</blockquote>
<blockquote><p><em>overflow value=45000<br />
Basic reference value=32768<br />
since the reference value is less than the overflow value ,we have to multiply it by 2<br />
i.e,32768*2=65536<br />
65536 is greater than the 45000.That means reference value(RV)=65536.SInce RV must be of same sign,RV=+65536<br />
now apply formula<br />
garbage value=overflow value-reference value.<br />
garbage value=45000-(+65536)<br />
garbage value=-20536<br />
-20536 comes within the limits of int datatype.</em></p></blockquote>
<p style="margin: 5px 20px 20px">&nbsp;</p>
<p class="smallfont" style="margin-bottom: 2px">&nbsp;</p>
<p>ex2:</p>
<blockquote><p><font color="#0000ff">b=4000000000;</font></p></blockquote>
<blockquote>
<blockquote><p>overflow value(OV)=4000000000<br />
SInce &#8216;b&#8217; is of long type Basic reference value=2147483648<br />
since the reference value is less than the overflow value ,we have to multiply it by 2<br />
i.e,2147483648*2=4294967296<br />
4294967296 is greater than the 4000000000.That means reference value(RV)=4294967296.SInce RV must be of same sign,RV=+4294967296<br />
now apply formula<br />
garbage value=overflow value-reference value.<br />
garbage value=4000000000-(+4294967296)<br />
garbage value=-294967296<br />
-294967296 comes within the limits of long datatype.</p>
<p>So the output of the program will be<br />
<font color="#ff6600">-20536                            -294967296</font></p></blockquote>
</blockquote>
<p style="margin: 5px 20px 20px">&nbsp;</p>
<blockquote></blockquote>
<p style="margin: 5px 20px 20px">&nbsp;</p>
<blockquote></blockquote>
<p>Now, run the program and see whether the output is same as calculated or not.</p>
<p>Modify the above program</p>
<p style="margin: 5px 20px 20px">&nbsp;</p>
<blockquote><p><font color="#0000ff">a=-40000;</font><br />
<font color="#0000ff">b=-4500000000;</font></p></blockquote>
<p>now , do not run the program.</p>
<p>ex3:</p>
<blockquote><p><font color="#0000ff">a=-40000;</font></p></blockquote>
<blockquote>
<blockquote><p>OV=-40000<br />
discard the sign take 40000.Basic reference value=32768<br />
since the reference value is less than the overflow value ,we have to multiply it by 2<br />
i.e,32768*2=65536<br />
65536 is greater than the 40000.That means reference value(RV)=65536.SInce RV must be of same sign as OV,RV=-65536<br />
Now apply the formula<br />
garbage value=overflow value-reference value.<br />
garbage value=-40000-(-65536)<br />
garbage value=-40000+65536<br />
garbage value=25536<br />
25536 comes within the limits of int datatype.</p></blockquote>
</blockquote>
<p style="margin: 5px 20px 20px">&nbsp;</p>
<p>ex4:</p>
<p class="smallfont" style="margin-bottom: 2px">Code:</p>
<blockquote><p><font color="#0000ff">b=-45000000000;</font></p></blockquote>
<blockquote>
<blockquote><p>overflow value(OV)=-4500000000<br />
SInce &#8216;b&#8217; is of long type Basic reference value=2147483648<br />
discard the sign take 4500000000.<br />
since the reference value is less than the overflow value ,we have to multiply it by 2<br />
i.e,2147483648*2=4294967296 is less than 4500000000.<br />
therefore 4294967296*2=8589934592<br />
8589934592 is greater than the 4500000000.That means reference value(RV)=8589934592.SInce RV must be of same sign,RV=-8589934592<br />
now apply formula<br />
garbage value=overflow value-reference value.<br />
garbage value=-4500000000-(-8589934592)<br />
garbage value=-4500000000+8589934592)<br />
garbage value=4089934592<br />
<strong>4089934592 does not come within the limits of long datatype.</strong></p>
<p style="margin: 5px 20px 20px">&nbsp;</p>
<p>The process is repeated again<br />
overflow value(OV)=4089934592<br />
RV=+4294967296<br />
garbage value=4089934592-(+4294967296)=-205032704<br />
-205032704 comes within the limits of long datatype.<br />
That means final garbage value is -205032704</p>
<p>So the output will be<br />
<font color="#ff6600">25536                      -205032704</font></p></blockquote>
</blockquote>
<p style="margin: 5px 20px 20px">&nbsp;</p>
<blockquote></blockquote>
<p><em>Now run the program.Is your output matching with mine??</em></p>
<p style="margin: 5px 20px 20px">&nbsp;</p>
<blockquote>
<blockquote><p><strong><font color="#993366"><em>Some special cases:</em></font></strong></p>
<p><font color="#993366"><em>There is a special overflow value which will return the same overflow value but opposite in sign. The values are 32768 in case of int and 2147483648 in case of long datatype.The garbage value will be -32768 and -2147483648 respectively.<br />
Lets understand what happens.</em></font></p>
<p><font color="#993366"><em>As i already explained ,the maximum +ve value that can be stored by int datatype variable is 32767 not 32768<br />
That means.OV=32768 Basic RV=32768. RV must be greater than OV.Therfore RV=32768*2=65536<br />
Now, garbage value(GV)=OV-RV=32768-65536=-32768</em></font></p>
<p><font color="#993366"><em>Similarly apply this to 2147483648 you will get</em></font><br />
<font color="#993366"><em> GV=2147483648-4294967296</em></font><br />
<font color="#993366"><em> GV=-2147483648</em></font><br />
<font color="#993366"><em> Quite fascinating.Isn&#8217;t it??</em></font></p></blockquote>
</blockquote>
<h3  class="related_post_title"><hr>You may also like</h3><ul class="related_post"><li><a href="http://blog.aditech.info/floor-and-ceil.html" title="floor and ceil">floor and ceil</a></li><li><a href="http://blog.aditech.info/how-to-install-oldincompatible-firefox-addon-or-extension.html" title="How to Install Old,Incompatible Firefox Addon or Extension">How to Install Old,Incompatible Firefox Addon or Extension</a></li><li><a href="http://blog.aditech.info/reg-jump-jump-into-the-registry.html" title="Reg Jump: Jump into the registry">Reg Jump: Jump into the registry</a></li><li><a href="http://blog.aditech.info/important-instructions-for-programs-using-mathh-library-function.html" title="Important instructions for programs using math.h library function">Important instructions for programs using math.h library function</a></li><li><a href="http://blog.aditech.info/windows-registry-in-depth-part-1.html" title="Windows Registry in depth-Part 1">Windows Registry in depth-Part 1</a></li><li><a href="http://blog.aditech.info/seo-for-wordpress-blogs-part-1.html" title="SEO for wordpress blogs-Part 1/5">SEO for wordpress blogs-Part 1/5</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://blog.aditech.info/how-c-compiler-decides-the-garbage-values-in-case-of-data-overflow.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Dynamic Output in C</title>
		<link>http://blog.aditech.info/dynamic-output-in-c.html</link>
		<comments>http://blog.aditech.info/dynamic-output-in-c.html#comments</comments>
		<pubDate>Wed, 20 Feb 2008 08:22:54 +0000</pubDate>
		<dc:creator>adithya</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[Lesser Known Facts]]></category>
		<category><![CDATA[dynamic output]]></category>
		<category><![CDATA[justification]]></category>
		<category><![CDATA[precision in output]]></category>
		<category><![CDATA[printing n number characters in the string]]></category>

		<guid isPermaLink="false">http://blog.aditech.info/2008/02/20/dynamic-output-in-c/</guid>
		<description><![CDATA[Dynamic output consider the following program &#160; &#160; #include&#60;stdio.h&#62; int main() { long double a=3.14159265; printf(&#8220;Value of pi=%.4Lf&#8221;,a); return(1); } the output of the program will be &#160; Value of pi=3.1416 wouldn&#8217;t it be nice if we allow the user to choose how the precession of the answer would be?? that is the output should [...]]]></description>
			<content:encoded><![CDATA[<h1 align="center"><font color="#000000">Dynamic output</font></h1>
<p>consider the following program</p>
<p style="margin: 5px 20px 20px">&nbsp;</p>
<p class="smallfont" style="margin-bottom: 2px">&nbsp;</p>
<blockquote><p><font color="#0000ff">#include&lt;stdio.h&gt;<br />
int main()<br />
{<br />
long double a=3.14159265;<br />
printf(&#8220;Value of pi=%.4Lf&#8221;,a);<br />
return(1);<br />
}</font></p></blockquote>
<p>the output of the program will be</p>
<p style="margin: 5px 20px 20px">&nbsp;</p>
<blockquote><p><font color="#ff6600">Value of pi=3.1416</font></p></blockquote>
<p>wouldn&#8217;t it be nice if we allow the user to choose how the precession of the answer would be??<br />
that is the output should be like this</p>
<p style="margin: 5px 20px 20px">&nbsp;</p>
<blockquote><p><font color="#ff6600">Enter</font><font color="#ff9900"> <font color="#ff6600">precision</font></font><br />
<font color="#ff6600">6</font><br />
<font color="#ff6600">The value of pi precised to 6 decimal number is 3.141593</font></p></blockquote>
<p>Well in that case the program will be like this</p>
<p style="margin: 5px 20px 20px">&nbsp;</p>
<blockquote><p><font color="#0000ff">#include&lt;stdio.h&gt;</font><br />
<font color="#0000ff">int main()</font><br />
<font color="#0000ff">{</font><br />
<font color="#0000ff">long double a=3.14159265;</font><br />
<font color="#0000ff">int pre;</font><br />
<font color="#0000ff">printf(&#8220;Enter </font><font color="#0000ff">precision</font><font color="#0000ff">\n&#8221;);</font><br />
<font color="#0000ff">scanf(&#8220;%d&#8221;,&amp;pre);</font><br />
<font color="#0000ff">printf(&#8220;The value of pi precised to %d decimal number is %.*Lf&#8221;,pre,pre,a);</font><br />
<font color="#0000ff">return(1);</font><br />
<font color="#0000ff">}</font></p></blockquote>
<p><em>see the difference,it is upto the user to decide how the output will be.<strong>The value of pi will be rounded off to fit the</strong></em><font color="#ff9900"><font color="#ff6600"> <strong><font color="#000000"><em>precision</em></font></strong> </font></font><em><strong>.</strong>Such an output is called dynamic output.</em></p>
<p>This is the part of the program which is important</p>
<p style="margin: 5px 20px 20px">&nbsp;</p>
<blockquote><p><font color="#0000ff">printf(&#8220;The value of pi precised to %d decimal number is %.*Lf&#8221;,pre,pre,a);</font></p></blockquote>
<blockquote>
<blockquote><p><em><strong> you can create a dynamic output by just specifing * before control string and specifying the variable having precesion in the arguments list.As you can see i have specified pre twice because one to print the The value of pi precesiced to__ and the other to suggest the compiler to fill the * with the value of pre.</strong></em></p></blockquote>
</blockquote>
<p>This  fact can not only be used to specify the precision of the decimal number but also for various other things.Some of them are</p>
<p><strong>1.Printing first n characters of the given string </strong></p>
<p>consider</p>
<blockquote>
<p style="margin: 5px 20px 20px"><font color="#0000ff">printf(&#8220;The Output is %.*s&#8221;,pre,str);</font></p>
</blockquote>
<p>suppose the string variable str has a string of &#8220;adithya&#8221; and the value of int datatype variable pre is 3 then the output will</p>
<p style="margin: 5px 20px 20px">&nbsp;</p>
<blockquote><p><font color="#0000ff">The Output is adi</font></p></blockquote>
<p>2.<strong>Justification</strong><br />
consider</p>
<p style="margin: 5px 20px 20px">&nbsp;</p>
<blockquote><p><font color="#0000ff"> printf(&#8220;The Output is %*.*f.Bye!&#8221;,just,pre,num);</font></p></blockquote>
<p>suppose num(float)=1983.78<br />
pre(int datatype variable)=2<br />
just(int datatype variable)=25</p>
<p>then the output will be in the form of right justification i.e,</p>
<p style="margin: 5px 20px 20px">&nbsp;</p>
<blockquote><p><font color="#0000ff">The Output is                                     1983.78.Bye!</font></p></blockquote>
<p>suppose you want the output to be left justified then either you can specify &#8216;-*.*f&#8217; or give a negative value to the variable &#8216;just&#8217;<br />
suppose for the above example just=-25 then the output will be</p>
<p style="margin: 5px 20px 20px">&nbsp;</p>
<p class="smallfont" style="margin-bottom: 2px">Code:</p>
<blockquote><p><font color="#0000ff">The Output is 1983.78                                   .Bye!</font></p></blockquote>
<h3  class="related_post_title"><hr>You may also like</h3><ul class="related_post"><li><a href="http://blog.aditech.info/how-c-compiler-decides-the-garbage-values-in-case-of-data-overflow.html" title="How C Compiler decides the Garbage values in case of data overflow">How C Compiler decides the Garbage values in case of data overflow</a></li><li><a href="http://blog.aditech.info/how-to-organize-your-firefox-web-history-just-like-opera.html" title="How to Organize your Firefox Web History just like Opera">How to Organize your Firefox Web History just like Opera</a></li><li><a href="http://blog.aditech.info/random-post-option-in-wordpress.html" title="Random post option in wordpress&#8230;">Random post option in wordpress&#8230;</a></li><li><a href="http://blog.aditech.info/how-to-refresh-or-flush-domain-name-systemdns-cache-in-windows.html" title="How to refresh or flush Domain Name System(DNS) Cache in windows">How to refresh or flush Domain Name System(DNS) Cache in windows</a></li><li><a href="http://blog.aditech.info/important-instructions-for-programs-using-mathh-library-function.html" title="Important instructions for programs using math.h library function">Important instructions for programs using math.h library function</a></li><li><a href="http://blog.aditech.info/presenmaker-10.html" title="Presenmaker 1.0">Presenmaker 1.0</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://blog.aditech.info/dynamic-output-in-c.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>floor and ceil</title>
		<link>http://blog.aditech.info/floor-and-ceil.html</link>
		<comments>http://blog.aditech.info/floor-and-ceil.html#comments</comments>
		<pubDate>Wed, 20 Feb 2008 08:09:38 +0000</pubDate>
		<dc:creator>adithya</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[Lesser Known Facts]]></category>
		<category><![CDATA[ceil]]></category>
		<category><![CDATA[floor]]></category>
		<category><![CDATA[math.h]]></category>
		<category><![CDATA[round down the number]]></category>

		<guid isPermaLink="false">http://blog.aditech.info/2008/02/20/floor-and-ceil/</guid>
		<description><![CDATA[&#160; floor and ceil floor and ceil functions comes under math.h header file. floor is used to round down the value of the decimal number and ceil is used to round up the value of the decimal number. consider the following c program #include&#60;stdio.h&#62; #include&#60;math.h&#62; #include&#60;conio.h&#62; { float a; clrscr(); printf(&#8220;Enter any decimal number\n&#8221;); scanf(&#8220;%f&#8221;,&#38;a); [...]]]></description>
			<content:encoded><![CDATA[<p align="center">&nbsp;</p>
<h1><strong>floor and ceil</strong></h1>
<p>floor and ceil functions comes under math.h header file.<br />
floor is used to round down the value of the decimal number and ceil is used to round up the value of the decimal number.</p>
<p>consider the following c program</p>
<blockquote><p><font color="#0000ff">#include&lt;stdio.h&gt;</font><br />
<font color="#0000ff">#include&lt;math.h&gt;</font><br />
<font color="#0000ff">#include&lt;conio.h&gt;</font><br />
<font color="#0000ff">{</font><br />
<font color="#0000ff">float a;</font><br />
<font color="#0000ff">clrscr();</font><br />
<font color="#0000ff">printf(&#8220;Enter any decimal number\n&#8221;);</font><br />
<font color="#0000ff">scanf(&#8220;%f&#8221;,&amp;a);</font><br />
<font color="#0000ff">printf(&#8220;Using floor the value is %d and using ceil it is %d\n&#8221;,floor(a),ceil(a));</font><br />
<font color="#0000ff">getch();</font><br />
<font color="#0000ff">}</font></p></blockquote>
<p><em>Output:</em><br />
<font color="#ff6600">Enter any decimal number<br />
5656.98<br />
Using floor the value is 5656 and using ceil it is 5657</font></p>
<blockquote>
<blockquote><p><em>In otherwords floor will neglect the decimal part while ceil will add 1 to the mantissa part.Hence we can say that floor is similar to casting.That means floor(a) is equivalent to (int)a or (long)a</em></p>
<p><em>These 2 functions do not depend upon the  value after decimal point that means if the program is run with the value 5656.01 then the output will be same.</em></p></blockquote>
</blockquote>
<p>Similarly there is floorl and also ceill which returns long type integers</p>
<blockquote></blockquote>
<p style="margin: 5px 20px 20px">&nbsp;</p>
<p class="smallfont" style="margin-bottom: 2px">&nbsp;</p>
<blockquote></blockquote>
<blockquote></blockquote>
<h3  class="related_post_title"><hr>You may also like</h3><ul class="related_post"><li><a href="http://blog.aditech.info/changing-cd-dvdfloppy-drives-label-and-icon.html" title="Changing CD/DVD/Floppy Drive(s) label and icon">Changing CD/DVD/Floppy Drive(s) label and icon</a></li><li><a href="http://blog.aditech.info/seo-for-wordpress-blogs-part-3.html" title="SEO for wordpress blogs-Part 3/5">SEO for wordpress blogs-Part 3/5</a></li><li><a href="http://blog.aditech.info/the-mistake-in-made-with-my-rss-feeds.html" title="The mistake in made with my RSS feeds&#8230;">The mistake in made with my RSS feeds&#8230;</a></li><li><a href="http://blog.aditech.info/saving-the-output-in-a-text-file.html" title="Saving the output in a text file">Saving the output in a text file</a></li><li><a href="http://blog.aditech.info/google-can-read-flash.html" title="Google can read Flash">Google can read Flash</a></li><li><a href="http://blog.aditech.info/seo-for-wordpress-blogs-part-5.html" title="SEO for wordpress blogs-Part 5/5">SEO for wordpress blogs-Part 5/5</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://blog.aditech.info/floor-and-ceil.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
