<?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 in linux/unix</title>
	<atom:link href="http://blog.aditech.info/category/c-in-linuxunix/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/seo-for-wordpress-blogs-part-4.html" title="SEO for wordpress blogs-Part 4/5">SEO for wordpress blogs-Part 4/5</a></li><li><a href="http://blog.aditech.info/adding-shoutbox-in-wordpress.html" title="Adding ShoutBox in wordpress">Adding ShoutBox in wordpress</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/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/dynamic-output-in-c.html" title="Dynamic Output in C">Dynamic Output in C</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/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-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/ascii-chart.html" title="ASCII Chart">ASCII Chart</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/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/floor-and-ceil.html" title="floor and ceil">floor and ceil</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></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-4.html" title="SEO for wordpress blogs-Part 4/5">SEO for wordpress blogs-Part 4/5</a></li><li><a href="http://blog.aditech.info/atoiatol-and-atof-functions.html" title="atoi,atol and atof functions">atoi,atol and atof functions</a></li><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-1.html" title="SEO for wordpress blogs-Part 1/5">SEO for wordpress blogs-Part 1/5</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/bulid-your-own-open-suse-distro-using-open-studio.html" title="Bulid Your Own Open Suse Distro Using Open Studio">Bulid Your Own Open Suse Distro Using Open Studio</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/seo-for-wordpress-blogs-part-4.html" title="SEO for wordpress blogs-Part 4/5">SEO for wordpress blogs-Part 4/5</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/ascii-chart.html" title="ASCII Chart">ASCII Chart</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/twitter-exposed.html" title="Twitter Exposed">Twitter Exposed</a></li><li><a href="http://blog.aditech.info/dynamic-output-in-c.html" title="Dynamic Output in C">Dynamic Output in C</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>
	</channel>
</rss>
