tag:blogger.com,1999:blog-30639417347681928392024-03-18T15:18:52.464+05:30Computer Programming And Technology For Dummiessee-programming is a popular blog that provides information on C programming basics, data structure, advanced unix programming, network programming, basic linux commands, interview question for freshers, video tutorials and essential softwares for students.CProgrammerhttp://www.blogger.com/profile/17633481395209852801noreply@blogger.comBlogger998125tag:blogger.com,1999:blog-3063941734768192839.post-10139428443360877072016-03-22T21:35:00.000+05:302016-03-22T21:35:07.498+05:30Simple Java program to print "Hello World"<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-family: "trebuchet ms" , sans-serif;">Let us write a simple Java program to print "Hello World".</span><br />
<div>
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
</div>
<div dir="ltr" style="text-align: left;" trbidi="on">
<pre class="brush:java;">/*
* HelloWorld.java - simple java program to print "Hello World"
*/
public class HelloWorld {
public static void main(String args[]) {
// print "Hello World"
System.out.println("Hello World");
}
}
</pre>
</div>
<br />
<br />
<span style="font-family: "trebuchet ms" , sans-serif;">Above is the simple Java program which would print "Hello World" on execution. While saving the Java program, please make sure that the file has .java extension and the name of the file matches with the name of the class that has main() function. We have stored the above source code in HelloWorld.java and the file names matches exactly with the name of the class in which main() function resides.</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;">Let us compile the above Java program. Basically, Java compiler would take Java source code as input and provide us bytecode version of the input Java program as output. As a result of compiling HelloWorld.java, we will get a class file named HelloWorld.class which contains bytecode of HelloWorld.java.</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;">d:\Java>javac HelloWorld.java</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;">Java Virtual Machine would take the bytecode as input and prints the program output on the console. Let us execute our Java program by providing our class name HelloWorld as command line argument to Java command or Java application launcher. As a result of program execution, "Hello World" is printed on output screen.</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;">d:\Java>java HelloWorld</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">Hello World</span><br />
<div>
<br /></div>
<div class="table_without_header">
<table>
<tbody>
<tr>
<td><span style="font-family: Trebuchet MS, sans-serif;">/* */</span></td>
<td><span style="font-family: Trebuchet MS, sans-serif;">Multi-line comments using which we can be used to provide comments in multiple lines to explain code flow or programming logic. These comments are ignored by the compiler</span></td>
</tr>
<tr>
<td><span style="font-family: Trebuchet MS, sans-serif;">//</span></td>
<td><span style="font-family: Trebuchet MS, sans-serif;">Single line comments</span></td>
</tr>
<tr>
<td><span style="font-family: Trebuchet MS, sans-serif;">public</span></td>
<td><span style="font-family: Trebuchet MS, sans-serif;">access modifier which helps to control the visibility of classes, methods and other members</span></td>
</tr>
<tr>
<td><span style="font-family: Trebuchet MS, sans-serif;">class <b>HelloWorld</b></span></td>
<td><span style="font-family: Trebuchet MS, sans-serif;">class is the Keyword used to declare class. HelloWorld is the class name</span></td>
</tr>
<tr>
<td><span style="font-family: Trebuchet MS, sans-serif;">main()</span></td>
<td><span style="font-family: Trebuchet MS, sans-serif;">Method where the program execution begins</span></td>
</tr>
<tr>
<td><span style="font-family: Trebuchet MS, sans-serif;">static</span></td>
<td><span style="font-family: Trebuchet MS, sans-serif;">static is the keyword which can be used on variables, methods or blocks. static methods can be called without instantiation. Here, main() is a static method</span></td>
</tr>
<tr>
<td><span style="font-family: Trebuchet MS, sans-serif;">void</span></td>
<td><span style="font-family: Trebuchet MS, sans-serif;">Return type of a function. void is used as return type when a function has nothing to return</span></td>
</tr>
<tr>
<td><span style="font-family: Trebuchet MS, sans-serif;">System.out.println()</span></td>
<td><span style="font-family: Trebuchet MS, sans-serif;">System is the built in class. out is the object and println() is a built-in method</span></td>
</tr>
<tr>
<td><span style="font-family: Trebuchet MS, sans-serif;">{ }</span></td>
<td><span style="font-family: Trebuchet MS, sans-serif;">open and close brace</span></td>
</tr>
</tbody></table>
</div>
</div>
CProgrammerhttp://www.blogger.com/profile/17633481395209852801noreply@blogger.com133tag:blogger.com,1999:blog-3063941734768192839.post-83188659481826881252016-03-19T19:25:00.001+05:302016-03-19T19:57:10.805+05:30How to install JDK and setting environment variables for java<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-family: "trebuchet ms" , sans-serif;">In this tutorial, we are going to see the following.</span><br />
<ul style="text-align: left;">
<li><span style="font-family: "trebuchet ms" , sans-serif;">How to install JDK</span></li>
<li><span style="font-family: "trebuchet ms" , sans-serif;">How to run java program without setting java path</span></li>
<li><span style="font-family: "trebuchet ms" , sans-serif;">How to set temporary java path in windows</span></li>
<li><span style="font-family: "trebuchet ms" , sans-serif;">How to set permanent java path in windows</span></li>
</ul>
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b>How to install JDK?</b></span><br />
<ul style="text-align: left;">
<li><span style="font-family: "trebuchet ms" , sans-serif;">Check whether JDK is installed on your PC. If it is not installed, please go to the below link and download JDK under Java SE(Java Standard Edition) in <a href="http://www.oracle.com/technetwork/java/javase/downloads/index.html">oracle website</a>.</span></li>
<li><span style="font-family: "trebuchet ms" , sans-serif;">Install the downloaded JDK in your PC.</span></li>
</ul>
<br />
<span style="font-family: "trebuchet ms" , sans-serif;">Once JDK is installed, you can run your java program using any method shown below.</span><br />
<ul style="text-align: left;">
<li><span style="font-family: "trebuchet ms" , sans-serif;">Having your java source code under jdk/bin, compile the source code and run the program from same directory</span></li>
<li><span style="font-family: "trebuchet ms" , sans-serif;">Setting temporary path for java, compiling the source code and running the program from any location using command prompt</span></li>
<li><span style="font-family: "trebuchet ms" , sans-serif;">Updating the java path in system or user variable(in system properties), compiling the source code and running the program anywhere using command prompt</span></li>
</ul>
<div>
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<div>
<span style="font-family: "trebuchet ms" , sans-serif;">Let us see how to run java program without setting environmental variables. Once java is installed in your PC, move to the location where java is installed and check whether javac and java executable are present(jdk/bin). In the below example, Java is installed in the below path.</span></div>
<div>
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<div>
<span style="font-family: "trebuchet ms" , sans-serif;">C:\Java\jdk1.6.0_22\bin</span></div>
<div>
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhNjDSop8rdFmugTLbUqAAko6SIwxCB4KycAiytl0NyQVHeaWRKfb1muKUQSHAAbgOxS3vSaPGz9QeKuIDaBZ-eGbIhmTo1a5uT3XIU_TbwseLIDP8Q0AZFcbHdCGa8_unhouZy5zBTBY3-/s1600/java04.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="364" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhNjDSop8rdFmugTLbUqAAko6SIwxCB4KycAiytl0NyQVHeaWRKfb1muKUQSHAAbgOxS3vSaPGz9QeKuIDaBZ-eGbIhmTo1a5uT3XIU_TbwseLIDP8Q0AZFcbHdCGa8_unhouZy5zBTBY3-/s640/java04.png" width="640" /></a></div>
<div>
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<div>
<span style="font-family: "trebuchet ms" , sans-serif;">I have a java program named javaexample.java stored under <b><i>C</i></b></span><span style="font-family: "trebuchet ms" , sans-serif;"><b><i>:\Java\jdk1.6.0_22\bin</i></b> directory where java compiler and java executable or application launcher are present. Now, let us try to compile and execute our java program.</span></div>
<div>
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<div>
<span style="font-family: "trebuchet ms" , sans-serif;">Launch the command prompt and move to the location where javac.exe and java.exe are located(C</span><span style="font-family: "trebuchet ms" , sans-serif;">:\Java\jdk1.6.0_22\bin)</span><span style="font-family: "trebuchet ms" , sans-serif;">. Then, compile the java source code as shown below.</span></div>
<div>
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<div>
<span style="font-family: "trebuchet ms" , sans-serif;">javac javaexample.c</span></div>
<div>
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<div>
<span style="font-family: "trebuchet ms" , sans-serif;">where javaexample.c is the file where our source code resides. As a result of compilation, we will get a class file named javaexample.class under the same directory where the source code is located.</span></div>
<div>
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgms08dWEC1fLfL6Vmg4gkadNYox5af-ZNO1v6zNMFWmzqiyjIwHSClkdhSOtkT_phusAQ-l7qaqH3-EICuDgq_yZDl_Een2ulOg11-GlnlDOPZYX_TKxgvqRdWjZII9W3nCr6MJcEmprcq/s1600/java05.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="328" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgms08dWEC1fLfL6Vmg4gkadNYox5af-ZNO1v6zNMFWmzqiyjIwHSClkdhSOtkT_phusAQ-l7qaqH3-EICuDgq_yZDl_Een2ulOg11-GlnlDOPZYX_TKxgvqRdWjZII9W3nCr6MJcEmprcq/s640/java05.png" width="640" /></a></div>
<div>
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<div>
<br /></div>
<div>
<span style="font-family: "trebuchet ms" , sans-serif;">Below is the source code of the program which we compiled above. And the program would print "Hello World" on execution.</span></div>
<div>
<br /></div>
</div>
<div dir="ltr" style="text-align: left;" trbidi="on">
<pre class="brush:java;">public class javaexample {
public static void main(String args[]) {
/* prints "Hello World" on the output screen */
System.out.println("Hello World");
}
}
</pre>
</div>
<br />
<span style="font-family: "trebuchet ms" , sans-serif;">Let us execute the program using java application launcher and check the output.</span><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgnSdx-E7dEX1zmnWuuAgR2cfvg0Hr9zG0-CDnC7LEKpjmBCZ6q8PowBFlcXKiwZhCxQ_4604VZOfjRy4KGtZwnKzQl8zwTHbUfDbEERsOzj2nzkIeK6IZ36s777e6QkwgGChcrm4h356UH/s1600/Java06.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="328" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgnSdx-E7dEX1zmnWuuAgR2cfvg0Hr9zG0-CDnC7LEKpjmBCZ6q8PowBFlcXKiwZhCxQ_4604VZOfjRy4KGtZwnKzQl8zwTHbUfDbEERsOzj2nzkIeK6IZ36s777e6QkwgGChcrm4h356UH/s640/Java06.png" width="640" /></a></div>
<span style="font-family: "verdana" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;">"Hello World" is printed on our output screen as a result of executing our java program.</span><br />
<span style="font-family: "verdana" , sans-serif;"><br /></span>
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b>How to set temporary java path in windows?</b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;">Below are the steps to set the temporary java(JDK) path to compile and execute your java program that is located anywhere in our computer.</span><br />
<ul style="text-align: left;">
<li><span style="font-family: "trebuchet ms" , sans-serif;">Command prompt would display <b><i>javac</i></b> as unrecognized command when the same is executed without setting path environmental variable.</span></li>
<li><span style="font-family: "trebuchet ms" , sans-serif;">Set the path environmental variable with the location where java compiler(javac) and java application launcher(java) are located. In my case, javac and java are located in C:\Java\jdk1.6.0_22\bin. Hence, I am setting the same location as value for <b><i>path</i></b> environmental variable in command prompt. This tells our system about the location of javac and java executable.</span></li>
<li><span style="font-family: "trebuchet ms" , sans-serif;">Run javac in command prompt again and check whether the command is recognized.</span></li>
</ul>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhvqvEOhqHtevZ46DAd_B8_gPNiUV-EKJ1U4Whqq8s-19oq6M18i6D5qqhY-jUU863xy3Ae3dSiQLDtjUg_nr0OqbaAx49C9mQObK2DEolNyn-_gmN96UJDsbK-rdkSlUN50RZ4yg7D3fUw/s1600/java07.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="326" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhvqvEOhqHtevZ46DAd_B8_gPNiUV-EKJ1U4Whqq8s-19oq6M18i6D5qqhY-jUU863xy3Ae3dSiQLDtjUg_nr0OqbaAx49C9mQObK2DEolNyn-_gmN96UJDsbK-rdkSlUN50RZ4yg7D3fUw/s640/java07.png" width="640" /></a></div>
<div>
<span style="font-family: "verdana" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;">Please note that the above set path would be <b><i>cleared on closing the command prompt</i></b>. We need to set the path again if we need to compile or execute the program after relaunching command prompt.</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<div>
<b style="color: #660000;"><span style="font-family: "trebuchet ms" , sans-serif;">How to set permanent java path in windows?</span></b></div>
<span style="font-family: "trebuchet ms" , sans-serif;">Below are the steps to set permanent java(JDK) path to compile and execute your java program that is located anywhere in our computer.</span><br />
<ul style="text-align: left;">
<li><span style="font-family: "trebuchet ms" , sans-serif;">Click Start -> Control panel -> System and Security -> System</span></li>
<li><span style="font-family: "trebuchet ms" , sans-serif;">Click <b><i>Advanced settings</i></b> which will open a pop up window named "system properties"</span></li>
<li><span style="font-family: "trebuchet ms" , sans-serif;">Choose "Advanced" tab in system properties</span></li>
<li><span style="font-family: "trebuchet ms" , sans-serif;">Click on "Environment variables" and update your Java(JDK/bin) path.</span></li>
<li><span style="font-family: "trebuchet ms" , sans-serif;">Run javac in command prompt again and check whether the command is recognized.</span></li>
</ul>
<div>
<span style="font-family: "trebuchet ms" , sans-serif;">Trying to execute javac in command prompt without setting path environment variable.</span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhGGSejD6OuC5aC5OVDPURBxcclfH2F330tjRr0yteT0DiD-mCHpYTHvJlfgAn4KXAuk-F3T_b7ERNeS57r9UPu-VpmyDN7xpkIpRO5YH-2lt970x0O3-ushJQWVNr9cX0kRboiy6pVo_TK/s1600/Java01.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="102" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhGGSejD6OuC5aC5OVDPURBxcclfH2F330tjRr0yteT0DiD-mCHpYTHvJlfgAn4KXAuk-F3T_b7ERNeS57r9UPu-VpmyDN7xpkIpRO5YH-2lt970x0O3-ushJQWVNr9cX0kRboiy6pVo_TK/s640/Java01.png" width="640" /></a></div>
<div>
<span style="font-family: "verdana" , sans-serif;"><br /></span></div>
<div>
<span style="font-family: Trebuchet MS, sans-serif;"><span style="font-family: verdana, sans-serif;">Trying to set the environment variable(</span><span style="font-family: verdana, sans-serif;">Start -> Control panel -> System and Security -> System -> Advanced system settings).</span></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgSXVvYm4x5HBqZLFulnU3yEpeqNzp1QcNw0XBr6QG-2mG_-2-eRsi0c3GDegezyNL2OyR1jJjtcZPFRJzqoWN7eVby_xX0pgAKZF669hJ-lSOsqTg3wygZqShwZXGfFjcOx0a0XqqXoZUW/s1600/Java03.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="428" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgSXVvYm4x5HBqZLFulnU3yEpeqNzp1QcNw0XBr6QG-2mG_-2-eRsi0c3GDegezyNL2OyR1jJjtcZPFRJzqoWN7eVby_xX0pgAKZF669hJ-lSOsqTg3wygZqShwZXGfFjcOx0a0XqqXoZUW/s640/Java03.png" width="640" /></a></div>
<div>
<span style="font-family: "verdana" , sans-serif;"><br /></span></div>
<div>
<span style="font-family: "trebuchet ms" , sans-serif;">Setting environment variable - Click </span><b><i>new</i></b><span style="font-family: "trebuchet ms" , sans-serif;"> under <b><i>user variable</i></b> and update PATH variable and its value. Please don't update system variable.</span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgqk7g8jo1p2UuGTekkM_pslRHYp3mRHd6YQnvA4e3Uq8D_vUIv2KdDpgMT8kv1P4oCoY0Cn_PE18-ALw8M3qMa7G9j93wey_YUNFNBkizeqwBOMooiHkZMJs8fLKov6oTuI166x8pVvICY/s1600/JavaPath.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgqk7g8jo1p2UuGTekkM_pslRHYp3mRHd6YQnvA4e3Uq8D_vUIv2KdDpgMT8kv1P4oCoY0Cn_PE18-ALw8M3qMa7G9j93wey_YUNFNBkizeqwBOMooiHkZMJs8fLKov6oTuI166x8pVvICY/s400/JavaPath.JPG" width="345" /></a></div>
<div>
<span style="font-family: "verdana" , sans-serif;"><br /></span></div>
<div>
<span style="font-family: "trebuchet ms" , sans-serif;">Checking whether javac command is getting recognized in command prompt.</span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiADLq7Vnfnv4NNqhN_9aY4hTaJ3-qhl4nvN7hS8qsEzIf46iPq8UrXv7vLc9pjd7JDOVgylDuqIgC_YSS6pjzTDuj6pCkJJj1JDUddTHXSszOxsAYr9XTdvnP6efFLVhCH1LvsRlWbcYU-/s1600/java08.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="328" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiADLq7Vnfnv4NNqhN_9aY4hTaJ3-qhl4nvN7hS8qsEzIf46iPq8UrXv7vLc9pjd7JDOVgylDuqIgC_YSS6pjzTDuj6pCkJJj1JDUddTHXSszOxsAYr9XTdvnP6efFLVhCH1LvsRlWbcYU-/s640/java08.png" width="640" /></a></div>
<div>
<span style="font-family: "verdana" , sans-serif;"><br /></span></div>
<div>
<br /></div>
</div>
CProgrammerhttp://www.blogger.com/profile/17633481395209852801noreply@blogger.com45tag:blogger.com,1999:blog-3063941734768192839.post-44637165856931124072016-03-19T15:18:00.002+05:302016-03-19T15:26:24.255+05:30Introduction to Java<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div>
<span style="font-family: "trebuchet ms" , sans-serif;">Here, we are going to see few key points on Java programming language and the difference between JDK, JRE and JVM.</span></div>
<ul style="text-align: left;">
<li><span style="font-family: "trebuchet ms" , sans-serif;">Java is a high level programming language which can be used to create standalone, web, mobile or enterprise applications.</span></li>
<li><span style="font-family: "trebuchet ms" , sans-serif;">It was developed by James Gosling, Patrick Naughton, Chris Warth, Ed Frank, and Mike Sheridan at Sun Microsystems.</span></li>
<li><span style="font-family: "trebuchet ms" , sans-serif;">It was initially named as Oak. In 1995, Oak was renamed as Java.</span></li>
<li><span style="font-family: "trebuchet ms" , sans-serif;">Java programming language helps us to create applications that can be run on multiple platforms without having to port the code and recompile. In a nutshell, Java is a portable language.</span></li>
<li><span style="font-family: "trebuchet ms" , sans-serif;">Java is object oriented as it helps us to correlate real world object or examples to java programs</span></li>
<li><span style="font-family: "trebuchet ms" , sans-serif;">It is known for its robustness as it avoid drawbacks in other programming languages like pointer arithmetic or memory management etc.</span></li>
<li><span style="font-family: "trebuchet ms" , sans-serif;">It allows us to create multi-threaded applications where each thread can do separate task.</span></li>
<li><span style="font-family: "trebuchet ms" , sans-serif;">Java was initially owned by Sun Microsystems. In 2010, Oracle acquired Sun Microsystems.</span></li>
</ul>
<br />
<br />
<span style="font-family: "trebuchet ms" , sans-serif;">Before getting into Java tutorials, we may need to learn about JVM, JDK and JRE</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b>What is JVM?</b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;">On compiling a Java program, we will get class file as output which consists of bytecodes. And these bytecodes are executed by java runtime system called Java Virtual Machine. It consists of class loader, security engine, bytecode verifier and garbage collector. </span><br />
<ul style="text-align: left;">
<li><span style="font-family: "trebuchet ms" , sans-serif;">Class loader helps to load all the class files that are needed for the program execution.</span></li>
<li><span style="font-family: "trebuchet ms" , sans-serif;">Security engine helps to prevent unauthorized access into the restricted area.</span></li>
<li><span style="font-family: "trebuchet ms" , sans-serif;">Bytecode verifier runs several test on our bytecode to find whether there is any error conditions like stack overflow, stack underflow, invalid type conversion, illegal code access etc.</span></li>
<li><span style="font-family: "trebuchet ms" , sans-serif;">Garbage collector takes care of cleaning the unfreed memory.</span></li>
</ul>
<br />
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b>What is JRE?</b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;">Java Runtime Environment which consists of JVM and Java Application Program interfaces. In other words, its the superset of JVM and it provides environment for running a Java application or program.</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b>What is JDK?</b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;">Java development kit provides environment for developers to develop, compile and run Java application. JDK consists of both JVM and JRE. In other words, it is the superset of JRE. It provides a large set of tools like complier, interpreter, Java packages, document generator etc.</span></div>
CProgrammerhttp://www.blogger.com/profile/17633481395209852801noreply@blogger.com35tag:blogger.com,1999:blog-3063941734768192839.post-36830574870378819922016-03-05T16:09:00.001+05:302016-03-05T16:10:19.271+05:30Save image in Microsoft Paint without white background<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-family: "trebuchet ms" , sans-serif;">Here, we are going to see how to save image in mspaint without white background</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;">1. Create your own drawing or painting</span><br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgFRZho34ELxqJCykFyup1mGaWB7AgKo1zYtVdLT1EC1kO-JQYcK_KNxGS7L1ezEmYVg6z43jh96h-AeOQWa-dNqffo73FSHbquWhP6TtwUk0nugbGkqP__Dhex7rcfuyH1GaBOYTOSGL5N/s1600/Image01.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="348" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgFRZho34ELxqJCykFyup1mGaWB7AgKo1zYtVdLT1EC1kO-JQYcK_KNxGS7L1ezEmYVg6z43jh96h-AeOQWa-dNqffo73FSHbquWhP6TtwUk0nugbGkqP__Dhex7rcfuyH1GaBOYTOSGL5N/s640/Image01.jpg" width="640" /></a></div>
<br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;">2. Crop the needed portion of your drawing or painting or image.</span><br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjr1AZG_UJlG8IQW7Xop_7BZpOaF_NS5XByW2WAqV-UOB0sgwXwx3ypqAEJ9hQUNRgYdFsiYghc3aXHgCpSvsck5dR2Ey5lDfWJWCt_CMR7gogk6gcK5ReERRp4NpBqa1lYAxU7BI7oy_-K/s1600/Image01.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="358" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjr1AZG_UJlG8IQW7Xop_7BZpOaF_NS5XByW2WAqV-UOB0sgwXwx3ypqAEJ9hQUNRgYdFsiYghc3aXHgCpSvsck5dR2Ey5lDfWJWCt_CMR7gogk6gcK5ReERRp4NpBqa1lYAxU7BI7oy_-K/s640/Image01.jpg" width="640" /></a></div>
<br />
<span style="font-family: "trebuchet ms" , sans-serif;">Here, I am going to crop the porting inside the red box.</span><br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhGP5QJn1tIAOzjdXHWJ82RHDPNoSMu-0FtBO60xd1Ee0kyi0J1sPvTEvsKcGNRxf3Kf5Ih0h2H9Xw4f_5sxztXYCOvGJEG72FtErirYxUAjA1yM_5Sh4nhQ50DPQjWwerr_9aQoVFbMnik/s1600/Image01.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="358" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhGP5QJn1tIAOzjdXHWJ82RHDPNoSMu-0FtBO60xd1Ee0kyi0J1sPvTEvsKcGNRxf3Kf5Ih0h2H9Xw4f_5sxztXYCOvGJEG72FtErirYxUAjA1yM_5Sh4nhQ50DPQjWwerr_9aQoVFbMnik/s640/Image01.jpg" width="640" /></a></div>
<br />
<span style="font-family: "trebuchet ms" , sans-serif;">Now, we have cropped the portion that needs to be saved as an image.</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;">5. Clear the screen and paste the image which we cropped earlier.</span><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgu3u-XMTNlM7ukpEpmKe9zVz831FlfocoT46wIv8bnnY31IUi8enCrEDgMkBWAoT4mQY1lkARbE2cD7TXPtmnXwpOpFa2LaKcw7ldk84O4H1tgqF8YL-dgIfOZXjjvQoLVsqccyYNeZMxq/s1600/Image01.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="358" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgu3u-XMTNlM7ukpEpmKe9zVz831FlfocoT46wIv8bnnY31IUi8enCrEDgMkBWAoT4mQY1lkARbE2cD7TXPtmnXwpOpFa2LaKcw7ldk84O4H1tgqF8YL-dgIfOZXjjvQoLVsqccyYNeZMxq/s640/Image01.jpg" width="640" /></a></div>
<br />
<br />
<span style="font-family: "trebuchet ms" , sans-serif;">6. Decrease the screen size by 50%</span><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhaL84EuMmBRUc1cie6WRPJI2lW0bo7obnk-8v94pvPRZuhNqYkszFMFLDQbMXiqg0pDbjUAGTjDyBK-xRP9YtS4rfXpTCGwbt9ObREj8BdeCmmKHMJpwAonHBmn-2o64y61ewYztZv__ZP/s1600/Image01.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="358" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhaL84EuMmBRUc1cie6WRPJI2lW0bo7obnk-8v94pvPRZuhNqYkszFMFLDQbMXiqg0pDbjUAGTjDyBK-xRP9YtS4rfXpTCGwbt9ObREj8BdeCmmKHMJpwAonHBmn-2o64y61ewYztZv__ZP/s640/Image01.jpg" width="640" /></a></div>
<br />
<br />
<span style="font-family: "trebuchet ms" , sans-serif;">7. Now, adjust your screen to fit to your image size.</span><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh_4Vyqkzr_P05ssE2gkCKXeXA5cuM1brYh60LY1bW6GrMRfckPi2uuNhwjhIkp0CVJM_qKbx8ixmpX-sj0CEkg_j44cKD1kgE3xne0c9QafFyfFMyKdOjg8bHwJ5JzjntU_Ar4CwWrq2l_/s1600/Image01.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="359" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh_4Vyqkzr_P05ssE2gkCKXeXA5cuM1brYh60LY1bW6GrMRfckPi2uuNhwjhIkp0CVJM_qKbx8ixmpX-sj0CEkg_j44cKD1kgE3xne0c9QafFyfFMyKdOjg8bHwJ5JzjntU_Ar4CwWrq2l_/s640/Image01.jpg" width="640" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgdISZD-VZjN6TokpA0yPnsVh-p2XaJcxHNDw5Yc4p8UXg-I4RYPawxj70YZagR9qeXkYOv7iH4zGVHlHwPfzXhVkn8bMQy0YxrW3W6rJiGrLUfvJcc8dIDYQJuDB2cW30sqqzqlaX1i5AW/s1600/Image01.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="358" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgdISZD-VZjN6TokpA0yPnsVh-p2XaJcxHNDw5Yc4p8UXg-I4RYPawxj70YZagR9qeXkYOv7iH4zGVHlHwPfzXhVkn8bMQy0YxrW3W6rJiGrLUfvJcc8dIDYQJuDB2cW30sqqzqlaX1i5AW/s640/Image01.jpg" width="640" /></a></div>
<br />
<br />
<span style="font-family: "trebuchet ms" , sans-serif;">8. Save the image to desired file format(jpeg, png, bmp etc)</span><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhikH4UvV9j7Hv2uM-mBBPLE2GsUDeu5fA1m7-k-fZCgMY-q0YIyc_rD8HctAM449XaTyVzjcTIY7d1JuCi_OITtSREC-jBeRsxwL-kCc21K9RKArNz6ZrvQIFsqAdRqU-hP9VMsWNgP8A2/s1600/Image01.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="358" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhikH4UvV9j7Hv2uM-mBBPLE2GsUDeu5fA1m7-k-fZCgMY-q0YIyc_rD8HctAM449XaTyVzjcTIY7d1JuCi_OITtSREC-jBeRsxwL-kCc21K9RKArNz6ZrvQIFsqAdRqU-hP9VMsWNgP8A2/s640/Image01.jpg" width="640" /></a></div>
<br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;">9. Go to the saved location and open the file. Below is the output image of mine</span><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiB6s5LKrVnQ5WUQHltRPv-9Kw84TBBFXULqDEI_lIp8TtPbGuZSDTiw3yom1hUQSKpkXR6AinFvZg-auWJZS2dmTz0ReR1FCKjHkptEb1K94S_My6CoBU2ojGtSruniaOqG2BBFw3Bog8r/s1600/Image.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="232" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiB6s5LKrVnQ5WUQHltRPv-9Kw84TBBFXULqDEI_lIp8TtPbGuZSDTiw3yom1hUQSKpkXR6AinFvZg-auWJZS2dmTz0ReR1FCKjHkptEb1K94S_My6CoBU2ojGtSruniaOqG2BBFw3Bog8r/s640/Image.jpg" width="640" /></a></div>
<br />
<span style="font-family: "trebuchet ms" , sans-serif;">Hope you liked this article!! For any suggestions, please comment below.</span></div>
CProgrammerhttp://www.blogger.com/profile/17633481395209852801noreply@blogger.com43tag:blogger.com,1999:blog-3063941734768192839.post-39299978823035656692016-02-24T23:36:00.001+05:302016-04-01T22:30:47.753+05:30LINUX cal command examples<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
cal utility is used to display calendar of any specific month or year. Below are few examples for linux cal command usage.<br />
<ul style="text-align: left;">
<li>Display calendar of any specific year</li>
<li>Display calendar of current month</li>
<li>Display calendar of any specific month in current year</li>
<li>Display calendar of any specific month in any specific year</li>
<li>Display Julian calendar of current year</li>
<li>Display Julian calendar of any specific year</li>
<li>Display Julian calendar of any specific month</li>
<li>Display Julian calendar of any specific month in any specific year</li>
<li>Display calendar without high lighting current date</li>
<li>Display previous, current and next month calendar highlighting current date</li>
<li>Display previous, current and next month calendar without highlighting current date</li>
<li>Display previous, current and next month in Julian calendar highlighting current date</li>
<li>Display previous, current and next month in Julian calendar without highlighting current date</li>
<li>Display calendar of given month in following year</li>
<li>Display calendar of given month in previous year</li>
</ul>
<div>
<br /></div>
LINUX cal command examples:<br />
Display calendar of any specific year
<br />
cal -y <year><br />
cal <year><br />
<br />
<pre class="linux_commands">jp@jp-VirtualBox:~$ cal 2016
2016
January February March
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 1 2 3 4 5 6 1 2 3 4 5
3 4 5 6 7 8 9 7 8 9 10 11 12 13 6 7 8 9 10 11 12
10 11 12 13 14 15 16 14 15 16 17 18 19 20 13 14 15 16 17 18 19
17 18 19 20 21 22 23 21 22 23 24 25 26 27 20 21 22 23 24 25 26
24 25 26 27 28 29 30 28 29 27 28 29 30 31
31
April May June
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 1 2 3 4 5 6 7 1 2 3 4
3 4 5 6 7 8 9 8 9 10 11 12 13 14 5 6 7 8 9 10 11
10 11 12 13 14 15 16 15 16 17 18 19 20 21 12 13 14 15 16 17 18
17 18 19 20 21 22 23 22 23 24 25 26 27 28 19 20 21 22 23 24 25
24 25 26 27 28 29 30 29 30 31 26 27 28 29 30
July August September
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 1 2 3 4 5 6 1 2 3
3 4 5 6 7 8 9 7 8 9 10 11 12 13 4 5 6 7 8 9 10
10 11 12 13 14 15 16 14 15 16 17 18 19 20 11 12 13 14 15 16 17
17 18 19 20 21 22 23 21 22 23 24 25 26 27 18 19 20 21 22 23 24
24 25 26 27 28 29 30 28 29 30 31 25 26 27 28 29 30
31
October November December
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 1 2 3 4 5 1 2 3
2 3 4 5 6 7 8 6 7 8 9 10 11 12 4 5 6 7 8 9 10
9 10 11 12 13 14 15 13 14 15 16 17 18 19 11 12 13 14 15 16 17
16 17 18 19 20 21 22 20 21 22 23 24 25 26 18 19 20 21 22 23 24
23 24 25 26 27 28 29 27 28 29 30 25 26 27 28 29 30 31
30 31
</pre>
<div>
<br /></div>
Display calendar of current month<br />
cal<br />
<br />
<pre class="linux_commands">jp@jp-VirtualBox:~$ cal
February 2016
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29
</pre>
<br />
Display calendar of any specific month in current year</div>
<pre class="linux_commands">jp@jp-VirtualBox:~$ cal -m 4
April 2016
Su Mo Tu We Th Fr Sa
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
</pre>
Display calendar of any specific month in any specific year
<br />
<pre class="linux_commands">jp@jp-VirtualBox:~$ cal 10 2010
October 2010
Su Mo Tu We Th Fr Sa
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
</pre>
<br />
Display Julian calendar of any specific year
<br />
<pre class="linux_commands">jp@jp-VirtualBox:~$ cal -j 2016
2016
January February
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 32 33 34 35 36 37
3 4 5 6 7 8 9 38 39 40 41 42 43 44
10 11 12 13 14 15 16 45 46 47 48 49 50 51
17 18 19 20 21 22 23 52 53 54 55 56 57 58
24 25 26 27 28 29 30 59 60
31
March April
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
61 62 63 64 65 92 93
66 67 68 69 70 71 72 94 95 96 97 98 99 100
73 74 75 76 77 78 79 101 102 103 104 105 106 107
80 81 82 83 84 85 86 108 109 110 111 112 113 114
87 88 89 90 91 115 116 117 118 119 120 121
May June
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
122 123 124 125 126 127 128 153 154 155 156
129 130 131 132 133 134 135 157 158 159 160 161 162 163
136 137 138 139 140 141 142 164 165 166 167 168 169 170
143 144 145 146 147 148 149 171 172 173 174 175 176 177
150 151 152 178 179 180 181 182
July August
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
183 184 214 215 216 217 218 219
185 186 187 188 189 190 191 220 221 222 223 224 225 226
192 193 194 195 196 197 198 227 228 229 230 231 232 233
199 200 201 202 203 204 205 234 235 236 237 238 239 240
206 207 208 209 210 211 212 241 242 243 244
213
September October
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
245 246 247 275
248 249 250 251 252 253 254 276 277 278 279 280 281 282
255 256 257 258 259 260 261 283 284 285 286 287 288 289
262 263 264 265 266 267 268 290 291 292 293 294 295 296
269 270 271 272 273 274 297 298 299 300 301 302 303
304 305
November December
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
306 307 308 309 310 336 337 338
311 312 313 314 315 316 317 339 340 341 342 343 344 345
318 319 320 321 322 323 324 346 347 348 349 350 351 352
325 326 327 328 329 330 331 353 354 355 356 357 358 359
332 333 334 335 360 361 362 363 364 365 366
</pre>
<br />
Display Julian calendar of any specific month in current year
<br />
<pre class="linux_commands">jp@jp-VirtualBox:~$ cal -j
February 2016
Su Mo Tu We Th Fr Sa
32 33 34 35 36 37
38 39 40 41 42 43 44
45 46 47 48 49 50 51
52 53 54 55 56 57 58
59 60
</pre>
Display Julian calendar of any specific month in any specific year
<br />
<pre class="linux_commands">jp@jp-VirtualBox:~$ cal -j 10 2012
October 2012
Su Mo Tu We Th Fr Sa
275 276 277 278 279 280
281 282 283 284 285 286 287
288 289 290 291 292 293 294
295 296 297 298 299 300 301
302 303 304 305
</pre>
Display calendar without highlighting current date
<br />
<pre class="linux_commands">jp@jp-VirtualBox:~$ cal -h -m 2
February 2016
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29
</pre>
Display previous, current and next month calendar highlighting current date
<br />
<pre class="linux_commands">jp@jp-VirtualBox:~$ cal -3
January 2016 February 2016 March 2016
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 1 2 3 4 5 6 1 2 3 4 5
3 4 5 6 7 8 9 7 8 9 10 11 12 13 6 7 8 9 10 11 12
10 11 12 13 14 15 16 14 15 16 17 18 19 20 13 14 15 16 17 18 19
17 18 19 20 21 22 23 21 22 23 24 25 26 27 20 21 22 23 24 25 26
24 25 26 27 28 29 30 28 29 27 28 29 30 31
31
</pre>
<br />
Display previous, current and next month calendar highlighting current date
<br />
<pre class="linux_commands">jp@jp-VirtualBox:~$ cal -h -3
January 2016 February 2016 March 2016
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 1 2 3 4 5 6 1 2 3 4 5
3 4 5 6 7 8 9 7 8 9 10 11 12 13 6 7 8 9 10 11 12
10 11 12 13 14 15 16 14 15 16 17 18 19 20 13 14 15 16 17 18 19
17 18 19 20 21 22 23 21 22 23 24 25 26 27 20 21 22 23 24 25 26
24 25 26 27 28 29 30 28 29 27 28 29 30 31
31
</pre>
<br />
Display previous, current and next month in Julian calendar highlighting current date
<br />
<pre class="linux_commands">jp@jp-VirtualBox:~$ cal -j -3
January 2016 February 2016 March 2016
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 32 33 34 35 36 37 61 62 63 64 65
3 4 5 6 7 8 9 38 39 40 41 42 43 44 66 67 68 69 70 71 72
10 11 12 13 14 15 16 45 46 47 48 49 50 51 73 74 75 76 77 78 79
17 18 19 20 21 22 23 52 53 54 55 56 57 58 80 81 82 83 84 85 86
24 25 26 27 28 29 30 59 60 87 88 89 90 91
31
</pre>
<br />
Display previous, current and next month in Julian calendar without highlighting current date
<br />
<pre class="linux_commands">jp@jp-VirtualBox:~$ cal -jh -3
January 2016 February 2016 March 2016
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 32 33 34 35 36 37 61 62 63 64 65
3 4 5 6 7 8 9 38 39 40 41 42 43 44 66 67 68 69 70 71 72
10 11 12 13 14 15 16 45 46 47 48 49 50 51 73 74 75 76 77 78 79
17 18 19 20 21 22 23 52 53 54 55 56 57 58 80 81 82 83 84 85 86
24 25 26 27 28 29 30 59 60 87 88 89 90 91
31
</pre>
<br />
Display calendar of given month in previous year
<br />
<pre class="linux_commands">jp@jp-VirtualBox:~$ cal -m2p
February 2015
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
</pre>
<br />
Display calendar of given month in following year
<br />
<pre class="linux_commands">jp@jp-VirtualBox:~$ cal -m2f
February 2017
Su Mo Tu We Th Fr Sa
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28
</pre>
</div>
CProgrammerhttp://www.blogger.com/profile/17633481395209852801noreply@blogger.com189tag:blogger.com,1999:blog-3063941734768192839.post-42778324703066829782014-01-18T14:37:00.001+05:302016-03-13T10:47:41.419+05:30Operator precedence and associativity in C language<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-family: "trebuchet ms" , sans-serif;">Below is the table for operator precedence and associativity in C programming language.</span><br />
<table class="func_table" id="precedence_table_td">
<tbody>
<tr>
<td>[ ]<br />
( )<br />
.<br />
-><br />
++ --
</td>
<td>Array subscript<br />
Function Call<br />
Structure reference<br />
Structure dereference<br />
Postfix increment/Postfix decrement
</td>
<td><br />
<br />
Left to right
</td>
</tr>
<tr>
<td>++ --<br />
+ -<br />
! ~<br />
(type)<br />
* &<br />
sizeof
</td>
<td>Prefix increment/Prefix decrement<br />
Unary plus/Unary minus<br />
Logical negation/One's complement<br />
Typecast operator<br />
Pointer dereference/Address of<br />
Size of type/variable in bytes
</td>
<td><br />
<br />
Right to left
</td>
</tr>
<tr>
<td>* / %
</td>
<td>Multiplication/Division/Modulo
</td>
<td>Left to Right
</td>
</tr>
<tr>
<td>+ -
</td>
<td>Addition/Subtraction
</td>
<td>Left to Right
</td>
</tr>
<tr>
<td><< >>
</td>
<td>Bitwise left shift/ Bitwise right shift
</td>
<td>Left to Right
</td>
</tr>
<tr>
<td>< ><br />
<= <br />
>= </td>
<td>Comparison less than/Comparision greater than<br />
Comparison less than or equal to<br />
Comparison greater than or equal to</td>
<td><br />
Left to Right
</td>
</tr>
<tr>
<td>== !=
</td>
<td>Comparison equal to/Comparison not equal to
</td>
<td>Left to Right
</td>
</tr>
<tr>
<td>&
</td>
<td>Bitwise AND
</td>
<td>Left to Right
</td>
</tr>
<tr>
<td>^
</td>
<td>Bitwise XOR
</td>
<td>Left to Right
</td>
</tr>
<tr>
<td>|
</td>
<td>Bitwise OR
</td>
<td>Left to Right
</td>
</tr>
<tr>
<td>&&
</td>
<td>Logical AND
</td>
<td>Left to Right
</td>
</tr>
<tr>
<td>||
</td>
<td>Logical OR
</td>
<td>Left to Right
</td>
</tr>
<tr>
<td>?:
</td>
<td>Ternary Conditional Operator
</td>
<td>Right to Left
</td>
</tr>
<tr>
<td>=<br />
*= /= %=<br />
+= -= <br />
<<= >>=<br />
&= ^=<br />
|=
</td>
<td>Assignment Operator<br />
Mulplication/division/modulo assignment<br />
Addition/Subtraction assignment<br />
Bitwise left shift/right shift assignment<br />
Bitwise AND/XOR assignment<br />
Bitwise OR assignment
</td>
<td><br />
<br />
Right to Left
</td>
</tr>
<tr>
<td>,
</td>
<td>Comma Operator
</td>
<td>Left to Right
</td></tr>
</tbody></table>
</div>
<br />
<ul class="prev_next_button">
<li class="previous"><a href="http://see-programming.blogspot.in/2012/04/conditional-operators.html">Previous</a></li>
<li class="next"><a href="http://see-programming.blogspot.com/2013/12/control-flow-overview.html">Next</a></li>
</ul>
<br /></div>
CProgrammerhttp://www.blogger.com/profile/17633481395209852801noreply@blogger.com19tag:blogger.com,1999:blog-3063941734768192839.post-28767015213474306642014-01-12T22:27:00.000+05:302016-03-16T22:56:21.389+05:30#ifdef, #ifndef and #endif directives in C<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b>#ifdef</b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;">If the named macro is defined, then the code between #ifdef and #endif will be compiled.</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b>#ifndef</b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;">If the named macro is not defined, then the code between #ifndef and #endif will be compiled.</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b>#ifdef, #ifndef and #endif example in C</b></span></div>
</div>
<div dir="ltr" style="text-align: left;" trbidi="on">
<pre class="brush:c++;">
#include <stdio.h>
#define NAME "Raj"
int main() {
#ifdef NAME
printf("#ifdef: Value of NAME is %s\n", NAME);
#endif
#ifndef VAL
printf("#ifndef: macro VAL is not defined\n");
#endif
return 0;
}
</pre>
<br /></div>
</div>
<div style="background-color: #999999; color: white; font-family: "Trebuchet MS",sans-serif;">
<b> Output:</b></div>
<div style="background-color: black;">
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> jp@jp-VirtualBox:~/$ ./a.out</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> #ifdef: Value of NAME is Raj</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> #ifndef: macro VAL is not defined</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<br />
<span style="font-family: "trebuchet ms" , sans-serif;">In the above program, macro NAME is defined. So, the code between #ifdef and #endif is executed. Similarly, the code between #ifndef and #endif is executed as the macro VAL is not defined.</span><br />
<br /></div>
<br />
<ul class="prev_next_button">
<li class="previous"><a href="http://see-programming.blogspot.in/2014/01/if-elif-else-and-endif-example.html">Previous</a></li>
<li class="next"><a href="http://see-programming.blogspot.com/2012/06/pragma-directive.html">Next</a></li>
</ul>
<br /></div>
CProgrammerhttp://www.blogger.com/profile/17633481395209852801noreply@blogger.com13tag:blogger.com,1999:blog-3063941734768192839.post-23521039912783689272014-01-12T21:51:00.000+05:302016-03-16T22:49:10.792+05:30#if, #elif, #else and #endif example<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-family: "trebuchet ms" , sans-serif;"><b><span style="color: #660000;">#if</span></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;">If the resultant value of the arithmetic expression is non-zero, then the code between <b><i>#if</i></b> and <b><i>#endif</i></b> will be compiled.</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b>Example:</b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>#if 10 > 5</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i> printf("10 is greater than 5");</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>#endif</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;">The code between <b><i>#if</i></b> and <b><i>#endif</i></b> will be compiled since the resultant value of the expression(10 > 5) is non-zero.</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b>#elif</b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;">This provides an alternate expression to evaluate</span><br />
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b><br /></b></span>
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b>Example:</b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>#if 10 < 5</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i> printf("10 is less than 5");</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>#elif 10 > 5</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i> printf("10 is greater than 5");</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>#endif</i></b></span><br />
<div>
<br /></div>
<span style="font-family: "trebuchet ms" , sans-serif;">The expression at <b><i>#if</i></b> directive evaluates to 0. So, the expression at <b><i>#elif</i></b> is evaluated. If it is non-zero, then the code between <b><i>#elif</i></b> and <b><i>#endif</i></b> will be compiled.</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b>#else</b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;">If the resultant value of the arithmetic expression is false for <b><i>#if, #ifdef</i></b> or <b><i>#ifndef</i></b>, then the code between <b><i>#else</i></b> and <b><i>#endif</i></b> will be compiled.</span><br />
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b><br /></b></span>
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b>Example:</b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>#if 10 < 5</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i> printf("10 is less than 5");</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>#else</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i> printf("10 is greater than 5");</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>#endif</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b>#endif</b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;">This acts as an end directive for <b><i>#if, #ifdef, #ifndef, #elif or #if</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>#if 100 < 50 // resultant value of expression is 0</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i> printf("#if directive");</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>#elif 50 < 10 // resultant value of the expression is 0</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i> printf("#elif directive");</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>#else</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i> printf("#else directive"); // this statement would be executed</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>#endif</i></b></span><br />
<br />
<div>
<b style="color: #660000; font-family: 'trebuchet ms', sans-serif;">Example C program to illustrate #if #elif #else #endif usage in C:</b><br />
<br /></div>
</div>
<div dir="ltr" style="text-align: left;" trbidi="on">
<pre class="brush:c++;"> #include <stdio.h>
#define VAL1 10
#define VAL2 20
#define VAL3 30
int main() {
#if VAL1 > VAL2
printf("%d is greater than %d\n", VAL1, VAL2);
#elif VAL1 > VAL3
printf("%d is greater than %d\n", VAL1, VAL3);
#else
printf("%d is less than %d and %d\n", VAL1, VAL2, VAL3);
#endif
return 0;
}
</pre>
</div>
<br />
<div style="background-color: #999999; color: white; font-family: "Trebuchet MS",sans-serif;">
<b> Output:</b></div>
<div style="background-color: black;">
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> jp@jp-VirtualBox:~/$ ./a.out</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> 10 is less than 20 and 30</span><br />
<div style="color: white; font-family: 'Trebuchet MS', sans-serif;">
<br /></div>
</div>
<br /></div>
</div>
<br />
<ul class="prev_next_button">
<li class="previous"><a href="http://see-programming.blogspot.in/2012/05/define-vs-const.html">Previous</a></li>
<li class="next"><a href="http://see-programming.blogspot.in/2014/01/ifdef-ifndef-and-endif-directives-in-c.html">Next</a></li>
</ul>
<br /></div>
CProgrammerhttp://www.blogger.com/profile/17633481395209852801noreply@blogger.com13tag:blogger.com,1999:blog-3063941734768192839.post-59662911563955128182014-01-11T18:11:00.000+05:302016-03-15T19:43:12.346+05:30How to access union members in C<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;">Dot operator is used to access(or assign) the data members of a union variable. Consider the following example,</span><br />
<span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 15px; line-height: 20px;"><b><i>union value {</i></b></span></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 15px; line-height: 20px;"><b><i> int x;</i></b></span></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 15px; line-height: 20px;"><b><i> float y;</i></b></span></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 15px; line-height: 20px;"><b><i>};</i></b></span></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 15px; line-height: 20px;"><br /></span></span>
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 15px; line-height: 20px;"><b><i>union value obj;</i></b></span></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 15px; line-height: 20px;">Here, <b><i>obj</i></b> is a union variable of type union <b><i>value</i></b>. <b><i>x</i></b> and <b><i>y</i></b> are data members of the union <b><i>value</i></b>. Let us see how to assign value to or access data members of union using dot operator.</span></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 15px; line-height: 20px;"><br /></span></span>
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 15px; line-height: 20px;">obj.x = 10;</span></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 15px; line-height: 20px;"><br /></span></span>
<span style="font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;">The above statement assigns value 10 to the data member x of the union variable obj.</span><br />
<span style="font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 15px; line-height: 20px;">obj - object name(union variable name)</span></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 15px; line-height: 20px;">x - data member in union value</span></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 15px; line-height: 20px;">. - dot operator</span></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 15px; line-height: 20px;"><br /></span></span>
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 15px; line-height: 20px;"><br /></span></span>
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b>Example C program to illustrate accessing union members using dot operator</b></span><br />
<br /></div>
<div dir="ltr" style="text-align: left;" trbidi="on">
<pre class="brush:c++;"> #include <stdio.h>
union value {
int x;
float y;
}obj;
int main() {
obj.x = 5;
printf("Value of x is %d\n", obj.x);
obj.y = 19.22;
printf("Value of y is %f\n", obj.y);
printf("Value of x is %d\n", obj.x);
return 0;
}
</pre>
</div>
<br />
<div style="background-color: #999999; color: white; font-family: "Trebuchet MS",sans-serif;">
<b> Output:</b></div>
<div style="background-color: black;">
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> jp@jp-VirtualBox:~/$ ./a.out</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Value of x is 5</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Value of y is 19.219999</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Value of x is 1100595855</span><br />
<div style="color: white; font-family: 'Trebuchet MS', sans-serif;">
<br /></div>
</div>
<br />
<span style="font-family: "trebuchet ms" , sans-serif;">The value of data member <b><i>y</i></b> overwrites the value of <b><i>x</i></b> and that is reason why we see junk value in <b><i>x</i></b>. Basically, all data members in a union shares same storage place.</span><br />
<br /></div>
</div>
<ul class="prev_next_button">
<li class="previous"><a href="http://see-programming.blogspot.com/2014/01/how-to-initialize-union-varibles-in-c.html">Previous</a></li>
<li class="next"><a href="http://see-programming.blogspot.in/2014/01/array-of-unions.html">Next</a></li>
</ul>
<br /></div>
CProgrammerhttp://www.blogger.com/profile/17633481395209852801noreply@blogger.com12tag:blogger.com,1999:blog-3063941734768192839.post-26906604131943419142014-01-11T17:42:00.000+05:302016-03-15T19:37:44.860+05:30How to initialize union variables in C<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="background-color: white; font-family: 'Trebuchet MS', sans-serif; font-size: 15px; line-height: 20px;">
Let us see all possible ways to initialize a union variable. Please remember that we can access one data member at a time in the case of union.<br />
<br />
<b><span style="color: #660000;">Method 1:</span></b></div>
<div style="background-color: white; font-family: 'Trebuchet MS', sans-serif; font-size: 15px; line-height: 20px;">
<b><i>union value {</i></b></div>
<div style="background-color: white; font-family: 'Trebuchet MS', sans-serif; font-size: 15px; line-height: 20px;">
<b><i> int x;</i></b></div>
<div style="background-color: white; font-family: 'Trebuchet MS', sans-serif; font-size: 15px; line-height: 20px;">
<b><i> float y;</i></b></div>
<div style="background-color: white; font-family: 'Trebuchet MS', sans-serif; font-size: 15px; line-height: 20px;">
<b><i>};</i></b></div>
<div style="background-color: white; font-family: 'Trebuchet MS', sans-serif; font-size: 15px; line-height: 20px;">
<b><i>union value obj = {5};</i></b></div>
<div style="background-color: white; font-family: 'Trebuchet MS', sans-serif; font-size: 15px; line-height: 20px;">
<br /></div>
<div style="background-color: white; font-family: 'Trebuchet MS', sans-serif; font-size: 15px; line-height: 20px;">
Here, initialization is done after union definition. <i style="font-weight: bold;">5</i> is assigned to the member <b><i>x</i></b> of union variable <b><i>obj</i></b>.<br />
<br />
<b><span style="color: #660000;">Method 2:</span></b><br />
<div>
<b><i>union value {</i></b></div>
<div>
<b><i> int x;</i></b></div>
<div>
<b><i> float y;</i></b></div>
<div>
<b><i>}obj = {5};</i></b></div>
<div>
<b><i><br /></i></b></div>
Here, initialization is done while defining the union. <i style="font-weight: bold;">5</i> is assigned to the member <b><i>x</i></b> of union variable <b><i>obj</i></b>.<br />
<br />
<b><span style="color: #660000;">Method 3:</span></b></div>
<div style="background-color: white; font-family: 'Trebuchet MS', sans-serif; font-size: 15px; line-height: 20px;">
<div>
<b><i>union value {</i></b></div>
<div>
<b><i> int x;</i></b></div>
<div>
<b><i> float y;</i></b></div>
<div>
<b><i>};</i></b></div>
<div>
<b><i>union value obj = {.y = 5.0};</i></b></div>
<div>
<br /></div>
<div>
Here, initialization is done after union definition. <i style="font-weight: bold;">5.0</i> is assigned to the member <b><i>y</i></b> of union variable <b><i>obj.</i></b> And we have used dot(.) operator for initialization.</div>
<div>
<br /></div>
<div>
<b><span style="color: #660000;">Method 4:</span></b></div>
<div>
<div>
<b><i>union value {</i></b></div>
<div>
<b><i> int x;</i></b></div>
<div>
<b><i> float y;</i></b></div>
<div>
<b><i>};</i></b></div>
<div>
<b><i>union value obj = {y:5.0};</i></b></div>
<div>
<br /></div>
<div>
This method is similar to method 3. Here, color(:) is used instead of '=' and period before union member is removed</div>
</div>
<div>
<br />
<br />
<b><span style="color: #660000;">Example C program to illustrate union variable initialization:</span></b><br />
<br /></div>
</div>
</div>
<div dir="ltr" style="text-align: left;" trbidi="on">
<pre class="brush:c++;"> #include <stdio.h>
union value {
int x;
float y;
}obj1 = {5}; //initializing union variable
int main() {
/* declaration and initialization for union variables */
union value obj2 = {6};
union value obj3 = {.y = 7.1};
union value obj4 = {y:8.2};
/* printing the values */
printf("obj1.x : %d\n", obj1.x);
printf("obj2.x : %d\n", obj2.x);
printf("obj3.y : %f\n", obj3.y);
printf("obj4.y : %f\n", obj4.y);
return 0;
}
</pre>
</div>
<br />
<div style="background-color: #999999; color: white; font-family: "Trebuchet MS",sans-serif;">
<b> Output:</b></div>
<div style="background-color: black;">
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> jp@jp-VirtualBox:~/$ ./a.out</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> obj1.x : 5</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> obj2.x : 6</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> obj3.y : 7.100000</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> obj4.y : 8.200000</span><br />
<div style="color: white; font-family: 'Trebuchet MS', sans-serif;">
<br /></div>
</div>
<br /></div>
<ul class="prev_next_button">
<li class="previous"><a href="http://see-programming.blogspot.in/2014/01/declaration-of-union-variable-in-c.html">Previous</a></li>
<li class="next"><a href="http://see-programming.blogspot.in/2014/01/how-to-access-union-members-in-c.html">Next</a></li>
</ul>
<br /></div>
CProgrammerhttp://www.blogger.com/profile/17633481395209852801noreply@blogger.com14tag:blogger.com,1999:blog-3063941734768192839.post-85777700964406291752014-01-11T17:02:00.000+05:302016-03-15T19:32:04.064+05:30Declaration of a union variable in C<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-family: "trebuchet ms" , sans-serif;">Union variable can be declared while defining a union or after defining a union.</span><br />
<div>
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<div>
<span style="font-family: "trebuchet ms" , sans-serif;">Let us see how to declare a union variable while defining a union.</span></div>
<div>
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>union book {</i></b></span></div>
<div>
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i> char name[32];</i></b></span></div>
<div>
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i> int price;</i></b></span></div>
<div>
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>}obj;</i></b></span></div>
<div>
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<div>
<span style="font-family: "trebuchet ms" , sans-serif;">Here, <b><i>obj</i></b> is a union variable of type union <b><i>book</i></b> and it is declared while defining the union <b><i>book</i></b>.</span></div>
<div>
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<div>
<span style="font-family: "trebuchet ms" , sans-serif;">Let us see how to declare a union variable after defining a union.</span></div>
<div>
<div>
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>union book {</i></b></span></div>
<div>
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i> char name[32];</i></b></span></div>
<div>
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i> int price;</i></b></span></div>
<div>
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>};</i></b></span></div>
</div>
<div>
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<div>
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>union book obj1, obj2;</i></b></span></div>
<div>
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<div>
<span style="font-family: "trebuchet ms" , sans-serif;">Here, <b><i>obj1</i></b> and <b><i>obj2</i></b> are union variables of type union <b><i>book</i></b>. And they are declared after defining the union book.</span></div>
<div>
<br />
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b>Union variable declaration example in C</b></span><br />
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b><br /></b></span></div>
</div>
<div dir="ltr" style="text-align: left;" trbidi="on">
<pre class="brush:c++;"> #include <stdio.h>
#include <string.h>
union book {
char name[32];
int price;
}obj1; // union variable declaration at definition
union book obj2; // union variable declaration after definition
int main() {
strcpy(obj1.name, "Davinci code");
printf("obj1.name: %s\n", obj1.name);
strcpy(obj2.name, "Twilight");
printf("obj2.name: %s\n", obj2.name);
return 0;
}
</pre>
</div>
<br />
<div style="background-color: #999999; color: white; font-family: "Trebuchet MS",sans-serif;">
<b> Output:</b></div>
<div style="background-color: black;">
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> jp@jp-VirtualBox:~/$ ./a.out</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> obj1.name: Davinci code</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> obj2.name: Twilight</span><br />
<div style="color: white; font-family: 'Trebuchet MS', sans-serif;">
<br /></div>
</div>
<br /></div>
<br />
<ul class="prev_next_button">
<li class="previous"><a href="http://see-programming.blogspot.in/2012/05/union-declaration.html">Previous</a></li>
<li class="next"><a href="http://see-programming.blogspot.com/2014/01/how-to-initialize-union-varibles-in-c.html">Next</a></li>
</ul>
<br /></div>
CProgrammerhttp://www.blogger.com/profile/17633481395209852801noreply@blogger.com6tag:blogger.com,1999:blog-3063941734768192839.post-28612558948218548132014-01-08T00:55:00.000+05:302016-03-15T20:07:02.453+05:30Array of unions<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-family: "trebuchet ms" , sans-serif;">We can create array of unions similar to creating array of any primitive data type. Below is the general form of declaration for array of union.</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>union <union_name> <array_name>[size];</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;">Consider the following example,</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>union values {</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i><span class="Apple-tab-span" style="white-space: pre;"> </span>int int_val;</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i><span class="Apple-tab-span" style="white-space: pre;"> </span>float float_val;</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>};</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>union values arr[2];</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;">Here, <b><i>arr</i></b> is an array of union which can hold two union elements.</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;"><b><span style="color: #660000;">Array of union initialization:</span></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;">Let us see how to initialize array of union.</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>union values arr[2] = {{1}, {2}};</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;">The above statement initializes first union member of both the array elements.</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>union values arr[2] = {{.float_val = 1.1}, {.float_val = 2.2}};</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;">The above statement initialized second union member of both the array elements.</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;"><b><span style="color: #660000;">How to access union members?</span></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;">Now, let us see how to access union members in the array element.</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i><br /></i></b></span>
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>arr[0].float_val = 2.2;</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;">Dot operator(access operator) is used to access union array element's data member.</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b>Note:</b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;">In union, only one data member will be active at a time.</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;">Size of a union is the size of its biggest data(union) member.</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b>Array of unions example in C</b></span><br />
<br /></div>
<div dir="ltr" style="text-align: left;" trbidi="on">
<pre class="brush:c++;">
#include <stdio.h>
union values {
int integer_val; // union member
float float_val; // union member
};
int main() {
/* array of unions initialization */
union values arr1[2] = {{10}, {20}};
union values arr2[2] = {{.float_val = 10.01},
{.float_val = 20.02}};
/* printing the array contents */
printf("arr1[0].integer_val: %d\n", arr1[0].integer_val);
printf("arr1[1].integer_val: %d\n", arr1[1].integer_val);
printf("arr2[0].float_val: %f\n", arr2[0].float_val);
printf("arr2[1].float_val: %f\n", arr2[1].float_val);
/* printing the size of the union */
printf("Sizeof(union values): %d\n", sizeof(union values));
return 0;
}
</pre>
</div>
<br />
<div style="background-color: #999999; color: white; font-family: "Trebuchet MS",sans-serif;">
<b> Output:</b></div>
<div style="background-color: black;">
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> jp@jp-VirtualBox:~/$ ./a.out</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> arr1[0].integer_val: 10</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> arr1[1].integer_val: 20</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> arr2[0].float_val: 10.010000</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> arr2[1].float_val: 20.020000</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Sizeof(union values): 4</span><br />
<div style="color: white; font-family: 'Trebuchet MS', sans-serif;">
<br /></div>
</div>
<br /></div>
<br />
<ul class="prev_next_button">
<li class="previous"><a href="http://see-programming.blogspot.in/2014/01/how-to-access-union-members-in-c.html">Previous</a></li>
<li class="next"><a href="http://see-programming.blogspot.com/2012/05/sizeof.html">Next</a></li>
</ul>
<br /></div>CProgrammerhttp://www.blogger.com/profile/17633481395209852801noreply@blogger.com3tag:blogger.com,1999:blog-3063941734768192839.post-75595907940059211412014-01-08T00:26:00.001+05:302016-03-15T09:47:45.425+05:30Array of structures<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-family: "trebuchet ms" , sans-serif;">We can create array of structures similar to creating array of any primitive data types.</span><span style="font-family: "trebuchet ms" , sans-serif;"> </span><span style="font-family: "trebuchet ms" , sans-serif;">Below is the general form of declaration for array of structure.</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>struct <structure_name> <array_name>[SIZE];</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;">Consider the following example</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>struct student {</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i><span class="Apple-tab-span" style="white-space: pre;"> </span>char name[32];</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i><span class="Apple-tab-span" style="white-space: pre;"> </span>int age, rollno;</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>};</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>struct student arr[2];</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;">Here, <b><i>arr</i></b> is an array of 2 structure elements.</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;">Let us see how to initialize an array of structures.</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b>Method 1:</b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>struct student arr[2] = { {"Tom", 10, 101}, {"Jerry", 11, 102} };</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b>Method 2:</b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>strcpy(arr[0].name, "Tom");</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>arr[0].age = 10;</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>arr[0].rollno = 101;</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i><br /></i></b></span>
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>strcpy(arr[1].name, "Jerry");</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>arr[1].age = 11;</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>arr[1].rollno = 102;</i></b></span><br />
<div>
<br /></div>
<div>
<div>
<span style="font-family: "trebuchet ms" , sans-serif;">Apart from the above, we are allowed to do partial initialization for structure elements in an array.</span></div>
<div>
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<div>
<span style="font-family: "trebuchet ms" , sans-serif;">Consider the following,</span></div>
<div>
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>struct student arr[2] = {{"Tom", 10, 101}, {"Jerry"}};</i></b></span></div>
<div>
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<div>
<span style="font-family: "trebuchet ms" , sans-serif;">In the above example, we have done partial initialization for second element in the structure array (ie) we have assigned value for the structure member <b><i>name</i></b>, whereas the structure members <b><i>age</i></b> and <b><i>rollno</i></b> are left uninitialized. We can assign values to those uninitialized structure members whenever it is needed.</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<div>
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>arr[1].age = 11;</i></b></span></div>
<div>
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>arr[1].rollno = 101;</i></b></span></div>
<div>
<br /></div>
<div>
<span style="font-family: "trebuchet ms" , sans-serif;">If the array of structure is declared as global variable, then structure members in each structure element are initialized to 0 by default. Similarly, if any structure element is partially initialized(few structure members alone initialized), then the left over structure members are initialized to 0 by default.</span></div>
</div>
<div>
<br /></div>
<div>
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b>Array of structures example in C</b></span></div>
<div>
<br /></div>
</div>
<div dir="ltr" style="text-align: left;" trbidi="on">
<pre class="brush:c++;"> #include <stdio.h>
#include <string.h>
struct student {
char name[32];
int age, rollno;
};
struct student arr1[2]; // global declaration for array of structures
/* prints the contents of the given array */
void printDetails(struct student arr[2]) {
int i;
for (i = 0; i < 2; i++) {
printf("Name: %s\n", arr[i].name);
printf("Age: %d Rollno: %d\n", arr[i].age, arr[i].rollno);
}
printf("\n");
return;
}
int main() {
/* complete initialization */
struct student arr2[2] = {{"Ram", 10, 101}, {"Raj", 11, 102}};
/* partial initialization */
struct student arr3[2] = {{"Jack", 10, 107}, {"Rose"}};
struct student arr4[2];
/* initializing array arr4 */
strcpy(arr4[0].name, "Mike");
arr4[0].age = 10;
arr4[0].rollno = 105;
strcpy(arr4[1].name, "Tyson");
arr4[1].age = 11;
arr4[1].rollno = 106;
/* printing the contents of arr1 */
printf("Contents of arr1:(uninitialized global variable)\n");
printDetails(arr1);
/* printing the contents of arr2 */
printf("Contents of arr2:\n");
printDetails(arr2);
/* printing the contents of partially initialized array arr3 */
printf("Contents of arr3:(arr3[1] - partially initialized)\n");
printDetails(arr3);
arr3[1].age = 10;
arr3[1].rollno = 108;
/* printing the contents of the array after above updation */
printf("printing the contents of arr3 after updation:\n");
printDetails(arr3);
/* printing the contents of arr4 */
printf("Contents of arr4:\n");
printDetails(arr4);
return 0;
}
</pre>
</div>
<br />
<div style="background-color: #999999; color: white; font-family: "Trebuchet MS",sans-serif;">
<b> Output:</b></div>
<div style="background-color: black;">
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> jp@jp-VirtualBox:~/$ ./a.out</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Contents of arr1:(uninitialized global variable)</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Name: </span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Age: 0 Rollno: 0</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Name: </span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Age: 0 Rollno: 0</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Contents of arr2:</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Name: Ram</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Age: 10 Rollno: 101</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Name: Raj</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Age: 11 Rollno: 102</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Contents of arr3:(arr3[1] - partially initialized)</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Name: Jack</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Age: 10 Rollno: 107</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Name: Rose</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Age: 0 Rollno: 0</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> printing the contents of arr3 after updation:</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Name: Jack</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Age: 10 Rollno: 107</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Name: Rose</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Age: 10 Rollno: 108</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Contents of arr4:</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Name: Mike</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Age: 10 Rollno: 105</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Name: Tyson</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Age: 11 Rollno: 106</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<br /></div>
<ul class="prev_next_button">
<li class="previous"><a href="http://see-programming.blogspot.in/2012/05/structure-within-structure.html">Previous</a></li>
<li class="next"><a href="http://see-programming.blogspot.in/2012/05/pointer-to-structure.html">Next</a></li>
</ul>
<br /></div>
CProgrammerhttp://www.blogger.com/profile/17633481395209852801noreply@blogger.com4tag:blogger.com,1999:blog-3063941734768192839.post-74020748103422643132014-01-07T09:59:00.000+05:302016-03-14T23:40:34.648+05:30Array of characters - Strings<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-family: "trebuchet ms" , sans-serif;">String is a sequence of characters terminated by null character. Character array can be used to store strings. Below is the general form of character array declaration.</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>char array_name[size];</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i><br /></i></b></span>
<span style="font-family: "trebuchet ms" , sans-serif;">Consider the following declaration,</span>
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>char str[100];</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;">Here, <b><i>str</i></b> is a character array which has the capacity to store 100 characters or string of length 100 character(99 char + 1 null char).</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;">Let us see how to initialize character array. Basically, character array can be initialized in either of the following ways.</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>char str[6] = {'I', 'N', 'D', 'I', 'A', '\0'};</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>char str[6] = "INDIA";</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>char str[] = {'I', 'N', 'D', 'I', 'A', '\0'};</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>char str[] = "INDIA";</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;">Here, all the above statement gives same meaning. The string "INDIA" is stored inside the array <b><i>str</i></b>.</span><br />
<div>
<br /></div>
<div>
<div>
<span style="font-family: "trebuchet ms" , sans-serif;">When a string is stored in an array in the form of comma delimited characters, then user has to explicitly include null character at the end as shown below.</span></div>
<div>
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>char str[6] = {'I', 'N', 'D', 'I', 'A', '\0'};</i></b></span></div>
<div>
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<div>
<span style="font-family: "trebuchet ms" , sans-serif;">For strings within double codes, null character would be added implicitly.</span></div>
<div>
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>char str[] = "INDIA";</i></b></span></div>
</div>
<div>
<br /></div>
</div>
<div dir="ltr" style="text-align: left;" trbidi="on">
<pre class="brush:c++;"> #include <stdio.h>
int main() {
int i;
char str[6] = "INDIA";
for (i = 0; i < 6; i++) {
printf("character: %c\tASCII: %d\n", str[i], str[i]);
}
return 0;
}
</pre>
</div>
<br />
<div style="background-color: #999999; color: white; font-family: "Trebuchet MS",sans-serif;">
<b> Output:</b></div>
<div style="background-color: black;">
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> jp@jp-VirtualBox:~/$ ./a.out</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> character: I<span class="Apple-tab-span" style="white-space: pre;"> </span>ASCII: 73</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> character: N<span class="Apple-tab-span" style="white-space: pre;"> </span>ASCII: 78</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> character: D<span class="Apple-tab-span" style="white-space: pre;"> </span>ASCII: 68</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> character: I<span class="Apple-tab-span" style="white-space: pre;"> </span>ASCII: 73</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> character: A<span class="Apple-tab-span" style="white-space: pre;"> </span>ASCII: 65</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> character: <span class="Apple-tab-span" style="white-space: pre;"> </span>ASCII: 0</span><br />
<div style="color: white; font-family: 'Trebuchet MS', sans-serif;">
<br /></div>
</div>
<br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><span style="color: #660000;">Note:</span></b> ASCII of null character is 0 and null character won't be displayed on the output screen.</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;">From the above output, we could see that the null character is added implicitly at the end of string literal.</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;"></span>
<span style="font-family: "trebuchet ms" , sans-serif;">String literal can be assigned to character array only during the time of declaration or initialization. Assigning string literal to character array after initialization or declaration would result in below error.</span><br />
<div>
<br /></div>
</div>
<div dir="ltr" style="text-align: left;" trbidi="on">
<pre class="brush:c++;"> #include <stdio.h>
int main() {
char str[100] = "INDIA"; // allowed
// assign string literal after declaration
str = "string"; // not allowed
return 0;
}
</pre>
</div>
<br />
<div style="background-color: #999999; color: white; font-family: "Trebuchet MS",sans-serif;">
<b> Output:</b></div>
<div style="background-color: black;">
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> jp@jp-VirtualBox:~/$ gcc ex71.c </span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> pgm.c: In function ‘main’:</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> pgm.c:5: error: incompatible types when assigning to type ‘char[100]’ from type ‘char *’</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<br />
<span style="font-family: "trebuchet ms" , sans-serif;">But, user is allowed to change the contents of the character array which is initialized to string literal as shown below.</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>char str[] = "INDIA";</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>str[0] = 'E';</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>str[1] = 'L';</i></b></span><br />
<div>
<span style="font-family: "trebuchet ms" , sans-serif;">Here, we are altering the string contents character by character.</span></div>
<div>
<br /></div>
<div>
<div>
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b>What happens when we assign a string literal with length greater than the size of the array?</b></span></div>
<div>
<span style="font-family: "trebuchet ms" , sans-serif;">Consider the following,</span></div>
<div>
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>char str[4] = "INDIA";</i></b></span></div>
<div>
<span style="font-family: "trebuchet ms" , sans-serif;">Here, the size of the array is 4 bytes(1 character = 1 byte). So, <b><i>str</i></b> array can hold 4 characters. But, we have assigned a string literal(6 characters - including null character) with length greater than the original size of the array. During compilation, we won't get any error message. But still, we will end in memory corruption. Because, we are writing data in a memory block which is not allocated for us.</span></div>
</div>
</div>
</div>
</div>
<ul class="prev_next_button">
<li class="previous"><a href="http://see-programming.blogspot.com/2014/01/multidimensional-arrays-in-c.html">Previous</a></li>
<li class="next"><a href="http://see-programming.blogspot.com/2012/05/what-is-structure.html">Next</a></li>
</ul>
<br /></div>
CProgrammerhttp://www.blogger.com/profile/17633481395209852801noreply@blogger.com6tag:blogger.com,1999:blog-3063941734768192839.post-47762456055432157952014-01-07T00:27:00.002+05:302016-03-14T23:24:11.696+05:30Multidimensional arrays in C<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-family: "trebuchet ms" , sans-serif;">Multidimensional array is also known as array of arrays. C allows user to write arrays with one or more dimensions. An array with more than one dimension is a multi-dimensional array. Two dimensional array is a simplest form of multidimensional array. Below is the general form of multidimensional array.</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>data_type array_name[size1][size2][size3]..[sizeN];</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;">Consider the following example</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>int arr[2][2][2] = </i>{ { {1, 1}, {1, 1} },</b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b> { {1, 1}, {1, 1} }</b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b> }<i>;</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><span class="Apple-tab-span" style="white-space: pre;"><b><i> </i></b> </span> </span><br />
<span style="font-family: "trebuchet ms" , sans-serif;">The above statement shows the declaration and initialization of multidimensional array(3d array). Total number of elements in an array can be obtained by multiplying the size value in each dimension.</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>int arr[2][2][2];</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;">2 X 2 X 2 = 8 elements in the array <b><i>arr</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;">And multidimensional array can be accessed using its array indices.</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>arr[0][0][1] = 2;</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;">The above statement assigns 2 to arr[0][0][1]. Then the resultant value of the array arr would be the following.</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b>{ </b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b> { {1, 2}, {1, 1} },</b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b> { {1, 1}, {1, 1} }</b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b>};</b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span><br />
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b>Example C program using multidimensional arrays:</b></span></div>
<div dir="ltr" style="text-align: left;" trbidi="on">
<pre class="brush:c++;">#include <stdio.h>
int main() {
int i, j, k, three_dim[2][2][2] = { // array initialization
{{1, 1}, {1, 1}},
{{1, 1}, {1, 1}}
};
// printing elements of the array
printf("Elements of the array three_dim:\n");
for (i = 0; i < 2; i++)
for (j = 0; j < 2; j++)
for (k = 0; k < 2; k++)
printf("three_dim[%d][%d][%d]: %d\n",
i, j, k, three_dim[i][j][k]);
printf("\nAssign 2 to three_dim[0][0][1]\n");
three_dim[0][0][1] = 2; // modifying an array element
// printing elements of the array
printf("\nElements of the array after modification:\n");
for (i = 0; i < 2; i++)
for (j = 0; j < 2; j++)
for (k = 0; k < 2; k++)
printf("three_dim[%d][%d][%d]: %d\n",
i, j, k, three_dim[i][j][k]);
return 0;
}
</pre>
</div>
<br />
<div style="background-color: #999999; color: white; font-family: "Trebuchet MS",sans-serif;">
<b> Output:</b></div>
<div style="background-color: black;">
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> jp@jp-VirtualBox:~/$ ./a.out</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Elements of the array three_dim:</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> three_dim[0][0][0]: 1</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> three_dim[0][0][1]: 1</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> three_dim[0][1][0]: 1</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> three_dim[0][1][1]: 1</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> three_dim[1][0][0]: 1</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> three_dim[1][0][1]: 1</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> three_dim[1][1][0]: 1</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> three_dim[1][1][1]: 1</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Assign 2 to three_dim[0][0][1]</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Elements of the array after modification:</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> three_dim[0][0][0]: 1</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> three_dim[0][0][1]: 2</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> three_dim[0][1][0]: 1</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> three_dim[0][1][1]: 1</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> three_dim[1][0][0]: 1</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> three_dim[1][0][1]: 1</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> three_dim[1][1][0]: 1</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> three_dim[1][1][1]: 1</span><br />
<div style="color: white; font-family: 'Trebuchet MS', sans-serif;">
<br /></div>
</div>
<br /></div>
<ul class="prev_next_button">
<li class="previous"><a href="http://see-programming.blogspot.in/2012/05/array-classifications.html">Previous</a></li>
<li class="next"><a href="http://see-programming.blogspot.com/2014/01/array-of-characters-strings.html">Next</a></li>
</ul>
<br /></div>
CProgrammerhttp://www.blogger.com/profile/17633481395209852801noreply@blogger.com4tag:blogger.com,1999:blog-3063941734768192839.post-87850930961769809622014-01-02T23:17:00.002+05:302016-03-16T09:41:43.549+05:30Formatted input and output functions in C<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div>
<span style="font-family: "trebuchet ms" , sans-serif;">The following are built-in functions available in C language which can be used to perform formatted input output operations.</span></div>
<div>
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<div>
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b>Formatted input functions in C:</b></span></div>
<div>
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b>int fscanf(FILE *stream, const char *format, ...)</b></span></div>
<div>
<span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;">Performs input format conversion. It reads the input from the stream and does format conversion. Then, it will assign those converted values to the subsequent arguments of <b><i>format</i></b> correspondingly. And it returns the number of inputs matched and got assigned with values successfully.</span></div>
<div>
<span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;"><br /></span></div>
<div>
<span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;"><b><span style="color: #660000;">int scanf(const char *format, ...)</span></b></span></div>
<div>
<span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;">scanf() is equivalent to fscanf(stdin, ...)</span></div>
<div>
<span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;"><br /></span></div>
<div>
<span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;"><b><span style="color: #660000;">int sscanf(char *str, const char *format)</span></b></span></div>
<div>
<span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;">sscanf() is identical to sscanf() except that the inputs are taken from the string <b><i>str</i></b>.</span><br />
<span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;"><br /></span>
<span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;"><br /></span>
<span style="background-color: white; color: #660000; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;"><b>scanf fscanf sscanf example in C</b></span></div>
<div>
<br /></div>
</div>
<div dir="ltr" style="text-align: left;" trbidi="on">
<pre class="brush:c++;"> #include <stdio.h>
int main() {
FILE *fp;
int i, age[3], rollno[3];
char name[3][32], str[128] = "13:103:Ram";
fp = fopen("input.txt", "r");
/* input data for student 1 from user */
printf("Enter age rollno and name for student 1:\n");
scanf("%d%d%s", &age[0], &rollno[0], name[0]);
printf("Reading age, rollno and name from file(input.txt)..\n");
/* input data for student 2 from file */
fscanf(fp, "%d%d%s", &age[1], &rollno[1], name[1]);
printf("Reading age, rollno and name from string..\n");
/* input data for student 3 from string */
sscanf(str, "%d:%d:%s", &age[2], &rollno[2], name[2]);
printf(" Age Rollno name\n");
/* print the result */
for (i = 0; i < 3; i++) {
printf("Student %d: %d %d %s\n",
i, age[i], rollno[i], name[i]);
}
fclose(fp);
return 0;
}
</pre>
</div>
<br />
<div style="background-color: #999999; color: white; font-family: "Trebuchet MS",sans-serif;">
<b> Output:</b></div>
<div style="background-color: black;">
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> jp@jp-VirtualBox:~/$ cat input.txt </span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> 10 102 Raj</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> jp@jp-VirtualBox:~/$ ./a.out</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Enter age rollno and name for student 1:</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> 11 101 Tom</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Reading age, rollno and name from file(input.txt)..</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Reading age, rollno and name from string..</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Age Rollno name</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Student 0: 11 101 Tom</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Student 1: 10 102 Raj</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Student 2: 13 103 Ram</span><br />
<div style="color: white; font-family: 'Trebuchet MS', sans-serif;">
<br /></div>
</div>
<br />
<br />
<br />
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b>Formatted output functions in C:</b></span><br />
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b>int fprintf(FILE *stream, const char *format, . . .)</b></span><br />
<span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;">Performs formatted output conversion and writes the output to <b><i>stream</i></b>. Return value is the number of characters printed (excludes '\0').</span><br />
<span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;"><br /></span>
<span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;"><b><span style="color: #660000;">int printf(const char *format, . . .)</span></b></span><br />
<span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;">printf() is identical to fprintf(stdout, . . .)</span><br />
<span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;"><br /></span>
<span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;"><b><span style="color: #660000;">int sprintf(char *str, const char *format, . . .)</span></b></span><br />
<span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;">sprintf() is similar to printf() except that the output is written into the string str.</span><br />
<span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;"><br /></span>
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 15px; line-height: 20px;"><b>int vfprintf(FILE *stream, const char *format, va_list arg)</b></span></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 15px; line-height: 20px;">vfprintf() is equivalent to fprintf() except that the variable argument is replaced by arg.</span></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 15px; line-height: 20px;"><br /></span></span>
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 15px; line-height: 20px;"><b>int vprintf(const char *format, va_list arg)</b></span></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 15px; line-height: 20px;">vprintf() is equivalent to printf() except that the variable argument is replaced by arg.</span></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 15px; line-height: 20px;"><br /></span></span>
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 15px; line-height: 20px;"><b>int vsprintf(char *str, const char *format, va_list arg)</b></span></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 15px; line-height: 20px;">vsprintf() is equivalent to sprintf() except that the variable argument is replaced by arg.</span></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 15px; line-height: 20px;"><br /></span></span>
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 15px; line-height: 20px;"><br /></span></span><span style="font-family: "trebuchet ms" , sans-serif;"><span style="color: #660000; font-size: 15px; line-height: 20px;"><b>printf fprintf sprintf example in C</b></span></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="color: #660000; font-size: 15px; line-height: 20px;"><b><br /></b></span></span></div>
<div dir="ltr" style="text-align: left;" trbidi="on">
<pre class="brush:c++;"> #include <stdio.h>
int main() {
FILE *fp;
int rollno = 101, age = 10;
char name[32] = "Ram", str[128];
fp = fopen("input.txt", "w");
/* printing rollno, age and name in stdout */
printf("Writing output to stdout:\n");
printf("Roll no: %d\n", rollno);
printf("age : %d\n", age);
printf("Name : %s\n", name);
/* printing rollno, age and name in a file */
printf("\nWriting rollno, age, name to file(input.txt)\n");
fprintf(fp, "Roll no: %d\nage : %d\nName : %s\n",
rollno, age, name);
/* printing rollno, age and name in a string */
printf("Writing rollno, age, name to string(str)\n");
sprintf(str, "Roll no: %d\nage : %d\nName : %s\n",
rollno, age, name);
printf("\nContents in string str:\n%s\n", str);
fclose(fp);
return 0;
}
</pre>
</div>
<br />
<div style="background-color: #999999; color: white; font-family: "Trebuchet MS",sans-serif;">
<b> Output:</b></div>
<div style="background-color: black;">
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> jp@jp-VirtualBox:~/$ ./a.out</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Writing output to stdout:</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Roll no: 101</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> age : 10</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Name : Ram</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Writing rollno, age, name to file(input.txt)</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Writing rollno, age, name to string(str)</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Contents in string str:</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Roll no: 101</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> age : 10</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Name : Ram</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> jp@jp-VirtualBox:~/$ cat input.txt </span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Roll no: 101</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> age : 10</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"></span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Name : Ram</span><br />
<div>
<br /></div>
</div>
<br />
<br />
<br />
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b>vprintf vfprintf vsprintf example in C</b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<div dir="ltr" style="text-align: left;" trbidi="on">
<pre class="brush:c++;"> #include <stdio.h>
#include <stdarg.h>
#include<stdarg .h="">
void vformat(char *format, ...) {
FILE *fp;
char str[128];
va_list arg;
/* intializing variable argument list */
va_start(arg, format);
/* writing output data to stdout using vprintf */
printf("Writing output to stdout using vprintf\n");
vprintf(format, arg);
/* writing data to stdout using vfprintf */
printf("\nWriting output to stdout using vfprintf\n");
vfprintf(stdout, format, arg);
/* writing data to string str using vsprintf */
printf("\nWriting output to string using vsprintf\n");
vsprintf(str, format, arg);
printf("Printing the contents in string str\n");
printf("%s", str);
va_end(arg);
}
int main() {
int age = 10, rollno = 101;
char name[32] = "Ram";
vformat("Age: %d\nRollno: %d\nName: %s\n",
age, rollno, name);
return 0;
}
</stdarg></pre>
</div>
<br />
<div style="background-color: #999999; color: white; font-family: "Trebuchet MS",sans-serif;">
<b> Output:</b></div>
<div style="background-color: black;">
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> jp@jp-VirtualBox:~/$ ./a.out</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Writing output to stdout using vprintf</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Age: 10</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Rollno: 101</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Name: Ram</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Writing output to stdout using vfprintf</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Age: 10</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Rollno: 101</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Name: Ram</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Writing output to string using vsprintf</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Printing the contents in string str</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Age: 10</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Rollno: 101</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Name: Ram</span><br />
<div style="color: white; font-family: 'Trebuchet MS', sans-serif;">
<br /></div>
</div>
<br /></div>
</div>
</div>
</div>
<br />
<ul class="prev_next_button">
<li class="previous"><a href="http://see-programming.blogspot.com/2014/01/character-inputoutput-functions-in-c.html">Previous</a></li>
<li class="next"><a href="http://see-programming.blogspot.in/2012/05/open-creat-close-and-lseek.html">Next</a></li>
</ul>
<br /></div>
CProgrammerhttp://www.blogger.com/profile/17633481395209852801noreply@blogger.com2tag:blogger.com,1999:blog-3063941734768192839.post-10391872775287916252014-01-02T20:52:00.002+05:302016-03-16T00:11:58.394+05:30Character Input and Output functions in C<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-family: "trebuchet ms" , sans-serif;">The following are the built in functions available in C which can be used to to perform character input & output operations.</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b>int fgetc(FILE *stream)</b></span><br />
<span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;">It reads the next character from the stream and returns the integer(ascii) value of the character read.</span><br />
<span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;"><br /></span>
<span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;"><b><span style="color: #660000;">int fputc(int c, FILE *stream)</span></b></span><br />
<span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;">It writes the character <b><i>c</i></b> on the given stream. Returns the written character on success. Otherwise, EOF is returned.</span><br />
<b style="font-family: 'Trebuchet MS', sans-serif; font-size: 15px; line-height: 20px;"><span style="color: #660000;"><br /></span></b>
<b style="font-family: 'Trebuchet MS', sans-serif; font-size: 15px; line-height: 20px;"><span style="color: #660000;">fgetc and fputc example in C</span></b><br />
<span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;"><br /></span></div>
<div dir="ltr" style="text-align: left;" trbidi="on">
<pre class="brush:c++;"> #include <stdio.h>
int main() {
int ch;
/* prints the input data on the output screen */
while ((ch = fgetc(stdin)) != EOF) { //ctrl + D to exit
fputc(ch, stdout);
}
return 0;
}
</pre>
</div>
<br />
<div style="background-color: #999999; color: white; font-family: "Trebuchet MS",sans-serif;">
<b> Output:</b></div>
<div style="background-color: black;">
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> jp@jp-VirtualBox:~/$ ./a.out</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> hello world</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> hello world</span><br />
<div style="color: white; font-family: 'Trebuchet MS', sans-serif;">
<br /></div>
</div>
<br />
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b><br /></b></span>
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b>int getc(FILE *stream);</b></span><br />
<span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;">It reads the next character from the given stream. It evaluates the given stream more than one time, if it is a macro. Returns integer value(ascii) of the character read.</span><br />
<span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;"><br /></span>
<span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;"><b><span style="color: #660000;">int putc(int c, FILE *stream);</span></b></span><br />
<span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;">It writes the character c into the given stream. It evaluates the given stream more than once, if it is a macro. Returns the integer value(ascii) of the written character on success, EOF on failure.</span><br />
<span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;"><br /></span>
<span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;"><b><span style="color: #660000;">getc and putc example in C</span></b></span><br />
<span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;"><br /></span>
<br />
<div dir="ltr" trbidi="on">
<pre class="brush:c++;"> #include <stdio.h>
int main() {
int ch;
/* prints the input data on the output screen */
while ((ch = getc(stdin)) != EOF) { //ctrl + D to exit
putc(ch, stdout);
}
return 0;
}
</pre>
</div>
<br />
<div style="background-color: #999999; color: white; font-family: 'Trebuchet MS', sans-serif;">
<b> Output:</b></div>
<div style="background-color: black;">
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> jp@jp-VirtualBox:~/$ ./a.out</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> hello world</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> hello world</span><br />
<div style="color: white; font-family: 'Trebuchet MS', sans-serif;">
<br /></div>
</div>
<span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;"><br /></span>
<span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;"><br /></span>
<span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;"><b><span style="color: #660000;">char *fgets(char *str, int n, FILE *stream)</span></b></span><br />
<span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;">It reads n-1 character from the input stream, stops reading if a newline character is encountered; newline + '\0' is included at the end of input string. String <b><i>str</i></b> is returned as the return value on success. Otherwise, NULL is returned.</span><br />
<span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;"><br /></span>
<br />
<div style="background-color: white; font-family: 'Trebuchet MS', sans-serif; font-size: 15px; line-height: 20px;">
<b><span style="color: #660000;">int fputs(const char *str, FILE *stream)</span></b></div>
<div>
<span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;">It writes the string <b><i>str</i></b> on <b><i>stream</i></b>. Returns the non-negative integer on success, EOF on failure.</span><br />
<span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;"><br /></span>
<span style="background-color: white; color: #660000; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;"><b>fgets and fputs example in C</b></span></div>
<div>
<br /></div>
</div>
<div dir="ltr" style="text-align: left;" trbidi="on">
<pre class="brush:c++;"> #include <stdio.h>
int main() {
char str[256];
printf("Enter your input string:");
fgets(str, 256, stdin); // gets input from standard i/p
printf("Below is your input string:\n");
fputs(str, stdout); // prints str in standard o/p screen
return 0;
}
</pre>
</div>
<br />
<div style="background-color: #999999; color: white; font-family: "Trebuchet MS",sans-serif;">
<b> Output:</b></div>
<div style="background-color: black;">
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> jp@jp-VirtualBox:~/$ ./a.out</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Enter your input string:India is my country</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Below is your input string:</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> India is my country</span><br />
<div style="color: white; font-family: 'Trebuchet MS', sans-serif;">
<br /></div>
</div>
<br />
<br />
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b>int getchar(void)</b></span><br />
<span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;">Inputs a character. Returns the integer value(ascii) of the character read.</span><br />
<span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;"><br /></span>
<span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;"><b><span style="color: #660000;">int putchar(int c)</span></b></span><br />
<span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;">Write character c on standard output.</span><br />
<br />
<span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;"><b><span style="color: #660000;">getchar and putchar example in C</span></b></span><br />
<span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;"><br /></span></div>
<div dir="ltr" style="text-align: left;" trbidi="on">
<pre class="brush:c++;"> #include <stdio.h>
int main() {
int ch;
/* prints the input data on the output screen */
while ((ch = getchar()) != EOF) {
putchar(ch);
}
return 0;
}
</pre>
</div>
<br />
<div style="background-color: #999999; color: white; font-family: "Trebuchet MS",sans-serif;">
<b> Output:</b></div>
<div style="background-color: black;">
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> jp@jp-VirtualBox:~/$ ./a.out</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> hello world</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> hello world</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<br />
<br />
<span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;"><b><span style="color: #660000;">char *gets(char *str)</span></b></span><br />
<span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;">Gets the given input string into the character array str. Returns str on success, EOF or NULL is returned on failure.</span><br />
<span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;"><br /></span>
<span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;"><b><span style="color: #660000;">int puts(const char *str)</span></b></span><br />
<span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;">It writes the given string and a newline to stdout. Returns non-negative integer on success, EOF is returned on failure.</span><br />
<span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;"><br /></span>
<span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;"><b><span style="color: #660000;">gets and puts example in C</span></b></span><br />
<span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;"><br /></span></div>
<div dir="ltr" style="text-align: left;" trbidi="on">
<pre class="brush:c++;"> #include <stdio.h>
int main() {
char str[256];
printf("Enter your input string:");
gets(str); // gets input from standard i/p
printf("Below is your input string:\n");
puts(str); // prints str in standard o/p screen
return 0;
}
</pre>
</div>
<br />
<div style="background-color: #999999; color: white; font-family: "Trebuchet MS",sans-serif;">
<b> Output:</b></div>
<div style="background-color: black;">
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> jp@jp-VirtualBox:~/$ ./a.out</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Enter your input string:Hello world</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Below is your input string:</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Hello world</span><br />
<div style="color: white; font-family: 'Trebuchet MS', sans-serif;">
<br /></div>
</div>
<br />
<br />
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b>int ungetc(int c, FILE *stream)</b></span><br />
<span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;">It pushes character c back onto the given stream. Returns character c on success, EOF on failure.</span><br />
<span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;"><br /></span>
<span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;"><b><span style="color: #660000;">ungetc example in C</span></b></span><br />
<span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 15px; line-height: 20px;"><br /></span></div>
<div dir="ltr" style="text-align: left;" trbidi="on">
<pre class="brush:c++;"> #include <stdio.h>
int main() {
char ch;
while ((ch = getchar()) != EOF) { // ctrl + d is EOF
if (ch == '\n') {
/*
* pushes # to stdin.. So, getchar()
* would read the character '#' on its
* subsequent call
*/
ungetc('#', stdin);
continue;
}
putchar(ch);
}
return 0;
}
</pre>
</div>
<br />
<div style="background-color: #999999; color: white; font-family: "Trebuchet MS",sans-serif;">
<b> Output:</b></div>
<div style="background-color: black;">
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> jp@jp-VirtualBox:~/$ ./a.out</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> india </span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> india#</span><br />
<div style="color: white; font-family: 'Trebuchet MS', sans-serif;">
<br /></div>
</div>
<br />
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b>Note:</b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;">fgetc, fputc, fgets, fputs, getc and putc are the built-in functions used to perform character input output operation on files.</span></div>
</div>
</div>
</div>
</div>
</div>
<br />
<ul class="prev_next_button">
<li class="previous"><a href="http://see-programming.blogspot.in/2012/05/file-opening-modes.html">Previous</a></li>
<li class="next"><a href="http://see-programming.blogspot.com/2014/01/formatted-inputoutput-functions-in-c.html">Next</a></li>
</ul>
<br /></div>
CProgrammerhttp://www.blogger.com/profile/17633481395209852801noreply@blogger.com2tag:blogger.com,1999:blog-3063941734768192839.post-80528529839592955812013-12-31T19:24:00.001+05:302016-03-13T20:44:56.073+05:30Nested function in C<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b>What is nested function?</b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;">If a function is defined inside another function, then it is called as nested function. </span><br />
<br />
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b>Nested function example in C:</b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;">Consider the following example,</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>int add(int a, int b) {</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i> void print(int res) {</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i><span class="Apple-tab-span" style="white-space: pre;"> </span> printf("Result is %d, res);</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i><span class="Apple-tab-span" style="white-space: pre;"> </span> }</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i><span class="Apple-tab-span" style="white-space: pre;"> </span> print(a+b);</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i><span class="Apple-tab-span" style="white-space: pre;"> </span> return 0;</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>}</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;">Here, <b><i>print()</i></b> is a nested function. Because, it is defined inside another function named <b><i>add()</i></b>.</span><br />
<div>
<br /></div>
<div>
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b>Example C program using nested functions:</b></span></div>
</div>
<div dir="ltr" style="text-align: left;" trbidi="on">
<pre class="brush:c++;">
#include <stdio.h>
void add(int a, int b) {
void print(int res) { // nested function
printf("Result is %d\n", res);
return;
}
print(a + b); // passing sum of a and b as parameter
return;
}
int main() {
add(10, 20);
return 0;
}
</pre>
</div>
<br />
<div style="background-color: #999999; color: white; font-family: "Trebuchet MS",sans-serif;">
<b> Output:</b></div>
<div style="background-color: black;">
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> jp@jp-VirtualBox:~/$ ./a.out</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Result is 30</span><br />
<div style="color: white; font-family: 'Trebuchet MS', sans-serif;">
<br /></div>
</div>
<br /></div>
</div>
<ul class="prev_next_button">
<li class="previous"><a href="http://see-programming.blogspot.in/2013/12/static-function-in-c.html">Previous</a></li>
<li class="next"><a href="http://see-programming.blogspot.in/2012/05/void-function.html">Next</a></li>
</ul>
<br /></div>
CProgrammerhttp://www.blogger.com/profile/17633481395209852801noreply@blogger.com2tag:blogger.com,1999:blog-3063941734768192839.post-53535038563033355872013-12-31T19:05:00.000+05:302016-03-13T20:41:05.455+05:30Static function in c<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b>What is static function in C?</b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;">Static function is nothing but a function that is callable only by other functions in the same file where the static function is defined.</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<b><span style="color: #660000;"><span style="font-family: "trebuchet ms" , sans-serif;">Static function example</span>:</span></b><br />
<span style="font-family: "trebuchet ms" , sans-serif;">Consider the following example,</span></div>
<div dir="ltr" style="text-align: left;" trbidi="on">
<pre class="brush:c++;">
// static.c is the file name
static int add(int a, int b) {
return (a + b);
}
</pre>
</div>
<br />
<span style="font-family: "trebuchet ms" , sans-serif;">Here, we have defined a static function named add() in static.c. Below is main.c file which has main function calling another function named add().</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<div dir="ltr" style="text-align: left;" trbidi="on">
<pre class="brush:c++;">
// main.c
#include <stdio.h>
int main() {
int res;
res = add(10, 20); // main function makes call to static function
printf("Result: %d\n", res);
return 0;
}
</pre>
</div>
<br />
<br />
<span style="font-family: "trebuchet ms" , sans-serif;">Let us try to compile both together to get a single executable.</span></div>
<div style="background-color: #999999; color: white; font-family: "Trebuchet MS",sans-serif;">
<b> Output:</b></div>
<div style="background-color: black;">
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> jp@jp-VirtualBox:~/$ gcc static.c main.c </span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> /tmp/ccSWRV5a.o: In function `main':</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> ex45.c:(.text+0x19): undefined reference to `add'</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> collect2: ld returned 1 exit status</span><br />
<div style="color: white; font-family: 'Trebuchet MS', sans-serif;">
<br /></div>
</div>
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;">From main function, we are calling a static function named </span><b style="font-family: 'Trebuchet MS', sans-serif;"><i>add()</i></b><span style="font-family: "trebuchet ms" , sans-serif;"> in another another file. This results to the above error. Basically, static functions are not allowed to be called from other files. </span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;">Let us see what happens if we make static function as non-static.</span><br />
<div dir="ltr" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<pre class="brush:c++;">
// static.c is the file name
int add(int a, int b) {
return (a + b);
}
</pre>
</div>
<br /></div>
<div dir="ltr" style="text-align: left;" trbidi="on">
<pre class="brush:c++;">
// main.c
#include <stdio.h>
int main() {
int res;
res = add(10, 20); // main function makes call to static function
printf("Result: %d\n", res);
return 0;
}
</pre>
</div>
<br />
<div style="background-color: #999999; color: white; font-family: 'Trebuchet MS', sans-serif;">
<b> Output:</b></div>
<div style="background-color: black;">
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> jp@jp-VirtualBox:~/$ gcc static.c main.c </span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> jp@jp-VirtualBox:~/$ ./a.out</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Result: 30</span><br />
<div>
<br /></div>
</div>
</div>
</div>
<br />
<ul class="prev_next_button">
<li class="previous"><a href="http://see-programming.blogspot.in/2012/05/recursion.html">Previous</a></li>
<li class="next"><a href="http://see-programming.blogspot.in/2013/12/nested-function-in-c.html">Next</a></li>
</ul>
<br /></div>
CProgrammerhttp://www.blogger.com/profile/17633481395209852801noreply@blogger.com2tag:blogger.com,1999:blog-3063941734768192839.post-72458203822718061212013-12-31T15:52:00.001+05:302016-03-13T20:23:33.545+05:30main function in C<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>main()</i></b> function is mandatory for any C program. Because, the program execution starts from <b><i>main()</i></b>. Default return type for main function is an integer. Return value from main function could help us to find the exit status of a C program.</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;">Consider the following example,</span></div>
<div dir="ltr" style="text-align: left;" trbidi="on">
<pre class="brush:c++;">
#include <stdio.h>
int main() {
int val;
printf("Enter an integer: ");
scanf("%d", &val);
if (val < 0) {
printf("%d is negative\n", val);
return (-1);
} else {
printf("%d is positive\n", val);
return (val);
}
}
</pre>
</div>
<br />
<div style="background-color: #999999; color: white; font-family: "Trebuchet MS",sans-serif;">
<b> Output:</b></div>
<div style="background-color: black;">
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> jp@jp-VirtualBox:~/$ ./a.out</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Enter an integer: -1</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> -1 is negative</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> jp@jp-VirtualBox:~/$ echo $?</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> 255 // exit status of the program</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> jp@jp-VirtualBox:~/$ ./a.out</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Enter an integer: 50</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> 50 is positive</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> jp@jp-VirtualBox:~/$ echo $?</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> 50 // exit status of the program</span><br />
<div style="color: white; font-family: 'Trebuchet MS', sans-serif;">
<br /></div>
</div>
<br />
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b>How to get exit status of a command?</b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>"echo $?"</i></b> provides exit status of any program. Here, the exit codes of our program are the return values from main function. Exit codes are from 0 to 255(unsigned char). So, shell would return 255 for the exit code -1(wraparound occurs).</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b>What is command line argument?</b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;">Basically, command is an executable used to perform a specific task(eg. ls command). Any arguments supplied to an executable(command) are called command line arguments.</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b>Example:</b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;">ls -a => lists all files in a directory(including hidden files)</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;">Here,</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;">ls - command</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;">-a - command line argument</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;"><b><span style="color: #660000;">main function with command line arguments:</span></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;">main function can be written with no arguments or with command line arguments.</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;">Consider the following example,</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>int main(int argc, char *argv[]) {</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i> : </i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i> return 0;</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>}</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;">Here, we have written main function with command line arguments.</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>argc</i></b> and <b><i>argv</i></b> are command line arguments.</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;">argc - number of command line arguments</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;">*argv[] - array of character pointers</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b>How to pass command line arguments in C?</b></span><br />
<div>
<span style="font-family: "trebuchet ms" , sans-serif;">While executing program in the shell, we can supply arguments along with executable name.</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;"><b><span style="color: #660000;">Example:</span></b> <b><i>./a.out arg1 arg2 arg3</i></b></span></div>
<div>
<span style="font-family: "trebuchet ms" , sans-serif;">./a.out - Executable</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;">arg1, arg2, arg3 - command line arguments</span><br />
<br /></div>
<div>
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b>Example C program to illustrate command line arguments:</b></span></div>
</div>
<div dir="ltr" style="text-align: left;" trbidi="on">
<pre class="brush:c++;">
#include <stdio.h>
int main(int argc, char *argv[]) {
int i;
for (i = 0; i < argc; i++) {
printf("argv[%d]: %s\n", i, argv[i]); // prints command line arguments
}
return 0;
}
</pre>
</div>
<br />
<div style="background-color: #999999; color: white; font-family: "Trebuchet MS",sans-serif;">
<b> Output:</b></div>
<div style="background-color: black;">
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> jp@jp-VirtualBox:~/$ ./a.out india is my country</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> argv[0]: ./a.out</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> argv[1]: india</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> argv[2]: is</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> argv[3]: my</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> argv[4]: country</span><br />
<div style="color: white; font-family: 'Trebuchet MS', sans-serif;">
<br /></div>
</div>
<br /></div>
</div>
</div>
<ul class="prev_next_button">
<li class="previous"><a href="http://see-programming.blogspot.in/2013/12/how-to-call-functions-using-function.html">Previous</a></li>
<li class="next"><a href="http://see-programming.blogspot.in/2012/05/recursion.html">Next</a></li>
</ul>
<br /></div>
CProgrammerhttp://www.blogger.com/profile/17633481395209852801noreply@blogger.com2tag:blogger.com,1999:blog-3063941734768192839.post-7663890119377356022013-12-31T14:16:00.001+05:302016-03-13T20:14:53.616+05:30How to call functions using function pointers?<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="background-color: white; color: #660000; font-family: "trebuchet ms" , sans-serif; font-size: medium;"><b>What is function pointer?</b></span><br />
<span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 14.85px; line-height: 20.79px;">Every function has an address. We can assign the address of functions to pointers. Then those pointers are called pointers to functions. Pointers to function is also called as function pointers</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;">Let us see how to call functions using function pointer. Below is an example function pointer</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>int (*func)(int, int);</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;">Here, func is a pointer to a function that takes two integer arguments and returns an integer</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;">Consider the following example,</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>int add(int a, int b) {</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i> return (a + b);</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>}</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i><br /></i></b></span>
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>int main() {</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i> int (*func) (int, int);</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i> func = add; //assigning address of function add()</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i> (*func)(10, 20); // calls the add function</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i> return 0;</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>}</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;">Here,</span><span style="font-family: "trebuchet ms" , sans-serif;"><br /></span><span style="font-family: "trebuchet ms" , sans-serif;"><b style="font-style: italic;">"func = add"</b> is equivalent to <b><i>"func = &add"</i></b>(& is optional)</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><i><b>"(*func)(10, 20)" </b></i>is equivalent to <b><i>"func(10, 20)"</i></b> (* is optional)</span><br />
<div>
<br /></div>
<div>
<span style="font-family: "trebuchet ms" , sans-serif;">Let us write an example C program that calls a function using function pointer.</span></div>
<div>
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
</div>
<div dir="ltr" style="text-align: left;" trbidi="on">
<pre class="brush:c++;">
#include <stdio.h>
int add(int a, int b) {
return (a + b);
}
int main() {
int res;
int (*func)(int, int);
func = &add; // assign address of the function add()
res = func(10, 20); // calling add()
printf("Sum of 10 and 20 is %d\n", res);
return 0;
}
</pre>
</div>
<br />
<div style="background-color: #999999; color: white; font-family: "Trebuchet MS",sans-serif;">
<b> Output:</b></div>
<div style="background-color: black;">
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> jp@jp-VirtualBox:~/$ ./a.out</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Sum of 10 and 20 is 30</span><br />
<div style="color: white; font-family: 'Trebuchet MS', sans-serif;">
<br /></div>
</div>
<br /></div>
</div>
<ul class="prev_next_button">
<li class="previous"><a href="http://see-programming.blogspot.com/2013/12/variable-arguments-in-c.html">Previous</a></li>
<li class="next"><a href="http://see-programming.blogspot.in/2013/12/main-function-in-c.html">Next</a></li>
</ul>
<br /></div>CProgrammerhttp://www.blogger.com/profile/17633481395209852801noreply@blogger.com2tag:blogger.com,1999:blog-3063941734768192839.post-47318040123970623042013-12-31T13:42:00.000+05:302016-03-13T20:06:04.076+05:30Variable arguments in C<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-family: "trebuchet ms" , sans-serif;">In C language, its possible to write functions with variable arguments. A function that takes variable number of arguments is also called as variadic function. Below is an example prototype for a variadic function.</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>int func_name(int num, ...);</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;">func_name - name of the function</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;">num - number of arguments in argument list</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;">...(ellipsis) - used to denote variable arguments</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;">Below are the list of macros availabe in 'stdarg.h' used to retrieve variable arguments.</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b>void va_start (va_list arg, last_arg)</b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;">It initializes argument pointer <b><i>arg</i></b> to point to first optional argument and <b><i>last_arg</i></b> is the last named argument to a function.</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;"><b><span style="color: #660000;">type va_arg (va list arg, type)</span> </b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;">Returns subsequent optional argument and <b><i>type</i></b> is data type of the actual argument</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b>void va_end (va list arg)</b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;">Ends the use of variable arguments.</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b>Example C program on variable length argument list</b></span></div>
<div dir="ltr" style="text-align: left;" trbidi="on">
<pre class="brush:c++;">
#include <stdio.h>
#include <stdarg.h>
int sum(int, ...); // function prototype
int sum(int num, ...) {
int i, val, total = 0;
va_list arg;
/* initializes arg pointer to point to 1st optional arg */
va_start(arg, num); // num - no of arguments in parameter list
for (i = 0; i < 5; i++) {
val = va_arg(arg, int); // get subsequent argument
printf("Argument %d: %d\n", i + 1, val);
total = total + val;
}
va_end(arg); // end the use of variable argument
return (total);
}
int main() {
int res;
/* first argument to sum denotes the no of arguments in parameter list */
res = sum(5, 10, 20, 30, 40, 50);
printf("Resultant sum value is %d\n", res);
return 0;
}
</pre>
</div>
<br />
<div style="background-color: #999999; color: white; font-family: "Trebuchet MS",sans-serif;">
<b> Output:</b></div>
<div style="background-color: black;">
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> jp@jp-VirtualBox:~/$ ./a.out</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Argument 1: 10</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Argument 2: 20</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Argument 3: 30</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Argument 4: 40</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Argument 5: 50</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Resultant sum value is 150</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<br /></div>
<ul class="prev_next_button">
<li class="previous"><a href="http://see-programming.blogspot.com/2013/12/call-by-value-vs-call-by-reference.html">Previous</a></li>
<li class="next"><a href="http://see-programming.blogspot.in/2013/12/how-to-call-functions-using-function.html">Next</a></li>
</ul>
<br /></div>
CProgrammerhttp://www.blogger.com/profile/17633481395209852801noreply@blogger.com2tag:blogger.com,1999:blog-3063941734768192839.post-55871771562201209872013-12-30T22:16:00.000+05:302016-03-13T19:43:42.797+05:30Call by value vs call by reference<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b>Difference between call by value and call by reference</b></span></div>
<span style="font-family: "trebuchet ms" , sans-serif;">There are two types of parameter passing schemes.</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"> 1. Call by value</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"> 2. Call by reference</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b>Call by Value:</b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"> A copy of actual arguments are passed to the formal arguments. So, any modification to formal arguments won't affect the original value of actual arguments.</span><br />
<br />
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b>Example C program to illustrate call by value:</b></span></div>
<div dir="ltr" style="text-align: left;" trbidi="on">
<pre class="brush:c++;">
#include <stdio.h>
/* swaps given two number */
void swap(int a, int b) {
int tmp;
tmp = a;
a = b;
b = tmp;
printf("Inside swap() -> a = %d\tb = %d\n", a, b);
return;
}
int main() {
int a, b;
printf("Enter two integers: ");
scanf("%d%d", &a, &b);
swap(a, b); // pass by value
printf("Inside main() -> a = %d\tb = %d\n", a, b);
return 0;
}
</pre>
</div>
<br />
<div style="background-color: #999999; color: white; font-family: "Trebuchet MS",sans-serif;">
<b> Output:</b></div>
<div style="background-color: black;">
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> jp@jp-VirtualBox:~/$ ./a.out</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Enter two integers: 10 20</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Inside swap() -> a = 20<span class="Apple-tab-span" style="white-space: pre;"> </span>b = 10</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Inside main() -> a = 10<span class="Apple-tab-span" style="white-space: pre;"> </span>b = 20</span><br />
<div style="color: white; font-family: 'Trebuchet MS', sans-serif;">
<br /></div>
</div>
<br />
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b><br /></b></span>
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b>Call by Reference:</b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;">Here, the address of variables are passed from the actual argument to the formal argument. So, the <b><i>called function</i></b> acts on addresses rather than values. Here, the formal arguments have the pointer to the actual arguments. So, any change made to formal arguments reflect on actual arguments.</span><br />
<div>
<br />
<b style="color: #660000; font-family: 'Trebuchet MS', sans-serif;">Example C program to illustrate call by reference</b></div>
</div>
<div dir="ltr" style="text-align: left;" trbidi="on">
<pre class="brush:c++;">
#include <stdio.h>
/* swaps given two number */
void swap(int *a, int *b) {
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
printf("Inside swap() -> *a = %d\t*b = %d\n", *a, *b);
return;
}
int main() {
int a, b;
printf("Enter two integers: ");
scanf("%d%d", &a, &b);
/* address of variable is passed to swap() */
swap(&a, &b); // pass by reference
printf("Inside main() -> a = %d\t b = %d\n", a, b);
return 0;
}
</pre>
</div>
<br />
<div style="background-color: #999999; color: white; font-family: "Trebuchet MS",sans-serif;">
<b> Output:</b></div>
<div style="background-color: black;">
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> jp@jp-VirtualBox:~/$ ./a.out</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Enter two integers: 10 20</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Inside swap() -> *a = 20<span class="Apple-tab-span" style="white-space: pre;"> </span>*b = 10</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Inside main() -> a = 20<span class="Apple-tab-span" style="white-space: pre;"> </span> b = 10</span><br />
<div style="color: white; font-family: 'Trebuchet MS', sans-serif;">
<br /></div>
</div>
<br /></div>
</div>
</div>
<ul class="prev_next_button">
<li class="previous"><a href="http://see-programming.blogspot.in/2012/05/parameters.html">Previous</a></li>
<li class="next"><a href="http://see-programming.blogspot.com/2013/12/variable-arguments-in-c.html">Next</a></li>
</ul>
<br /></div>
CProgrammerhttp://www.blogger.com/profile/17633481395209852801noreply@blogger.com3tag:blogger.com,1999:blog-3063941734768192839.post-11428686921777835112013-12-30T13:34:00.000+05:302016-03-12T21:15:55.071+05:30Comma operator in C<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-family: "trebuchet ms" , sans-serif;">Basically, comma operator is used to separate expressions. Consider the following,</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>i++, j++, k++, val = val + 10;</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;">Here, there are four expression and they are separated by comma operator. The above statement is equivalent to the following.</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>i++;</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>j++;</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>k++;</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>val = val + 10;</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;">Comma operator is most often used in for statement and function arguments. Below are few examples on comma operator usage.</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;"><b><span style="color: #660000;">Example 1:</span></b> (comma operator in for loop)</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>for (i = 0, j = 0, k = 0; i < 10; i++, j++, k++) {</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i> : :</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>}</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;"><b><span style="color: #660000;">Example 2:</span></b> (comma operator in function argument)</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>int add(int a, int b) { // comma operator</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i> return (a + b);</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>}</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>main() {</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i> add(10, 20); // comma operator in function argument</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>}</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b>Example C program using comma operator</b></span><br />
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b><br /></b></span></div>
<div dir="ltr" style="text-align: left;" trbidi="on">
<pre class="brush:c++;"> #include <stdio.h>
void add(int a, int b) {
printf("Sum of %d and %d is %d\n", a, b, a + b);
}
int main() {
int i, j, k, val = 0; // variable declaration
/* comma operator in for loop */
for (i =0, j = 0, k = 0; i < 5; i++, j++, k++) {
printf("Hello world\n");
}
/* comma operator in function argument */
printf("\ni = %d, j = %d, k = %d\n", i, j, k);
i++, j++, k++, val = val + 10; // comma operator in statement
printf("i = %d, j = %d, k = %d, val = %d\n", i, j, k, val);
add(10, 20); // comma operator in function argument
return 0;
}
</pre>
</div>
<br />
<div style="background-color: #999999; color: white; font-family: "Trebuchet MS",sans-serif;">
<b> Output:</b></div>
<div style="background-color: black;">
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> jp@jp-VirtualBox:~/$ ./a.out</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Hello world</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Hello world</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Hello world</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Hello world</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Hello world</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> i = 5, j = 5, k = 5</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> i = 6, j = 6, k = 6, val = 10</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> Sum of 10 and 20 is 30</span><br />
<div style="color: white; font-family: 'Trebuchet MS', sans-serif;">
<br /></div>
</div>
<br /></div>
</div>
<ul class="prev_next_button">
<li class="previous"><a href="http://see-programming.blogspot.com/2013/12/sizeof-operator-in-c.html">Previous</a></li>
<li class="next"><a href="http://see-programming.blogspot.in/2012/04/conditional-operators.html">Next</a></li>
</ul>
<br /></div>
CProgrammerhttp://www.blogger.com/profile/17633481395209852801noreply@blogger.com2tag:blogger.com,1999:blog-3063941734768192839.post-13766762886994569052013-12-30T13:14:00.000+05:302016-03-12T21:10:43.396+05:30sizeof operator in C<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-family: "trebuchet ms" , sans-serif;">It is used to find the size(in bytes) of data type or variable. Consider the following examples,</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b>Example 1:</b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>int arr[100];</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;">sizeof(int) - 4 bytes // size of an integer</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;">sizeof(arr) - 400 bytes // size of 100 elements in an integer array</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b>Example 2:</b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="background-color: white; font-family: "trebuchet ms" , sans-serif; font-size: 14.85px; line-height: 20.79px;">Structure is a user defined compound data type which can have elements of different data types grouped under a common name. We will see more about structures in forth coming tutorials.</span></span><br />
<b style="font-family: 'trebuchet ms', sans-serif;"><i><br /></i></b>
<b style="font-family: 'trebuchet ms', sans-serif;"><i>struct student {</i></b><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i> char name[100];</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i> int age, rollno;</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>};</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i>struct student obj, *ptr</i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><b><i><br /></i></b></span>
<span style="font-family: "trebuchet ms" , sans-serif;">sizeof(struct student) - 108 bytes // size of structure student</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;">sizeof(obj) - 108 bytes // size of structure variable of type student</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;">sizeof(ptr) - 4 bytes // size of pointer to structure of type student</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="font-family: "trebuchet ms" , sans-serif;">The total size of the <b><i>structure student</i></b> is 108 bytes(name - 100 bytes, age - 4 bytes, rollno - 4 bytes). Here, obj is a structure variable of type student and ptr is a pointer to structure of type student.</span><br />
<br />
<span style="font-family: "trebuchet ms" , sans-serif;">The return type of <b><i>sizeof()</i></b> operator is unsigned integer. And this operator is defined in <b><i><stddef.h></i></b></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<span style="color: #660000; font-family: "trebuchet ms" , sans-serif;"><b>Example C program on sizeof() operator</b></span></div>
<div dir="ltr" style="text-align: left;" trbidi="on">
<pre class="brush:c++;">
#include <stdio.h>
struct student {
char name[100];
int age, rollno;
};
int main() {
int arr[100];
struct student obj, *ptr;
printf("sizeof(int): %d\n", sizeof(int)); // size of integer
printf("sizeof(arr): %d\n", sizeof(arr)); // sizeof array
printf("sizeof(struct student): %d\n",
sizeof(struct student)); // size of structure
printf("sizeof(obj): %d\n", sizeof(obj)); // obj is of type struct student
printf("sizeof(ptr): %d\n", sizeof(ptr)); // ptr is pointer to struct student
return 0;
}
</pre>
</div>
<br />
<br /></div>
<div style="background-color: #999999; color: white; font-family: "Trebuchet MS",sans-serif;">
<b> Output:</b></div>
<div style="background-color: black;">
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> jp@jp-VirtualBox:~/$ ./a.out</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> sizeof(int): 4</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> sizeof(arr): 400</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> sizeof(struct student): 108</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> sizeof(obj): 108</span><br />
<span style="color: white; font-family: "trebuchet ms" , sans-serif;"> sizeof(ptr): 4</span><br />
<div style="color: white; font-family: 'Trebuchet MS', sans-serif;">
<br /></div>
</div>
</div>
<br />
<ul class="prev_next_button">
<li class="previous"><a href="http://see-programming.blogspot.in/2013/12/pointer-operators-in-c.html">Previous</a></li>
<li class="next"><a href="http://see-programming.blogspot.in/2013/12/comma-operator-in-c.html">Next</a></li>
</ul>
<br /></div>
<br /></div>
CProgrammerhttp://www.blogger.com/profile/17633481395209852801noreply@blogger.com2