tag:blogger.com,1999:blog-86833284916879288722024-03-05T15:15:01.888+05:30Learn:C/C++ Programming language tutorial in hindicomputer programming बेहतर तरीके से सीखें, अपनी मात्रभाषा hindi में!Hridayesh Guptahttp://www.blogger.com/profile/09764967341453023170noreply@blogger.comBlogger31125tag:blogger.com,1999:blog-8683328491687928872.post-1467788903587189182017-07-11T09:24:00.001+05:302017-07-13T21:25:56.492+05:30Java Installation<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="margin-bottom: .0001pt; margin: 0in;">
<span lang="HI" style="font-family: "mangal" , serif;">पिछले</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">टॉपिक</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">में</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">हमने</span><span lang="HI"> </span>Java <span lang="HI" style="font-family: "mangal" , serif;">के</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">बारे</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">में</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">समझा</span>, <span lang="HI" style="font-family: "mangal" , serif;">कि</span><span lang="HI"> </span>Java <span lang="HI" style="font-family: "mangal" , serif;">किस</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">तरह</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">से</span><span lang="HI"> </span>C/C++ <span lang="HI" style="font-family: "mangal" , serif;">से</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">अलग</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">है</span>,<o:p></o:p></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<span lang="HI" style="font-family: "mangal" , serif; font-size: 10pt;">(<a href="http://programming-tutorial-hindi.blogspot.in/2016/12/java-introduction.html">Java Introduction</a></span><span lang="HI" style="font-family: "mangal" , serif; font-size: 10pt;"> पढने के लिए क्लिक करे)<o:p></o:p></span></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<span lang="HI" style="font-family: "mangal" , serif;">आज</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">हम</span><span lang="HI"> </span>Java <span lang="HI" style="font-family: "mangal" , serif;">के</span><span lang="HI"> </span>Installation <span lang="HI" style="font-family: "mangal" , serif;">के</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">बारे</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">में</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">समझते</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">है</span><span lang="HI">.</span><o:p></o:p></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
Windows <span lang="HI" style="font-family: "mangal" , serif;">में</span><span lang="HI"> </span>Java Install <span lang="HI" style="font-family: "mangal" , serif;">करने</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">के</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">लिए</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">आपको</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">सबसे</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">पहले</span><span lang="HI"> </span>JDK-7 <span lang="HI" style="font-family: "mangal" , serif;">का</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">सेटअप</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">डाउनलोड</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">करना</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">होगा</span>,<o:p></o:p></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
(JDK-7 <span lang="HI" style="font-family: "mangal" , serif;">डाउनलोड</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">करने</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">के</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">लिए</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">इस</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">लिंक</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">पर</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">क्लिक</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">करे</span><span lang="HI">-<a href="https://en.softonic.com/s/java-jdk-7"> https://en.softonic.com/s/java-jdk-7</a></span><span lang="HI">)</span><o:p></o:p></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<span lang="HI" style="font-family: "mangal" , serif;">अपने</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">सिस्टम</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">के</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">अनुसार</span><span lang="HI"> 32-bit/64-bit </span><span lang="HI" style="font-family: "mangal" , serif;">चुने</span><span lang="HI"><o:p></o:p></span></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
JDK-7 <span lang="HI" style="font-family: "mangal" , serif;">को</span><span lang="HI"> </span>Install <span lang="HI" style="font-family: "mangal" , serif;">करे</span><span lang="HI">(</span>Install <span lang="HI" style="font-family: "mangal" , serif;">करने</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">की</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">प्रक्रिया</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">बेहद</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">आसान</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">है</span><span lang="HI">.)</span>, JDK-7 <span lang="HI" style="font-family: "mangal" , serif;">इनस्टॉल</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">करने</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">के</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">बाद</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">आपको</span><span lang="HI"> </span>drJava <span lang="HI" style="font-family: "mangal" , serif;">को</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">डाउनलोड</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">करना</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">होगा</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">और</span><span lang="HI"> </span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">इनस्टॉल</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">करना</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">होगा</span><span lang="HI">.</span><o:p></o:p></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
drJava <span lang="HI" style="font-family: "mangal" , serif;">इनस्टॉल</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">करने</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">के</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">बाद</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">आपको</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">इस</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">तरह</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">की</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">एक</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">विंडो</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">खुल</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">कर</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">सामने</span><span lang="HI"> </span><span lang="HI" style="font-family: "mangal" , serif;">आएगी</span><span lang="HI">.</span><o:p></o:p></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjl_OZYxExchfmzNGajuYrbazCnc_aDCYnmzl5qCeYROb5EX3fdzWPmS69lfivzrmT_R4uqpKuODm79RHfN_lA651v7WDuSTIiRSn2GFqOyxQ-W_NXtSeUm_VRNY-LB4sx-xQUPRT-gYkM/s1600/drJava.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="506" data-original-width="549" height="367" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjl_OZYxExchfmzNGajuYrbazCnc_aDCYnmzl5qCeYROb5EX3fdzWPmS69lfivzrmT_R4uqpKuODm79RHfN_lA651v7WDuSTIiRSn2GFqOyxQ-W_NXtSeUm_VRNY-LB4sx-xQUPRT-gYkM/s400/drJava.JPG" width="400" /></a></div>
<div style="margin-bottom: .0001pt; margin: 0in;">
<span lang="HI"><br /></span></div>
<div class="MsoNormal">
<span style="font-family: "times new roman" , "serif"; font-size: 12.0pt; line-height: 115%;"><v:shapetype coordsize="21600,21600" filled="f" id="_x0000_t75" o:preferrelative="t" o:spt="75" path="m@4@5l@4@11@9@11@9@5xe" stroked="f">
<v:stroke joinstyle="miter">
<v:formulas>
<v:f eqn="if lineDrawn pixelLineWidth 0">
<v:f eqn="sum @0 1 0">
<v:f eqn="sum 0 0 @1">
<v:f eqn="prod @2 1 2">
<v:f eqn="prod @3 21600 pixelWidth">
<v:f eqn="prod @3 21600 pixelHeight">
<v:f eqn="sum @0 0 1">
<v:f eqn="prod @6 1 2">
<v:f eqn="prod @7 21600 pixelWidth">
<v:f eqn="sum @8 21600 0">
<v:f eqn="prod @7 21600 pixelHeight">
<v:f eqn="sum @10 21600 0">
</v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:formulas>
<v:path gradientshapeok="t" o:connecttype="rect" o:extrusionok="f">
<o:lock aspectratio="t" v:ext="edit">
</o:lock></v:path></v:stroke></v:shapetype><v:shape id="Picture_x0020_1" o:spid="_x0000_i1025" style="height: 379.5pt; mso-wrap-style: square; visibility: visible; width: 411.75pt;" type="#_x0000_t75">
<v:imagedata o:title="drJava" src="file:///C:\Users\Vivek\AppData\Local\Temp\msohtmlclip1\01\clip_image001.jpg">
</v:imagedata></v:shape></span><span style="font-family: "times new roman" , "serif"; font-size: 12.0pt; line-height: 115%;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="HI" style="font-family: "times new roman" , "serif"; font-size: 12.0pt; line-height: 115%;">DrJava </span><span lang="HI" style="font-family: "mangal" , "serif"; font-size: 12.0pt; line-height: 115%;">कम्पाइलर</span><span lang="HI" style="font-family: "times new roman" , "serif"; font-size: 12.0pt; line-height: 115%;">
</span><span lang="HI" style="font-family: "mangal" , "serif"; font-size: 12.0pt; line-height: 115%;">के</span><span lang="HI" style="font-family: "times new roman" , "serif"; font-size: 12.0pt; line-height: 115%;">
</span><span lang="HI" style="font-family: "mangal" , "serif"; font-size: 12.0pt; line-height: 115%;">साथ</span><span lang="HI" style="font-family: "times new roman" , "serif"; font-size: 12.0pt; line-height: 115%;">-2
</span><span lang="HI" style="font-family: "mangal" , "serif"; font-size: 12.0pt; line-height: 115%;">एडिटर</span><span lang="HI" style="font-family: "times new roman" , "serif"; font-size: 12.0pt; line-height: 115%;">
</span><span lang="HI" style="font-family: "mangal" , "serif"; font-size: 12.0pt; line-height: 115%;">भी</span><span lang="HI" style="font-family: "times new roman" , "serif"; font-size: 12.0pt; line-height: 115%;">
</span><span lang="HI" style="font-family: "mangal" , "serif"; font-size: 12.0pt; line-height: 115%;">है</span><span lang="HI" style="font-family: "times new roman" , "serif"; font-size: 12.0pt; line-height: 115%;">,
</span><span lang="HI" style="font-family: "mangal" , "serif"; font-size: 12.0pt; line-height: 115%;">जिसमें</span><span lang="HI" style="font-family: "times new roman" , "serif"; font-size: 12.0pt; line-height: 115%;"> Java </span><span lang="HI" style="font-family: "mangal" , "serif"; font-size: 12.0pt; line-height: 115%;">के</span><span lang="HI" style="font-family: "times new roman" , "serif"; font-size: 12.0pt; line-height: 115%;">
Program </span><span lang="HI" style="font-family: "mangal" , "serif"; font-size: 12.0pt; line-height: 115%;">को</span><span lang="HI" style="font-family: "times new roman" , "serif"; font-size: 12.0pt; line-height: 115%;"> </span><span lang="HI" style="font-family: "mangal" , "serif"; font-size: 12.0pt; line-height: 115%;">लिख</span><span lang="HI" style="font-family: "times new roman" , "serif"; font-size: 12.0pt; line-height: 115%;">
</span><span lang="HI" style="font-family: "mangal" , "serif"; font-size: 12.0pt; line-height: 115%;">कर</span><span lang="HI" style="font-family: "times new roman" , "serif"; font-size: 12.0pt; line-height: 115%;">
</span><span lang="HI" style="font-family: "mangal" , "serif"; font-size: 12.0pt; line-height: 115%;">एडिट</span><span lang="HI" style="font-family: "times new roman" , "serif"; font-size: 12.0pt; line-height: 115%;">
</span><span lang="HI" style="font-family: "mangal" , "serif"; font-size: 12.0pt; line-height: 115%;">भी</span><span lang="HI" style="font-family: "times new roman" , "serif"; font-size: 12.0pt; line-height: 115%;">
</span><span lang="HI" style="font-family: "mangal" , "serif"; font-size: 12.0pt; line-height: 115%;">कर</span><span lang="HI" style="font-family: "times new roman" , "serif"; font-size: 12.0pt; line-height: 115%;">
</span><span lang="HI" style="font-family: "mangal" , "serif"; font-size: 12.0pt; line-height: 115%;">सकते</span><span lang="HI" style="font-family: "times new roman" , "serif"; font-size: 12.0pt; line-height: 115%;">
</span><span lang="HI" style="font-family: "mangal" , "serif"; font-size: 12.0pt; line-height: 115%;">है</span><span lang="HI" style="font-family: "times new roman" , "serif"; font-size: 12.0pt; line-height: 115%;">,
</span><span lang="HI" style="font-family: "mangal" , "serif"; font-size: 12.0pt; line-height: 115%;">और</span><span lang="HI" style="font-family: "times new roman" , "serif"; font-size: 12.0pt; line-height: 115%;">
</span><span lang="HI" style="font-family: "mangal" , "serif"; font-size: 12.0pt; line-height: 115%;">प्रोग्राम</span><span lang="HI" style="font-family: "times new roman" , "serif"; font-size: 12.0pt; line-height: 115%;">
Run </span><span lang="HI" style="font-family: "mangal" , "serif"; font-size: 12.0pt; line-height: 115%;">करना</span><span lang="HI" style="font-family: "times new roman" , "serif"; font-size: 12.0pt; line-height: 115%;">
</span><span lang="HI" style="font-family: "mangal" , "serif"; font-size: 12.0pt; line-height: 115%;">और</span><span lang="HI" style="font-family: "times new roman" , "serif"; font-size: 12.0pt; line-height: 115%;">
Execute </span><span lang="HI" style="font-family: "mangal" , "serif"; font-size: 12.0pt; line-height: 115%;">करने</span><span lang="HI" style="font-family: "times new roman" , "serif"; font-size: 12.0pt; line-height: 115%;"> </span><span lang="HI" style="font-family: "mangal" , "serif"; font-size: 12.0pt; line-height: 115%;">का</span><span lang="HI" style="font-family: "times new roman" , "serif"; font-size: 12.0pt; line-height: 115%;"> </span><span lang="HI" style="font-family: "mangal" , "serif"; font-size: 12.0pt; line-height: 115%;">काम</span><span lang="HI" style="font-family: "times new roman" , "serif"; font-size: 12.0pt; line-height: 115%;">
</span><span lang="HI" style="font-family: "mangal" , "serif"; font-size: 12.0pt; line-height: 115%;">भी</span><span lang="HI" style="font-family: "times new roman" , "serif"; font-size: 12.0pt; line-height: 115%;">
DrJava </span><span lang="HI" style="font-family: "mangal" , "serif"; font-size: 12.0pt; line-height: 115%;">में</span><span lang="HI" style="font-family: "times new roman" , "serif"; font-size: 12.0pt; line-height: 115%;"> </span><span lang="HI" style="font-family: "mangal" , "serif"; font-size: 12.0pt; line-height: 115%;">किया</span><span lang="HI" style="font-family: "times new roman" , "serif"; font-size: 12.0pt; line-height: 115%;">
</span><span lang="HI" style="font-family: "mangal" , "serif"; font-size: 12.0pt; line-height: 115%;">जाता</span><span lang="HI" style="font-family: "times new roman" , "serif"; font-size: 12.0pt; line-height: 115%;">
</span><span lang="HI" style="font-family: "mangal" , "serif"; font-size: 12.0pt; line-height: 115%;">है</span><span lang="HI" style="font-family: "times new roman" , "serif"; font-size: 12.0pt; line-height: 115%;">|</span><span style="font-family: "times new roman" , "serif"; font-size: 12.0pt; line-height: 115%;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="HI" style="font-family: "mangal" , "serif"; font-size: 12.0pt; line-height: 115%;">यदि</span><span lang="HI" style="font-family: "times new roman" , "serif"; font-size: 12.0pt; line-height: 115%;"> </span><span lang="HI" style="font-family: "mangal" , "serif"; font-size: 12.0pt; line-height: 115%;">आप</span><span lang="HI" style="font-family: "times new roman" , "serif"; font-size: 12.0pt; line-height: 115%;"> Java </span><span lang="HI" style="font-family: "mangal" , "serif"; font-size: 12.0pt; line-height: 115%;">के</span><span lang="HI" style="font-family: "times new roman" , "serif"; font-size: 12.0pt; line-height: 115%;">
</span><span lang="HI" style="font-family: "mangal" , "serif"; font-size: 12.0pt; line-height: 115%;">प्रोग्राम</span><span lang="HI" style="font-family: "times new roman" , "serif"; font-size: 12.0pt; line-height: 115%;">
</span><span lang="HI" style="font-family: "mangal" , "serif"; font-size: 12.0pt; line-height: 115%;">को</span><span lang="HI" style="font-family: "times new roman" , "serif"; font-size: 12.0pt; line-height: 115%;">
</span><span lang="HI" style="font-family: "mangal" , "serif"; font-size: 12.0pt; line-height: 115%;">ऑनलाइन</span><span lang="HI" style="font-family: "times new roman" , "serif"; font-size: 12.0pt; line-height: 115%;">
</span><span lang="HI" style="font-family: "mangal" , "serif"; font-size: 12.0pt; line-height: 115%;">चलाना</span><span lang="HI" style="font-family: "times new roman" , "serif"; font-size: 12.0pt; line-height: 115%;">
</span><span lang="HI" style="font-family: "mangal" , "serif"; font-size: 12.0pt; line-height: 115%;">चाहते</span><span lang="HI" style="font-family: "times new roman" , "serif"; font-size: 12.0pt; line-height: 115%;">
</span><span lang="HI" style="font-family: "mangal" , "serif"; font-size: 12.0pt; line-height: 115%;">है</span><span lang="HI" style="font-family: "times new roman" , "serif"; font-size: 12.0pt; line-height: 115%;">
</span><span lang="HI" style="font-family: "mangal" , "serif"; font-size: 12.0pt; line-height: 115%;">तो</span><span lang="HI" style="font-family: "times new roman" , "serif"; font-size: 12.0pt; line-height: 115%;">
</span><span lang="HI" style="font-family: "mangal" , "serif"; font-size: 12.0pt; line-height: 115%;">इस</span><span lang="HI" style="font-family: "times new roman" , "serif"; font-size: 12.0pt; line-height: 115%;">
</span><span lang="HI" style="font-family: "mangal" , "serif"; font-size: 12.0pt; line-height: 115%;">लिंक</span><span lang="HI" style="font-family: "times new roman" , "serif"; font-size: 12.0pt; line-height: 115%;">
</span><span lang="HI" style="font-family: "mangal" , "serif"; font-size: 12.0pt; line-height: 115%;">पर</span><span lang="HI" style="font-family: "times new roman" , "serif"; font-size: 12.0pt; line-height: 115%;">
</span><span lang="HI" style="font-family: "mangal" , "serif"; font-size: 12.0pt; line-height: 115%;">क्लिक</span><span lang="HI" style="font-family: "times new roman" , "serif"; font-size: 12.0pt; line-height: 115%;">
</span><span lang="HI" style="font-family: "mangal" , "serif"; font-size: 12.0pt; line-height: 115%;">करे</span><span lang="HI" style="font-family: "times new roman" , "serif"; font-size: 12.0pt; line-height: 115%;">-(
<a href="https://ideone.com/">https://ideone.com/</a></span><span style="font-family: "times new roman" , "serif"; font-size: 12.0pt; line-height: 115%;"><span lang="HI"> )</span></span></div>
<div class="MsoNormal">
<b><span lang="HI" style="background: white; font-family: "mangal" , serif; line-height: 115%;">ध्यान रहे किसी </span><span style="background: white; font-family: "arial" , sans-serif; line-height: 115%;">program </span><span lang="HI" style="background: white; font-family: "mangal" , serif; line-height: 115%;">को रन करने से
पहले </span><span style="background: white; font-family: "arial" , sans-serif; line-height: 115%;">compile </span><span lang="HI" style="background: white; font-family: "mangal" , serif; line-height: 115%;">करना आवश्यक
होता है. अगर आपने </span></b><b><span style="background: white; font-family: "arial" , sans-serif; line-height: 115%;">program modify </span><span lang="HI" style="background: white; font-family: "mangal" , serif; line-height: 115%;">किया है तो उसे फिर से </span><span style="background: white; font-family: "arial" , sans-serif; line-height: 115%;">compile </span><span lang="HI" style="background: white; font-family: "mangal" , serif; line-height: 115%;">करना होगा.</span></b><br />
<span style="background-color: white; font-family: "mangal" , serif;">अगले टॉपिक में Java के पहले प्रोग्राम को समझेंगे |</span></div>
<div class="MsoNormal">
<span lang="HI" style="background: white; font-family: "mangal" , serif; font-size: 14pt; line-height: 115%;"><br /></span></div>
<div class="MsoNormal">
<br /></div>
<br />
<div class="MsoNormal">
<br /></div>
</div>
Vivekhttp://www.blogger.com/profile/15969174941979792808noreply@blogger.comtag:blogger.com,1999:blog-8683328491687928872.post-20801387859061176642016-12-31T14:31:00.001+05:302017-07-13T21:35:32.066+05:30Java Introduction<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;">
<div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: "arial"; font-size: 14.6667px; vertical-align: baseline; white-space: pre-wrap;">C/C++ के बाद आइये हम </span><span style="font-family: "arial"; font-size: 14.6667px; font-weight: 700; vertical-align: baseline; white-space: pre-wrap;">Java Programming Language</span><span style="font-family: "arial"; font-size: 14.6667px; vertical-align: baseline; white-space: pre-wrap;"> के बारे जानते है, जिस तरह C Language दुनिया की सबसे पुरानी कंप्यूटर की भाषा है, उसी तरह से आज के आधुनिक युग में </span><span style="font-family: "arial"; font-size: 14.6667px; font-weight: 700; vertical-align: baseline; white-space: pre-wrap;">Java</span><span style="font-family: "arial"; font-size: 14.6667px; vertical-align: baseline; white-space: pre-wrap;"> का अपना स्थान है, जावा कई मायनो में C/C++ से अलग है, लेकिन अगर आपने C/C++ अच्छे ढंग से सीखी हुई है, तो आपको </span><span style="font-family: "arial"; font-size: 14.6667px; font-weight: 700; vertical-align: baseline; white-space: pre-wrap;">Java</span><span style="font-family: "arial"; font-size: 14.6667px; vertical-align: baseline; white-space: pre-wrap;"> सीखने में ज्यादा कठिनाई नही होगी, परन्तु आपको ये tutorial इस तरह से पढाया जायेगा कि बिना कोई Computer Language जाने भी आप </span><span style="font-family: "arial"; font-size: 14.6667px; font-weight: 700; vertical-align: baseline; white-space: pre-wrap;">Java</span><span style="font-family: "arial"; font-size: 14.6667px; vertical-align: baseline; white-space: pre-wrap;"> को बड़ी आसानी से सीख पाएंगे.</span></div>
<div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: "arial"; font-size: 14.6667px; font-weight: 700; vertical-align: baseline; white-space: pre-wrap;">Java Language क्या है ?</span></div>
<div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;">
<span style="font-family: "arial"; font-size: 14.6667px; font-weight: 700; vertical-align: baseline; white-space: pre-wrap;">Java Language</span><span style="font-family: "arial"; font-size: 14.6667px; vertical-align: baseline; white-space: pre-wrap;"> एक प्रकार की </span><span style="background-color: white; color: #252525; font-family: "arial"; font-size: 14px; font-weight: 700; vertical-align: baseline; white-space: pre-wrap;">general-purpose</span><span style="background-color: white; color: #252525; font-family: "arial"; font-size: 14px; vertical-align: baseline; white-space: pre-wrap;"> कंप्यूटर Programming Language है, इसके साथ-2 Java “</span><span style="background-color: white; color: #252525; font-family: "arial"; font-size: 14px; font-weight: 700; vertical-align: baseline; white-space: pre-wrap;">platform-independent” </span><span style="background-color: white; color: #252525; font-family: "arial"; font-size: 14px; vertical-align: baseline; white-space: pre-wrap;">Language है, यानि Java Language में लिखे गये Program हम किसी भी Operating System(OS) के ऊपर रन कर सकते है, “</span><span style="background-color: white; color: #252525; font-family: "arial"; font-size: 14px; font-weight: 700; vertical-align: baseline; white-space: pre-wrap;">platform-independent” </span><span style="background-color: white; color: #252525; font-family: "arial"; font-size: 14px; vertical-align: baseline; white-space: pre-wrap;">को अब दूसरे प्रकार से समझते है, C/C++ के Program को Windows Operating System में Run करने के लिये code को windows के लिए लिखा गया Compile करना पड़ेगा. किसी और OS के लिए हर बार अलग अलग compile करना पड़ेगा. परन्तु Java में लिखे Program को हम एक बार compile करके किसी भी Operating System या मशीन में Run कर सकते है.</span></div>
<div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: white; color: #252525; font-family: "arial"; font-size: 14px; vertical-align: baseline; white-space: pre-wrap;">आइये अब </span><span style="background-color: white; color: #252525; font-family: "arial"; font-size: 14px; font-weight: 700; vertical-align: baseline; white-space: pre-wrap;"> Java </span><span style="background-color: white; color: #252525; font-family: "arial"; font-size: 14px; vertical-align: baseline; white-space: pre-wrap;">के उपयोग को समझते है.</span></div>
<div dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: white; color: #252525; font-family: "arial"; font-size: 14px; vertical-align: baseline; white-space: pre-wrap;">Java आधुनिक युग की बहुत महत्वपूर्ण Computer Language है, </span><span style="background-color: white; color: #252525; font-family: "arial"; font-size: 14px; font-weight: 700; vertical-align: baseline; white-space: pre-wrap;">Android</span><span style="background-color: white; color: #252525; font-family: "arial"; font-size: 14px; vertical-align: baseline; white-space: pre-wrap;"> के अधिकांशतः एप्लीकेशन Java </span><span style="background-color: white; color: #252525; font-family: "arial"; font-size: 14px; font-weight: 700; vertical-align: baseline; white-space: pre-wrap;"> </span><span style="background-color: white; color: #252525; font-family: "arial"; font-size: 14px; vertical-align: baseline; white-space: pre-wrap;">में लिखे जाते है, इसके साथ-2 </span><span style="background-color: white; color: #252525; font-family: "arial"; font-size: 14px; font-weight: 700; vertical-align: baseline; white-space: pre-wrap;">Gmail, Linkdin, Amazon</span><span style="background-color: white; color: #252525; font-family: "arial"; font-size: 14px; vertical-align: baseline; white-space: pre-wrap;"> जैसे बड़े-2 </span><span style="background-color: white; color: #252525; font-family: "arial"; font-size: 14px; font-weight: 700; vertical-align: baseline; white-space: pre-wrap;">Server </span><span style="background-color: white; color: #252525; font-family: "arial"; font-size: 14px; vertical-align: baseline; white-space: pre-wrap;">भी Java में लिखे गए है,</span></div>
<span id="docs-internal-guid-3673e71c-5418-11ec-110b-0afeb1f37a26"></span><br />
<div style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: white; color: #252525; font-family: "arial"; font-size: 14px; vertical-align: baseline; white-space: pre-wrap;">डेस्कटॉप एप्लीकेशन </span><span style="background-color: white; color: #252525; font-family: "arial"; font-size: 14px; font-weight: 700; vertical-align: baseline; white-space: pre-wrap;">Eclipse </span><span style="background-color: white; color: #252525; font-family: "arial"; font-size: 14px; vertical-align: baseline; white-space: pre-wrap;">भी Java में लिखी गयी है |</span></div>
</div>
</div>
Vivekhttp://www.blogger.com/profile/15969174941979792808noreply@blogger.comtag:blogger.com,1999:blog-8683328491687928872.post-50242356590004876402016-11-07T22:38:00.000+05:302016-11-10T07:09:54.792+05:30अभाज्य संख्या check करना <div dir="ltr" style="text-align: left;" trbidi="on">
निचे दिया गया program 0 से 19 तक की संख्याओं के लिए यह print करता है कि कौन सी संख्या अभाज्य है और कौन सी नहीं है।<br />
क्या आप इसको आगे बढ़ाते हुए ऐसा program भी लिख सकते हैं जो 20 से 40 तक की अभाज्य संख्या print करे। <br />
<br />
<pre class="brush: cpp">#include <stdio.h>
int checkPrime(int n) {
if(n < 2) return 0;
int i = 2;
for(i = 2; i<n; i++) {
if(n%i == 0) return 0;
}
return 1;
}
int main() {
int num = 0;
for(num = 0; num < 20; num++) {
int isPrime = checkPrime(num);
if(isPrime == 0) {
printf("%d is not prime\n", num);
}
else if(isPrime == 1) {
printf("%d is prime\n", num);
}
}
}
</pre>
<br />
Output
<br />
<pre>0 is not prime
1 is not prime
2 is prime
3 is prime
4 is not prime
5 is prime
6 is not prime
7 is prime
8 is not prime
9 is not prime
10 is not prime
11 is prime
12 is not prime
13 is prime
14 is not prime
15 is not prime
16 is not prime
17 is prime
18 is not prime
19 is prime
</pre>
<!-- http://ideone.com/e.js/sYymVC -->
</div>
Hridayesh Guptahttp://www.blogger.com/profile/09764967341453023170noreply@blogger.comtag:blogger.com,1999:blog-8683328491687928872.post-25695316035355855632012-07-05T09:47:00.000+05:302015-11-24T08:09:55.268+05:30logical comparision operator<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<b><span style="font-size: large;">logical and comparision operator</span></b><br />
आज हम C programming के इस hindi tutorial को आगे पढ़ते हुए operators के बारे में और जानेंगे.<br />
<div>
सबसे पहले नीचे Logical operators दिए जा रहे हैं.</div>
<div>
1. ! - यह एक <span id="6_TRN_10">uniary</span> operator(NOT) है, जो केवल एक bool value लेता है और उसका उल्टा कर देता है, जैसे कि !true = false, !false = true </div>
<div>
2. || - यह एक binary operator(OR) है जो दो bool value लेता है उसमे से कोई भी true होने पर true देता है अन्यथा false देता है. जैसे कि true || false = true, false || false = false </div>
<div>
3. && <span style="line-height: 1.8;">- यह एक binary operator(AND) है जो दो bool value लेता है उसमे से कोई भी false होने पर false देता है अन्यथा true देता है. जैसे कि true || false = false, false || false = false </span></div>
<div>
<span style="line-height: 1.8;">इनको इस तरह से समझ सकते हैं कि OR यानि अथवा(</span><span style="background-color: white; font-family: Arial, Helvetica, sans-serif; line-height: 28px; text-align: -webkit-auto;">या</span><span style="background-color: white; line-height: 1.8;">), यानि कि अगर कोई भी एक सही(true) है तो result सही(true) होगा. अगर दोनो गलत(false) हैं तभी result गलत(false) होगा.</span></div>
<div>
<span style="background-color: white; line-height: 1.8;">इसी तरह AND </span><span style="background-color: white; line-height: 1.8;">यानि और(दोनो), यानि कि दोनो सही(true) है तभी result सही(true) होगा. कोई भी गलत(false) हैं तो result गलत(false) हो जायेगा.</span></div>
<div>
<span style="line-height: 1.8;"><br /></span><br />
<span style="line-height: 1.8;">अब Comparison Operators को एक बार देखते हैं जिनका use पहले भी कर चुके हैं. उसके बार कुछ example में इनको use करेंगे.</span></div>
<div>
<span style="line-height: 1.8;">1. == - यह </span><span style="line-height: 1.8;"> </span><span style="line-height: 1.8;">एक binary operator(equal) है जो दो int या दो bool value लेता है, अगर दोनो बराबर है तो bool value true देता है अन्यथा false देता है. जैसे कि (5 == 5) = true , (1 == 2) == false </span></div>
<div>
<span style="line-height: 1.8;">2. != - यह </span><span style="line-height: 1.8;"> </span><span style="line-height: 1.8;">एक binary operator (not equal) है जो दो int या दो bool value लेता है, अगर दोनो बराबर नहीं है तो bool value true देता है अन्यथा false देता है.</span><span style="line-height: 1.8;"> </span><span style="line-height: 1.8;">जैसे कि (5 != 5) = false , (1 != 2) == true </span></div>
<div>
<span style="line-height: 1.8;">3. > -</span><span style="line-height: 1.8;"> यह </span><span style="line-height: 1.8;"> </span><span style="line-height: 1.8;">एक binary operator (greater than) है जो दो int value लेता है, अगर पहला int बड़ा है तो bool value true देता है अन्यथा false देता है.</span><span style="line-height: 1.8;"> </span><span style="line-height: 1.8;">जैसे कि (5 > 5) = false , (3 > 2) == true </span></div>
<div>
<span style="line-height: 1.8;">4. < -</span><span style="line-height: 1.8;"> यह </span><span style="line-height: 1.8;"> </span><span style="line-height: 1.8;">एक binary operator (less than) है जो दो int value लेता है, अगर पहला int छोटा है तो bool value true देता है अन्यथा false देता है.</span><span style="line-height: 1.8;"> </span><span style="line-height: 1.8;">जैसे कि (5 < 5) = false , (1 < 2) == true </span></div>
<div>
<div>
<span style="line-height: 1.8;">3. >= -</span><span style="line-height: 1.8;"> यह </span><span style="line-height: 1.8;"> </span><span style="line-height: 1.8;">एक binary operator (greater than or equal) है जो दो int value लेता है, अगर पहला int बड़ा या बराबर है तो bool value true देता है अन्यथा false देता है.</span><span style="line-height: 1.8;"> </span><span style="line-height: 1.8;">जैसे कि (5 > 5) = true , (3 > 2) == true </span></div>
<div>
<span style="line-height: 1.8;">4. <= -</span><span style="line-height: 1.8;"> यह </span><span style="line-height: 1.8;"> </span><span style="line-height: 1.8;">एक binary operator (less than or equal) है जो दो int value लेता है, अगर पहला int छोटा या बराबर है तो bool value true देता है अन्यथा false देता है.</span><span style="line-height: 1.8;"> </span><span style="line-height: 1.8;">जैसे कि (5 < 5) = true , (1 < 2) == true </span></div>
</div>
<div>
<br />
<span style="font-family: Arial, Helvetica, sans-serif; line-height: 28px; text-align: -webkit-auto;">एक example से समझते हैं कि इनका use कैसे होता है. </span><span style="background-color: white; font-family: Arial, Helvetica, sans-serif; line-height: 28px; text-align: -webkit-auto;">इस example में वर्ष(सन) input में लेंगे और यह print करेंगे कि फरवरी 28 की होगी या 29 की.(सामान्यतः जिस वर्ष में 4 का भाग चला जाये उसमे फरवरी 29 की होती है जिस वर्ष में 100 का भाग जाता है उसमे 400 का भाग भी जाना चाहिए, जैसे क़ी 1900,2100... में फरवरी 28 क़ी है पर 2000 में 29 की)</span></div>
<pre class="brush: cpp">#include<stdio.h>
void main() {
int i;
printf("Enter the year: ");
scanf("%d",&i);
if(i%4==0 && i%100!=0) printf("Feb have 29 days");
else if(i%400==0) printf("Feb have 29 days");
else printf("Feb have 28 days");
}
</pre>
<span style="font-family: Arial, Helvetica, sans-serif; line-height: 28px; text-align: -webkit-auto;">% यह बताता है भाग देने पर शेष कितना आया. अगर शेष 0 आया इसका मतलब भाग चला गया. इस example में पहले हमने ये check किया है क़ी वर्ष में 4 का भाग जाये(i%4 ==0 ) और(&&) 100 का भाग न(i %100 !=0) जाये(1984, 2012) या फिर 400 का भाग चला जाये. </span><span style="background-color: white; font-family: Arial, Helvetica, sans-serif; line-height: 28px; text-align: -webkit-auto;">इसी को थोडा दुसरे तरीके से भी लिख सकते हैं.</span></div>
<pre class="brush: cpp">#include<stdio.h>
void main() {
int i;
printf("Enter the year: ");
scanf("%d",&i);
if((i%4==0 && i%100!=0) || i%400==0) printf("Feb have 29 days");
else printf("Feb have 28 days");
}
</pre>
<div style="text-align: center;">
<a href="http://programming-tutorial-hindi.blogspot.com/">Index</a></div>
<iframe allowtransparency="true" frameborder="0" scrolling="no" src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.facebook.com%2F%23%21%2Fpages%2Fkampyutara-sikhe-aba-hindi-mem%2F157896914235350&layout=standard&show_faces=false&width=450&action=like&font=verdana&colorscheme=light&height=35" style="border: none; height: 35px; overflow: hidden; width: 450px;"></iframe>
</div>Hridayesh Guptahttp://www.blogger.com/profile/09764967341453023170noreply@blogger.comtag:blogger.com,1999:blog-8683328491687928872.post-49289980137370109702011-12-26T23:23:00.000+05:302015-11-24T08:10:36.808+05:30Binary Number System<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<b><span style="font-size: large;">Binary Number System</span></b><br />
आज इस c++ programming के इस hindi tutorial को आगे बढ़ाते हुए logical operators के बारे में जानेंगे. इसके लिए निम्न बातों का ज्ञान जरुरी है.<br />
1. Binary Number System - इसके बारे में नीचे बताया जा रहा है, उसके बाद logical operator पढेंगे.<br />
Binary से decimal और decimal से binary में convert करना भी सीखेंगे.<br />
<br />
किसी चीज को गिनने के लिए गिनती जरुरत होती है. इस गिनती को लिखने के लिए एक ऐसे तरीके की जरुरत होती है जिसके द्वारा कितनी भी ज्यादा चीजों को गिन सकें. इसके लिए बचपन में हमने जो गिनती सीखी है उसमे 0 से 9 तक अंक होते हैं. इन अंको का use करके कहीं तक भी गिनती लिख सकते हैं. किसी भी संख्या का अगला अंक जानने के लिए हम इकाई का अंक एक बढ़ा देते हैं जैसे कि 85 के बाद 86 आता है(क्योंकि इकाई 5 का अगला अंक 6 है). अगर इकाई का अंक सबसे बड़ा अंक हो, तो फिर उसे वापस 0 कर देते हैं और दहाई का अंक 1 बढ़ा देते हैं. जैसे कि 59 के बाद 60 आता है क्योंकि इकाई 9 है जो कि सबसे बड़ा अंक है(अंक सिर्फ 0 से 9 तक हैं) इसलिए 0 हो गया और दहाई एक बढ़ गया.<br />
अब मान लीजिए आपके पास 0 से 7 तक कुल 8 अंक ही हैं. सिर्फ इन 8 अंको से गिनती कैसे लिखेंगे? उसी तरह जैसे कि ऊपर बताया गया है. 7 के बाद 10 आ जायेगा, क्योंकि हमारे पास 0 से 7 तक अंक ही हैं. गिनती कुछ इस तरह होगी.<br />
0,1,2,3,4,5,6,7, 10,11,12,13,14,15,16,17,20, 21,22 .... 71,72,73,74,75,76,77,100,101,102 ...<br />
इसे लिखने में अगला अंक निकालने के लिए में वही तरीका अपनाया गया है जो ऊपर बताया गया है. अगर समझने में कोई दिक्कत है तो मेरी discussion site पर आकर मुझसे discuss कर सकते हैं.<br />
* * * * * * * * * * * *<br />
अगर आपसे पूछा जाये कि ऊपर कितने * हैं लिखकर बताइए तो आप लिखेंगे 12. परन्तु अगर आपकी गिनती में 0 से 7 तक अंक ही होते तो आप लिखते 14(ऊपर लिखी गिनती के अनुसार गिनिए) . <br />
गिनती लिखने का system(Counting system) जिसमे 0 से 9 तक कुल 10 अंक होते हैं, decimal system(आधार 10,base 10) कहते हैं. ऊपर हमने 0 से 7 तक कुल 8 अंको का use करके जिस system का use करके गिनती लिखी उसे octal system(आधार 8,base 8) कहते हैं.<br />
अब स्थानीय मान को याद करते हैं जो कि बचपन में पढ़ा था. चूँकि हमारे Counting system 12 को स्थानीय मान के रूप में इस तरह लिखेंगे.<br />
12 = 2x10<sup>0</sup> + 1x10<sup>1</sup> = 10 + 2<br />
Octal system(आधार 8,base 8) में वही संख्या 14 थी. चूँकि octal system में 8 अंक ही होते हैं इसलिए इसे स्थानीय मान के रूप में इस तरह लिखेंगे.<br />
14 = 4x8<sup>0</sup> + 1x8<sup>1</sup> = 8 + 4<br />
संख्या वही है परन्तु अलग अलग system में लिखने का तरीका अलग है. * उतने ही हैं, परन्तु decimal system में उसे 12 और octal system में उसे 14 लिखेंगे.<br />
<br />
इसी तरह अगर आपके पास 0 से 3 तक कुल 4 अंक ही होते तो गिनती इस तरह लिखी जाती. 0,1,2,3,10,11,12,13,20,21,22,23,30,31,32,33,100,101,102 ... इसे Quaternary system(आधार 4,base 4)कहते हैं. इस गिनती में ऊपर दिए गए * की संख्या को 30 लिखते.<br />
<br />
<table border="1" style="border-collapse: collapse;">
<tbody>
<tr><td>गिनने का तरीका ꜜ</td> <td>*</td><td>*</td><td>*</td><td>*</td><td>*</td><td>*</td><td>*</td><td>*</td><td>*</td><td>*</td><td>*</td><td>*</td></tr>
<tr><td>decimal(0-9 अंक)</td><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td><td>8</td><td>9</td><td>10</td><td>11</td><td>12</td></tr>
<tr><td>0-7 अंक</td> <td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td><td>10</td><td>11</td><td>12</td><td>13</td><td>14</td></tr>
<tr><td>0-3 अंक</td> <td>1</td><td>2</td><td>3</td><td>10</td><td>11</td><td>12</td><td>13</td><td>20</td><td>21</td><td>22</td><td>23</td><td>30</td></tr>
</tbody></table>
अगर अगल-बगल में अलग अलग base वाली गिनती लिखें तो यह पता चलेगा कि 11(base 10) को base 8 में 13, base 4 में 23 लिखा जायेगा. आप जानते ही हैं कि सामान्यता लिखने के लिए हम base 10 वाली संख्याए use करते हैं. किसी अन्य base वाली संख्या लिखते समय उसके नीचे base भी लिख देते हैं. जैसे कि base 8 में 75 = (75)<sub>8</sub>, base 4 में 123 = (123)<sub>4</sub><br />
किसी भी base वाली संख्या को base 10 वाली संख्या में convert करना<br />
इसके लिए उस संख्या को स्थानीय मान के रूप में लिखकर जोड़ दें. नीचे example दिए जा रहे हैं.<br />
(75)<sub>8</sub> = 5x8<sup>0</sup> + 7x8<sup>1</sup> = 5x1 + 7x8 = 61 (base 10 में)<br />
(123)<sub>4</sub> = 3x4<sup>0</sup> + 2x4<sup>1</sup> + 1x4<sup>2</sup> = 3x1+ 2x4 + 1x16 = 27 (base 10 में)<br />
base 10 वाली संख्या को किसी अन्य base वाली संख्या में convert करना<br />
जिस base में convert करना है उस संख्या से भाग दें. शेष जो बचेगा वह इकाई का अंक हो जायेगा. भागफल को फिर base से भाग दें. नया शेष जो बचेगा वह दहाई का अंक हो जायेगा. नए भागफल को फिर से base से भाग दें. यह process तब तक करते रहे जब तक भागफल 0 न आ जाये. नीचे कुछ example दिए जा रहे हैं.<br />
98 को base 8 में बदलने का example:<br />
98/8 = 12(भागफल),2(शेष) ---> 2(इकाई)<br />
12/8 = 1(भागफल), 4(शेष) ---> 4(दहाई)<br />
1/8 = 0(भागफल), 1(शेष) --> 1 (सैकडा)<br />
इसलिए 98 = (142)<sub>8</sub><br />
98 को base 4 में बदलने का example:<br />
98/4 = 24(भागफल),2(शेष) ---> 2(इकाई)<br />
24/4 = 6(भागफल), 0(शेष) ---> 0(दहाई)<br />
6/4 = 1(भागफल), 2(शेष) --> 2 (सैकडा)<br />
1/4 = 0(भागफल), 1(शेष) --> 1(हज़ार)<br />
इसलिए 98 = (1202)<sub>4</sub><br />
Binary Number System: अगर हमारे पास सिर्फ 2 अंक ही होते - 0 और 1, तो हम गिनती कुछ इस तरह लिखते: <br />
0,1,10,11,100,101,110,111,1000,1001,1010,1011,1100 ... इस system को जिसमे सिर्फ 2 अंक ही होते हैं, Binary system(द्विआधारी) कहते हैं.<br />
98 को base 2 में बदलने का example:<br />
98/2 = 49(भागफल),0(शेष) ---> 0(इकाई)<br />
49/2 = 24(भागफल), 1(शेष) ---> 1(दहाई)<br />
24/2 = 12(भागफल), 0(शेष) --> 0(सैकडा)<br />
12/2 = 6(भागफल), 0(शेष) --> 0(हज़ार)<br />
6/2 = 3(भागफल), 0(शेष) --> 0(दस हज़ार)<br />
3/2 = 1(भागफल), 1(शेष) --> 1(लाख)<br />
1/2 = 0(भागफल), 1(शेष) --> 1(दस लाख)<br />
इसलिए 98 = (1100010)<sub>2</sub><br />
1100010 को base 10 में बदलने का example:<br />
(1100010)<sub>2</sub> = 0x2<sup>0</sup> + 1x2<sup>1</sup> + 0x2<sup>2</sup> + 0x2<sup>3</sup> + 0x2<sup>4</sup> + 1x2<sup>5</sup> + 1x2<sup>6</sup> = 0+2+0+0+0+32+64 = 98<br />
logical operators के बारे में अगले भाग में जानेंगे. </div>
<div style="text-align: center;">
<a href="http://programming-tutorial-hindi.blogspot.com/">Index</a><span style="text-align: center;"> </span><a href="http://programming-tutorial-hindi.blogspot.in/2012/07/logical-comparision-operator.html">Next(Logical,comparison operators)</a>
</div>
<iframe allowtransparency="true" frameborder="0" scrolling="no" src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.facebook.com%2F%23%21%2Fpages%2Fkampyutara-sikhe-aba-hindi-mem%2F157896914235350&layout=standard&show_faces=false&width=450&action=like&font=verdana&colorscheme=light&height=35" style="border: none; height: 35px; overflow: hidden; width: 450px;"></iframe></div>
Hridayesh Guptahttp://www.blogger.com/profile/09764967341453023170noreply@blogger.comtag:blogger.com,1999:blog-8683328491687928872.post-2891681392148893582011-11-20T19:53:00.001+05:302015-11-24T08:11:03.939+05:30arithmetic operators<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<b><span style="font-size: large;">Arithmetic Operators</span></b>
<br />
आज Hindi के इस C/C++ programming language tutorial को आगे बढ़ाते हुए हम arithmetic operators के बारे में और जानेंगे.
<br />
operator यानि संक्रिया. operator एक function की तरह ही है जिसे एक चिन्ह के द्वारा लिख सकते हैं, जो एक या एक से अधिक मान लेता है और output में एक मान देता है(एक मान return करता है) इनकी जानकारी नीचे दी गयी है.
<br />
<ol style="text-align: left;">
<li><span style="color: blue;">+</span>: यह दो संख्याए लेता है और उनका sum return करता है. जैसे कि 5+3 8 return करेगा, इसलिए a = 5+3 लिखने पर a की value 8 हो जाती है.</li>
<li><span style="color: blue;">-</span>:यह दो संख्याए लेता है और उनका difference return करता है.</li>
<li><span style="color: blue;">*</span>:यह दो संख्याए लेता है और उनका product return करता है.</li>
<li><span style="color: blue;">/</span>:यह दो संख्याए लेता है और उनका भागफल(divisor) return करता है.</li>
<li><span style="color: blue;">%</span>:यह दो संख्याए लेता है और उनका शेषफल(remainder) return करता है.</li>
</ol>
ऊपर दिए गए operator arithmetic operator हैं.
<br />
<pre class="brush: cpp">int a = 5;
int b = 3;
a + b;
</pre>
ऊपर दिए गए program की तीसरी line में a और b को add किया गया है जो कि 8 return करेगा. a और b की value change नहीं होंगी. return value 8 को हमने किसी variable में save नहीं किया इसलिए यह lost हो जायेगा. नीचे example में हमने उस return value को c में save किया है.
<br />
<pre class="brush: cpp">int a = 5;
int b = 3;
int c = a + b;
</pre>
= भी एक operator है जो अपने right side लिखे गए expression की value को left side में लिखे गए variable में डाल देता है. जैसे कि ऊपर c = a+b लिखने पर right side में a+b 8 देगा इसलिए c की value 8 हो जायेगी.
<br />
<pre class="brush: cpp">int a = 5;
int b = 3;
a = b;
</pre>
ध्यान दे कि ऊपर दिए गए example में a = b लिखने के बाद a में 3(b की value) आ जायेगा, b में 5 नहीं आएगा क्योंकि = अपने left side में लिखे variable में right side की value डालता है, इसका उल्टा नहीं करता. यह भी ध्यान दें कि left side में सिर्फ एक variable ही होना चाहिए, कुछ और नहीं. यदि a+b = c लिखेंगे तो c में a+b की value नहीं आएगी बल्कि program error दे देगा, क्योंकि left side में सिर्फ एक variable नहीं है बल्कि a+b है.
= में एक बात और है कि यह अपने right side के expression की value return भी करता है.
<br />
<pre class="brush: cpp">int a = 5;
int b = 3;
int c,d;
d = (c = a % b);//% शेषफल देता है.
</pre>
ऊपर दिए गए example में c = a%b करने से c की value 2 हो जायेगी और यह(c=a%b) 2 return भी करेगा इसलिए d = (c=a%b) लिखने से return value भी d में भी आ जायेगी.<br />
ऊपर दिए गए सभी operators जिस तरह का input(int,float etc.) लेते हैं उसी तरह का output देते हैं. जैसे कि अगर 30/8 करेंगे तो 3 आएगा. 30 और 8 दोनों int हैं इसलिए result भी int आएगा, दशमलव में मान नहीं आएगा, दशमलव के बाद वाले अंक हट जायेंगे. agar 30.0/8.0 करेंगे तो 3.75 आएगा.<br />
ऊपर दिए गे सभी operator, binary operator हैं. इसका मतलब यह हुआ कि वो 2 संख्याए लेते हैं. अब हम कुछ unary operators के बारे में जानेंगे जो सिर्फ एक ही संख्या लेते हैं.<br />
1. x++ और ++x : ये दोनों ही variable x की value 1 बढ़ा देते हैं. x++ x की पुरानी value return करेगा और ++x x की नयी value return करेगा. अगर सिर्फ x++ या ++x लिखना हो तब दोनों एक है हैं परन्तु यदि इनका use किसी expression में करना हो तब ध्यान रखना चाहिए. नीचे example से और स्पष्ट हो जायेगा.
<br />
<pre class="brush: cpp">int a = 5;
int b = a++;
int c = ++a;
int d = (b++) + (++c);
</pre>
ऊपर दिए गए program में दूसरी line run होने के बाद a की value 1 बढ़कर 6 हो जायेगी पर चूंकि a++ पुरानी value return करता है इसलिए b में 5 जायेगा. तीसरी line run होने के बाद a की value 6 से बढ़कर 7 हो जायेगी और c में 7 जायेगा क्योंकि ++a a की नयी value return करता है. चौथी line run होने के बाद b की value 5 से बढ़कर 6, c की value 7 से बढ़कर 8 हो जायेगी, d में 13 जायेगा(d = b की पुरानी value + c की बढ़ी हुई value = 5+8)<br />
x-- और --x भी ठीक इसी तरह कम करते हैं परन्तु यह x की value 1 कम करते हैं. ये दोनों operator(++ और --) सिर्फ int(पूर्णांक) के लिए हैं. float और double datatype में use करने पर error आएगी.<br />
<br /><div style="text-align: center;">
<a href="http://programming-tutorial-hindi.blogspot.com/">Index</a><span style="text-align: center;"> </span><a href="http://programming-tutorial-hindi.blogspot.com/2011/12/c-programming-hindi-tutorial-logical.html">Next(Binary Number System)</a>
</div>
<iframe allowtransparency="true" frameborder="0" scrolling="no" src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.facebook.com%2F%23%21%2Fpages%2Fkampyutara-sikhe-aba-hindi-mem%2F157896914235350&layout=standard&show_faces=false&width=450&action=like&font=verdana&colorscheme=light&height=35" style="border: none; height: 35px; overflow: hidden; width: 450px;"></iframe></div>
</div>
Hridayesh Guptahttp://www.blogger.com/profile/09764967341453023170noreply@blogger.comtag:blogger.com,1999:blog-8683328491687928872.post-14000860034806356052011-01-18T22:37:00.012+05:302012-07-19T08:57:49.191+05:30preprocessor c++ hindi programming tutorial<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="btn_sw" href="#" id="hi_btn" onclick="javascript:return showhindi(this);">
Read in Hindi</div>
<div class="btn_sw" href="#" id="en_btn" onclick="javascript:return showeng(this);">
Read in English</div>
<br />
<div id="eng">
Now we have learned basics of C, but yet we do not know why we #include <stdio.h> is written on the 1st line of every program. Today we will know things related to this.<br />
<br />
In the beginning we learned that after we write a program, computer converts it to machine language. That process is called compilation. Compilation process goes through many steps. Today we will learn about those steps then why we write #include <stdio.h> in the beginning of every program.<br />
<br />
We know that printf is a function. This function is defined(declared) in stdio.h file. When program is compiled, in 1st step compiler replaces #include <stdio.h> with content of stdio.h file. This process is called pre-processing. By doing this program gets to know definition of printf function, hence can run this function successfully otherwise it will give compile error that printf function not defined.<br />
<br />
stdio means Standard Input and Output. In this file(stdio.h) all function used for input/output are defined. printf used to print output on console whereas scanf used to read input from console. Both functions are declared in stdio.h file.<br />
<iframe allowtransparency="true" frameborder="0" scrolling="no" src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.facebook.com%2F%23%21%2Fpages%2Fkampyutara-sikhe-aba-hindi-mem%2F157896914235350&layout=standard&show_faces=false&width=450&action=like&font=verdana&colorscheme=light&height=35" style="border: none; height: 35px; overflow: hidden; width: 450px;"></iframe><br />
<h3>
Pre-processing</h3>
In a program any line starting with # is called pre-processor. In 1st step of compilation process compiler modifies them.<b> #include</b> is a pre-processing directive which replaces it with content file given. There are some more pre-processing directive which are described below.<br />
<br />
<b>#define ABC 1</b><br />
#define is used to define constant. If #define ABC 1 is present in program, in 1st step of compilation ABC will be replaced with 1 in the program. #define is called macro too.<br />
<br />
<b>#define SUM(a,b) (a+b)</b><br />
This is also a macro which can take parameter. If use this in program, SUM(x,y) will be replaced with (x+y) in the program.<br />
<br />
<b>#ifdef xyz</b><br />
...<br />
<b>#endif</b><br />
If xyz is defined anywhere using #define then program between #ifdef and #endif will be compiled otherwise it will be removed.<br />
<br />
<b>Note: </b>One pre-processor should be written in one line only and nothing else in that line.<br />
We are seeing #include from beginning. Below we will see example of some other pre-processor.<br />
<br />
<pre><span class="pre">#include <stdio.h>
#define AREA(r) (PI*r*r)
#define PI 3.14159
</span><span class="type">
int</span><span class="keyword"> main</span><span class="operator">() {</span><span class="type">
int</span> rad<span class="operator"> =</span><span class="int"> 10</span><span class="operator">;</span><span class="type">
float</span> area<span class="operator"> =</span> AREA<span class="operator">(</span>rad<span class="operator">);</span>
printf<span class="operator">(</span><span class="string">"Area of circle is %f\n"</span><span class="operator">,</span> area<span class="operator">);</span>
scanf<span class="operator">(</span><span class="string">"%d"</span><span class="operator">, &</span>r<span class="operator">);</span><span class="flow">
return</span><span class="int"> 0</span><span class="operator">;
}</span>
</pre>
<br />
In the program given above AREA(rad) will be replaced with (PI*rad*rad) and then in this PI will be replaced with 3.14159. Note that it is not same as function call. In function call return value should be assigned to area but here AREA(rad) is replaced with (3.14159*rad*rad) and answer is calculated here itself without any function call, that is assigned to area, but effect is same as function call.<br />
<div align="center">
<a class="fb_share_button" href="http://www.facebook.com/share.php?u=http://programming-tutorial-hindi.blogspot.com/" onclick="return fbs_click()" rel="nofollow" style="text-decoration: none;" target="_blank">Share</a></div>
</div>
<div id="hindi">
हमने C का basic जान लिया है, परन्तु #include <stdio.h> क्यों लिखते हैं ये नहीं जाना. आज इस यह और इससे related बाते जानेंगे.<br />
<br />
शुरू में हमने पढ़ा था कि हम program लिखते हैं, उसके बाद computer उसे machine की भाषा में बदलता है. इस प्रक्रिया(process) को कहते हैं program को compile करना. यह compile करने कि प्रक्रिया कई चरणों(steps) में होती है. आज हम इसके पहले step को जानेंगे. इसके साथ ही हम यह भी जान लेंगे कि हर program के शुरू में #include <stdio.h> क्यों लिखते हैं.<br />
<br />
हम जानते हैं कि printf एक function है. यह function stdio.h नाम की file में defined (declared) है. जब program को compile करते हैं तो पहले step में compiler #include <stdio.h> को हटाकर उसकी जगह stdio.h file का content डाल देता है. इस process को pre-processing कहते हैं. ऐसा करने से program को यह पता चल जाता है कि printf function कहाँ किस तरह से defined है और उसे किस तरह से run करना है. अन्यथा error आ जायेगी कि printf function नहीं मिल रहा.<br />
<br />
stdio का मतलब है Standard Input and Output. इस file(stdio.h) में वो सब function defined हैं जो input/output लेने के काम आते हैं. printf output को print करने के काम आता है जबकि scanf input लेने के काम आता है. दोनों ही function stdio.h file में defined हैं.<br />
<iframe allowtransparency="true" frameborder="0" scrolling="no" src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.facebook.com%2F%23%21%2Fpages%2Fkampyutara-sikhe-aba-hindi-mem%2F157896914235350&layout=standard&show_faces=false&width=450&action=like&font=verdana&colorscheme=light&height=35" style="border: none; height: 35px; overflow: hidden; width: 450px;"></iframe><br />
<h3>
Pre-processing</h3>
किसी भी program में जो line # से start होती हैं उन्हें pre-processor कहते हैं. Compiler पहले step में इन्हें process करके program को modify कर देता है. जैसा कि ऊपर बताया गया है.<b> #include</b> एक pre-processing directive हैं जो उसके बाद लिखे गए file के content को उसकी जगह लिख देता है. कुछ और भी pre-processing directive होते हैं जिनके बारे में नीचे बताया गया है.<br />
<br />
<b>#define ABC 1</b><br />
#define किसी भी constant को define करने के लिए उपयोग में लाया जाता है. #define ABC 1 लिखने पर जहाँ जहाँ program में ABC लिखा होगा उसकी जगह 1 compiler पहले step में 1 लिख देगा. #define को macro भी कहते हैं.<br />
<br />
<b>#define ADD4(a) (a+4)</b><br />
यह भी एक macro है जो parameter ले सकता है. इसका use करने पर program compile करने के 1st step में जहाँ भी ADD4(x) लिखा होगा वहां (x+4) लिख जायेगा जहाँ x कुछ भी हो सकता है, variable या कुछ और. जैसे कि अगर ADD4(5) लिखा है तो उसकी जगह (5+4) हो जायेगा.<br />
<b>#ifdef xyz</b><br />
...<br />
<b>#endif</b><br />
अगर हमने #define का use करके xyz कहीं define किया है तो #ifdef और #endif के बीच में लिखा हुआ program compile होगा अन्यथा दोनों के बीच लिखा हुआ program हटा दिया जायेगा.<br />
<br />
<b>Note: </b>एक pre-processor एक line में ही लिखा जाता है. एक line में एक से ज्यादा pre-processor या एक pre-processor एक से ज्यादा line में नहीं लिख सकते, अन्यथा program नहीं चलेगा.<br />
#include का example हम शुरू से देखते आये हैं. नीचे अन्य pre-processor commands के example भी देखते हैं.<br />
<br />
<pre class="brush: cpp">#include <stdio.h>
#define AREA(r) (PI*r*r)
#define PI 3.14159
int main() {
int rad = 10;
float area = AREA(rad);
printf("Area of circle is %f\n", area);
scanf("%d", &r);
return 0;
}
</pre>
<br />
ऊपर दिए गए program में AREA(rad) की जगह (PI*rad*rad) और फिर इसकी जगह (3.14159*rad*rad) हो जायेगा. ध्यान दे कि यह function call करने के बराबर नहीं है. function call करने में function द्वारा return की हुई value area को मिल जाती पर यहाँ पर AREA(rad) कि जगह (3.14159*rad*rad) लिख दिया जाता है और बिना कोई function call किये यहीं से जो value calculate होती है वो area को मिल जाती है.<br />
<div align="center">
<a class="fb_share_button" href="http://www.facebook.com/share.php?u=http://programming-tutorial-hindi.blogspot.com/" onclick="return fbs_click()" rel="nofollow" style="text-decoration: none;" target="_blank">Share</a></div>
अगर आपको यह लेख पसंद आया हो तो अपने दोस्तों को भी बताएं क्योंकि ये उनके लिए भी उपयोगी सिद्ध हो सकता है!! नीचे दिए गए link के माध्यम से इसे आसानी से facebook twitter और Google Buzz पर भी Share कर सकते हैं.</div>
<div style="text-align: center;">
<a href="http://programming-tutorial-hindi.blogspot.com/">Index</a> <a href="http://programming-tutorial-hindi.blogspot.com/2011/11/operators.html">Next(Arithmetical Operators)</a></div>
</div>
Hridayesh Guptahttp://www.blogger.com/profile/09764967341453023170noreply@blogger.comtag:blogger.com,1999:blog-8683328491687928872.post-7046425179504803212011-01-12T09:20:00.000+05:302015-11-24T08:11:27.654+05:30void pointer hindi programming language<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-size: large;"><b>void pointer</b></span><br />
अभी तक हमने जाना कि किसी भी pointer को define करते समय यह बताना पड़ता है कि वह किस तरह के variable का address store करेगा. int* p में p int का address store करेगा. void pointer वह होता है जो किसी भी तरह के variable का address store कर सकता है. इसलिए जब void pointer से value पढते हैं तो उस समय यह बताना पड़ता है कि वह value हम int की तरह पढ़ना चाहते हैं या char या float...<br />
इसे हम एक example की help से समझते हैं.<br />
<br />
<pre class="brush: cpp">#include <stdio.h>
int main() {
void* p;
int i = 65;
p = &i;
printf("int value = %d\n", (int) *p);
char c = 'h';
p = &c;
printf("char value = %c\n", (char) *p);
scanf("%d", &i);
return 0;
}
</pre>
ऊपर दिए गए example में जो pointer define किया गया है वो int* p से न करके void* p से किया गया है. इसलिए यह pointer किसी भी datatype के variable का address store कर सकता है. ऊपर दिए गए program में पहले हमने int का address store किया है फिर char का. जब हम int pointer से value read करते हैं तो program को पता होता है कि इसमें value भी int होगी, पर जब void pointer से value पढ़ते हैं तो program को पता नहीं होता कि इसमें किस datatype कि value है इसलिए हमें यह पता होना चाहिए की उसमे किस datatype की value stored हैं जिससे कि हम पढ़ी गयी value को उस datatype में typecast कर सकें. इसीलिए ऊपर दिए गए example में जब हमने void pointer में int का address store किया है तो उसे print करते समय (int) *p के द्वारा stored value को int में बदला गया(typecast) है. ध्यान दें यहाँ *p से value पढ़ी गयी है जैसा कि हमने pointer वाले पाठ में भी देखा था और उसके पहले (int) लगाने से पढ़ी गयी value int में बदल गयी. इसी तरह char के साथ भी किया गया है.
void pointer में किसी struct का address भी store कर सकते हैं, और उससे value पढ़ते समय वापस struct में typecast कर सकते हैं.<br />
<br />
<span style="font-size: large;"><b>malloc and free</b></span><br />
आपने pointer के बारे में पढ़ा होगा। जब कोई भी variable define करते हैं तो वह memory में चला जाता है(अर्थात उस variable के लिए memory allocate होती है) और pointer का use करके उस memory का address ले सकते हैं. memory allocate करने का दूसरा तरीका है malloc.<br />
malloc(4) call करने पर 4 byte memory allocate हो जाएगी। और उसका address return हो जायेगा. आपको याद होगा की int 4 byte जगह लेता है, इसलिए malloc(4) call करने पर एक int के लिए memory मिल जाएगी.<br />
malloc एक memory का address देता है उसमे किसी भी type का data रख सकते हैं चाहे int हो या float string double char array कुछ भी, इसलिए malloc void pointer (void*) return करता है. उसका
use int store करने के लिए इस तरह करेंगे.<br />
<pre class="brush: cpp">int* i = (int*) malloc(4); //malloc void* देता है. उसमे int रखने के लिए int pointer(int*)में typecast किया गया
*i = 1;
printf("%d",*i);
</pre>
अलग अलग system में int अलग अलग memory भी ले सकता है, जरुरी नहीं की 4 byte ही ले. C में एक function होता है sizeof जो यह बता देता है की कोई datatype कितनी जगह लेगा.<br />
<br />
जैसे sizeof(int) 4 देगा(अगर आपके system में int 4 byte ले रहा है).इसलिए हम int के लिए memory लेने के लिए malloc(sizeof(int )) use करते हैं.अगर 10 int store करने की जगह चाहिए तो इस तरह कर सकते हैं.<br />
<pre class="brush: cpp">int* a = (int*) malloc(10*sizeof(int));
int i;
for(i=0;i<10;i++) *(a+i) = 12;// *(a+0), *(a+1), *(a+2), *(a+3) ... etc
</pre>
यहाँ पर malloc(10*sizeof(int)) 10 int की memory बना देगा और उस memory के पहले byte का address दे देगा. इसके बाद हम पहली byte(a) पर पहला int store करेंगे. दूसरा int अगली byte यानि (a+1) पर store होगा. (a+1) सिर्फ अगली byte का address होगा. उस address पर value store/read करने के लिए *(a+1) का use होगा(आपको याद होगा कि address(pointer) p पर value store/read करने के लिए *p का use करते हैं)<br />
<br />
अगर ये सारे user से input में लेने हैं तो इस तरह <br />
<pre class="brush: cpp">int* a = (int*) malloc(10*sizeof(int));
int i;
for(i=0;i<10;i++) scanf("Please enter number: %d", a+i);// *(a+0), *(a+1), *(a+2), *(a+3) ... etc
</pre>
जब आप कोई variable define करते हैं तो उसके लिए अपने आप memory बनती है. जब आप उस variable का use बंद कर देते हैं तो वह memory अपने आप free हो जाती है. पर अगर आपने malloc का use करके memory ली है तो वह कभी अपने आप free नहीं होगी. उसे free करने के लिए आप free call करना पड़ेगा. for example <br />
<pre class="brush: cpp">int* a = (int*) malloc(10*sizeof(int));
//use memory
free(a);
</pre>
अगर memory free नहीं करते तो आपका program memory(RAM) लेता ही जायेगा. अगर बहुत सारी memory ले ली तो आपका system slow या hang भी हो सकता है.जब वह program बंद होता तो उसके द्वारा ली गयी सारी memory अपने आप free हो जाती है.<br />
<br /><div style="text-align: center;">
<a href="http://programming-tutorial-hindi.blogspot.com/">Index</a> <a href="http://programming-tutorial-hindi.blogspot.com/2011/01/preprocessor-c-hindi-programming.html">Next(Preprocessor)</a></div>
</div>
Hridayesh Guptahttp://www.blogger.com/profile/09764967341453023170noreply@blogger.comtag:blogger.com,1999:blog-8683328491687928872.post-21769341624759930662011-01-06T09:50:00.005+05:302015-11-24T08:11:48.214+05:30c programming hindi tutorial arraypointer<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="btn_sw" href="#" id="hi_btn" onclick="javascript:return showhindi(this);">
Read in Hindi</div>
<div class="btn_sw" href="#" id="en_btn" onclick="javascript:return showeng(this);">
Read in English</div>
<br />
<div id="eng">
<h3>
Array and pointer</h3>
Going forward with this C/C++ programming language tutorial we will understand how to access array as a pointer.<br />
We define array as shown below.<br />
int A[10];<br />
A[0] = 0; A[1] = 10; A[2] = 20; ...<br />
Here A is pointer as well. A contains address of 1st int, hence A+1 contains address of 2nd int, A+2 contains address of 3rd int.<br />
Since A is address hence *A will give 0(A[0] = 0 as defined above), value of *(A+1) will be 10, *(A+2) will be 20. Note that *(A)+1 and *(A+1) is not same. *(A)+1 means add 1 to value stored to address stored in A, but *(A+1) means value stored at the address next to A. So now to access value at any index(n) at array we have 2 ways. A[n] और *(A+n)<br />
Lets see an example.<br />
<pre class="brush: cpp">#include <stdio.h>
int main() {
int A[] = {1,2,3,4};
printf("[%d, %d, %d, %d]\n",A[0], A[1], A[2], A[3]);
*A = 10;
*(A+1) = 20;
*(A+2) = 30;
*(A+3) = 40;
printf("[%d, %d, %d, %d]\n",A[0], A[1], A[2], A[3]);
A[0] = 0; A[1] = 2; A[2] = 4; A[3] = 6;
printf("[%d, %d, %d, %d]\n", *A, *(A+1), *(A+2), *(A+3));
scanf("%d", A);
return 0;
}
</pre>
Run the program to see the output.</div>
<div id="hindi">
<h3>
Array and pointer</h3>
आज Hindi के इस C/C++ programming language tutorial को आगे बढ़ाते हुए हम array को pointer के तरीके से जानेंगे.<br />
हम नीचे दिए गए तरीके से array define करते हैं.<br />
int A[10];<br />
A[0] = 0; A[1] = 10; A[2] = 20; ...<br />
इसमें A एक pointer ही होता है. A में array के पहली position वाले int का address store रहता है. A पहली position वाले int का address है, इसलिए A+1 दूसरी position वाले int का address हो जायेगा, A+2 तीसरी position वाले int का...<br />
चूंकि A address है इसलिए *A का मान 0 आएगा(ऊपर A[0] = 0 है), *(A+1) का मान 10 आएगा, *(A+2) का मान 20 आएगा. ध्यान दे कि *(A)+1 और *(A+1) एक ही नहीं हैं. *(A)+1 का मतलब है A में जहाँ का address है उस position पर stored value पर 1 जोड़ना, जबकि *(A+1) का मतलब है A में जहाँ का address है उस position एक आगे वाली position पर stored value. इस तरह हमारे पास किसी array के किसी position(index) पर value को access करने के दो तरीके हैं. A[n] और *(A+n)<br />
इसका एक example देखते हैं.<br />
<pre class="brush: cpp">#include <stdio.h>
int main() {
int A[] = {1,2,3,4};
printf("[%d, %d, %d, %d]\n",A[0], A[1], A[2], A[3]);
*A = 10;
*(A+1) = 20;
*(A+2) = 30;
*(A+3) = 40;
printf("[%d, %d, %d, %d]\n",A[0], A[1], A[2], A[3]);
A[0] = 0; A[1] = 2; A[2] = 4; A[3] = 6;
printf("[%d, %d, %d, %d]\n", *A, *(A+1), *(A+2), *(A+3));
scanf("%d", A);
return 0;
}
</pre>
इसको run करके output का अध्ययन करें.</div>
<div style="text-align: center;">
<a href="http://programming-tutorial-hindi.blogspot.com/">Index</a> <a href="http://programming-tutorial-hindi.blogspot.com/2012/07/void-pointer-hindi-programming-language.html">Next(void pointer)</a></div>
</div>
Hridayesh Guptahttp://www.blogger.com/profile/09764967341453023170noreply@blogger.comtag:blogger.com,1999:blog-8683328491687928872.post-27675355434101636092010-12-30T00:12:00.011+05:302015-11-24T08:12:05.141+05:30c programming hindi tutorial pointer-arg<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="btn_sw" href="#" id="hi_btn" onclick="javascript:return showhindi(this);">
Read in Hindi</div>
<div class="btn_sw" href="#" id="en_btn" onclick="javascript:return showeng(this);">
Read in English</div>
<br />
<div id="eng">
Going forward with this C/C++ programming language tutorial we will know more about pointer.<br />
See the example given below and think for a while what it will print. you may wonder to see the actual output.<br />
<br />
<pre><span class="pre">#include <stdio.h>
</span><span class="type">
void</span> add1<span class="operator">(</span><span class="type">int</span> i<span class="operator">) {</span>
i<span class="operator"> =</span> i<span class="operator"> +</span><span class="int"> 1</span><span class="operator">;
}</span><span class="type">
int</span><span class="keyword"> main</span><span class="operator">() {</span><span class="type">
int</span> x<span class="operator"> =</span><span class="int"> 5</span><span class="operator">;</span>
printf<span class="operator">(</span><span class="string">"before adding x = %d \n"</span><span class="operator">,</span> x<span class="operator">);</span>
add1<span class="operator">(</span>x<span class="operator">);</span>
printf<span class="operator">(</span><span class="string">"after adding x = %d \n"</span><span class="operator">,</span> x<span class="operator">);</span>
scanf<span class="operator">(</span><span class="string">"%d"</span><span class="operator">, &</span>x<span class="operator">);</span><span class="flow">
return</span><span class="int"> 1</span><span class="operator">;
}</span>
</pre>
<br />
It will give the following output when run.<br />
<pre>before adding x = 5
after adding x = 5
</pre>
Now lets see why it gives this output. If you see add1 function, it is increasing value of parameter passed to it. In main value of x is 5 hence 1st time it prints x = 5 print. Then x is passed to add1 function, hence value of x should be increased by 1. but its value is still 5 hence 5 is printed again not 6.<br />
<br />
Reason for this is that when ad1 function was called, it made separate copy of parameter x. add1 function changed value of copy, not original hence original x is still same.<br />
<br />
When a is function called, a separate copy is made for all the parameters passed to it and that copy is given to called function. hence if called function changes values of parameters, it changes only copy, not original parameters.<br />
<br />
Now see the C++ program given below which uses pointer and think what will be output.<br />
<pre><span class="pre">#include <stdio.h>
</span><span class="type">
void</span> add1<span class="operator">(</span><span class="type">int</span><span class="operator">*</span> i<span class="operator">) {
*</span>i<span class="operator"> = *</span>i<span class="operator"> +</span><span class="int"> 1</span><span class="operator">;
}</span><span class="type">
int</span><span class="keyword"> main</span><span class="operator">() {</span><span class="type">
int</span> x<span class="operator"> =</span><span class="int"> 5</span><span class="operator">;</span>
printf<span class="operator">(</span><span class="string">"before adding x = %d \n"</span><span class="operator">,</span> x<span class="operator">);</span>
add1<span class="operator">(&</span>x<span class="operator">);</span>
printf<span class="operator">(</span><span class="string">"after adding x = %d \n"</span><span class="operator">,</span> x<span class="operator">);</span>
scanf<span class="operator">(</span><span class="string">"%d"</span><span class="operator">, &</span>x<span class="operator">);</span><span class="flow">
return</span><span class="int"> 1</span><span class="operator">;
}</span>
</pre>
It will give the output -<br />
<pre>before adding x = 5
after adding x = 6
</pre>
Now lets understand this why value value of x changes after function call. Here add1 function does not take int variable but address of int variable. It goes to that address that increases value stored at that address.<br />
In main when add1 function is called, address of x is passed to it.As explained above, it will make copy of address of x that will be given to add1 function. If add1 function changes address given to it then value of x will not change but this function changes value stored in that address. Be it original address or copy of address, it will remain address of given variable, hence if we change value stored in address then value of original variable will also be changed<br />
Later we will know more about pointer.</div>
<div id="hindi">
आज Hindi के इस C/C++ programming language tutorial को आगे बढ़ाते हुए हम pointer के बारे में और जानेंगे.<br />
नीचे दिए गए इस example को देखिये और सोचिये कि यह क्या print करेगा, आपको इसका actual output देखकर आश्चर्य होगा.<br />
<br />
<pre><span class="pre">#include <stdio.h>
</span><span class="type">
void</span> add1<span class="operator">(</span><span class="type">int</span> i<span class="operator">) {</span>
i<span class="operator"> =</span> i<span class="operator"> +</span><span class="int"> 1</span><span class="operator">;
}</span><span class="type">
int</span><span class="keyword"> main</span><span class="operator">() {</span><span class="type">
int</span> x<span class="operator"> =</span><span class="int"> 5</span><span class="operator">;</span>
printf<span class="operator">(</span><span class="string">"before adding x = %d \n"</span><span class="operator">,</span> x<span class="operator">);</span>
add1<span class="operator">(</span>x<span class="operator">);</span>
printf<span class="operator">(</span><span class="string">"after adding x = %d \n"</span><span class="operator">,</span> x<span class="operator">);</span>
scanf<span class="operator">(</span><span class="string">"%d"</span><span class="operator">, &</span>x<span class="operator">);</span><span class="flow">
return</span><span class="int"> 1</span><span class="operator">;
}</span>
</pre>
<br />
run करने पर इसका output यह आएगा.<br />
<pre>before adding x = 5
after adding x = 5
</pre>
अब समझते हैं कि यह output क्यों आ रहा है. अगर आप add1 function देखेंगे तो उसमे जो parameter(argument) pass किया जाता है उसकी value 1 बढ़ा रहा है. main में पहले x की value 5 है इसलिए पहली बार x = 5 print हुआ है. उसके बाद add1 function में x को pass किया है इसलिए x की value 1 बढ़ जाना चाहिए परन्तु उसकी value 5 ही है और इसीलिए बाद में भी x = 5 ही print हो रहा है.<br />
<br />
इसका कारण यह है कि जब add1 function call हुआ तो उसमे pass किये गए variable x की एक अलग copy बन गयी, add1 function ने उस copy की value change की है, इसलिए original x की value change नहीं हुई.<br />
<br />
जब भी कोई function call होता है उसमे pass किये गए variable की copy बन जाती है और वह copy call किये गए function को दी जाती है. इसलिए call किया गया function अगर arguments की value change करता है तो original variable की value change नहीं होती, copy की value change होती है.<br />
<br />
अब नीचे वाला C++ program देखिये जो pointer का use करके लिखा गया है और सोचिये कि इसका output क्या होगा<br />
<pre><span class="pre">#include <stdio.h>
</span><span class="type">
void</span> add1<span class="operator">(</span><span class="type">int</span><span class="operator">*</span> i<span class="operator">) {
*</span>i<span class="operator"> = *</span>i<span class="operator"> +</span><span class="int"> 1</span><span class="operator">;
}</span><span class="type">
int</span><span class="keyword"> main</span><span class="operator">() {</span><span class="type">
int</span> x<span class="operator"> =</span><span class="int"> 5</span><span class="operator">;</span>
printf<span class="operator">(</span><span class="string">"before adding x = %d \n"</span><span class="operator">,</span> x<span class="operator">);</span>
add1<span class="operator">(&</span>x<span class="operator">);</span>
printf<span class="operator">(</span><span class="string">"after adding x = %d \n"</span><span class="operator">,</span> x<span class="operator">);</span>
scanf<span class="operator">(</span><span class="string">"%d"</span><span class="operator">, &</span>x<span class="operator">);</span><span class="flow">
return</span><span class="int"> 1</span><span class="operator">;
}</span>
</pre>
run करने पर इसका output यह आएगा.<br />
<pre>before adding x = 5
after adding x = 6
</pre>
अब इसे समझते हैं कि यहाँ function call करने पर x की value बढ़ क्यों गयी. यहाँ add1 function int नहीं लेता बल्कि int variable का address लेता है, और उस address पर जो भी value होती है उसे 1 बढ़ा देता है. <br />
main में जब add1 function को call किया है तो उसमे x का address pass किया है. ऊपर बताये गए अनुसार x के address की एक copy बनेगी जो add1 function को दी जायेगी. अगर add1 function address को change करता तो x की value change नहीं होती पर add1 function उस address में <b>store variable</b> की value change कर रहा है. चाहे वह original address हो या address की copy, address तो उसी variable का ही रहेगा, इसलिए अगर हम उस address पर store variable को change करते हैं original variable भी change हो जायेगा.<br />
<br />
अभी pointer के बारे में और जानना भी बाकी है. इसलिए आगे के लेखों का wait करें.<br />
<br /></div>
<div style="text-align: center;">
<a href="http://programming-tutorial-hindi.blogspot.com/">Index</a><a href="http://programming-tutorial-hindi.blogspot.com/2011/01/c-programming-hindi-tutorial.html"> Next(Array and pointer)</a></div>
</div>
Hridayesh Guptahttp://www.blogger.com/profile/09764967341453023170noreply@blogger.comtag:blogger.com,1999:blog-8683328491687928872.post-23935681598805157042010-12-11T13:05:00.009+05:302015-11-24T08:12:17.966+05:30struct pointer<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="btn_sw" href="#" id="hi_btn" onclick="javascript:return showhindi(this);">
Read in Hindi</div>
<div class="btn_sw" href="#" id="en_btn" onclick="javascript:return showeng(this);">
Read in English</div>
<br />
<div id="eng">
Going forward with this C/C++ programming language tutorial today we will know more about pointer.<br />
Last time we have seen example of int pointer, but pointer can be of any datatype. It can even be struct. Lets see example of struct pointer here. If you do not know how to use struct then Read this <a href="http://programming-tutorial-hindi.blogspot.com/2010/11/c-hindi-tutorial-structurestruct.html">C/C++ struct</a>. <br />
<pre><span class="pre">#include <stdio.h>
</span><span class="keyword">
struct</span> rectangle<span class="operator"> {</span><span class="type">
int</span> width<span class="operator">;</span><span class="type">
int</span> length<span class="operator">;
};</span><span class="type">
int</span><span class="keyword"> main</span><span class="operator">() {</span><span class="keyword">
struct</span> rectangle r1<span class="operator">;</span>
r1<span class="operator">.</span>width<span class="operator"> =</span><span class="int"> 10</span><span class="operator">;</span>
r1<span class="operator">.</span>length<span class="operator"> =</span><span class="int"> 15</span><span class="operator">;</span><span class="keyword">
struct</span> rectangle<span class="operator">*</span> r2<span class="operator">;</span>
r2<span class="operator"> = &</span>r1<span class="operator">;</span>
printf<span class="operator">(</span><span class="string">"Original width=%d, length=%d\n"</span><span class="operator">, (*</span>r2<span class="operator">).</span>width<span class="operator">, (*</span>r2<span class="operator">).</span>length<span class="operator">);</span>
r1<span class="operator">.</span>width<span class="operator"> =</span><span class="int"> 20</span><span class="operator">;</span>
r1<span class="operator">.</span>length<span class="operator"> =</span><span class="int"> 25</span><span class="operator">;</span>
printf<span class="operator">(</span><span class="string">"r1 changed, width=%d, length=%d\n"</span><span class="operator">, (*</span>r2<span class="operator">).</span>width<span class="operator">, (*</span>r2<span class="operator">).</span>length<span class="operator">);
(*</span>r2<span class="operator">).</span>width<span class="operator"> =</span><span class="int"> 5</span><span class="operator">;
(*</span>r2<span class="operator">).</span>length<span class="operator"> =</span><span class="int"> 10</span><span class="operator">;</span>
printf<span class="operator">(</span><span class="string">"*r2 changed, width=%d, length=%d\n"</span><span class="operator">,</span> r1<span class="operator">.</span>width<span class="operator">,</span> r1<span class="operator">.</span>length<span class="operator">);</span><span class="flow">
return</span><span class="int"> 0</span><span class="operator">;
}</span>
</pre>
Run the example given above, we will understand output here. In main we have defined variable r1 of type struct rectangle 1st and declared pointer r2 which can store address of any struct rectangle. We have assigned address of r1 in that pointer. Now r1 is a variable and r2 is a pointer which has address of r1 hence if we change value of r1 then reading value from r2 will give changed value. Similarly if we change value through r2 value change then reading from r1 will give changed value.<br />
<br />
See the example given above carefully. r2 is address(pointer) of r1, hence *r2 will give struct rectangle(*r2 and r1 is same. hence (*r2).width and r1.width is also same, changing one will be reflected on other.)<br />
Important Note about pointer<br />
1. shortcut for writing (*r2).width is r2->width. In program in place of (*r2).width, r2->width and in place of (*r2).length, r2->length can also be written. Replace this in program above and run to see output.<br />
2. Declaring any variable any value have not assigned(as in int x;) defining means assigning value also(as in int x=1;) if you have just declared pointer and not assigned any address, then reading value from that pointer(using * as in *r2) will crash the program and it will give segmentation fault.</div>
<div id="hindi">
आज Hindi के इस C/C++ programming language tutorial को आगे बढ़ाते हुए हम pointer के बारे में और जानेंगे.<br />
पिछली बार हमने इस C/C++ programming language tutorial में int pointer का example देखा था परन्तु pointer किसी भी datatype का हो सकता है. struct का भी pointer हो सकता है. हम struct pointer का एक example देखते हैं. अगर आपको struct का use करना नहीं आता तो पहले इसे पढ़ लें. <a href="http://programming-tutorial-hindi.blogspot.com/2010/11/c-hindi-tutorial-structurestruct.html">C/C++ struct hindi में पढ़ने के लिए यहाँ click करें</a>. <br />
<pre><span class="pre">#include <stdio.h>
</span><span class="keyword">
struct</span> rectangle<span class="operator"> {</span><span class="type">
int</span> width<span class="operator">;</span><span class="type">
int</span> length<span class="operator">;
};</span><span class="type">
int</span><span class="keyword"> main</span><span class="operator">() {</span><span class="keyword">
struct</span> rectangle r1<span class="operator">;</span>
r1<span class="operator">.</span>width<span class="operator"> =</span><span class="int"> 10</span><span class="operator">;</span>
r1<span class="operator">.</span>length<span class="operator"> =</span><span class="int"> 15</span><span class="operator">;</span><span class="keyword">
struct</span> rectangle<span class="operator">*</span> r2<span class="operator">;</span>
r2<span class="operator"> = &</span>r1<span class="operator">;</span>
printf<span class="operator">(</span><span class="string">"Original width=%d, length=%d\n"</span><span class="operator">, (*</span>r2<span class="operator">).</span>width<span class="operator">, (*</span>r2<span class="operator">).</span>length<span class="operator">);</span>
r1<span class="operator">.</span>width<span class="operator"> =</span><span class="int"> 20</span><span class="operator">;</span>
r1<span class="operator">.</span>length<span class="operator"> =</span><span class="int"> 25</span><span class="operator">;</span>
printf<span class="operator">(</span><span class="string">"r1 changed, width=%d, length=%d\n"</span><span class="operator">, (*</span>r2<span class="operator">).</span>width<span class="operator">, (*</span>r2<span class="operator">).</span>length<span class="operator">);
(*</span>r2<span class="operator">).</span>width<span class="operator"> =</span><span class="int"> 5</span><span class="operator">;
(*</span>r2<span class="operator">).</span>length<span class="operator"> =</span><span class="int"> 10</span><span class="operator">;</span>
printf<span class="operator">(</span><span class="string">"*r2 changed, width=%d, length=%d\n"</span><span class="operator">,</span> r1<span class="operator">.</span>width<span class="operator">,</span> r1<span class="operator">.</span>length<span class="operator">);</span><span class="flow">
return</span><span class="int"> 0</span><span class="operator">;
}</span>
</pre>
ऊपर दिए C/C++ program को चला कर देख लें. उसका output यहाँ समझते हैं. main में हमने पहले struct rectangle type का variable r1 define किया है और pointer r2 declare किया है जो struct rectangle का address store कर सकता है. उसमे r1 का address डाल दिया गया है. अब r1 एक variable है और r2 pointer है जिसमे r1 का address है इसलिए अगर हम r1 में value change करते हैं तो r2 से value read करने पर changed value मिलेगी. इसी तरह r2 में value change करते हैं तो r1 से read करने पार changed value मिलेगी.<br />
<br />
ऊपर दिए गए program को ध्यान से देखिये. r2 address(pointer) है r1 का, इसलिए *r2 हमें struct rectangle देगा(pointer वाले address की value * लगाने पर आती है इसलिए *r2 और r1 एक ही हैं. इसी तरह (*r2).width और r1.width भी एक ही हैं दोनों में से किसी एक को change करने पर दूसरा भी change हो जाता है.)<br />
Important Note about pointer<br />
1. (*r2).width लिखने का shortcut r2->width भी होता है. program मे (*r2).width की जगह r2->width और (*r2).length कि जगह r2->length भी लिख सकते हैं. आप ऊपर दिए गए program में यह लिखकर चलाकर देखें.<br />
2. कोई भी variable declare करने का मतलब है कि उसमे कोई value नहीं डाली(जैसे int x;) और define करने का मतलब है कि उसमे value भी डाल दी है(जैसे int x=1;) अगर आपने pointer सिर्फ declare किया है और उसमे किसी variable का address नहीं डाला तो उससे value read करने में(* का use करके जैसे *r2) program crash हो जायेगा और segmentation fault दे देगा.<br />
आज के लिए इतना ही. अभी pointer के बारे में बहुत कुछ जानना बाकी है. <br />
<br /></div>
<div style="text-align: center;">
<a href="http://programming-tutorial-hindi.blogspot.com/">Index</a><a href="http://programming-tutorial-hindi.blogspot.com/2010/12/c-programming-hindi-tutorial-pointer_30.html"> Next </a></div>
</div>
Hridayesh Guptahttp://www.blogger.com/profile/09764967341453023170noreply@blogger.comtag:blogger.com,1999:blog-8683328491687928872.post-75371324045647823952010-12-08T17:43:00.004+05:302015-11-24T08:12:30.249+05:30pointer basic<div dir="ltr" style="text-align: left;" trbidi="on">
<style type="text/css">
table {
border-collapse:collapse;
border:1px solid;
align:center;
}
.mytable table #boxrow td {
border: 1px solid black;
width:40px;
height: 40px;
text-align:center;
}
</style><br />
<div class="btn_sw" href="#" id="hi_btn" onclick="javascript:return showhindi(this);">
Read in Hindi</div>
<div class="btn_sw" href="#" id="en_btn" onclick="javascript:return showeng(this);">
Read in English</div>
<br />
<div id="eng">
Going forward with this C/C++ programming language tutorial today we will learn pointer which is considered most difficult in C/C++.<br />
<h3>
What is Pointer in C/C++ programming language</h3>
Earlier we have seen how any variable is stored in memory. Address of variable tells where variable is stored in memory. This address is called pointer. C/C++ gives us facility know the address of variable where its value is stored. To know address of any variable & is used. For example if variable is int x; then &x will give address of x. As we store any variable int, char, float etc similarly address of any variable can also be stored. There is new datatype which is used to store address of variable. To store address of int variable int* datatype is used. similarly to store address of char variable char* datatype is used. Example given below shows how to store address of variable.<br />
<pre>int x = 5;
int* p;
p = &x
</pre>
Here int variable x define, Then variable p is declared which can store address of any int variable. Then we have assigned address of x to p variable.<br />
<div class="mytable">
<table><tbody>
<tr><td>Address→</td><td>0</td><td>1</td><td>2</td><td>3</td><td>4</td></tr>
<tr id="boxrow"><td style="border: medium none;">Memory→</td> <td>10000111</td><td>11100101</td><td>00100110</td><td>0000101</td> <td style="border-right: medium none;">01100101</td> <td style="border: medium none;">. . . </td> </tr>
<tr style="border-top: 1px none;"><td colspan="4"></td><td align="center">↑</td><td></td></tr>
<tr style="border-top: 1px none;"><td colspan="4"><code>p = &x = 3</code></td><td align="center">int x</td><td></td></tr>
</tbody></table>
</div>
Now we have variable p which is of int* type and address of x is stored in it - means if we print p, address of x will be print.( As shown above here address of x is 3 running program at different time, address of x will change.) If we want to know what is stored in the Memory pointed by p then *p is used(Here p address of memory where x is and value there is 5 hence *p will give 5 here). lets see an small example. Change this example as per yourself and run it. do experiments. <br />
<pre><span class="pre">#include <stdio.h>
</span><span class="type">
int</span><span class="keyword"> main</span><span class="operator">() {</span><span class="type">
int</span> x<span class="operator"> =</span><span class="int"> 5</span><span class="operator">;</span><span class="type">
int</span><span class="operator">*</span> p<span class="operator"> = &</span>x<span class="operator">;</span>
printf<span class="operator">(</span><span class="string">"x = %d\n"</span><span class="operator">,</span>x<span class="operator">);</span>
printf<span class="operator">(</span><span class="string">"address of x = %d\n"</span><span class="operator">,</span> p<span class="operator">);</span>
printf<span class="operator">(</span><span class="string">"value at location p = %d\n"</span><span class="operator">, *</span>p<span class="operator">);</span>
scanf<span class="operator">(</span><span class="string">"%d"</span><span class="operator">, &</span>x<span class="operator">);</span><span class="flow">
return</span><span class="int"> 0</span><span class="operator">;
}</span></pre>
In next topic we will see use of pointer.</div>
<div id="hindi">
आज Hindi के इस C/C++ programming language tutorial को आगे बढ़ाते हुए हम pointer के बारे में जानेंगे जो C/C++ में सबसे कठिन माना जाता है. <br />
<h3>
What is Pointer in C/C++ programming language</h3>
इससे पहले हम यह जान चुके हैं कोई भी variable Computer की memory में किस तरह से store होता है. जहाँ store होता है उसका address भी होता है जो यह बताता है कि variable की value memory में कहाँ stored है. इस address को ही pointer कहते हैं. C/C++ programming language हमें यह सुविधा देती है कि हम किसी variable का address जान सकें(variable का address = वह Memory address/location जहाँ variable की value stored है). C/C++ programming language में किसी भी variable का address जानने के लिए & का use करते हैं. जैसे कि अगर कोई variable int x; है तो x का address &x से मिल जायेगा. जिस तरह से हम int, char, float etc को variable में store कर लेते हैं उसी तरह किसी variable के address को भी. इसके लिए एक नया datatype होता है जो address store करने के काम आता है जिस तरह से integer store करने के लिए int datatype का use होता है. किसी int variable का address store करने के लिए int* datatype का use करते हैं. इसी तरह char variable का address store करने के लिए char* datatype का use करते हैं. नीचे एक छोटा सा example यह show कर रहा है कि किसी variable में दूसरे variable का address कैसे store करते हैं. <br />
<pre>int x = 5;
int* p;
p = &x
</pre>
यहाँ पहले एक int variable x define किया है, फिर p ऐसा variable declare किया है जो किसी int का address store करता है. फिर p variable में x का address डाल दिया है.(जैसा कि हम जानते हैं कि किसी भी variable का address जानने के लिए & का use करते हैं.) <br />
<div class="mytable">
<table><tbody>
<tr><td>Address→</td><td>0</td><td>1</td><td>2</td><td>3</td><td>4</td></tr>
<tr id="boxrow"><td style="border: medium none;">Memory→</td> <td>10000111</td><td>11100101</td><td>00100110</td><td>0000101</td> <td style="border-right: medium none;">01100101</td> <td style="border: medium none;">. . . </td> </tr>
<tr style="border-top: 1px none;"><td colspan="4"></td><td align="center">↑</td><td></td></tr>
<tr style="border-top: 1px none;"><td colspan="4"><code>p = &x = 3</code></td><td align="center">int x</td><td></td></tr>
</tbody></table>
</div>
अब हमारे पास एक variable p है जो कि int* type का है और उसमे x का address stored है - means p को print करेंगे तो x का address print हो जायेगा.(ऊपर दिखाए गए अनुसार यहाँ पर x का address 3 है परन्तु अलग अलह time पर C/C++ program run करने पर address अलग अलग आएगा) यदि हमें यह जानना है कि p में जिस Memory का address लिखा हुआ उस memory पर क्या stored है तो *p का use करते हैं(यहाँ p में उस memory का address है जहाँ x है और उस memory यानि x में 5 stored है इसलिए *p यहाँ पर 5 देगा. इसका एक छोटा सा example देखते हैं. इस example को अपने अनुसार change करके चलाकर देखें और experiement करें. <br />
<pre><span class="pre">#include <stdio.h>
</span><span class="type">
int</span><span class="keyword"> main</span><span class="operator">() {</span><span class="type">
int</span> x<span class="operator"> =</span><span class="int"> 5</span><span class="operator">;</span><span class="type">
int</span><span class="operator">*</span> p<span class="operator"> = &</span>x<span class="operator">;</span>
printf<span class="operator">(</span><span class="string">"x = %d\n"</span><span class="operator">,</span>x<span class="operator">);</span>
printf<span class="operator">(</span><span class="string">"address of x = %d\n"</span><span class="operator">,</span> p<span class="operator">);</span>
printf<span class="operator">(</span><span class="string">"value at location p = %d\n"</span><span class="operator">, *</span>p<span class="operator">);</span>
scanf<span class="operator">(</span><span class="string">"%d"</span><span class="operator">, &</span>x<span class="operator">);</span><span class="flow">
return</span><span class="int"> 0</span><span class="operator">;
}</span></pre>
आज के लिए इतना ही. अगले topic में Hindi में C/C++ tutorial को आगे बढ़ाते हुए pointer के use देखेंगे. </div>
<br />
<div style="text-align: center;">
<a href="http://programming-tutorial-hindi.blogspot.com/">Index</a> <a href="http://programming-tutorial-hindi.blogspot.com/2010/12/c-programming-hindi-tutorial-pointer.html"> Next(struct pointer)</a></div>
</div>
Hridayesh Guptahttp://www.blogger.com/profile/09764967341453023170noreply@blogger.comtag:blogger.com,1999:blog-8683328491687928872.post-58742591470810599182010-12-01T00:54:00.009+05:302015-11-24T08:12:44.250+05:30memory 2<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="btn_sw" href="#" id="hi_btn" onclick="javascript:return showhindi(this);">
Read in Hindi</div>
<div class="btn_sw" href="#" id="en_btn" onclick="javascript:return showeng(this);">
Read in English</div>
<br />
<div id="eng">
Going forward with this C/C++ programming language tutorial today we will continue to learn about Memory. Last time we have seen that computer reads 1 byte(8 bits) from memory in one access. since 1 bit can store either of 0 or 1 hence 1 byte can store any number from 00000000 to 11111111(8 digit binary number). It is not convenient to read or write numbers in binary format hence we write it is decimal form.<br />
00000000 = 0 (in decimal) <br />
11111111 = 2<sup>8</sup> -1 = 255 (in decimal) <br />
Hence in 8 bit or 1 byte numbers from 0 to 255 can be stored.<br />
Similarly if we want to store number greater than 255, we have to take 2 bytes together. In 2 byte number from 00000000 00000000 to 11111111 11111111(0 to 65535) can be stored. Now we will see how different kind of datatype variables are stored.<br />
<b>char</b>: See <a href="http://www.asciitable.com/">ASCII table</a>. In the table each character have corresponding Decimal number. When we store any letter char variable, computer stores corresponding number in memory. If we store 'a', number corresponding to that is 97(01100001) which will be stored. According to the table we do not need to store number greater than 255 to store character hence 1 byte memory is needed to store char variable.<br />
<b>short int</b>: short int variable is stored in 2 byte(16 bits). In 2 bytes any number from 0 to 65535(2<sup>16</sup>-1) can be stored. Since short int can take negative value too hence it can take values from-32768(-2<sup>15</sup>) to 32767(2<sup>15</sup>-1).<br />
<b>unsigned short int</b>: This also takes 2 bytes but does not take negative values hence any number from 0 to 65535 can be stored<br />
<b>int</b>: It takes 4 bytes and can take negative value hence any number from -2<sup>31</sup> to 2<sup>31</sup>-1 can be stored in this variable.<br />
<b>unsigned int</b>: This also takes 4 byte but only positive values hence any number from 0 to 2<sup>32</sup>-1 can be stored.<br />
<b>float</b>: format to store this type of number is complex because it can take fraction values too. I takes 4 byte.<br />
<b>double</b>: It takes 8 byte and can take fraction values.<br />
There are some other datatypes also which are not needed at this point of time.<br />
<br />
Last time we have seen that computer reads memory as byte by byte. Each byte have an address, as 1st byte have address 0, 2nd byte have address 1... When computre writes a value to variable, it also remembers address of memory where value is written. If variable takes more than 1 byte then all bytes are stored continuously and address of 1st byte is remembered. For example if int(4 bytes) is stored in memory from 101st byte to 104th byte then its address will be 101.<br />
Now you are ready to know about pointer. In next topic we will learn about pointer.</div>
<div id="hindi">
आज Hindi के इस C/C++ programming language tutorial को आगे बढ़ाते हुए हम Memory के बारे में थोडा और जानेंगे. पिछली बार हमने यह जाना कि computer एक बार में 1 byte(8 bits) read करता है. चूंकि हर 1 bit में 0 या 1 में से कुछ store हो सकता है इसलिए 1 byte में 00000000 से लेकर 11111111 तक की कोई भी संख्या (8 अंको की binary संख्या) store हो सकती है. लिखने और पढ़ने में 00000000 और 11111111 आसान नहीं है इसलिए हम इसे decimal में convert करके पढते हैं.<br />
00000000 = 0 (in decimal) <br />
11111111 = 2<sup>8</sup> -1 = 255 (in decimal) <br />
इसलिए इस 8 bit या 1 byte में 0 से 255 तक की कोई भी संख्या store हो सकती है.<br />
इसी तरह अगर हमें और ज्यादा बड़ी संख्या store करना हो तो हम 2 byte को एक साथ लेकर उसमे store करेंगे. 2 byte में 00000000 00000000 से 11111111 11111111 यानी 0 से 65535 तक कि संख्या store हो सकती है. अब हम देखते हैं कि विभिन्न datatype किस तरह से store किये जाते हैं.<br />
<b>char</b>: <a href="http://www.asciitable.com/">ASCII table</a> को देखिये इसमें हर एक अक्षर(character) के सामने एक Decimal संख्या लिखी हैं. जब हम char variable में कोई अक्षर store करते हैं तो computer उस अक्षर के सामने वाली संख्या को store कर देता है. जैसे कि अगर हम 'a' store करते हैं तो उसके सामने लिखी संख्या 97(01100001) store हो जायेगी. उस table के अनुसार किसी भी अक्षर को store करने के लिए 255 से बड़ी संख्या की जरूरत नहीं है इसलिए char को store करने के लिए 1 byte memory कि जरूरत होती है.<br />
<b>short int</b>: short int variable 2 byte(16 bits) में store होता है. 2 byte में 0 से 65535(2<sup>16</sup>-1) तक कि संख्या store कर सकते हैं. चूंकि short int negative value भी ले सकता है इसलिए यह -32768(-2<sup>15</sup>) से 32767(2<sup>15</sup>-1) तक के मान ले सकता है.<br />
<b>unsigned short int</b>: यह भी 2 byte कि जगह लेता है परन्तु negative value नहीं ले सकता इसलिए 0 से 65535 तक के मान ले सकता है.<br />
<b>int</b>: यह 4 byte लेता है और negative value भी ले सकता है इसलिए -2<sup>31</sup> से 2<sup>31</sup>-1 तक की value ले सकता है.<br />
<b>unsigned int</b>: यह भी 4 byte लेता है परन्तु केवल positive value इसलिए 0 से 2<sup>32</sup>-1 तक की value ले सकता है. <br />
<b>float</b>: इसके store करने का format complex है क्योंकि यह दशमलव value भी ले सकता है. यह 4 byte लेता है.<br />
<b>double</b>: यह 8 byte लेता है और दशमलव value भी ले सकता है.<br />
इसी तरह कुछ और भी datatype होते हैं जिनके बारे में जानना अभी आवश्यक नहीं है.<br />
<br />
पिछली बार हमने देखा था कि computer, memory को एक एक byte read कर सकता है. हर byte का एक address होता है जैसे पहली byte का address 0, दूसरी byte का address 1... इस तरह से. जब computer किसी variable की value memory में कहीं किसी byte पर लिखता है तो उस byte का address भी याद रखता है. अगर कोई variable(जैसे int) एक से ज्यादा byte लेता है तो continuous store करते हुए पहली byte का address याद रखा जाता है. जैसे कि अगर int(4 bytes) 101st byte से 104th byte तक store हुआ है तो इस int का address 101 हुआ.<br />
अब आप pointer के बारे में जानने के लिए ready है. अगली बार हम pointer के बारे में पढेंगे.</div>
<br />
<div style="text-align: center;">
<a href="http://programming-tutorial-hindi.blogspot.com/">Index</a> <a href="http://programming-tutorial-hindi.blogspot.com/2010/12/c-programming-tutorial-hindi.html"> Next(Pointer basic)</a></div>
</div>
Hridayesh Guptahttp://www.blogger.com/profile/09764967341453023170noreply@blogger.comtag:blogger.com,1999:blog-8683328491687928872.post-53459680585794114612010-11-27T19:11:00.011+05:302015-11-24T08:12:57.524+05:30memory<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="btn_sw" href="#" id="hi_btn" onclick="javascript:return showhindi(this);">
Read in Hindi</div>
<div class="btn_sw" href="#" id="en_btn" onclick="javascript:return showeng(this);">
Read in English</div>
<style type="text/css">
table {
border-collapse:collapse;
}
.mytable table tr td {
border: 1px solid black;
width:40px;
height: 40px;
text-align:center;
}
</style><br />
<div id="eng">
We have learned very much about C/C++ programming and now you are ready to write big programs using this tutorial. Now we will learn pointers in easy way which is considered most difficult in C/C++. I will try to present it in easy way but feel free to leave comment if you have any problem.<br />
<br />
It is important to know about Memory before learning Pointer so today we will learn how memory in organized in computer. You know that computer or any electronic device understand language of 0 and 1 only. Today we will understand it in detail. Memory in any device can be seen as an array as shown below.<br />
<div class="mytable">
<table><tbody>
<tr> <td>1</td><td>1</td><td>0</td><td>1</td><td>0</td><td>0</td><td>0</td><td>1</td><td>1</td> <td style="border-right: medium none;">0</td> <td style="border: medium none;">. . .</td> </tr>
</tbody></table>
</div>
<br />
Memory looks like an Array shown above, each of the position can store either 0 or 1. Each location is called a bit. 1 bit can store either 0 or 1. In magnetic memory this is represented by magnetic field. If direction of magnetic field is clockwise then computer reads it as 0 otherwise reads it as 1 if direction is anticlockwise. Computer never reads 1 bit a time, it always reads 8 bits together which is called a byte. Hence computer reads memory as shown below.<br />
<div class="mytable">
<table><tbody>
<tr> <td>11010001</td><td>10011101</td><td>00101001</td><td>11111001</td><td>10000011</td> <td>00100110</td> <td style="border-right: medium none;">. . . </td> </tr>
</tbody></table>
</div>
<br />
If computer reads 1st address of memory(array) shown below then it will get 11010001. value at 2nd address will read as 10011101 etc. When Computer reads 1st address of Memory, it will get 1st byte(8 bits), 2nd byte at 2nd address... If it is needed to get value of 3rd bit inside 1st byte then it have to read whole 1st byte 1st then get 3rd bit from that byte. Directly 3rd bit can not be read. In summary Computer has byte level addressing of memory. Each byte have an address but bit do not have direct address.<br />
<br />
In next topic we will see how do we store so many variables in the memory then we will learn about pointer. Before going to next topic, learn how to convert number in decimal representation to binary representation and how to add 2 binary numbers.</div>
<div id="hindi">
अब तक हमने C/C++ programming के बारे में बहुत कुछ जान लिया है और इस hindi tutorial के द्वारा आप बड़े बड़े program लिखने के लिए भी Ready हैं. अब हम pointer को सरल तरीके से सीखेंगे जिसे C/C++ में सबसे कठिन माना जाता हैं. इसे सरल तरीके से पेश करने कि कोशिश रहेगी, फिर भी समझने में दिक्कत हो तो नीचे comment करें.<br />
<br />
Pointer समझने से पहले Memory के बारे में जानना जरूरी है इसलिए आज हम Memory Structure को समझेंगे. आप में से बहुत से लोगो को पता होगा कि computer या कोई भी electronic device जैसे calculator, micro processor, mobile phone सभी सिर्फ 0 और 1 की भाषा समझते हैं, आज हम इसे detail में समझते हैं. किसी भी device की Memory को हम एक बहुत बड़ी array की तरह देख सकते हैं. जैसा कि नीचे चित्र में दिखाया गया है.<br />
<div class="mytable">
<table><tbody>
<tr> <td>1</td><td>1</td><td>0</td><td>1</td><td>0</td><td>0</td><td>0</td><td>1</td><td>1</td> <td style="border-right: medium none;">0</td> <td style="border: medium none;">. . .</td> </tr>
</tbody></table>
</div>
<br />
Memory ऊपर दिखाई गयी Array की तरह दिखती है जिसकी हर एक position पर या तो 0 store हो सकता है या 1. हर एक position को एक bit कहते हैं. 1 bit में या तो 0 store होगा या 1. Magnetic memory में इस एक bit को magnetic field की direction के द्वारा represent किया जाता है. अगर direction clockwise है तो इसे computer 0 read करता है, aniclockwise है तो 1. Computer कभी इस एक bit को अकेले read या write नहीं करता. वह हमेशा 8 bits को एक साथ पढता है. इस 8 bit को 1 byte कहते हैं. इसलिए computer Memory को नीचे दिखाए गए चित्र जैसा पढता है.<br />
<br />
<div class="mytable">
<table><tbody>
<tr> <td>11010001</td><td>10011101</td><td>00101001</td><td>11111001</td><td>10000011</td> <td>00100110</td> <td style="border-right: medium none;">. . . </td> </tr>
</tbody></table>
</div>
<br />
अगर Computer ऊपर दिखाई गयी memory(array) का पहला address पढ़े तो उसे मिलेगा 11010001. इसी तरह दूसरे address पर 10011101 etc. Computer किसी भी Memory का पहला address पढ़े तो उसे पहली byte(8 bits) मिलेगी, दूसरे address पर दूसरी byte,... अगर पहली byte के अंदर की तीसरी bit पढनी है तो पहले उसे पहली byte पढनी पड़ेगी उसके बाद उस byte से तीसरी bit. directly पहली byte की तीसरी bit नहीं पढ़ सकते, क्योंकि पहला address पढ़ेगा तो पहली byte (पहली 8 bits) मिल जाएँगी. कहने का मतलब यह है कि Computer में byte level addressing होती है. हर byte का एक address होता है, पर byte के अंदर bit का direct address नहीं होता.<br />
<br />
आज के लिए इतना ही. अगली बार हम ये देखेंगे कि इस Memory में इतने variable store कैसे होते हैं उसके बाद pointer सीखेंगे. अगला लेख पढ़ने से पहले Binary to Decimal Conversion सीख लें और दो Binary number को जोड़ना भी सीख लें. </div>
<br />
<div style="text-align: center;">
<a href="http://programming-tutorial-hindi.blogspot.com/">Index</a> <a href="http://programming-tutorial-hindi.blogspot.com/2010/12/computer-1-byte8-bits-read.html"> Next(Memory II)</a></div>
</div>
Hridayesh Guptahttp://www.blogger.com/profile/09764967341453023170noreply@blogger.comtag:blogger.com,1999:blog-8683328491687928872.post-23896273733920623262010-11-25T23:04:00.007+05:302012-07-19T09:04:04.955+05:30struct<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="btn_sw" href="#" id="hi_btn" onclick="javascript:return showhindi(this);">
Read in Hindi</div>
<div class="btn_sw" href="#" id="en_btn" onclick="javascript:return showeng(this);">
Read in English</div>
<div id="eng">
Going forward with C/C++ programming language tutorial today we will learn about structure(struct). Till now we have learned about many datatypes like int, char, float, double etc. These all datatypes store data in some format. If we want to store names of people, we can store in char array, If we want to store ages of people, we can store in int array but if we want to store length and width of rectangles(storing rectangle) then there are 2 ways. One is make 2 variables and store length and width on them. If we want to store multiple rectangles then we have to make 2 arrays, one for length and another for width. Using struct one rectangle can be stored in one variable and Rectangles can be stored in one array.<br />
<br />
Lets see the program which does not use struct to store length and width of a rectangle. Later we will see better version which uses struct.<br />
<pre><span class="pre">#include <stdio.h>
</span><span class="type">
int</span><span class="keyword"> main</span><span class="operator">() {</span><span class="type">
int</span> length1<span class="operator"> =</span><span class="int"> 12</span><span class="operator">;</span><span class="type">
int</span> width1<span class="operator"> =</span><span class="int"> 8</span><span class="operator">;</span><span class="type">
int</span> length2<span class="operator"> =</span><span class="int"> 20</span><span class="operator">;</span><span class="type">
int</span> width2<span class="operator"> =</span><span class="int"> 11</span><span class="operator">;</span>
printf<span class="operator">(</span><span class="string">"Rectangle1: %d %d\n"</span><span class="operator">,</span> length1<span class="operator">,</span> width1<span class="operator">);</span>
printf<span class="operator">(</span><span class="string">"Rectangle2: %d %d\n"</span><span class="operator">,</span> length2<span class="operator">,</span> width2<span class="operator">);</span>
scanf<span class="operator">(</span><span class="string">"%d"</span><span class="operator">, &</span>length1<span class="operator">);
}</span></pre>
It is easy to understand program given above. Now we will write same thing with the use of struct.<br />
<pre><span class="pre">#include <stdio.h>
</span><span class="keyword">
struct</span> Rectangle<span class="operator"> {</span><span class="type">
int</span> length<span class="operator">;</span><span class="type">
int</span> width<span class="operator">;
};</span><span class="type">
int</span><span class="keyword"> main</span><span class="operator">() {</span><span class="keyword">
struct</span> Rectangle rect1<span class="operator">;</span><span class="keyword">
struct</span> Rectangle rect2<span class="operator">;</span>
rect1<span class="operator">.</span>length<span class="operator"> =</span><span class="int"> 12</span><span class="operator">;</span>
rect1<span class="operator">.</span>width<span class="operator"> =</span><span class="int"> 8</span><span class="operator">;</span>
rect2<span class="operator">.</span>length<span class="operator"> =</span><span class="int"> 20</span><span class="operator">;</span>
rect2<span class="operator">.</span>width<span class="operator"> =</span><span class="int"> 11</span><span class="operator">;</span>
<span class="operator"></span>
printf<span class="operator">(</span><span class="string">"Rectangle1: %d %d\n"</span><span class="operator">,</span> rect1<span class="operator">.</span>length<span class="operator">,</span> rect1<span class="operator">.</span>width<span class="operator">);</span>
printf<span class="operator">(</span><span class="string">"Rectangle2: %d %d\n"</span><span class="operator">,</span> rect2<span class="operator">.</span>length<span class="operator">,</span> rect2<span class="operator">.</span>width<span class="operator">);</span>
<span class="operator"></span>
scanf<span class="operator">(</span><span class="string">"%d"</span><span class="operator">, &(</span>rect1<span class="operator">.</span>length<span class="operator">));
}</span>
</pre>
<br />
Here we have defined a struct named Rectangle which contains 2 variables width and height. Here Rectangle becomes a datatype like int, char. This Rectangle datatype can store 2 int: height and width. To define new datatype Rectangle see above how we have done it. struct Rectangle { ... };<br />
This have defined a new datatype Rectangle. To use it and make a variable of type Rectangle as shown in main(){...} Here two variable rect1 and rect2 of Rectangle type have been defined. Each of two variable can have length and width. To access length and width of rect1 we use rect1.length and rect1.width.<br />
<br />
Using struct we can make any kind of object which can store several variables.<br />
If you liked this blog, tell your friends also because it can useful to them also!!</div>
<div id="hindi">
आज Hindi के इस C/C++ programming language tutorial को आगे बढ़ाते हुए हम structure(struct) के बारे में और जानेंगे. अभी तक हम बहुत से datatype के बारे में पढ़ चुके हैं जैसे कि int, char, float, double etc. ये सभी datatype किसी format में data store करते हैं. जैसे कि अगर हमें लोगों के नाम store करना है तो हम char की array में store कर लेंगे, किसी की age store करना है तो int में store कर लेंगे. परन्तु अगर हमें किसी आयत(rectangle) की length और width store करना है तो एक तरीका ये हैं 2 int variable बनाये और उसमें store करें. उसके बाद अगर दूसरे आयत की length और width store करना है तो 2 नए variable अलग नाम से बनाना पड़ेंगे. struct का use करके अलग अलग नाम से variable बनाने कि दिक्कत दूर की जा सकती है. struct का use करने हर एक आयत का एक variable ही बनाना पड़ेगा <br />
<br />
दो आयत की length और width store करने का program struct का use किये बिना नीचे दिया जा रहा है, उसके बाद हम उसका better version struct का use करके भी देखेंगे.<br />
<pre><span class="pre">#include <stdio.h>
</span><span class="type">
int</span><span class="keyword"> main</span><span class="operator">() {</span><span class="type">
int</span> length1<span class="operator"> =</span><span class="int"> 12</span><span class="operator">;</span><span class="type">
int</span> width1<span class="operator"> =</span><span class="int"> 8</span><span class="operator">;</span><span class="type">
int</span> length2<span class="operator"> =</span><span class="int"> 20</span><span class="operator">;</span><span class="type">
int</span> width2<span class="operator"> =</span><span class="int"> 11</span><span class="operator">;</span>
printf<span class="operator">(</span><span class="string">"Rectangle1: %d %d\n"</span><span class="operator">,</span> length1<span class="operator">,</span> width1<span class="operator">);</span>
printf<span class="operator">(</span><span class="string">"Rectangle2: %d %d\n"</span><span class="operator">,</span> length2<span class="operator">,</span> width2<span class="operator">);</span>
scanf<span class="operator">(</span><span class="string">"%d"</span><span class="operator">, &</span>length1<span class="operator">);
}</span></pre>
ऊपर दिए गए program को समझाने कि कोई जरूरत नहीं है. अब हम इसी को struct का use करके कैसे लिखेंगे वो देखते हैं.<br />
<pre><span class="pre">#include <stdio.h>
</span><span class="keyword">
struct</span> Rectangle<span class="operator"> {</span><span class="type">
int</span> length<span class="operator">;</span><span class="type">
int</span> width<span class="operator">;
};</span><span class="type">
int</span><span class="keyword"> main</span><span class="operator">() {</span><span class="keyword">
struct</span> Rectangle rect1<span class="operator">;</span><span class="keyword">
struct</span> Rectangle rect2<span class="operator">;</span>
rect1<span class="operator">.</span>length<span class="operator"> =</span><span class="int"> 12</span><span class="operator">;</span>
rect1<span class="operator">.</span>width<span class="operator"> =</span><span class="int"> 8</span><span class="operator">;</span>
rect2<span class="operator">.</span>length<span class="operator"> =</span><span class="int"> 20</span><span class="operator">;</span>
rect2<span class="operator">.</span>width<span class="operator"> =</span><span class="int"> 11</span><span class="operator">;</span>
<span class="operator"></span>
printf<span class="operator">(</span><span class="string">"Rectangle1: %d %d\n"</span><span class="operator">,</span> rect1<span class="operator">.</span>length<span class="operator">,</span> rect1<span class="operator">.</span>width<span class="operator">);</span>
printf<span class="operator">(</span><span class="string">"Rectangle2: %d %d\n"</span><span class="operator">,</span> rect2<span class="operator">.</span>length<span class="operator">,</span> rect2<span class="operator">.</span>width<span class="operator">);</span>
<span class="operator"></span>
scanf<span class="operator">(</span><span class="string">"%d"</span><span class="operator">, &(</span>rect1<span class="operator">.</span>length<span class="operator">));
}</span>
</pre>
<br />
इसे ध्यान से समझने कि जरूरत है. सबसे पहले हमने एक struct define किया है जिसका नाम Rectangle है और उसमे दो variable length और width हैं. यहाँ Rectangle एक datatype बन जायेगा. जिस तरह int एक datatype होता है जो कोई भी संख्या store कर सकता है उसी तरह से यहाँ पर Rectangle एक datatype बन जायेगा जो दो int store कर सकता है जिनके नाम length और width हैं. इसे बनाने का तरीका ध्यान से ऊपर देखिये. struct के बाद वह नाम लिखे जिस नाम से datatype बनाना है. जिस तरह से int नाम का datatype है उसी तरह यहाँ Rectangle नाम का datatype बनाया है. उसके बाद {} में वो सब variable लिखते हैं जो उस datatype में store होंगे. जैसे यहाँ Rectangle datatype में दो variable length और width store होंगे. अंत में ; लगाना न भूले अन्यथा compile करने में error आएगा.<br />
अब Rectangle datatype define हो गया है अब हम इसका use करना देखते हैं. जिस तरह से हम int variable define करते हैं उसी तरह Rectangle variable define करते हैं, परन्तु Rectangle से पहले struct लिखना जरूरी है. यहाँ Rectangle type के दो variable rect1 और rect2 define किये गए हैं. प्रत्येक variable दो value length और width ले सकता है जैसा कि पहले struct datatype बनाते समय define किये थे. अब rect1 की length और width rect1.length, rect1.width से access होती है.<br />
<br />
यहाँ rect1 एक Rectangle datatype बन गया है यहाँ यह datatype दो value length और width store कर सकता है. इस तरह से किसी भी object को इस तरह के struct बनाकर store कर सकते हैं.<br />
इसे अपने दोस्तों को बताना न भूले. नीचे दिए गए link की सहायता से इसे facebook, twitter, Buzz पर share अपने दोस्तों से करें.</div>
<br />
<div style="text-align: center;">
<a href="http://programming-tutorial-hindi.blogspot.com/">Index </a> <a href="http://programming-tutorial-hindi.blogspot.com/2010/11/c-programming-language-tutorial-hindi.html">Next(Memory)</a></div>
</div>
Hridayesh Guptahttp://www.blogger.com/profile/09764967341453023170noreply@blogger.comtag:blogger.com,1999:blog-8683328491687928872.post-48330213615714601062010-11-21T16:44:00.006+05:302015-11-24T08:13:09.845+05:30break continue<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="btn_sw" href="#" id="hi_btn" onclick="javascript:return showhindi(this);">
Read in Hindi</div>
<div class="btn_sw" href="#" id="en_btn" onclick="javascript:return showeng(this);">
Read in English</div>
<br />
<div id="eng">
Going forward with this C/C++ programming language tutorial today we will learn about two important keywords break and continue, which are needed in loops (for loop, while loop, switch case).<br />
<br />
<h4>
<b>use of break in c/c++</b></h4>
In c/c++ to break a loop in the middle to come out of loop break statement is used. For example we want to search a number in an array. Using for loop we will go to each element of array and test if number is present or not. Here for loop will continue till we scan whole array. But if somewhere in middle we find desired number then we do not want to scan remaining elements of array. For this we will use break. Similarly break can be used to break while loop also.<br />
Lets see the example below in which we want to search a number divisible by 21 between 100 and 200.<br />
<pre><span style="color: #000099;">#include <stdio.h>
</span><span style="color: #ff6633;">
int</span><span style="color: #990000;"> main</span><b><span style="color: #663300;">() {</span></b><span style="color: #ff6633;"> </span>
<span style="color: #ff6633;"> int</span> i<b><span style="color: #663300;">;</span></b><span style="color: red;">
for</span><b><span style="color: #663300;">(</span></b>i<b><span style="color: #663300;">=</span></b><span style="color: #999900;">100</span><b><span style="color: #663300;">;</span></b> i<b><span style="color: #663300;"><=</span></b><span style="color: #999900;">200</span><b><span style="color: #663300;">;</span></b> i<b><span style="color: #663300;">++) {</span></b><span style="color: red;"> </span>
<span style="color: red;"> if</span><b><span style="color: #663300;">(</span></b>i<b><span style="color: #663300;">%</span></b><span style="color: #999900;">21</span><b><span style="color: #663300;"> ==</span></b><span style="color: #999900;"> 0</span><b><span style="color: #663300;">) {</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"1st such number is %d\n"</span><b><span style="color: #663300;">,</span></b> i<b><span style="color: #663300;">);</span></b>
<span style="color: red;"> break</span><b><span style="color: #663300;">;
}
}</span></b>
scanf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"%d"</span><b><span style="color: #663300;">, &</span></b>i<b><span style="color: #663300;">);</span></b><span style="color: red;">
return</span><span style="color: #999900;"> 0</span><b><span style="color: #663300;">;
}</span></b></pre>
<br />
Here we have if statement inside for loop. Remember that % gives remainder when dividing 1st number with 2nd. It will not go inside if for the numbers which gives i%21 does not give 0, in other words number is not divisible by 21. Hence in the beginning numbers are not divisible by 21, but as soon as we reach 105 it is divisible by 21 and it will go inside if and break will break the for loop. Hence finally for loop will run from 100 to 105 and because of break for loop will exit.<br />
Similarly in c/c++ to break while loop break is used. We have seen use of break to break switch case statement.<br />
<h4>
<b>use of continue in c/c++</b></h4>
As we know that loop is used to run some statements number of times. Some times without going to end statement work is done and we want to start again same loop. For this we use continue statement. To understand this lets see the example below. Using continue statement we will print only odd numbers in given array.<br />
<pre><span style="color: #000099;">#include <stdio.h>
</span><span style="color: #ff6633;">int</span><span style="color: #990000;"> main</span><b><span style="color: #663300;">() {</span></b>
<span style="color: #ff6633;">int</span> arr<b><span style="color: #663300;">[] = {</span></b><span style="color: #999900;">1</span><b><span style="color: #663300;">,</span></b><span style="color: #999900;">4</span><b><span style="color: #663300;">,</span></b><span style="color: #999900;">7</span><b><span style="color: #663300;">,</span></b><span style="color: #999900;">2</span><b><span style="color: #663300;">,</span></b><span style="color: #999900;">0</span><b><span style="color: #663300;">,-</span></b><span style="color: #999900;">5</span><b><span style="color: #663300;">,</span></b><span style="color: #999900;">8</span><b><span style="color: #663300;">,</span></b><span style="color: #999900;">17</span><b><span style="color: #663300;">,</span></b><span style="color: #999900;">5</span><b><span style="color: #663300;">,-</span></b><span style="color: #999900;">10</span><b><span style="color: #663300;">};</span></b>
<span style="color: #ff6633;"> int</span> length<b><span style="color: #663300;"> =</span></b><span style="color: #999900;"> 10</span><b><span style="color: #663300;">;</span></b><span style="color: #ff6633;">
int</span> i<b><span style="color: #663300;">;</span></b><span style="color: red;">
for</span><b><span style="color: #663300;">(</span></b>i<b><span style="color: #663300;">=</span></b><span style="color: #999900;">0</span><b><span style="color: #663300;">;</span></b> i<b><span style="color: #663300;"><</span></b><span style="color: #999900;">10</span><b><span style="color: #663300;">;</span></b> i<b><span style="color: #663300;">++) {</span></b>
<span style="color: red;"> if</span><b><span style="color: #663300;">(</span></b>arr<b><span style="color: #663300;">[</span></b>i<b><span style="color: #663300;">]%</span></b><span style="color: #999900;">2</span><b><span style="color: #663300;"> ==</span></b><span style="color: #999900;"> 0</span><b><span style="color: #663300;">) {</span></b><span style="color: red;">
continue</span><b><span style="color: #663300;">;
}</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"Odd number is %d\n"</span><b><span style="color: #663300;">,</span></b> arr<b><span style="color: #663300;">[</span></b>i<b><span style="color: #663300;">]);
}</span></b>
scanf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"%d"</span><b><span style="color: #663300;">, &</span></b>i<b><span style="color: #663300;">);</span></b>
<span style="color: red;"> return</span><span style="color: #999900;"> 0</span><b><span style="color: #663300;">;
}</span></b></pre>
<br />
In the example given above using for loop we start reading number from given array. If number is even, we use continue statement to skip and go to next iteration of for loop.<br />
Similarly continue can be used in while loop.<br />
If you like this tutorial, share it with your friends on facebook, orkut, twitter etc.</div>
<div id="hindi">
आज Hindi के इस C/C++ programming language tutorial को आगे बढ़ाते हुए हम दो important keywords break और continue का use करना सीखेंगे, जो loops (for loop, while loop, switch case) में कभी कभी काम आ जाते हैं.<br />
<h4>
<b>use of break in c/c++</b></h4>
<br />
c/c++ में किसी loop को बीच में ही खत्म करके उससे बाहर निकलने के लिए break का use किया जाता है. उदाहरण के लिए मान लीजिए एक int array में हम किसी number को खोजना चाहते हैं, इसके लिए for loop का use करके array के हर एक element को check करेंगे कि वह number वहां है कि नहीं. यहाँ for loop तब तक चलता रहेगा जब तक हम पूरी array check नहीं कर लेते, पर हम चाहते हैं कि बीच में जब भी वो number मिल जाये जिसे खोज रहे हैं तो for loop बंद करके loop से बाहर आ जाएँ. इसके लिए break का use करेंगे. for loop ही नहीं while loop से बीच में बाहर आने के लिए भी break जा use करते हैं.<br />
इसे नीचे दिए गए example से समझते हैं. इसमें हम 100 से 200 के बीच पहली संख्या search करेंगे जो 21 से विभाजित हो जाये.<br />
<pre><span style="color: #000099;">#include <stdio.h>
</span><span style="color: #ff6633;">
int</span><span style="color: #990000;"> main</span><b><span style="color: #663300;">() {</span></b><span style="color: #ff6633;"> </span>
<span style="color: #ff6633;"> int</span> i<b><span style="color: #663300;">;</span></b><span style="color: red;">
for</span><b><span style="color: #663300;">(</span></b>i<b><span style="color: #663300;">=</span></b><span style="color: #999900;">100</span><b><span style="color: #663300;">;</span></b> i<b><span style="color: #663300;"><=</span></b><span style="color: #999900;">200</span><b><span style="color: #663300;">;</span></b> i<b><span style="color: #663300;">++) {</span></b><span style="color: red;"> </span>
<span style="color: red;"> if</span><b><span style="color: #663300;">(</span></b>i<b><span style="color: #663300;">%</span></b><span style="color: #999900;">21</span><b><span style="color: #663300;"> ==</span></b><span style="color: #999900;"> 0</span><b><span style="color: #663300;">) {</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"1st such number is %d\n"</span><b><span style="color: #663300;">,</span></b> i<b><span style="color: #663300;">);</span></b>
<span style="color: red;"> break</span><b><span style="color: #663300;">;
}
}</span></b>
scanf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"%d"</span><b><span style="color: #663300;">, &</span></b>i<b><span style="color: #663300;">);</span></b><span style="color: red;">
return</span><span style="color: #999900;"> 0</span><b><span style="color: #663300;">;
}</span></b></pre>
<br />
यहाँ for loop के अंदर if statement है. आपको याद दिला दें कि % यह बताता है कि पहली संख्या में दूसरी का भाग देने पर शेष क्या बचेगा. जिन संख्याओ के लिए i%21 की value 0 नहीं है अर्थात वो 21 से विभाजित नहीं होते for loop में i के उन मानो लिए हम if के अंदर नहीं जायेंगे (ध्यान दे जैसा कि हम जानते हैं कि यहाँ for loop में i के 100 से 200 तक हर मान के लिए for loop के अंदर लिखे सारे statement run होंगे.) शुरू में if के अंदर वाले statement run नहीं होंगे पर जैसे ही i की value 105(21 से विभाजित) होगी if के अंदर चले जायेंगे और break run हो जायेगा और for loop खत्म हो जायेगा. इसलिए finally for loop के अंदर लिखे statement i के 100 से 105 तक की value के लिए ही run हो पाएंगे क्योंकि i=105 आने के बाद break run होने के कारण for loop खत्म हो जायेगा.<br />
इसी तरह c/c++ में while loop को भी बीच में खत्म करने के लिए break का use करते हैं. switch case statement में हम break का use देख ही चुके हैं.<br />
<br />
<h4>
<b>use of continue in c/c++</b></h4>
जैसा कि हम जानते हैं कि loop में (for loop, while loop) के कुछ statement बार बार run होते रहते हैं. कभी कभी loop के अंत तक जाये बिना ही हमारा काम हो जाता है और हम चाहते हैं कि loop बंद न हो पर इस बार loop के अंदर जो run हो रहा है वो यहीं बंद हो जाये और loop का अगला iteration start हो जाये, इसके लिए continue का use करते हैं. इसे भी नीचे दिए गए example से समझते हैं. इस example में एक array में कुछ number दिए गए हैं. हम continue का use करके odd numbers (विषम संख्याए) print करेंगे.<br />
<pre><span style="color: #000099;">#include <stdio.h>
</span><span style="color: #ff6633;">int</span><span style="color: #990000;"> main</span><b><span style="color: #663300;">() {</span></b>
<span style="color: #ff6633;">int</span> arr<b><span style="color: #663300;">[] = {</span></b><span style="color: #999900;">1</span><b><span style="color: #663300;">,</span></b><span style="color: #999900;">4</span><b><span style="color: #663300;">,</span></b><span style="color: #999900;">7</span><b><span style="color: #663300;">,</span></b><span style="color: #999900;">2</span><b><span style="color: #663300;">,</span></b><span style="color: #999900;">0</span><b><span style="color: #663300;">,-</span></b><span style="color: #999900;">5</span><b><span style="color: #663300;">,</span></b><span style="color: #999900;">8</span><b><span style="color: #663300;">,</span></b><span style="color: #999900;">17</span><b><span style="color: #663300;">,</span></b><span style="color: #999900;">5</span><b><span style="color: #663300;">,-</span></b><span style="color: #999900;">10</span><b><span style="color: #663300;">};</span></b>
<span style="color: #ff6633;"> int</span> length<b><span style="color: #663300;"> =</span></b><span style="color: #999900;"> 10</span><b><span style="color: #663300;">;</span></b><span style="color: #ff6633;">
int</span> i<b><span style="color: #663300;">;</span></b><span style="color: red;">
for</span><b><span style="color: #663300;">(</span></b>i<b><span style="color: #663300;">=</span></b><span style="color: #999900;">0</span><b><span style="color: #663300;">;</span></b> i<b><span style="color: #663300;"><</span></b><span style="color: #999900;">10</span><b><span style="color: #663300;">;</span></b> i<b><span style="color: #663300;">++) {</span></b>
<span style="color: red;"> if</span><b><span style="color: #663300;">(</span></b>arr<b><span style="color: #663300;">[</span></b>i<b><span style="color: #663300;">]%</span></b><span style="color: #999900;">2</span><b><span style="color: #663300;"> ==</span></b><span style="color: #999900;"> 0</span><b><span style="color: #663300;">) {</span></b><span style="color: red;">
continue</span><b><span style="color: #663300;">;
}</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"Odd number is %d\n"</span><b><span style="color: #663300;">,</span></b> arr<b><span style="color: #663300;">[</span></b>i<b><span style="color: #663300;">]);
}</span></b>
scanf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"%d"</span><b><span style="color: #663300;">, &</span></b>i<b><span style="color: #663300;">);</span></b>
<span style="color: red;"> return</span><span style="color: #999900;"> 0</span><b><span style="color: #663300;">;
}</span></b></pre>
<br />
ऊपर दिए गए example में for loop का use करके शुरू से array के एक एक number को देखते हैं. अगर number even है तो हम continue का use करके skip कर देते हैं और अगली बार for loop के अन्दर आते हैं.<br />
इसी तरह while loop में भी continue का use कर सकते हैं.<br />
इस blog को बेहतर बनाने के लिए आपके सुझावों का सदैव स्वागत रहेगा. सुझाव देने के लिए अपना Reply यहाँ पर दें. अगले लेख में हम function के बारे में जानेंगे जो कि बहुत important है.<br />
<br /></div>
<br />
<div style="text-align: center;">
<a href="http://programming-tutorial-hindi.blogspot.com/">index</a><a href="http://programming-tutorial-hindi.blogspot.com/2010/11/c-hindi-tutorial-structurestruct.html"> Next(Struct)</a></div>
</div>
Hridayesh Guptahttp://www.blogger.com/profile/09764967341453023170noreply@blogger.comtag:blogger.com,1999:blog-8683328491687928872.post-80253703550382530912010-11-17T17:26:00.007+05:302015-11-24T08:17:10.857+05:30variable scope<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="btn_sw" href="#" id="hi_btn" onclick="javascript:return showhindi(this);">
Read in Hindi</div>
<div class="btn_sw" href="#" id="en_btn" onclick="javascript:return showeng(this);">
Read in English</div>
<br />
<div id="eng">
Going forward with this C/C++ programming language tutorial today we will know about scope of variable using function examples. If you don't know about <a href="http://programming-tutorial-hindi.blogspot.com/2010/10/c-hindi-tutorial-function.html">C function</a> and <a href="http://programming-tutorial-hindi.blogspot.com/2010/10/c-hindi-tutorialvariables-expressions.html">C variables</a> then read them 1st.<br />
<br />
<b>What is scope</b><br />
In C scope of variable means after declaring/defining a variable where can we read value stored in that variable. If we have defined a variable inside any function then that variable can be read after defining it anywhere in that function, but not outside. If variable is defined inside loop then it can not be read outside loop. It is easy to understand. Variable defined inside any {...} can be read anywhere inside that after defining, but not outside.<br />
In the example given below we will make a register where any name can be added and all names in register can be printed. When you run this program, a menu will be displayed which will give you option to add any name in register or print all names.<br />
<pre><span style="color: #000099;">#include <stdio.h></span><span style="color: #ff6633;">
char</span> list<b><span style="color: #663300;">[</span></b><span style="color: #999900;">10</span><b><span style="color: #663300;">][</span></b><span style="color: #999900;">20</span><b><span style="color: #663300;">];</span></b><span style="color: #ff6633;"> </span>
<span style="color: #ff6633;">int</span> length<b><span style="color: #663300;"> =</span></b><span style="color: #999900;"> 0</span><b><span style="color: #663300;">;</span></b>
<span style="color: #ff6633;">void</span> add_name<b><span style="color: #663300;">() {</span></b><span style="color: red;">
if</span><b><span style="color: #663300;">(</span></b>length<b><span style="color: #663300;"> ></span></b><b><span style="color: #663300;">=</span></b><span style="color: #999900;"> 10</span><b><span style="color: #663300;">) {</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"list is full\n"</span><b><span style="color: #663300;">);</span></b><span style="color: red;">
return</span><b><span style="color: #663300;">;
}</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"Enter the name: "</span><b><span style="color: #663300;">);</span></b>
scanf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"%s"</span><b><span style="color: #663300;">,</span></b> list<b><span style="color: #663300;">[</span></b>length<b><span style="color: #663300;">]);</span></b>
length<b><span style="color: #663300;"> =</span></b> length<b><span style="color: #663300;"> +</span></b><span style="color: #999900;"> 1</span><b><span style="color: #663300;">;</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"name added\n"</span><b><span style="color: #663300;">);
}</span></b>
<span style="color: #ff6633;">void</span> print_list<b><span style="color: #663300;">() {</span></b><span style="color: #ff6633;">
int</span> i<b><span style="color: #663300;"> =</span></b><span style="color: #999900;"> 0</span><b><span style="color: #663300;">;</span></b><span style="color: red;">
for</span><b><span style="color: #663300;">(</span></b>i<b><span style="color: #663300;">=</span></b><span style="color: #999900;">0</span><b><span style="color: #663300;">;</span></b> i<b><span style="color: #663300;"> < </span></b>length<b><span style="color: #663300;">;</span></b> i<b><span style="color: #663300;">++) {</span></b>
<span style="color: #ff6633;"> int</span> serial_no<b><span style="color: #663300;"> =</span></b> i<b><span style="color: #663300;"> +</span></b><span style="color: #999900;"> 1</span><b><span style="color: #663300;">;</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"%d : %s\n"</span><b><span style="color: #663300;">,</span></b> serial_no<b><span style="color: #663300;">,</span></b> list<b><span style="color: #663300;">[</span></b>i<b><span style="color: #663300;">]);
}
}</span></b>
<span style="color: #ff6633;">int</span><span style="color: #990000;"> main</span><b><span style="color: #663300;">() {</span></b><span style="color: #ff6633;">
int</span> input<b><span style="color: #663300;"> =</span></b><span style="color: #999900;"> 0</span><b><span style="color: #663300;">;</span></b><span style="color: red;">
while</span><b><span style="color: #663300;">(</span></b>input<b><span style="color: #663300;"> !=</span></b><span style="color: #999900;"> 3</span><b><span style="color: #663300;">) {</span></b>
<span style="color: red;"> if</span><b><span style="color: #663300;">(</span></b>input<b><span style="color: #663300;"> ==</span></b><span style="color: #999900;"> 0</span><b><span style="color: #663300;">) {</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"0 - Print this menu\n"</span><b><span style="color: #663300;">);</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"1 - add name to list\n"</span><b><span style="color: #663300;">);</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"2 - print list\n"</span><b><span style="color: #663300;">);</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"3 - quit program\n"</span><b><span style="color: #663300;">);
}</span></b><span style="color: red;">
else if</span><b><span style="color: #663300;">(</span></b>input<b><span style="color: #663300;"> ==</span></b><span style="color: #999900;"> 1</span><b><span style="color: #663300;">) {</span></b>
add_name<b><span style="color: #663300;">();
}</span></b><span style="color: red;">
else if</span><b><span style="color: #663300;">(</span></b>input<b><span style="color: #663300;"> ==</span></b><span style="color: #999900;"> 2</span><b><span style="color: #663300;">) {</span></b>
print_list<b><span style="color: #663300;">();
}</span></b>
<span style="color: red;"> else</span><b><span style="color: #663300;"> {</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"Wrong Input, try again\n"</span><b><span style="color: #663300;">);
}</span></b>
scanf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"%d"</span><b><span style="color: #663300;">, &</span></b>input<b><span style="color: #663300;">);
}
}</span></b></pre>
<br />
Running program given above will print a menu. Enter 1 to add a name, Then it will ask what name you want to enter. Type the desired name and enter. To enter another name Again enter 1. To print all names you have entered press 2. Try to rum this program 1st then we will understand how it works.<br />
<br />
Now lets see how it works. In beginning we have defined 2 variables: list and length. Since these 2 variables are not inside any function or { } hence can be accessed from anywhere in program.<br />
Now lets see char list[10][20]; just consider char list[10]; 1st which means array of characters with capacity 10. char list[10][20]; means it can contain 10 arrays with each array have capacity of 20 char. Here instead of 10 chars it will contains 10 arrays.<br />
If it was int a[2][3] then we can see in the way show below.<br />
<pre>a = { {1,2,3} , {4,5,6} };</pre>
<br />
Here a can hold 2 arrays one at a[0] and another at a[1]. Each of these two arr can hold 3 integers.<br />
We will use char list[10][20]; to store names. We have seen that name is a string which can be stored in char array. Here list can contain 10 arrays hence we can store 10 names. Each array can hold up to 20 char Hence we can store names having less than 20 char. 2nd variable length how many names have been added to the list which is 0 in the beginning since no name on the list.<br />
After that there is add_name function, calling which adds name into the list. 1st we check if list is full(10 names added) then stop executing function using return statement, otherwise read name in the list using scanf. 1st argument of scanf is %s hence it will read string, hence 2nd argument should be char array. list[0], list[1], list[2] ... list[19] are char array.<br />
After adding name to the list length is increased by 1. list[length] always gives next empty space in list. In the beginning list is empty hence list[length] (length=0) is empty. after adding a name at list[0] length becomes 1 now list[1] (length = 1) is empty.<br />
After that print_list function is defined, which reads length variable to know how many named are on the list. It prints all those names using for loop.Inside loop a variable serial_no is defined which can not be accessed outside for loop.<br />
<br />
In the end there is main function from where execution starts. Using if-else according to input variable either we add name to the list, or print list or print menu inside a while loop. While loop will continue till user enters 3.<br />
If you like this, share this with you friends in facebook, twitter,orkut.</div>
<div id="hindi">
आज Hindi के इस C/C++ programming language tutorial को आगे बढ़ाते हुए हम function के कुछ examples के द्वारा variable के scope के बारे में जानेंगे. अगर आपको <a href="http://programming-tutorial-hindi.blogspot.com/2010/10/c-hindi-tutorial-function.html">C function</a> और <a href="http://programming-tutorial-hindi.blogspot.com/2010/10/c-hindi-tutorialvariables-expressions.html">C variables</a> का ज्ञान नहीं है तो पहले इन्हें पढ़ लें.<br />
<br />
<b>What is scope</b><br />
C में किसी variable के scope का मतलब यह है कि उस variable को declare या define करने के बाद कहाँ कहाँ पर उसकी value पढ़ सकते हैं. अगर हमने किसी function के अंदर कोई variable x define किया है तो उस function के अंदर कहीं भी उस variable को read/write कर सकते हैं परन्तु किसी अन्य function के अंदर नहीं. अगर किसी loop के अंदर कोई variable define किया है तो उस loop के अंदर कहीं भी उस variable को read/write कर सकते हैं परन्तु उसके बाहर नहीं.<br />
इसे समझना बहुत आसान है. किसी भी {...} के अंदर define किया गया variable उसके अंदर कहीं भी read/write किया जा सकता है परन्तु उसके बाहर नहीं. इसे हम नीचे दिए गए example से समझेंगे.<br />
नीचे दिए गए program में हम एक register बनायेंगे जिसमेकोई भी नाम add कर सकते हैं और सभी नामों को print कर सकते हैं. program को run करने पर एक Menu आयेगा जिसके द्वारा हम उस register में name add कर सकते हैं या register के सभी नामो को print कर सकते हैं.<br />
<pre><span style="color: #000099;">#include <stdio.h></span><span style="color: #ff6633;">
char</span> list<b><span style="color: #663300;">[</span></b><span style="color: #999900;">10</span><b><span style="color: #663300;">][</span></b><span style="color: #999900;">20</span><b><span style="color: #663300;">];</span></b><span style="color: #ff6633;"> </span>
<span style="color: #ff6633;">int</span> length<b><span style="color: #663300;"> =</span></b><span style="color: #999900;"> 0</span><b><span style="color: #663300;">;</span></b>
<span style="color: #ff6633;">void</span> add_name<b><span style="color: #663300;">() {</span></b><span style="color: red;">
if</span><b><span style="color: #663300;">(</span></b>length<b><span style="color: #663300;"> ></span></b><b><span style="color: #663300;">=</span></b><span style="color: #999900;"> 10</span><b><span style="color: #663300;">) {</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"list is full\n"</span><b><span style="color: #663300;">);</span></b><span style="color: red;">
return</span><b><span style="color: #663300;">;
}</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"Enter the name: "</span><b><span style="color: #663300;">);</span></b>
scanf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"%s"</span><b><span style="color: #663300;">,</span></b> list<b><span style="color: #663300;">[</span></b>length<b><span style="color: #663300;">]);</span></b>
length<b><span style="color: #663300;"> =</span></b> length<b><span style="color: #663300;"> +</span></b><span style="color: #999900;"> 1</span><b><span style="color: #663300;">;</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"name added\n"</span><b><span style="color: #663300;">);
}</span></b>
<span style="color: #ff6633;">void</span> print_list<b><span style="color: #663300;">() {</span></b><span style="color: #ff6633;">
int</span> i<b><span style="color: #663300;"> =</span></b><span style="color: #999900;"> 0</span><b><span style="color: #663300;">;</span></b><span style="color: red;">
for</span><b><span style="color: #663300;">(</span></b>i<b><span style="color: #663300;">=</span></b><span style="color: #999900;">0</span><b><span style="color: #663300;">;</span></b> i<b><span style="color: #663300;"> < </span></b>length<b><span style="color: #663300;">;</span></b> i<b><span style="color: #663300;">++) {</span></b>
<span style="color: #ff6633;"> int</span> serial_no<b><span style="color: #663300;"> =</span></b> i<b><span style="color: #663300;"> +</span></b><span style="color: #999900;"> 1</span><b><span style="color: #663300;">;</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"%d : %s\n"</span><b><span style="color: #663300;">,</span></b> serial_no<b><span style="color: #663300;">,</span></b> list<b><span style="color: #663300;">[</span></b>i<b><span style="color: #663300;">]);
}
}</span></b>
<span style="color: #ff6633;">int</span><span style="color: #990000;"> main</span><b><span style="color: #663300;">() {</span></b><span style="color: #ff6633;">
int</span> input<b><span style="color: #663300;"> =</span></b><span style="color: #999900;"> 0</span><b><span style="color: #663300;">;</span></b><span style="color: red;">
while</span><b><span style="color: #663300;">(</span></b>input<b><span style="color: #663300;"> !=</span></b><span style="color: #999900;"> 3</span><b><span style="color: #663300;">) {</span></b>
<span style="color: red;"> if</span><b><span style="color: #663300;">(</span></b>input<b><span style="color: #663300;"> ==</span></b><span style="color: #999900;"> 0</span><b><span style="color: #663300;">) {</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"0 - Print this menu\n"</span><b><span style="color: #663300;">);</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"1 - add name to list\n"</span><b><span style="color: #663300;">);</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"2 - print list\n"</span><b><span style="color: #663300;">);</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"3 - quit program\n"</span><b><span style="color: #663300;">);
}</span></b><span style="color: red;">
else if</span><b><span style="color: #663300;">(</span></b>input<b><span style="color: #663300;"> ==</span></b><span style="color: #999900;"> 1</span><b><span style="color: #663300;">) {</span></b>
add_name<b><span style="color: #663300;">();
}</span></b><span style="color: red;">
else if</span><b><span style="color: #663300;">(</span></b>input<b><span style="color: #663300;"> ==</span></b><span style="color: #999900;"> 2</span><b><span style="color: #663300;">) {</span></b>
print_list<b><span style="color: #663300;">();
}</span></b>
<span style="color: red;"> else</span><b><span style="color: #663300;"> {</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"Wrong Input, try again\n"</span><b><span style="color: #663300;">);
}</span></b>
scanf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"%d"</span><b><span style="color: #663300;">, &</span></b>input<b><span style="color: #663300;">);
}
}</span></b></pre>
<br />
ऊपर दिए गए program को run करने पर शुरू में एक menu print होगा जो यह बताएगा कि क्या enter करने पर क्या होगा. List में नाम add करने के लिए 1 press करके enter करें, इसके बाद या पूछेगा कि कौन सा नाम add करना है. नाम type करके enter करें. फिर से दूसरा नाम add करना हो तो फिर से 1 enter करें. List print करने के लिए 2 Enter करें. पहले इस program को run करके देखें फिर इसे समझते हैं कि यह कैसे काम करता है.<br />
<br />
अब देखते हैं कि यह कैसे काम करता है. शुरू में हमने 2 variable list और length declare किया है. चूंकि ये दोनों variable किसी function के अंदर नहीं बल्कि सबसे बाहर हैं इसलिए इन्हें किसी भी function या loop के अंदर से read/write कर सकते हैं. नियम यह है कि variable जिस {} के अंदर declare किये गए हैं उससे बाहर कहीं भी access नहीं किये जा सकते, उसके अंदर कहीं भी access किये जा सकते हैं.<br />
अब आते हैं char list[10][20]; पर. अगर सिर्फ char list[10]; होता तो इसका मतलब यह होता कि list एक array है जिसमे 10 char आ सकते हैं. पर यहाँ char list[10][20]; है इसका मतलब है कि list एक array है जिसमे 20 char वाली 10 array आ सकती हैं. यहाँ 10 char की जगह 10 array आएँगी और हर एक array 20 char वाली होगी.<br />
अगर int a[2][3] होता तो इस तरीके से देख सकते हैं.<br />
<pre>a = { {1,2,3} , {4,5,6} };</pre>
<br />
यहाँ पर a के अंदर 2 array आ सकती हैं एक array a[0] में और एक a[1] में. उन दोनों array में 3 3 int आ सकते हैं.<br />
char list[10][20]; का use नाम store करने के लिए करेंगे. हम देख चुके हैं कि नाम string है जिसे char array में store करते हैं. यहाँ list में 10 char array आ सकती हैं इसलिए इसमें हम 10 नाम store कर सकते हैं. हर एक array में 20 char आ सकते हैं इसलिए हम वही नाम store कर सकते हैं जिसमे 20 या उससे कम letter हो. दूसरा variable length या बताता है कि currently list में कितने नाम हैं, जोकि शुरू में 0 है.<br />
इसके बाद add_name function है जिसे call करने पर यह list में एक नाम add कर देगा. पहले हम check करते हैं अगर list में 10 नाम आ चुके हैं तो return का use करके function को वहीँ समाप्त कर देते हैं, अन्यथा scanf का use करके list variable में सीधे नाम read कर लेते हैं. ध्यान दें कि scanf का पहला argument "%s" है जो कि string read करता है इसलिए दूसरा argument char array होना चाहिए. list[0], list[1], list[2] ... list[19] सभी 20 char वाली array हैं.<br />
list में नाम add होने के बाद हम length 1 बढ़ा देते हैं. list[length] हमें list में हमेशा अगली खाली जगह दे देगा क्योंकि शुरू में list खाली है इसलिए list[length] (length=0) वाली char array खाली है. एक नाम list[0] में add होने के बाद length = 1 हो जायेगा, अब list[length] (length = 1) वाली array खाली है.<br />
इसके बाद print_list function define किया है, जिसे length से यह पता चल जाता है कि list में कितने नाम हैं उतना ही for loop से print कर लेते हैं. यहाँ for loop के अंदर एक variable serial_no define किया है जो उस loop के बाहर से access नहीं होगा.<br />
<br />
अंत में main function है जहाँ program run होना start होता है. जिसमे if-else statement का use करके input variable की value के according list में नाम add करते हैं, list print करते हैं, menu print करते हैं. इस पुरे if-else को while loop के अंदर लिखा गया है, जो कि तब तक run होगा जब तक input की value 3 नहीं हो जाती. while loop खत्म होने से पहले input variable में scanf का use करके input read करते हैं, अगर input 3 है तो while loop खत्म होके program finish हो जायेगा otherwise while loop के अंदर फिर से आकर input value के according if-else का use करके list में नाम add करते हैं, list print करते हैं, menu print करते हैं और फिर से input variable में scanf का use करके input read करते हैं.<br />
अगले लेख में हम function के बारे में जानेंगे जो कि बहुत important है.</div>
<div style="text-align: center;">
<a href="http://programming-tutorial-hindi.blogspot.com/">Index</a> <a href="http://programming-tutorial-hindi.blogspot.com/2010/11/c-hindi-tutorial-break-and-continue.html" title="c hindi tutorial: break and continue">Next(break & continue)</a></div>
</div>
Hridayesh Guptahttp://www.blogger.com/profile/09764967341453023170noreply@blogger.comtag:blogger.com,1999:blog-8683328491687928872.post-42591476622954777762010-10-31T14:59:00.005+05:302012-07-19T09:06:48.214+05:30function<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="btn_sw" href="#" id="hi_btn" onclick="javascript:return showhindi(this);">
Read in Hindi</div>
<div class="btn_sw" href="#" id="en_btn" onclick="javascript:return showeng(this);">
Read in English</div>
<br />
<div id="eng">
Moving forward with this C/C++ programming language tutorial today we will learn about function. To do some task many times and avoid writing same program multiple times we can use function. Remind that in <a href="http://programming-tutorial-hindi.blogspot.com/2010/10/c-hindi-tutorial-if-else-statement.html" target="_self">if else statement </a>we have written a program which calculates percent and division and prints it based on total marks and obtained marks. Now if we want to calculate percent and division for 5 students, Then one way is to modify program and run 5 times which is not good way(it can be 100 students). Another way is to copy-paste part of code which calculates division and percent 5 times which is also not good way.<br />
Use of function can solve those problems. We can write a function which gives percent given total marks and obtained marks. Now we will see program to print percentage only. Later we will see program to print division too.<br />
function is define in program given below. Do not run this program, it will not run.<br />
<pre><span style="color: #ff6633;">int</span> get_percent<b><span style="color: #663300;">(</span></b><span style="color: #ff6633;">int</span> total marks<b><span style="color: #663300;">,</span></b><span style="color: #ff6633;"> int</span> obtained marks<b><span style="color: #663300;">) {</span></b><span style="color: #ff6633;">
int</span> percent<b><span style="color: #663300;"> =</span></b> obtained_marks<b><span style="color: #663300;">*</span></b><span style="color: #999900;">100</span><b><span style="color: #663300;">/</span></b>total_marks<b><span style="color: #663300;">;</span></b><span style="color: red;">
return</span> percent<b><span style="color: #663300;">;
}</span></b></pre>
<br />
int is written 1st word which tells that this function will give us int type of value. function name comes after that which is get_percent here. After that inside ()input variable comes which is called parameter or argument. while writing input variable we have tell what type of value they will hold(int,char etc) and by what name function will refer to them. here both input are int. In function we can give any number of inputs. If function does not require any argument then do not write anything inside (). here we have 2 inputs. After that inside {} program is written to achieve functionality. In given function percent have been calculated using those 2 input parameters. In the end desired value is returned using return. anything written after return statement won't run because at that point function will know what value to return and proceeding further.<br />
Lets see below how we use the above function.<br />
<pre><span style="color: #000099;">#include <stdio.h>
</span><span style="color: #ff6633;">int</span> get_percent<b><span style="color: #663300;">(</span></b><span style="color: #ff6633;">int</span> total_marks<b><span style="color: #663300;">,</span></b><span style="color: #ff6633;"> int</span> obtained_marks<b><span style="color: #663300;">) {</span></b><span style="color: #ff6633;">
int</span> percent<b><span style="color: #663300;"> =</span></b> obtained_marks<b><span style="color: #663300;">*</span></b><span style="color: #999900;">100</span><b><span style="color: #663300;">/</span></b>total_marks<b><span style="color: #663300;">;</span></b><span style="color: red;">
return</span> percent<b><span style="color: #663300;">;
}</span></b><span style="color: #ff6633;">
int</span><span style="color: #990000;"> main</span><b><span style="color: #663300;">() {</span></b><span style="color: #ff6633;">
int</span> percent<b><span style="color: #663300;">;</span></b>
percent<b><span style="color: #663300;"> =</span></b> get_percent<b><span style="color: #663300;">(</span></b><span style="color: #999900;">500</span><b><span style="color: #663300;">,</span></b><span style="color: #999900;"> 360</span><b><span style="color: #663300;">);</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"Percent is %d\n"</span><b><span style="color: #663300;">,</span></b> percent<b><span style="color: #663300;">);</span></b>
percent<b><span style="color: #663300;"> =</span></b> get_percent<b><span style="color: #663300;">(</span></b><span style="color: #999900;">500</span><b><span style="color: #663300;">,</span></b><span style="color: #999900;"> 340</span><b><span style="color: #663300;">);</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"Percent is %d\n"</span><b><span style="color: #663300;">,</span></b> percent<b><span style="color: #663300;">);</span></b>
scanf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"%d"</span><b><span style="color: #663300;">, &</span></b>percent<b><span style="color: #663300;">);</span></b><span style="color: red;">
return</span><span style="color: #999900;"> 0</span><b><span style="color: #663300;">;
}</span></b></pre>
<br />
Firstly we have defined function named get_percent, which returns int value and receives 2 parameters of int type as we have seen above in program.<br />
Program starts to execute from main(). In the main int type percent variable have been defined. Then we called function get_percent which is defined above. We have passed 2 parameters 500 and 360 to this function when calling. Note that order of parameters in function call is very important. According to function defined above value of total_marks will be value 500 and obtained_marks will be 360 when executing get_percent function. On the basis of these values get_percent function will return 80.<br />
percent = get_percent(500, 360);<br />
Writing this will assign percent the return value of function call. Then in the main function we are printing value of percent.<br />
main is also a function which does not take any parameter hence we write main(), nothing in the braces, and main returns int, hence we write return 0 at the end of main function. Whenever we run a c/c++ program, it's main function is called. Program finishes when we reach at the end of main. If you write such a program which does not have main function then running that will give error that main method not found.<br />
<br />
Lets see and run a program which gives the percent as well well prints it.<br />
<pre><span style="color: #000099;">#include <stdio.h>
</span><span style="color: #ff6633;">void</span> get_percent<b><span style="color: #663300;">(</span></b><span style="color: #ff6633;">int</span> total_marks<b><span style="color: #663300;">,</span></b><span style="color: #ff6633;"> int</span> obtained_marks<b><span style="color: #663300;">) {</span></b>
<span style="color: #ff6633;"> int</span> percent<b><span style="color: #663300;"> =</span></b> obtained_marks<b><span style="color: #663300;">*</span></b><span style="color: #999900;">100</span><b><span style="color: #663300;">/</span></b>total_marks<b><span style="color: #663300;">;</span></b><span style="color: red;">
if</span><b><span style="color: #663300;">(</span></b>percent<b><span style="color: #663300;"> >=</span></b><span style="color: #999900;"> 60</span><b><span style="color: #663300;">) {</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"Congrats!! You passed in 1st division.\n"</span><b><span style="color: #663300;">);</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"Your percentage is %d.\n"</span><b><span style="color: #663300;">,</span></b>percent<b><span style="color: #663300;">);
}</span></b><span style="color: red;">
else if</span><b><span style="color: #663300;">(</span></b>percent<b><span style="color: #663300;"> >=</span></b><span style="color: #999900;"> 45</span><b><span style="color: #663300;">) {</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"You passed in 2nd division.\n"</span><b><span style="color: #663300;">);</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"Your percentage is %d.\n"</span><b><span style="color: #663300;">,</span></b>percent<b><span style="color: #663300;">);
}</span></b><span style="color: red;">
else if</span><b><span style="color: #663300;">(</span></b>percent<b><span style="color: #663300;"> >=</span></b><span style="color: #999900;"> 33</span><b><span style="color: #663300;">) {</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"You just passed in 3rd division.\n"</span><b><span style="color: #663300;">);</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"Your percentage is %d.\n"</span><b><span style="color: #663300;">,</span></b>percent<b><span style="color: #663300;">);
}</span></b><span style="color: red;">
else</span><b><span style="color: #663300;"> {</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"Sorry! you failed.\n"</span><b><span style="color: #663300;">);</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"Your percentage is %d.\n"</span><b><span style="color: #663300;">,</span></b>percent<b><span style="color: #663300;">);
}
}</span></b><span style="color: #ff6633;">
int</span><span style="color: #990000;"> main</span><b><span style="color: #663300;">() {</span></b>
get_percent<b><span style="color: #663300;">(</span></b><span style="color: #999900;">500</span><b><span style="color: #663300;">,</span></b><span style="color: #999900;"> 360</span><b><span style="color: #663300;">);</span></b>
get_percent<b><span style="color: #663300;">(</span></b><span style="color: #999900;">500</span><b><span style="color: #663300;">,</span></b><span style="color: #999900;"> 340</span><b><span style="color: #663300;">);</span></b>
<span style="color: #ff6633;"> int</span> abc<b><span style="color: #663300;">;</span></b>
scanf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"%d"</span><b><span style="color: #663300;">, &</span></b>abc<b><span style="color: #663300;">);</span></b><span style="color: red;">
return</span><span style="color: #999900;"> 0</span><b><span style="color: #663300;">;
}</span></b></pre>
<br />
In this example get_percent function also prints division. Difference with previous function is that here we are printing inside get_percent function hence no need to print in main function. return type of this function is void that means this function does not need to return anything, hence in main function this function is just called without storing return value to some variable.<br />
Exercise:<br />
1. Write a function print_month which takes int parameter as month number and that function prints name of month. This function should not return anything, just need to print. Call that function with some parameter from main function.<br />
2. Modify above program to take input at runtime using scanf and pass that value to call function defined above.<br />
We will see some more examples on next topic.<br />
If you liked this blog, tell your friends also because it can useful to them also!!</div>
<div id="hindi">
आज Hindi के इस C/C++ programming language tutorial को आगे बढ़ाते हुए हम function के बारे में और जानेंगे. किसी एक काम को अलग अलग समय बार बार करवाने के लिए function का use करते हैं. याद करे कि <a href="http://programming-tutorial-hindi.blogspot.com/2010/10/c-hindi-tutorial-if-else-statement.html" target="_self">if else statement </a>वाले topic में हमने एक program लिखा था जो कि total marks और obtained marks के आधार पर percent और division निकाल कर print करता था. अब अगर हमें 5 students के लिए percentage और division निकलना है तो एक तरीका यह है कि हम पहले वाले program में total marks और obtained marks को बार बार modify करें और बार बार program को compile करके run करें. पर इसमें बहुत time लग जायेगा. एक और तरीका यह है कि program का वह भाग जो percentage निकाल कर division print करता है उसे 5 बार copy-paste करें.<br />
इसे आसानी से करने का सबसे अच्छा तरीका है function का use. हम एक ऐसा function लिखेंगे जिसे total marks और obtained marks देने पर वह हमें percentage देदे. अभी percentage print करने का program ही देखते हैं, उसके बाद division print करने का program भी देखेंगे.<br />
नीचे लिखे हुए program को देखिये जिसमे हमने function define किया है. पर इसे run न करे क्योंकि यह run नहीं होगा. इसके बाद function call करने और run करके का तरीका देखेंगे.<br />
<pre><span style="color: #ff6633;">int</span> get_percent<b><span style="color: #663300;">(</span></b><span style="color: #ff6633;">int</span> total marks<b><span style="color: #663300;">,</span></b><span style="color: #ff6633;"> int</span> obtained marks<b><span style="color: #663300;">) {</span></b><span style="color: #ff6633;">
int</span> percent<b><span style="color: #663300;"> =</span></b> obtained_marks<b><span style="color: #663300;">*</span></b><span style="color: #999900;">100</span><b><span style="color: #663300;">/</span></b>total_marks<b><span style="color: #663300;">;</span></b><span style="color: red;">
return</span> percent<b><span style="color: #663300;">;
}</span></b></pre>
<br />
सबसे पहले int लिखा गया है जोकि यह बताता है कि function किस तरह का मान हमें return करेगा. इस example में function int type का मान हमें return करेगा. उसके बाद function का नाम लिखते हैं, जोकि यहाँ पर get_percent है. उसके बाद () में function के input variable लिखते हैं जिन्हें parameter या argument कहते हैं input variable लिखते समय यह भी बताना पड़ता है कि वो किस type के हैं और उन्हें function के अंदर किस नाम से access करेंगे. यहाँ दोनों input int हैं. किसी function में हम कितने भी input दे सकते हैं, या फिर अगर एक भी input नहीं देना चाहते तो () के अंदर कुछ नहीं लिखते. यहाँ 2 input दिए गए हैं. उसके बाद {} के अंदर वो लिखते हैं जो function काम करेगा. यहाँ हमने दिए गए input का उपयोग करके percent निकाला है. अंत में return करते हैं.<br />
शुरू में लिखा हुआ int यह बता रहा था कि यह function int type की value हमें देगा(return करेगा). अंत में return लिखकर हम percent return कर रहे हैं जो int type का ही है. अगर return statement के बाद कुछ लिखा है तो वह run नहीं होगा, क्योंकि return आते ही function को यह पता लग जाता है कि क्या value return करना है और function वह value return करके उसी समय खत्म हो जाता है.<br />
अब यह देखते हैं कि इस function का use कैसे करेंगे. नीचे दिए गए program को run करके देखें.<br />
<pre><span style="color: #000099;">#include <stdio.h>
</span><span style="color: #ff6633;">int</span> get_percent<b><span style="color: #663300;">(</span></b><span style="color: #ff6633;">int</span> total_marks<b><span style="color: #663300;">,</span></b><span style="color: #ff6633;"> int</span> obtained_marks<b><span style="color: #663300;">) {</span></b><span style="color: #ff6633;">
int</span> percent<b><span style="color: #663300;"> =</span></b> obtained_marks<b><span style="color: #663300;">*</span></b><span style="color: #999900;">100</span><b><span style="color: #663300;">/</span></b>total_marks<b><span style="color: #663300;">;</span></b><span style="color: red;">
return</span> percent<b><span style="color: #663300;">;
}</span></b><span style="color: #ff6633;">
int</span><span style="color: #990000;"> main</span><b><span style="color: #663300;">() {</span></b><span style="color: #ff6633;">
int</span> percent<b><span style="color: #663300;">;</span></b>
percent<b><span style="color: #663300;"> =</span></b> get_percent<b><span style="color: #663300;">(</span></b><span style="color: #999900;">500</span><b><span style="color: #663300;">,</span></b><span style="color: #999900;"> 360</span><b><span style="color: #663300;">);</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"Percent is %d\n"</span><b><span style="color: #663300;">,</span></b> percent<b><span style="color: #663300;">);</span></b>
percent<b><span style="color: #663300;"> =</span></b> get_percent<b><span style="color: #663300;">(</span></b><span style="color: #999900;">500</span><b><span style="color: #663300;">,</span></b><span style="color: #999900;"> 340</span><b><span style="color: #663300;">);</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"Percent is %d\n"</span><b><span style="color: #663300;">,</span></b> percent<b><span style="color: #663300;">);</span></b>
scanf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"%d"</span><b><span style="color: #663300;">, &</span></b>percent<b><span style="color: #663300;">);</span></b><span style="color: red;">
return</span><span style="color: #999900;"> 0</span><b><span style="color: #663300;">;
}</span></b></pre>
<br />
सबसे पहले हमने function define किया है किसका नाम है get_percent, जो int type की value return करेगा और 2 int type के parameter लेगा जैसे कि हम ऊपर भी देख चुके हैं. फिर<br />
main() के अंदर से program run होना start होता है जैसा कि हम अभी तक देखते आये हैं. main के अंदर पहले int type का percent variable define किया है. उसके बाद get_percent function call किया है जो ऊपर define किया था. इस function को हमने 2 parameter 500 और 360 दिए हैं. यहाँ parameter का क्रम भी important है. इस function को call करने पर ऊपर वाला function run होना शुरू हो जायेगा, जिसमे total_marks की value 500 और obtained_marks की value 360 चली जायेगी. इन values के basis पर get_percent percent की value 80 निकालकर return कर देगा.<br />
percent = get_percent(500, 360);<br />
यह लिखने पर get_percent जो भी return करेगा वह percent में store जायेगा. उसके बाद हम main() के अंदर ही percent print कर रहे हैं. इसी तरह एक बार और get_percent function को call करके जो value आती है उसे percent variable में store करके print कर रहे हैं.<br />
यहाँ एक बात note करें कि main भी एक function है जो कोई भी parameter नहीं लेता(इसलिए main() लिखा है () में कुछ नहीं लिखा) और int return करता है इसीलिए हम अंत में हर बार return 0; लिखते आ रहे हैं. जब हम कोई भी C program run करते हैं तो सिर्फ उसका main function call होता है. main के अंदर हम जो जो function call करते हैं वो उसी sequence में call होते हैं. अंत में जब main function के अंत में पहुँच जाते हैं तो program finish हो जाता है. अगर आप किसी ऐसे program को run करे जिसमे main function न हो तो वह कुछ इस तरह कि error देगा कि main method not found.<br />
<br />
अब हम एक और program देखते हैं जो division भी print करे. इसे run करके देखें.<br />
<pre><span style="color: #000099;">#include <stdio.h>
</span><span style="color: #ff6633;">void</span> get_percent<b><span style="color: #663300;">(</span></b><span style="color: #ff6633;">int</span> total_marks<b><span style="color: #663300;">,</span></b><span style="color: #ff6633;"> int</span> obtained_marks<b><span style="color: #663300;">) {</span></b>
<span style="color: #ff6633;"> int</span> percent<b><span style="color: #663300;"> =</span></b> obtained_marks<b><span style="color: #663300;">*</span></b><span style="color: #999900;">100</span><b><span style="color: #663300;">/</span></b>total_marks<b><span style="color: #663300;">;</span></b><span style="color: red;">
if</span><b><span style="color: #663300;">(</span></b>percent<b><span style="color: #663300;"> >=</span></b><span style="color: #999900;"> 60</span><b><span style="color: #663300;">) {</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"Congrats!! You passed in 1st division.\n"</span><b><span style="color: #663300;">);</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"Your percentage is %d.\n"</span><b><span style="color: #663300;">,</span></b>percent<b><span style="color: #663300;">);
}</span></b><span style="color: red;">
else if</span><b><span style="color: #663300;">(</span></b>percent<b><span style="color: #663300;"> >=</span></b><span style="color: #999900;"> 45</span><b><span style="color: #663300;">) {</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"You passed in 2nd division.\n"</span><b><span style="color: #663300;">);</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"Your percentage is %d.\n"</span><b><span style="color: #663300;">,</span></b>percent<b><span style="color: #663300;">);
}</span></b><span style="color: red;">
else if</span><b><span style="color: #663300;">(</span></b>percent<b><span style="color: #663300;"> >=</span></b><span style="color: #999900;"> 33</span><b><span style="color: #663300;">) {</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"You just passed in 3rd division.\n"</span><b><span style="color: #663300;">);</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"Your percentage is %d.\n"</span><b><span style="color: #663300;">,</span></b>percent<b><span style="color: #663300;">);
}</span></b><span style="color: red;">
else</span><b><span style="color: #663300;"> {</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"Sorry! you failed.\n"</span><b><span style="color: #663300;">);</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"Your percentage is %d.\n"</span><b><span style="color: #663300;">,</span></b>percent<b><span style="color: #663300;">);
}
}</span></b><span style="color: #ff6633;">
int</span><span style="color: #990000;"> main</span><b><span style="color: #663300;">() {</span></b>
get_percent<b><span style="color: #663300;">(</span></b><span style="color: #999900;">500</span><b><span style="color: #663300;">,</span></b><span style="color: #999900;"> 360</span><b><span style="color: #663300;">);</span></b>
get_percent<b><span style="color: #663300;">(</span></b><span style="color: #999900;">500</span><b><span style="color: #663300;">,</span></b><span style="color: #999900;"> 340</span><b><span style="color: #663300;">);</span></b>
<span style="color: #ff6633;"> int</span> abc<b><span style="color: #663300;">;</span></b>
scanf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"%d"</span><b><span style="color: #663300;">, &</span></b>abc<b><span style="color: #663300;">);</span></b><span style="color: red;">
return</span><span style="color: #999900;"> 0</span><b><span style="color: #663300;">;
}</span></b></pre>
<br />
इस example में get_percent function division भी print कर रहा है. इसमें पहले वाले function से थोडा difference यह है कि यह function ही सब कुछ print कर रहा है इसलिए main में अलग से print करने कि जरुरत नहीं है. शुरू में void लिखा गया है इसका मतलब है कि यह function कुछ return नहीं करेगा, इसलिए main में function को सिर्फ call किया गया है, जबकि पहले वाले example में जो value function return कर रहा था उसे एक variable में store कर रहे थे.<br />
Exercise:<br />
1. एक function print_month लिखो जिसे एक int parameter दे जो किसी महीने का number होगा और वह function महीने का नाम print कर दे. जैसे अगर हम उसे 10 दे तो वह October print कर दे. इस function को कुछ return नहीं करना चाहिए, सिर्फ print करना चाहिए. उसके बाद इस function को main से अलग अलग parameter देके call करें.<br />
2. ऊपर वाले program में main में पहले scanf का use करके input ले और इस input parameter के साथ print_month call करें.<br />
अगले लेख में हम कुछ और example देखेंगे.<br />
अगर आपको यह लेख पसंद आया हो तो अपने दोस्तों को भी बताएं क्योंकि ये उनके लिए भी उपयोगी सिद्ध हो सकता है !! नीचे दिए गए link के माध्यम से इसे आसानी से facebook twitter और Google Buzz पर भी Share कर सकते हैं.</div>
<br />
<div style="text-align: center;">
<a href="http://programming-tutorial-hindi.blogspot.com/">Index</a> <a href="http://programming-tutorial-hindi.blogspot.com/2010/11/c-hindi-tutorial-function-example.html" title="c hindi tutorial: function example">Next(scope of variable)</a></div>
</div>
Hridayesh Guptahttp://www.blogger.com/profile/09764967341453023170noreply@blogger.comtag:blogger.com,1999:blog-8683328491687928872.post-51626879754425790152010-10-30T06:45:00.004+05:302012-07-19T09:07:26.816+05:30string<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="btn_sw" href="#" id="hi_btn" onclick="javascript:return showhindi(this);">
Read in Hindi</div>
<div class="btn_sw" href="#" id="en_btn" onclick="javascript:return showeng(this);">
Read in English</div>
<div id="eng">
Going forward with this C language tutorial in hindi today we will learn about string. It is necessary to know about <a href="http://programming-tutorial-hindi.blogspot.com/2010/10/c-hindi-tutorial-array-and-input.html">Array</a> and <a href="http://programming-tutorial-hindi.blogspot.com/2010/10/c-hindi-tutorial-variables-expressions.html">char type variable</a>.<br />
<b> What is String</b><br />
We have known about char type variable that it stores a character. Now if we want to store a word(Which consists of more than one characters), how should we do it? For this we will make a char array, because array can store more than one variable. Hence with help of array we can store words and sentences. Lets see with the help of example how will we store word "hindi". 3 different ways are shown, all 3 are important to understand concept well.<br />
<pre><code><span style="color: #000099;">#include <stdio.h>
</span><span style="color: #ff6633;">
int</span><span style="color: #990000;"> main</span><b><span style="color: #663300;">() {</span></b><span style="color: #ff6633;">
char</span> w<b><span style="color: #663300;">[</span></b><span style="color: #999900;">6</span><b><span style="color: #663300;">];</span></b>
w<b><span style="color: #663300;">[</span></b><span style="color: #999900;">0</span><b><span style="color: #663300;">] =</span></b><span style="color: #009900;"> 'h'</span><b><span style="color: #663300;">;</span></b>
w<b><span style="color: #663300;">[</span></b><span style="color: #999900;">1</span><b><span style="color: #663300;">] =</span></b><span style="color: #009900;"> 'i'</span><b><span style="color: #663300;">;</span></b>
w<b><span style="color: #663300;">[</span></b><span style="color: #999900;">2</span><b><span style="color: #663300;">] =</span></b><span style="color: #009900;"> 'n'</span><b><span style="color: #663300;">;</span></b>
w<b><span style="color: #663300;">[</span></b><span style="color: #999900;">3</span><b><span style="color: #663300;">] =</span></b><span style="color: #009900;"> 'd'</span><b><span style="color: #663300;">;</span></b>
w<b><span style="color: #663300;">[</span></b><span style="color: #999900;">4</span><b><span style="color: #663300;">] =</span></b><span style="color: #009900;"> 'i'</span><b><span style="color: #663300;">;</span></b>
w<b><span style="color: #663300;">[</span></b><span style="color: #999900;">5</span><b><span style="color: #663300;">] =</span></b><span style="color: #009900;"> '<span style="color: #009900;">\</span>0'</span><b><span style="color: #663300;">;</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"Word we stored is %s \n"</span><b><span style="color: #663300;">,</span></b> w<b><span style="color: #663300;">);</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"1st letter of array is %c \n"</span><b><span style="color: #663300;">,</span></b> w<b><span style="color: #663300;">[</span></b><span style="color: #999900;">0</span><b><span style="color: #663300;">]);</span></b>
scanf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"%s"</span><b><span style="color: #663300;">,</span></b> w<b><span style="color: #663300;">);</span></b><span style="color: red;">
return</span><span style="color: #999900;"> 0</span><b><span style="color: #663300;">;
}</span></b></code></pre>
<br />
Rum then program given above and it will print<br />
Word we stored is hindi<br />
1st letter of array is h<br />
Now lets see how it works. 1st we have made a char array which can store upto 6 char. Now as we put values in array, same way we put h in 1st place, then i and so on. Notice that last place w[5] have <span style="color: black;">\</span>0 which is not 2 char and it tells that here the word ends. As \n is line break character after which print goes to new line, similarly characters after <span style="color: black;">\</span>0 does not get printed. To place this character we have made 6 char array otherwise 5 char array was enough for the word hindi. One more thing to note that %s has been used to print string. w is a string but w[0], w[1]... are characters. In 1st printf statement string w is printed hence %s is used but in 2nd printf statement char w[0] is printed hence is %c used.<br />
Now see the program below which works same way as above.<br />
<pre><code><span style="color: #000099;">#include <stdio.h>
</span><span style="color: #ff6633;">
int</span><span style="color: #990000;"> main</span><b><span style="color: #663300;">() {</span></b><span style="color: #ff6633;">
char</span> w<b><span style="color: #663300;">[] = {</span></b><span style="color: #009900;">'h'</span><b><span style="color: #663300;">,</span></b><span style="color: #009900;"> 'i'</span><b><span style="color: #663300;">,</span></b><span style="color: #009900;"> 'n'</span><b><span style="color: #663300;">,</span></b><span style="color: #009900;"> 'd'</span><b><span style="color: #663300;">,</span></b><span style="color: #009900;"> 'i'</span><b><span style="color: #663300;">,</span></b><span style="color: #009900;"> '<span style="color: #009900;">\</span>0'</span><b><span style="color: #663300;">};</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"Word we stored is %s \n"</span><b><span style="color: #663300;">,</span></b> w<b><span style="color: #663300;">);</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"1st letter of array is %c \n"</span><b><span style="color: #663300;">,</span></b> w<b><span style="color: #663300;">[</span></b><span style="color: #999900;">0</span><b><span style="color: #663300;">]);</span></b>
scanf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"%s"</span><b><span style="color: #663300;">,</span></b> w<b><span style="color: #663300;">);</span></b><span style="color: red;">
return</span><span style="color: #999900;"> 0</span><b><span style="color: #663300;">;
}</span></b>
</code></pre>
<br />
The only difference in this program with previous one is that in this in same line array is defined as well as values are filled in array, Similar to defining int var and assigning value in same line using int x = 1; In this way we do not need to specify length of array since it will calculate length according to how many places we have filled on same line.<br />
Incorrect way: char w[6] = {'h', 'i', 'n', 'd', 'i', '<span style="color: black;">\</span>0'};<br />
Correct way: char w[] = {'h', 'i', 'n', 'd', 'i', '<span style="color: black;">\</span>0'};<br />
<br />
Now lets see one more program which also works same as above.<br />
<pre><code><span style="color: #000099;">#include <stdio.h>
</span><span style="color: #ff6633;">
int</span><span style="color: #990000;"> main</span><b><span style="color: #663300;">() {</span></b><span style="color: #ff6633;">
char</span> w<b><span style="color: #663300;">[] =</span></b><span style="color: #009900;"> "hindi"</span><b><span style="color: #663300;">;</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"Word we stored is %s \n"</span><b><span style="color: #663300;">,</span></b> w<b><span style="color: #663300;">);</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"1st letter of array is %c \n"</span><b><span style="color: #663300;">,</span></b> w<b><span style="color: #663300;">[</span></b><span style="color: #999900;">0</span><b><span style="color: #663300;">]);</span></b>
scanf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"%s"</span><b><span style="color: #663300;">,</span></b> w<b><span style="color: #663300;">);</span></b><span style="color: red;">
return</span><span style="color: #999900;"> 0</span><b><span style="color: #663300;">;
}</span></b>
</code></pre>
<br />
Only difference in this with above one is that in this program to make char array a shortcut is used. This kind of shortcut can be used only for char array, not other kind of arrays.<br />
char w[] = "hindi";<br />
Writing this will make char array w. 1st element of this array w[0] is 'h', 2nd element w[1] is 'i' ... so on. last element w[5] is '<span style="color: black;">\</span>0'. In this syntax <span style="color: black;">\</span>0 does not need to write explicitly.<br />
<br />
Its enough for now, but string is widely used and we will know more about it as we go on.<br />
If you like this tutorial, share it with your friends on facebook, orkut, twitter etc.</div>
<div id="hindi">
C language tutorial in hindi को आगे बढ़ाते हुए आज हम string के बारे में जानेंगे. इससे पहले <a href="http://programming-tutorial-hindi.blogspot.com/2010/10/c-hindi-tutorial-array-and-input.html">Array</a> और <a href="http://programming-tutorial-hindi.blogspot.com/2010/10/c-hindi-tutorial-variables-expressions.html">char type variable</a> के बारे में जानना आवश्यक है.<br />
<b> What is String</b><br />
हम char variable के बारे में जान चुके हैं कि यह variable किसी भी एक अक्षर(letter) को store करता है. अब अगर मान लीजिए कि हमें हमें एक word(जो एक से ज्यादा अक्षरों से मिलकर बना होगा) को store करना है तो उसे कैसे करेंगे? इसके लिए हम char की एक array बनायेंगे, चूंकि array एक ही तरह के एक से ज्यादा variable store कर सकता है अतः इसकी help से हम word या sentence store कर सकते हैं. examples की help से हम word "hindi" को store करने और उसे print करने के 3 अलग अलग तरीके देखते हैं और ये तीनों ही important हैं.<br />
<pre><code><span style="color: #000099;">#include <stdio.h>
</span><span style="color: #ff6633;">
int</span><span style="color: #990000;"> main</span><b><span style="color: #663300;">() {</span></b><span style="color: #ff6633;">
char</span> w<b><span style="color: #663300;">[</span></b><span style="color: #999900;">6</span><b><span style="color: #663300;">];</span></b>
w<b><span style="color: #663300;">[</span></b><span style="color: #999900;">0</span><b><span style="color: #663300;">] =</span></b><span style="color: #009900;"> 'h'</span><b><span style="color: #663300;">;</span></b>
w<b><span style="color: #663300;">[</span></b><span style="color: #999900;">1</span><b><span style="color: #663300;">] =</span></b><span style="color: #009900;"> 'i'</span><b><span style="color: #663300;">;</span></b>
w<b><span style="color: #663300;">[</span></b><span style="color: #999900;">2</span><b><span style="color: #663300;">] =</span></b><span style="color: #009900;"> 'n'</span><b><span style="color: #663300;">;</span></b>
w<b><span style="color: #663300;">[</span></b><span style="color: #999900;">3</span><b><span style="color: #663300;">] =</span></b><span style="color: #009900;"> 'd'</span><b><span style="color: #663300;">;</span></b>
w<b><span style="color: #663300;">[</span></b><span style="color: #999900;">4</span><b><span style="color: #663300;">] =</span></b><span style="color: #009900;"> 'i'</span><b><span style="color: #663300;">;</span></b>
w<b><span style="color: #663300;">[</span></b><span style="color: #999900;">5</span><b><span style="color: #663300;">] =</span></b><span style="color: #009900;"> '<span style="color: #009900;">\</span>0'</span><b><span style="color: #663300;">;</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"Word we stored is %s \n"</span><b><span style="color: #663300;">,</span></b> w<b><span style="color: #663300;">);</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"1st letter of array is %c \n"</span><b><span style="color: #663300;">,</span></b> w<b><span style="color: #663300;">[</span></b><span style="color: #999900;">0</span><b><span style="color: #663300;">]);</span></b>
scanf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"%s"</span><b><span style="color: #663300;">,</span></b> w<b><span style="color: #663300;">);</span></b><span style="color: red;">
return</span><span style="color: #999900;"> 0</span><b><span style="color: #663300;">;
}</span></b></code></pre>
<br />
ऊपर दिए गए program को run करके output देखें. जो कि print करेगा<br />
Word we stored is hindi<br />
1st letter of array is h<br />
अब इसे देखते हैं कि यह काम कैसे करता है. हमने char की एक array बनाई है जिसमे 6 char आ सकते हैं. इसके बाद जिस तरह से हम array में values डालते हैं उसी तरह से इसमें भी सबसे पहले स्थान पर h फिर i इस तरह से value डाल दी हैं. ध्यान दे कि अंतिम स्थान w[5] पर <span style="color: black;">\</span>0 है जो कि 2 char नहीं बल्कि 1 char है, जो कि यह बताता है यह इस word का अंतिम letter है. जिस तरह से \n line break character है जिसके बाद अगली लाइन में print होता है उसी तरह <span style="color: black;">\</span>0 के बाद आने वाले letter read या print नहीं होते . इसी को जगह देने के लिए हमने 6 char वाली array बनायीं थी वरना hindi शब्द में 5 char ही हैं. अब इसमें यह भी ध्यान दें कि string को print करने के लिए %s का use किया जाता है. w एक string है जबकि w[0], w[1]... सभी char हैं. पहले वाले printf statement में string w को print किया है इसलिए %s का use किया है जबकि दूसरे वाले printf statement में char w[0] को print किया है इसलिए %c का use किया है.<br />
अब नीचे वाला program देखिये जोकि बिलकुल पहले वाले कि तरह काम करता है.<br />
<pre><code><span style="color: #000099;">#include <stdio.h>
</span><span style="color: #ff6633;">
int</span><span style="color: #990000;"> main</span><b><span style="color: #663300;">() {</span></b><span style="color: #ff6633;">
char</span> w<b><span style="color: #663300;">[] = {</span></b><span style="color: #009900;">'h'</span><b><span style="color: #663300;">,</span></b><span style="color: #009900;"> 'i'</span><b><span style="color: #663300;">,</span></b><span style="color: #009900;"> 'n'</span><b><span style="color: #663300;">,</span></b><span style="color: #009900;"> 'd'</span><b><span style="color: #663300;">,</span></b><span style="color: #009900;"> 'i'</span><b><span style="color: #663300;">,</span></b><span style="color: #009900;"> '<span style="color: #009900;">\</span>0'</span><b><span style="color: #663300;">};</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"Word we stored is %s \n"</span><b><span style="color: #663300;">,</span></b> w<b><span style="color: #663300;">);</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"1st letter of array is %c \n"</span><b><span style="color: #663300;">,</span></b> w<b><span style="color: #663300;">[</span></b><span style="color: #999900;">0</span><b><span style="color: #663300;">]);</span></b>
scanf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"%s"</span><b><span style="color: #663300;">,</span></b> w<b><span style="color: #663300;">);</span></b><span style="color: red;">
return</span><span style="color: #999900;"> 0</span><b><span style="color: #663300;">;
}</span></b>
</code></pre>
<br />
इसमें पहले वाले से सिर्फ इतना difference है कि इसमें एक ही line में array को define भी कर रहे हैं और उसमे values भी डाल रहे हैं जिस तरह से किसी variable जैसे int को एक ही line में define करने और value देने के लिए int x = 1; लिखते हैं. इस तरीके से array define करते समय array की length नहीं बताना पड़ती(अर्थात ये नहीं लिखना पड़ता कि array में कितने element आयेंगे)<br />
गलत तरीका char w[6] = {'h', 'i', 'n', 'd', 'i', '<span style="color: black;">\</span>0'};<br />
सही तरीका char w[] = {'h', 'i', 'n', 'd', 'i', '<span style="color: black;">\</span>0'};<br />
<br />
अब एक और program देखते हैं वो भी बिलकुल पहले वाले कि तरह काम करता है.<br />
<pre><code><span style="color: #000099;">#include <stdio.h>
</span><span style="color: #ff6633;">
int</span><span style="color: #990000;"> main</span><b><span style="color: #663300;">() {</span></b><span style="color: #ff6633;">
char</span> w<b><span style="color: #663300;">[] =</span></b><span style="color: #009900;"> "hindi"</span><b><span style="color: #663300;">;</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"Word we stored is %s \n"</span><b><span style="color: #663300;">,</span></b> w<b><span style="color: #663300;">);</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"1st letter of array is %c \n"</span><b><span style="color: #663300;">,</span></b> w<b><span style="color: #663300;">[</span></b><span style="color: #999900;">0</span><b><span style="color: #663300;">]);</span></b>
scanf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"%s"</span><b><span style="color: #663300;">,</span></b> w<b><span style="color: #663300;">);</span></b><span style="color: red;">
return</span><span style="color: #999900;"> 0</span><b><span style="color: #663300;">;
}</span></b>
</code></pre>
<br />
इस program में ऊपर वाले से सिर्फ 1 line में difference यह है कि यहाँ char array बनाने के लिए shortcut का use किया है. यह shortcut सिर्फ char array बनाने के लिए use होता है.<br />
char w[] = "hindi";<br />
यह लिखने से एक char array w बन जायेगी. इस array का पहला element w[0] 'h', दूसरा element w[1] 'i' .... हो जायेगा. अंतिम element w[5] '<span style="color: black;">\</span>0' हो जायेगा. इस syntax(तरीके) में <span style="color: black;">\</span>0 नहीं लिखना पड़ता.<br />
<br />
अभी के लिए string में इतना ही, परन्तु आगे इसका बहुत उपयोग आता है. समय समय पर recall करते रहेंगे और बाद में धीरे धीरे इसके बारे में जानते रहेंगे.<br />
इस blog को बेहतर बनाने के लिए आपके सुझावों का सदैव स्वागत रहेगा. सुझाव देने के लिए अपना Reply यहाँ पर दें. अगले लेख में हम function के बारे में जानेंगे जो कि बहुत important है.<br />
अगर आपको यह लेख पसंद आया हो तो अपने दोस्तों को भी बताएं क्योंकि ये उनके लिए भी उपयोगी सिद्ध हो सकता है !! नीचे दिए गए link के माध्यम से इसे आसानी से facebook twitter और Google Buzz पर भी Share कर सकते हैं.</div>
<br />
<div style="text-align: center;">
<a href="http://programming-tutorial-hindi.blogspot.com/">Index</a> <a href="http://programming-tutorial-hindi.blogspot.com/2010/10/c-hindi-tutorial-function.html" target="_self">Next(function)</a></div>
</div>
Hridayesh Guptahttp://www.blogger.com/profile/09764967341453023170noreply@blogger.comtag:blogger.com,1999:blog-8683328491687928872.post-48275877946533382782010-10-27T15:08:00.004+05:302012-07-19T09:08:03.910+05:30while loop<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="btn_sw" href="#" id="hi_btn" onclick="javascript:return showhindi(this);">
Read in Hindi</div>
<div class="btn_sw" href="#" id="en_btn" onclick="javascript:return showeng(this);">
Read in English</div>
<br />
<div id="eng">
Going forward with this C/C++ programming language tutorial today we will learn to use while loop. This needs a boolean statement. Statement will continue to run repeated times till given boolean expression evaluates to true. Lets look into this by an example given below. Again we will print square of numbers from 1 to 10 using while loop.<br />
<pre><code style="background: none repeat scroll 0% 0% rgb(227, 228, 250); border: 1px solid; display: block; overflow: auto;"><span style="color: #000099;">#include <stdio.h>
</span><span style="color: #ff6633;">
int</span><span style="color: #990000;"> main</span><b><span style="color: #663300;">() {</span></b><span style="color: #ff6633;">
int</span> i=1<b><span style="color: #663300;">;</span></b><span style="color: #ff6633;">
int</span> sq<b><span style="color: #663300;">;</span></b><span style="color: red;">
while</span><b><span style="color: #663300;">(</span></b><b></b>i<b><span style="color: #663300;"><=</span></b><span style="color: #999900;">10</span><b></b><b></b><b></b><b><span style="color: #663300;">) {</span></b>
sq<b><span style="color: #663300;"> =</span></b> i<b><span style="color: #663300;">*</span></b>i<b><span style="color: #663300;">;</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"square of %d is %d.\n"</span><b><span style="color: #663300;">,</span></b> i<b><span style="color: #663300;">,</span></b> sq<b><span style="color: #663300;">);
i<b><span style="color: #663300;"> =</span></b> i<b><span style="color: #663300;">+</span></b>1<b><span style="color: #663300;">;</span></b>
}</span></b><b></b>
scanf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"%d"</span><b><span style="color: #663300;">, &i);</span></b><span style="color: red;">
return</span><span style="color: #999900;"> 1</span><b><span style="color: #663300;">;
}</span></b>
</code></pre>
In the example given above initial value of i is 1. then boolean expression inside while loop is i<=10 which evaluates to true because i is 1, Hence all statements inside loop will be executed. See that we are increasing value of i inside loop by 1. Again boolean statement will be true because i is 2. Similarly program will continue. When value of i will reach 11, boolean statement will be false and program will go out of while loop. Try to run this program to see the output.<br />
<br />
In next topic we will learn to use string and see some interesting examples.<br />
If you like this tutorial, share it with your friends on facebook, orkut, twitter etc.</div>
<div id="hindi">
आज Hindi के इस C/C++ programming language tutorial को आगे बढ़ाते हुए हम while loop का उपयोग करना सीखेंगे. इसमें एक boolean statement दिया जाता है. जब तक उसकी value true आती है तब तक loop के अंदर लिखे statement run होते रहते हैं. जब तक नीचे दिए गए example से समझते हैं. while loop का उपयोग करके एक बार फिर से 1 से 10 तक कि संख्याओ के square print करेंगे<br />
<pre><code style="background: none repeat scroll 0% 0% rgb(227, 228, 250); border: 1px solid; display: block; overflow: auto;"><span style="color: #000099;">#include <stdio.h>
</span><span style="color: #ff6633;">
int</span><span style="color: #990000;"> main</span><b><span style="color: #663300;">() {</span></b><span style="color: #ff6633;">
int</span> i=1<b><span style="color: #663300;">;</span></b><span style="color: #ff6633;">
int</span> sq<b><span style="color: #663300;">;</span></b><span style="color: red;">
while</span><b><span style="color: #663300;">(</span></b><b></b>i<b><span style="color: #663300;"><=</span></b><span style="color: #999900;">10</span><b></b><b></b><b></b><b><span style="color: #663300;">) {</span></b>
sq<b><span style="color: #663300;"> =</span></b> i<b><span style="color: #663300;">*</span></b>i<b><span style="color: #663300;">;</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"square of %d is %d.\n"</span><b><span style="color: #663300;">,</span></b> i<b><span style="color: #663300;">,</span></b> sq<b><span style="color: #663300;">);
i<b><span style="color: #663300;"> =</span></b> i<b><span style="color: #663300;">+</span></b>1<b><span style="color: #663300;">;</span></b>
}</span></b><b></b>
scanf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"%d"</span><b><span style="color: #663300;">, &i);</span></b><span style="color: red;">
return</span><span style="color: #999900;"> 1</span><b><span style="color: #663300;">;
}</span></b>
</code></pre>
<br />
ऊपर दिए गए program में i की प्रारंभिक value 1 है. उसके बाद while loop का boolean statement है i<=10 जो कि true है क्योंकि i की value 1 है, इसलिए loop के अंदर लिखे सारे statement execute हो जायेंगे. ध्यान दें कि हम loop के अंदर i की value 1 बढ़ा रहे हैं. फिर से boolean statement true हो जायेगा क्योंकि i की value 2 हो गयी है. इसी तरह आगे बढते रहेंगे. जब i की value 11 हो जायेगी तब boolean statement false हो जाएगा और हम while loop से बाहर आ जायेंगे. इस program को चला कर देखें. यह 1 से 10 तक सभी संख्याओ के square print करेगा.<br />
<br />
अगले लेख में हम string का उपयोग करना सीखेंगे और साथ ही उसका use करके कुछ interesting example देखेंगे.<br />
अगर आपको यह लेख पसंद आया हो तो अपने दोस्तों को भी बताएं क्योंकि ये उनके लिए भी उपयोगी सिद्ध हो सकता है !! नीचे दिए गए link के माध्यम से इसे आसानी से facebook twitter और Google Buzz पर भी Share कर सकते हैं.</div>
<br />
<div style="text-align: center;">
<a href="http://programming-tutorial-hindi.blogspot.com/">Index</a> <a href="http://programming-tutorial-hindi.blogspot.com/2010/10/c-hindi-tutorial-string.html" target="_self">Next(String)</a></div>
</div>
Hridayesh Guptahttp://www.blogger.com/profile/09764967341453023170noreply@blogger.comtag:blogger.com,1999:blog-8683328491687928872.post-17865152243830564582010-10-22T07:00:00.009+05:302012-12-19T00:28:52.166+05:30runtime input<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="btn_sw" href="#" id="hi_btn" onclick="javascript:return showhindi(this);">
Read in Hindi</div>
<div class="btn_sw" href="#" id="en_btn" onclick="javascript:return showeng(this);">
Read in English</div>
<div id="eng">
Going forward with C/C++ programming language tutorial today we will learn how to take input from user when program is running. Lets see an example for this. The program below will ask user to enter a number then it will print whether number is even or odd.<br />
<pre><code><span style="color: #000099;">#include <stdio.h>
</span><span style="color: #ff6633;">
int</span><span style="color: #990000;"> main</span><b><span style="color: #663300;">() {</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"Please enter a number: "</span><b><span style="color: #663300;">);</span></b><span style="color: #ff6633;">
int</span> i<b><span style="color: #663300;"> =</span></b><span style="color: #999900;"> 0</span><b><span style="color: #663300;">;</span></b>
scanf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"%d"</span><b><span style="color: #663300;">, &</span></b>i<b><span style="color: #663300;">);</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"You entered %d \n"</span><b><span style="color: #663300;">,</span></b> i<b><span style="color: #663300;">);</span></b><span style="color: red;">
if</span><b><span style="color: #663300;">(</span></b>i<b><span style="color: #663300;">%</span></b><span style="color: #999900;">2</span><b><span style="color: #663300;"> ==</span></b><span style="color: #999900;"> 0</span><b><span style="color: #663300;">) {</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"Number is even\n"</span><b><span style="color: #663300;">);
}</span></b><span style="color: red;"> else</span><b><span style="color: #663300;"> {</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"Number is odd\n"</span><b><span style="color: #663300;">);
}</span></b>
scanf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"%d"</span><b><span style="color: #663300;">, &</span></b>i<b><span style="color: #663300;">);
}</span></b>
</code></pre>
<br />
Run this program and it will print "Please enter a number:" and stop. Type any number and press enter. Then it will print that number you have entered and it will also print if number is even or odd. Now lets see how it works.<br />
After main() 1st line is printf("Please enter a number: "); You are familiar with this. After that variable i is defined Which will hold the number entered by user. See the next line which says scanf("%d", &i);<br />
scanf works similar as printf. Its 1st argument "%d" means same as printf which tells input will be int. 2nd argument &i specifies the variable in which input will be stored. (Here & sign is necessary before variable. We will know the reason when we learn about pointer) If you enter 25 on screen then value of i will be 25. Program after that line you can understand easily with help of previous topics - Here in screen we have printed the number entered by user. Note that % gives the remainder. Hence if diving that number with 2 given 0 remainder then we print that "Number is even" otherwise we print "Number is odd".<br />
<b>Reason to write scanf at the end:</b> If you run program in windows, it shows output in black windows which closes very quickly as program finished and you won't have much time to read the output. If we write scanf in end, window will not close, rather it will wait for input. This will give us enough chance to examine the output.<br />
<br />
<b>Array.</b><br />
We have studied about variable that a variable can store a value but when defining variable we have to tell what type of value store करेगा integer, character etc. Array can store more than one values. If you want to store 100 int, you can store all of them in an array. Lets see by an example how do we do it.<br />
In the example given below we will store squares of 0 to 9 in an array then print all of them. Try running this program.<br />
<br />
<pre><code><span style="color: #000099;">#include <stdio.h>
</span><span style="color: #ff6633;">
int</span><span style="color: #990000;"> main</span><b><span style="color: #663300;">() {</span></b><span style="color: #ff6633;">
int</span> i<b><span style="color: #663300;"> =</span></b><span style="color: #999900;"> 0</span><b><span style="color: #663300;">;</span></b><span style="color: #ff6633;">
int</span> arr<b><span style="color: #663300;">[</span></b><span style="color: #999900;">10</span><b><span style="color: #663300;">];</span></b><span style="color: red;">
for</span><b><span style="color: #663300;">(</span></b>i<b><span style="color: #663300;"> =</span></b><span style="color: #999900;"> 0</span><b><span style="color: #663300;">;</span></b> i<b><span style="color: #663300;"> <</span></b><span style="color: #999900;"> 10</span><b><span style="color: #663300;">;</span></b> i<b><span style="color: #663300;">++) {</span></b>
arr<b><span style="color: #663300;">[</span></b>i<b><span style="color: #663300;">] =</span></b> i<b><span style="color: #663300;">*</span></b>i<b><span style="color: #663300;">;
}</span></b><span style="color: red;">
for</span><b><span style="color: #663300;">(</span></b>i<b><span style="color: #663300;"> =</span></b><span style="color: #999900;"> 0</span><b><span style="color: #663300;">;</span></b> i<b><span style="color: #663300;"> <</span></b><span style="color: #999900;"> 10</span><b><span style="color: #663300;">;</span></b> i<b><span style="color: #663300;">++) {</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"square of %d is %d\n"</span><b><span style="color: #663300;">,</span></b> i<b><span style="color: #663300;">,</span></b> arr<b><span style="color: #663300;">[</span></b>i<b><span style="color: #663300;">]);
}</span></b>
scanf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"%d"</span><b><span style="color: #663300;">, &</span></b>i<b><span style="color: #663300;">);
}</span></b>
</code></pre>
<br />
In int arr[10]; arr is a variable which can hold 10 integers. Remember it can store only integers from index 0 to 9. Hence in this case 1st integer will be at arr[0], 2nd at arr[1] and so on. Size of arr is 10 hence it will store 10 integers from arr[0] to arr[9]. After that its easy to understand program. Statements inside for loop(which will run 10 times) when we come 1st time, i will be 0 hence 1st integer will be stored at arr[0] will be 0. Similarly going forward inside for loop when we come last time inside for loop, i will be 9 hence arr[9] will be 81. Similarly in next for loop we reading values of array and printing.<br />
Usually we use for loop as shown above to access elements in the array.<br />
In next topic we will learn about while loop.<br />
If you like this tutorial, share it with your friends on facebook, orkut, twitter etc.</div>
<div id="hindi">
आज Hindi के इस C/C++ programming language tutorial को आगे बढ़ाते हुए ये सीखते हैं कि हम runtime पर यानि जिस समय program run हो रहा है तब input कैसे लेते हैं. इसे एक example से समझते हैं. नीचे दिए गए example में हम user से एक संख्या input में लेंगे और उसके बाद यह print करेंगे कि वह संख्या सम है या विषम.<br />
<pre class="brush: cpp">
#include <stdio.h>
int main() {
printf("Please enter a number: ");
int i = 0;
scanf("%d", &i);
printf("You entered %d \n", i);
if(i%2 == 0) {
printf("Number is even\n");
} else {
printf("Number is odd\n");
}
scanf("%d", &i);
}
</pre>
<br />
इस program को run करे. यह screen पर print करेगा Please enter a number: और रुक जायेगा. कोई भी संख्या type करके enter दबा दें. इसके बाद यह print करेगा कि आपने कौन सी संख्या type कि थी और यह भी बताएगा कि यह सम(even) है या विषम(odd). अब देखते हैं यह काम कैसे करता है.<br />
इस program में सबसे पहले हमने printf("Please enter a number: "); लिखा है जिससे आप परिचित ही हैं कि यह screen पर क्या print करेगा. इसके बाद हमने variable i define किया है जो उस मान को ग्रहण करेगा जो हम input लेंगे. इसके बाद वाली line को ध्यान से देखिये जिसमे लिखा है scanf("%d", &i);<br />
scanf printf की तरह ही काम करता है. इसका पहला argument "%d" का ठीक वही मतलब है जो printf में होता था, इसके द्वारा हम यह बताते हैं input में int लेंगे. दूसरा argument &i यह बताता है कि जो input लेंगे वह किस variable में store होगा. (यहाँ variable से पहले & चिन्ह लगाना आवश्यक होता है इसका कारण आगे समझेंगे जब pointer के बारे में पढेंगे.) अगर आप screen में 25 enter करेंगे तो i का मान 25 हो जायेगा. उसके बाद वाला program पिछले लेखो के अनुसार आप आसानी से समझ सकते हैं - हमने screen पर उस संख्या को print किया है जो आपने enter किया था. यहाँ यह ध्यान रहे % भाग देने से बचने वाला शेष दे देता है. अतः उस संख्या 2 में का भाग देने पर यही शेष 0 आता है तो हमने print किया है कि Number is even अन्यथा यह print किया है कि Number is odd.<br />
<b>हर program के अंत में scanf लिखने का कारण:</b> अगर आप windows में program run कर रहे है तो एक काली window खुलती है जिसमे output दिखता है परन्तु जैसे ही program समाप्त होता है यह काली window बंद हो जाती है. अंत में scanf लिखने से काली window बंद नहीं होती क्योकि वो हमारे input का wait करती है. अगर ये नहीं लिखेंगे तो program इतनी जल्दी run होकर काली window बंद हो जायेगी कि हम अपने program का output ही नहीं देख पाएंगे.<br />
<br />
<b>अब समझते हैं Array के बारे में.</b><br />
हम variable के बारे में जान चुके हैं कि हर variable कोई value store कर सकता है परन्तु variable define करते समय यह बताना होता है कि वह किस type की value store करेगा integer, character etc. Array एक से अधिक value store कर सकता है. अगर आप 100 int store करना चाहते तो उसे एक array में कर सकते हैं. इसे एक example कि सहायता से समझते है कि एक से अधिक variable Array में कैसे store करते हैं.<br />
नीचे दिए गए example में 0 से 9 तक संख्याओं के square array में store करेंगे और उन्हें print करेंगे. पहले इस program को run करके देखें.<br />
<br />
<pre class="brush: cpp">
#include <stdio.h>
int main() {
int i = 0;
int arr[10];
for(i = 0; i < 10; i++) {
arr[i] = i*i;
}
for(i = 0; i < 10; i++) {
printf("square of %d is %d\n", i, arr[i]);
}
scanf("%d", &i);
}
</pre>
<br />
अब इसे समझते हैं. int arr[10]; arr variable define कर रहा है जो 10 int store कर सकता है. ध्यान रहे यह केवल int store कर सकता है. ये सारे int इसमें number से store रहते हैं. ध्यान रहे कि इनकी numbering 0 से start होती है जैसे कि इस 10 int की Array arr में पहला int arr[0] पर होगा, दूसरा arr[1] पर ... ऊपर हमने arr कि size 10 रखी है अतः यह 10 int(arr[0] से arr[9]) ही store कर सकता है. इसके बाद के program को आप समझ ही सकते हैं. for loop के अंदर (जो कि 10 बार run होगा) जब हम पहली बार आयेंगे तो i का मान 0 होगा अतः arr[0] (arr array का पहला int) में 0 आ जायेगा इसी तरह आगे बढते हुए जब for loop के अंदर अंतिम बार आयेंगे तो array arr का अंतिम int(arr[9]) में 81 आ जायेगा. इसी तरह अगले for loop में हम arr के उन सभी मानो को print कर रहे हैं.<br />
अधिकांशतः हम किसी array के प्रत्येक मान को access करने के लिए ऊपर दिखाए अनुसार for loop का प्रयोग करते हैं.<br />
<br />
अगले लेख में हम while loop के बारे में जानेंगे.<br />
अगर आपको यह लेख पसंद आया हो तो अपने दोस्तों को भी बताएं क्योंकि ये उनके लिए भी उपयोगी सिद्ध हो सकता है !! नीचे दिए गए link के माध्यम से इसे आसानी से facebook twitter और Google Buzz पर भी Share कर सकते हैं.</div>
<br />
<div style="text-align: center;">
<a href="http://programming-tutorial-hindi.blogspot.com/">Index</a> <a href="http://programming-tutorial-hindi.blogspot.com/2010/10/c-hindi-tutorial-while-loop.html" target="_self">Next(while loop)</a></div>
</div>
Hridayesh Guptahttp://www.blogger.com/profile/09764967341453023170noreply@blogger.comtag:blogger.com,1999:blog-8683328491687928872.post-89987900959110830332010-10-13T05:58:00.007+05:302015-11-24T08:16:49.190+05:30for loop<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="btn_sw" href="#" id="hi_btn" onclick="javascript:return showhindi(this);">
Read in Hindi</div>
<div class="btn_sw" href="#" id="en_btn" onclick="javascript:return showeng(this);">
Read in English</div>
<br />
<div id="eng">
Moving forward with this C/C++ programming language tutorial today we will learn about for loop. Before for loop lets see about 1 more operator.<br />
<br />
%: Using this we can know remainder when a number divides other. See the examples below.<br />
<ul>
<li>int a = 15 % 2;Here value of a will be 1 because dividing 15 with 2 gives 1.</li>
<li>int b = 254 % 2;Here value of b will be 0 because dividing 254 with 2 gives 0.</li>
<li> int c = 37 % 10;Here value of c will be 7 because dividing 37 with 15 gives 7.</li>
</ul>
<br />
Now we will understand for loop with an example. In this example we will print square of numbers from 1 to 10.<br />
<pre class="brush: cpp">#include <stdio.h>
int main() {
int i;
int sq;
for(i=1; i<=10; i = i+1) {
sq = i*i;
printf("square of %d is %d.\n", i, sq);
}
scanf("%s");
return 1;
}</pre>
<br />
2 variable i and sq are declared firstly. See carefully inside for(). I have ; at 2 places which divided it into 3 parts. Lets see how programs goes with this.<br />
<ul>
<li>1st part contains initialization statement. More than one statements can be separated using ; . Statements in this part executed only once. In this example initialization statement is: i = 1 Hence i will be 1.</li>
<li>2nd part contains only one boolean statement which gives true or false. After executing 1st part if this middle part evaluates to true then all statements inside for { } will be executed once. In this example inside {} value of sq will become 1 because value of i is 1, Then "square of 1 is 1" will be printed on screen.</li>
<li>Then 3rd part will be executed and again 2nd part will be checked. If it is true then again all statements inside {} will be executed once again. Again 3rd part will be executed and 2nd part will be checked. If it is true, again statements inside {} will be executed. This process will go till 2nd part evaluates to false. In this example value of i will be 2 hence i <= 10 will be true and value of sq inside {} will be 4 because value of i is now 2. Similarly value of i will increase and statements inside {} will keep executing. When i will be 11 i <= 10 will be false and for loop will finish.</li>
</ul>
<br />
Run the program above and check the output. Change the value to anything and run to check the output. Program given below will check if number is even or odd for 1 to 10.<br />
<pre class="brush: cpp">#include <stdio.h>
int main() {
int i;
int sq;
for(i=1; i<=10; i = i+1) {
switch(i%2) {
case 0: printf("%d is even.\n", i);
break;
case 1: printf("%d is odd.\n", i);
break;
}
}
scanf("%s");
return 1;
}</pre>
<br />
In program given above for loop will run as explained earlier and for each value of i from 1 to 10 statements inside {} block will be executed. here inside {} we have written <a href="http://programming-tutorial-hindi.blogspot.com/2010/10/c-hindi-tutorial-switch-case-statement.html">switch case statement</a> which we have learned in <a href="http://programming-tutorial-hindi.blogspot.com/2010/10/c-hindi-tutorial-switch-case-statement.html">last topic</a>. 1st time when we will come in switch case inside forloop i will be 1 hence i%2 will give 1 which matched with case 1 hence "1 is odd" will be printed. 2nd time when we come in switch case inside for loop, i will be 2 hence i%2 will give 0 which will match with case 0 hence "2 is even" will be printed. Similarly it will go on.<br />
<br />
In next topic we will learn about array and taking input at runtime, which will explain why we write scanf(%s) at the end of each program.<br />
If you like this tutorial, share it with your friends on facebook, orkut, twitter etc.</div>
<div id="hindi">
आज Hindi के इस C/C++ programming language tutorial को आगे बढ़ाते हुए हम for loop के बारे में और जानेंगे. for loop से पहले हम एक और operator के बारे में बात करते हैं.<br />
<br />
%: किसी संख्या को दूसरी से भाग देने पर बचने वाला शेष हम इसके द्वारा जान सकते हैं. कुछ example नीचे दिए जा रहे हैं.<br />
<ul>
<li>int a = 15 % 2;यहाँ a का मान 1 हो जायेगा क्योंकि 15 में 2 का भाग देने पर शेष 1 प्राप्त होगा.</li>
<li>int b = 254 % 2;यहाँ b का मान 0 हो जायेगा क्योंकि 254 में 2 का भाग देने पर शेष 0 प्राप्त होगा.</li>
<li> int c = 37 % 10;यहाँ c का मान 7 हो जायेगा क्योंकि 37 में 10 का भाग देने पर शेष 7 प्राप्त होगा.</li>
</ul>
<br />
अब हम for loop को एक example से समझते हैं. इस example में हम 1 से 10 तक की संख्याओ के square print करेंगे.<br />
<pre class="brush: cpp">#include <stdio.h>
int main() {
int i;
int sq;
for(i=1; i<=10; i = i+1) {
sq = i*i;
printf("square of %d is %d.\n", i, sq);
}
scanf("%s");
return 1;
}</pre>
<br />
सबसे पहले 2 variable i, sq declare किये गए हैं. for() के अंदर ध्यान से देखें. इसमें 2 जगह ; का चिन्ह लगा होने चाहिए जो इसके अंदर लिखे हुए को 3 भागो में बांटता है. program किस तरह आगे बढ़ता है नीचे बताया गया है.<br />
<ul>
<li>सबसे पहले भाग में initialization statement आते हैं. अगर एक से ज्यादा statement हैं तो ; लगाकर लिखते हैं. इस भाग में लिखे गए statement केवल एक बार execute होते हैं. इस example में ये statement i = 1 है अतः सबसे पहले i की value 1 हो जाएगी.</li>
<li>दूसरे भाग में सिर्फ एक boolean statement होती है जिसका output true या false होता है. पहला भाग execute होने के बाद यह भाग check होता है. अगर यह true है तो हम for के साथ { } के अंदर लिखे सारे statement एक बार execute करते हैं. इस example में {} के अंदर sq का मान 1 हो जायेगा क्योँकि अभी i का मान 1 है, उसके बाद screen पर print होगा square of 1 is 1.</li>
<li>इसके बाद तीसरा भाग execute होता है, और फिर से दूसरा भाग check करते हैं अगर वह true है तो तो हम फिर से {} के अंदर लिखे सारे statement execute हो जाते है. फिर से तीसरा भाग run होता है और फिर से दूसरा भाग check करते हैं और वह true है तो फिर से {} के अंदर लिखे statement execute कर देते हैं.. यह तब तक चलता रहता है जब दूसरा भाग false नहीं हो जाता. इस example में i का मान 2 हो जायेगा जिससे i <= 10 true हो जायेगा और अब {} के अंदर sq का मान 4 हो जायेगा क्योंकि i का मान 2 है. इसी तरह i का मान बढ़ता रहेगा और {} के अंदर लिखे statement execute होते रहेंगे. जब i का मान 11 हो जायेगा तब i <= 10 false आ जायेगा और for loop खत्म हो जायेगा.</li>
</ul>
<br />
ऊपर दिए program को run करके output देखे. अपने अनुसार value change करके run करें और output देखें. नीचे एक और program दिया जा रहा है. जो 1 से 10 तक हर संख्या के लिए print करेगा कि वह सम है या विषम.<br />
<pre class="brush: cpp">#include <stdio.h>
int main() {
int i;
int sq;
for(i=1; i<=10; i = i+1) {
switch(i%2) {
case 0: printf("%d is even.\n", i);
break;
case 1: printf("%d is odd.\n", i);
break;
}
}
scanf("%s");
return 1;
}</pre>
<br />
ऊपर दिए गए program में for loop उसी तरह से चलेगा जैसे पहले बताया गया था और i के 1 से 10 तक हर एक मान के लिए for वाले {} block के अंदर लिखे statement execute होंगे. यहाँ हमने {} के अंदर <a href="http://programming-tutorial-hindi.blogspot.com/2010/10/c-hindi-tutorial-switch-case-statement.html">switch case statement</a> लिखा हुआ है जो हमने <a href="http://programming-tutorial-hindi.blogspot.com/2010/10/c-hindi-tutorial-switch-case-statement.html">पिछले लेख</a> में पढ़ा था. पहली बार जब for के अंदर switch case में आयेंगे तो i का मान 1 होगा अतः i%2 का मान 1 आएगा जोकि case 1 से match होगा इसलिए print होगा 1 is odd. जब दूसरी बार for के अंदर switch case में आयेंगे तो i का मान 2 होगा इसलिए i%2 का मान 0 आएगा जोकि case 0 से match होगा इसलिए print होगा 2 is even. इसी तरह आगे बढते जायेंगे.<br />
<br />
अगले लेख में हम array और runtime पर input लेना सीखेंगे. जिससे यह समझ में आ जायेगा कि हर program के अंत में scanf("%s") क्यों लिखते हैं.<br />
</div>
<br />
<div style="text-align: center;">
<a href="http://programming-tutorial-hindi.blogspot.com/">Index</a> <a href="http://programming-tutorial-hindi.blogspot.com/2010/10/c-hindi-tutorial-array-and-input.html" target="_self">Next(input at runtime:scanf)</a></div>
</div>
Hridayesh Guptahttp://www.blogger.com/profile/09764967341453023170noreply@blogger.comtag:blogger.com,1999:blog-8683328491687928872.post-47707304928203577432010-10-12T05:15:00.006+05:302015-11-24T08:15:56.812+05:30switch case<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div id="hindi">
आज Hindi के इस C/C++ programming language tutorial को आगे बढ़ाते हुए हम switch case statement का उपयोग करना सीखेंगे. इसकी if else statement से बहुत समानता है. वास्तव में बिना switch case statement के भी सारा काम if else के द्वारा भी किया जाता है परन्तु इसके उपयोग से कई बार हमें आसानी हो जाती है. इसे हम एक example द्वारा समझते हैं. नीचे दिए गए example में दो integer दिए गए हैं. हम एक variable की value के द्वारा decide करेंगे कि उनको जोड़ना है या घटाना है...<br />
<pre><code style="background: none repeat scroll 0% 0% rgb(227, 228, 250); border: 1px solid; display: block; overflow: auto;"><span style="color: #000099;">#include <stdio.h>
</span><span style="color: #ff6633;">
int</span><span style="color: #990000;"> main</span><b><span style="color: #663300;">() {</span></b><span style="color: #ff6633;">
float</span> v1<b><span style="color: #663300;"> =</span></b><span style="color: #999900;"> 23</span><b><span style="color: #663300;">;</span></b><span style="color: #ff6633;">
float</span> v2<b><span style="color: #663300;"> =</span></b><span style="color: #999900;"> 9</span><b><span style="color: #663300;">;</span></b><span style="color: #ff6633;">
float</span> result<b><span style="color: #663300;">;</span></b><span style="color: #ff6633;">
char</span> c<b><span style="color: #663300;"> =</span></b><span style="color: #009900;"> 's'</span><b><span style="color: #663300;">;</span></b><span style="color: red;">
switch</span><b><span style="color: #663300;">(</span></b>c<b><span style="color: #663300;">) {</span></b><span style="color: red;">
case</span><span style="color: #009900;"> 'a'</span><b><span style="color: #663300;">:</span></b>
result<b><span style="color: #663300;"> =</span></b> v1<b><span style="color: #663300;"> +</span></b> v2<b><span style="color: #663300;">;</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"Result of addition is %f \n"</span><b><span style="color: #663300;">,</span></b> result<b><span style="color: #663300;">);
break;
</span></b><span style="color: red;"> case</span><span style="color: #009900;"> 's'</span><b><span style="color: #663300;">:</span></b>
result<b><span style="color: #663300;"> =</span></b> v1<b><span style="color: #663300;"> -</span></b> v2<b><span style="color: #663300;">;</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"Result of subtraction is %f \n"</span><b><span style="color: #663300;">,</span></b> result<b><span style="color: #663300;">);
break;
</span></b><span style="color: red;"> case</span><span style="color: #009900;"> 'm'</span><b><span style="color: #663300;">:</span></b>
result<b><span style="color: #663300;"> =</span></b> v1<b><span style="color: #663300;"> *</span></b> v2<b><span style="color: #663300;">;</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"Result of multiplication is %f \n"</span><b><span style="color: #663300;">,</span></b> result<b><span style="color: #663300;">);
break;
</span></b><span style="color: red;"> default</span><b><span style="color: #663300;">:</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"No operation selected.\n"</span><b><span style="color: #663300;">);
}</span></b>
scanf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"%s"</span><b><span style="color: #663300;">);</span></b><span style="color: red;">
return</span><span style="color: #999900;"> 1</span><b><span style="color: #663300;">;
}</span></b>
</code></pre>
<br />
अब इसे समझते हैं. हमने एक variable c define किया है जिसका type char है और value s है. (char के बारे में जानने के लिए मेरे<a href="http://programming-tutorial-hindi.blogspot.com/2010/10/c-hindi-tutorial-variables-expressions.html"> पिछले लेख में जाएँ</a>) अब code को ध्यान से देखें. switch के अंदर लिखे गए variable c का मान हर एक case लिखे गए मान से compare किया जाता है. जैसे कि इस example में c का मान s है. पहले case में लिखा गया मान a है इसलिए इस case के अंदर लिखे गए statements execute नहीं होंगे. इसी तरह आगे बढते जायेंगे. switch(c) अंदर लिखे गए c का मान case 's': में लिखे s से match हो जायेगा इसलिए result variable में v1 और v2 के difference का मान आ जायेगा और screen पर print होगा Result of substraction is 14. break statement run होने पर यह switch() { } block से बाहर आ जायेगा. अगर break नहीं लिखा जाता तो यह किसी एक case match होने के बाद आगे के सारे case execute कर देता चाहे वो switch() के अंदर लिखे variable से match हो या न हो. अगर कोई भी case match न हो तो default के अंदर लिखे statement execute हो जाते हैं. ऊपर दिए program में c के अलग अलग मान देकर program run करे और output देखें.<br />
<br />
यहाँ एक और ध्यान देने योग्य बात है कि हमने float को print करने के लिए printf में %f use किया है.<br />
अगले लेख में हम for loop के बारे में जानेंगे.</div>
<br />
<div style="text-align: center;">
<a href="http://programming-tutorial-hindi.blogspot.com/">Index</a> <a href="http://programming-tutorial-hindi.blogspot.com/2010/10/c-hindi-tutorial-for-loop.html" target="_self">Next(for loop)</a></div>
</div>
Hridayesh Guptahttp://www.blogger.com/profile/09764967341453023170noreply@blogger.comtag:blogger.com,1999:blog-8683328491687928872.post-12325996628511076492010-10-09T16:53:00.006+05:302015-11-24T08:14:36.782+05:30if else 2<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="btn_sw" href="#" id="hi_btn" onclick="javascript:return showhindi(this);">
Read in Hindi</div>
<div class="btn_sw" href="#" id="en_btn" onclick="javascript:return showeng(this);">
Read in English</div>
<br />
<div id="eng">
Moving forward with this C/C++ programming language tutorial today we will see some more examples about if-else statement.<br />
<ul>
<li>Most basic example.<br />
<pre><code style="background: none repeat scroll 0% 0% rgb(227, 228, 250); border: 1px solid; display: block; overflow: auto;"><span style="color: #000099;">#include <stdio.h>
</span><span style="color: #ff6633;">
int</span><span style="color: #990000;"> main</span><b><span style="color: #663300;">() {</span></b><span style="color: #ff6633;">
int</span> percent<b><span style="color: #663300;"> =</span></b><span style="color: #999900;"> 45</span><b><span style="color: #663300;">;</span></b><span style="color: red;">
if</span><b><span style="color: #663300;">(</span></b>percent<b><span style="color: #663300;"> >=</span></b><span style="color: #999900;"> 33</span><b><span style="color: #663300;">) {</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"Congrats!! You passed.\n"</span><b><span style="color: #663300;">);
}</span></b><span style="color: red;">
else </span><b><span style="color: #663300;">{</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"Sorry.! you failed\n"</span><b><span style="color: #663300;">);
}</span></b>
scanf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"%s"</span><b><span style="color: #663300;">);</span></b><span style="color: red;">
return</span><span style="color: #999900;"> 1</span><b><span style="color: #663300;">;
}</span></b>
</code></pre>
<br />
In the example given above change value of percent and run the program. 1st it will check whether value of percent is greater than or equal to 33[if(percent >= 33)] If so, on screen it will print<br />
<pre><code>Congrats!! You passed.</code></pre>
<br />
Otherwise prints(else)<br />
<pre><code>Sorry.! you failed</code></pre>
<br />
</li>
<li>It is not necessary to write something inside { }, It can left blank. In this example speed of vehicle is taken and if it is more than 60 warning will be printed otherwise nothing will be printed.<br />
<pre><code style="background: none repeat scroll 0% 0% rgb(227, 228, 250); border: 1px solid; display: block; overflow: auto;">
<span style="color: #000099;">#include <stdio.h>
</span><span style="color: #ff6633;">
int</span><span style="color: #990000;"> main</span><b><span style="color: #663300;">() {</span></b><span style="color: #ff6633;">
int</span> speed<b><span style="color: #663300;"> =</span></b><span style="color: #999900;"> 65</span><b><span style="color: #663300;">;</span></b><span style="color: red;">
if</span><b><span style="color: #663300;">(</span></b>speed<b><span style="color: #663300;"> ></span></b><span style="color: #999900;"> 60</span><b><span style="color: #663300;">) {</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"Warning: Speed is in danger zone.\n"</span><b><span style="color: #663300;">);
}</span></b><span style="color: red;">
else</span><b><span style="color: #663300;"> {
}</span></b>
scanf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"%s"</span><b><span style="color: #663300;">);</span></b><span style="color: red;">
return</span><span style="color: #999900;"> 1</span><b><span style="color: #663300;">;
}</span></b>
</code></pre>
<br />
In this example change the value of speed and run program and see what gets printed. If speed is 60 or below then nothing will be printed. This program can be written in folowing way too.<br />
<pre><code style="background: none repeat scroll 0% 0% rgb(227, 228, 250); border: 1px solid; display: block; overflow: auto;">
<span style="color: #000099;">#include <stdio.h>
</span><span style="color: #ff6633;">
int</span><span style="color: #990000;"> main</span><b><span style="color: #663300;">() {</span></b><span style="color: #ff6633;">
int</span> speed<b><span style="color: #663300;"> =</span></b><span style="color: #999900;"> 65</span><b><span style="color: #663300;">;</span></b><span style="color: red;">
if</span><b><span style="color: #663300;">(</span></b>speed<b><span style="color: #663300;"> ></span></b><span style="color: #999900;"> 60</span><b><span style="color: #663300;">) {</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"Warning: Speed is in danger zone.\n"</span><b><span style="color: #663300;">);
}</span></b><span style="color: red;">
</span>
scanf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"%s"</span><b><span style="color: #663300;">);</span></b><span style="color: red;">
return</span><span style="color: #999900;"> 1</span><b><span style="color: #663300;">;
}</span></b>
</code></pre>
<br />
It is not necessary to write else part. In program above we have skipped else part since we do not want anything in else part.</li>
</ul>
<br />
In next topic we will learn about switch case statement.<br />
If you like this tutorial, share it with your friends on facebook, orkut, twitter etc.</div>
<div id="hindi">
आज Hindi के इस C/C++ programming language tutorial को आगे बढ़ाते हुए हम if else के कुछ और example देखते हैं.<br />
<ul>
<li> सबसे basic example.<br />
<pre><code style="background: none repeat scroll 0% 0% rgb(227, 228, 250); border: 1px solid; display: block; overflow: auto;"><span style="color: #000099;">#include <stdio.h>
</span><span style="color: #ff6633;">
int</span><span style="color: #990000;"> main</span><b><span style="color: #663300;">() {</span></b><span style="color: #ff6633;">
int</span> percent<b><span style="color: #663300;"> =</span></b><span style="color: #999900;"> 45</span><b><span style="color: #663300;">;</span></b><span style="color: red;">
if</span><b><span style="color: #663300;">(</span></b>percent<b><span style="color: #663300;"> >=</span></b><span style="color: #999900;"> 33</span><b><span style="color: #663300;">) {</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"Congrats!! You passed.\n"</span><b><span style="color: #663300;">);
}</span></b><span style="color: red;">
else </span><b><span style="color: #663300;">{</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"Sorry.! you failed\n"</span><b><span style="color: #663300;">);
}</span></b>
scanf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"%s"</span><b><span style="color: #663300;">);</span></b><span style="color: red;">
return</span><span style="color: #999900;"> 1</span><b><span style="color: #663300;">;
}</span></b>
</code></pre>
<br />
ऊपर दिए गए example में percent की value change कर करके program को run करके देखें. सबसे पहले यह check करेगा कि percent की value 33 के बराबर या ज्यादा है [if(percent >= 33)] अगर ऐसा है तो screen पर print होगा<br />
<pre><code>Congrats!! You passed.</code></pre>
<br />
अन्यथा print होगा (else)<br />
<pre><code>Sorry.! you failed</code></pre>
<br />
</li>
<li>यह जरूरी नहीं कि हमें { } के अंदर कुछ लिखना ही पड़े, हम इसे खाली भी छोड़ सकते हैं. इस example में हम गाड़ी की speed लेंगे और अगर यह 60 से ज्यादा है तो warning print करेंगे अन्यथा कुछ नहीं करेंगे.<br />
<pre><code style="background: none repeat scroll 0% 0% rgb(227, 228, 250); border: 1px solid; display: block; overflow: auto;">
<span style="color: #000099;">#include <stdio.h>
</span><span style="color: #ff6633;">
int</span><span style="color: #990000;"> main</span><b><span style="color: #663300;">() {</span></b><span style="color: #ff6633;">
int</span> speed<b><span style="color: #663300;"> =</span></b><span style="color: #999900;"> 65</span><b><span style="color: #663300;">;</span></b><span style="color: red;">
if</span><b><span style="color: #663300;">(</span></b>speed<b><span style="color: #663300;"> ></span></b><span style="color: #999900;"> 60</span><b><span style="color: #663300;">) {</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"Warning: Speed is in danger zone.\n"</span><b><span style="color: #663300;">);
}</span></b><span style="color: red;">
else</span><b><span style="color: #663300;"> {
}</span></b>
scanf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"%s"</span><b><span style="color: #663300;">);</span></b><span style="color: red;">
return</span><span style="color: #999900;"> 1</span><b><span style="color: #663300;">;
}</span></b>
</code></pre>
<br />
इसमें speed की value बदल बदलकर program run करे और देखे क्या print होता है. अगर speed 60 या इससे कम है तो कुछ print नहीं होगा. इस program को हम इस प्रकार से भी लिख सकते हैं.<br />
<pre><code style="background: none repeat scroll 0% 0% rgb(227, 228, 250); border: 1px solid; display: block; overflow: auto;">
<span style="color: #000099;">#include <stdio.h>
</span><span style="color: #ff6633;">
int</span><span style="color: #990000;"> main</span><b><span style="color: #663300;">() {</span></b><span style="color: #ff6633;">
int</span> speed<b><span style="color: #663300;"> =</span></b><span style="color: #999900;"> 65</span><b><span style="color: #663300;">;</span></b><span style="color: red;">
if</span><b><span style="color: #663300;">(</span></b>speed<b><span style="color: #663300;"> ></span></b><span style="color: #999900;"> 60</span><b><span style="color: #663300;">) {</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"Warning: Speed is in danger zone.\n"</span><b><span style="color: #663300;">);
}</span></b><span style="color: red;">
</span>
scanf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"%s"</span><b><span style="color: #663300;">);</span></b><span style="color: red;">
return</span><span style="color: #999900;"> 1</span><b><span style="color: #663300;">;
}</span></b>
</code></pre>
<br />
else वाले भाग में अगर हम कुछ नहीं चाहते हो तो else लिखना जरूरी नहीं है जैसा कि ऊपर program में दिखाया गया है.</li>
</ul>
<br />
अगले topic में हम switch case statement के बारे में जानेंगे.</div>
<br />
<div style="text-align: center;">
<a href="http://programming-tutorial-hindi.blogspot.com/">Index</a> <a href="http://programming-tutorial-hindi.blogspot.com/2010/10/c-hindi-tutorial-switch-case-statement.html" target="_self">Next(switch case)</a></div>
</div>
Hridayesh Guptahttp://www.blogger.com/profile/09764967341453023170noreply@blogger.comtag:blogger.com,1999:blog-8683328491687928872.post-66401096228038910772010-10-07T04:29:00.006+05:302015-11-24T08:14:22.692+05:30if else<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="btn_sw" href="#" id="hi_btn" onclick="javascript:return showhindi(this);">
Read in Hindi</div>
<div class="btn_sw" href="#" id="en_btn" onclick="javascript:return showeng(this);">
Read in English</div>
<br />
<div id="eng">
Moving forward with this tutorial today we will lean about if-else statement. before this we should learn comparison operator hence let us see this 1st below. As we have seen some operators earlier like +, - ... Comparison operator given Boolean output, meaning that they tell if statement is true or false.<br />
<ul>
<li><b>< and ></b> : Eg 3 > 1 given true but 3 < 1 gives false. Similarly 3 > 3 gives false because 3 is not greater than 3. 3 < 3 is also false.</li>
<li><b><= and >=</b> : They are similar as above but 3 >= 3 and 3 <= 3 will give true. Note that =< can not be used in place of <=. = should come after > or <.</li>
<li><b>==</b> : Tells if 2 valued are equal. This operator can compare int, char... every datatype. Remember that == is not same as =. = is assignment operator which assigns value on right side to variable on left sidebut == compares 2 values.</li>
</ul>
<br />
Now we will see how to use them in if else.<br />
Format of if statement is following.<br />
<br />
if(booelan expression1) {<br />
statement1;<br />
} else if(booelan expression2) {<br />
statement2;<br />
}<br />
.....<br />
....<br />
....<br />
else {<br />
statementN;<br />
}<br />
<br />
boolean expression evaluates to true or false. If booelan expression1 evaluates to true then only statement1 will be executed. If booelan expression1 is false we see booelan expression2. If this is true then statement2 will be execute. Similarly we will ahead. Wherever booelan expression evaluates to true, related statement will be execute, nothing else. If none of booelan expression evaluates to true then statements under else will be executed. Lets see this by an example<br />
In the example given below based on total marks and obtained marks we will calculate percent then grade and print it.<br />
<pre><code style="background: none repeat scroll 0% 0% rgb(227, 228, 250); border: 1px solid; display: block; overflow: auto;"><span style="color: #000099;">#include <stdio.h>
</span><span style="color: #ff6633;">
int</span><span style="color: #990000;"> main</span><b><span style="color: #663300;">() {</span></b><span style="color: #ff6633;">
int</span> total_marks<b><span style="color: #663300;"> =</span></b><span style="color: #999900;"> 500</span><b><span style="color: #663300;">;</span></b><span style="color: #ff6633;">
int</span> obtained_marks<b><span style="color: #663300;"> =</span></b><span style="color: #999900;"> 272</span><b><span style="color: #663300;">;</span></b><span style="color: #ff6633;">
int</span> percent<b><span style="color: #663300;"> =</span></b> obtained_marks<b><span style="color: #663300;">*</span></b><span style="color: #999900;">100</span><b><span style="color: #663300;">/</span></b>total_marks<b><span style="color: #663300;">;</span></b><span style="color: red;">
if</span><b><span style="color: #663300;">(</span></b>percent<b><span style="color: #663300;"> >=</span></b><span style="color: #999900;"> 60</span><b><span style="color: #663300;">) {</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"Congrats!! You passed in 1st division.\n"</span><b><span style="color: #663300;">);</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"Your percentage is %d.\n"</span><b><span style="color: #663300;">,</span></b>percent<b><span style="color: #663300;">);
}</span></b><span style="color: red;">
else if</span><b><span style="color: #663300;">(</span></b>percent<b><span style="color: #663300;"> >=</span></b><span style="color: #999900;"> 45</span><b><span style="color: #663300;">) {</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"You passed in 2nd division.\n"</span><b><span style="color: #663300;">);</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"Your percentage is %d.\n"</span><b><span style="color: #663300;">,</span></b>percent<b><span style="color: #663300;">);
}</span></b><span style="color: red;">
else if</span><b><span style="color: #663300;">(</span></b>percent<b><span style="color: #663300;"> >=</span></b><span style="color: #999900;"> 33</span><b><span style="color: #663300;">) {</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"You just passed in 3rd division.\n"</span><b><span style="color: #663300;">);</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"Your percentage is %d.\n"</span><b><span style="color: #663300;">,</span></b>percent<b><span style="color: #663300;">);
}</span></b><span style="color: red;">
else</span><b><span style="color: #663300;"> {</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"Sorry! you failed.\n"</span><b><span style="color: #663300;">);</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"Your percentage is %d.\n"</span><b><span style="color: #663300;">,</span></b>percent<b><span style="color: #663300;">);
}</span></b>
scanf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"%s"</span><b><span style="color: #663300;">);</span></b><span style="color: red;">
return</span><span style="color: #999900;"> 1</span><b><span style="color: #663300;">;
}</span></b></code></pre>
<br />
Here value of percent will be calculated as 54.(Note that it's value won't be 54.4 because in our progrm it's not int) percent is below 60 hence 1st condition (percent >= 60) will give false hence we will check next condition(percent >= 45) which will be true hence on screen this will be printed.<br />
<pre><code>You passed in 2nd division.
Your percentage is 54</code></pre>
<br />
If any of the condition is true, only related statement will be executed, subsequent conditions will not be checked, hence this program will not print anything else. One more thing to note that we have used \n inside printf which is not printed because meaning of \n is new line hence whatever comes after \n will be printed on new line. You can try to remove \n and run program again. Similarly try to change value of obtained_marks in given example and run C/C++ program and see output carefully. If you have any question related to this, comment below to ask.<br />
<br />
In next topic we will see some other examples related to if-else.<br />
If you like this tutorial, share it with your friends on facebook, orkut, twitter etc.</div>
<!-- #eng --><br />
<div id="hindi">
आज Hindi के इस C/C++ programming language tutorial को आगे बढ़ाते हुए हम if-else statement के बारे में जानेंगे. इससे पहले comparison operator के बारे में जानना आवश्यक है अतः इसकी जानकारी नीचे दी जा रही है. operator का मतलब है चिन्ह (जैसे की +, - ...). नीचे दिए गए comparison operator Boolean output देते हैं, मतलब ये यह बताते हैं की दिया गया statement true है या false<br />
<ul>
<li><b>< और ></b> : जैसे की 3 > 1 का output है true जबकि 3 < 1 ka output है false. इसी तरह 3 > 3 का output false होगा 3 से बड़ा 3 नहीं है. 3 < 3 का output भी false है.</li>
<li><b><= और >=</b> : ये ऊपर वाले की तरह ही हैं परन्तु 3 >= 3 और 3 <= 3 का output true हो जायेगा. ध्यान रहे की <= की जगह =< का उपयोग नहीं कर सकते. = का चिन्ह > या < के बाद ही आना चाहिए.</li>
<li><b>==</b> : यह बताता है की दो value बराबर हैं या नहीं. यह int, char, आदि सभी को compare कर सकता है. याद रहे की यह = से अलग है. = assignment operator है जो की बाये और लिखे variable को दाई और लिखी value देता है, जबकि == दोनों और लिखे variable या value को compare करता है.</li>
</ul>
<br />
अब हम देखते हैं की इनका उपयोग if else में कैसे करते हैं.<br />
if statement का format निम्न है.<br />
<br />
if(boolean expression1) {<br />
statement1;<br />
} else if(boolean expression2) {<br />
statement2;<br />
}<br />
.....<br />
....<br />
....<br />
else {<br />
statementN;<br />
}<br />
<br />
boolean expression वह है जिसकी value true हो या false. अगर boolean expression1 का मान true है तो सिर्फ statement1 execute होगा. अगर boolean expression1 का मान false है तो हम boolean expression2 देखेंगे. अगर यह true है तो statement2 execute होगा. इसी तरह आगे बढ़ते जायेंगे. जहाँ भी हमें boolean expression का मान true प्राप्त होगा केवल उसी से सम्बन्धित statement execute होगा, कोई और नहीं. अगर कोई boolean expression true नहीं है तो else के अन्दर जो statement है वो execute होगा. इसे एक उदहारण के द्वारा समझते हैं.<br />
नीचे दिए गए example में दिए गए total marks और obtained marks के अनुसार हम percent निकालेंगे और percent के आधार पर grade print करेंगे.<br />
<pre><code style="background: none repeat scroll 0% 0% rgb(227, 228, 250); border: 1px solid; display: block; overflow: auto;"><span style="color: #000099;">#include <stdio.h>
</span><span style="color: #ff6633;">
int</span><span style="color: #990000;"> main</span><b><span style="color: #663300;">() {</span></b><span style="color: #ff6633;">
int</span> total_marks<b><span style="color: #663300;"> =</span></b><span style="color: #999900;"> 500</span><b><span style="color: #663300;">;</span></b><span style="color: #ff6633;">
int</span> obtained_marks<b><span style="color: #663300;"> =</span></b><span style="color: #999900;"> 272</span><b><span style="color: #663300;">;</span></b><span style="color: #ff6633;">
int</span> percent<b><span style="color: #663300;"> =</span></b> obtained_marks<b><span style="color: #663300;">*</span></b><span style="color: #999900;">100</span><b><span style="color: #663300;">/</span></b>total_marks<b><span style="color: #663300;">;</span></b><span style="color: red;">
if</span><b><span style="color: #663300;">(</span></b>percent<b><span style="color: #663300;"> >=</span></b><span style="color: #999900;"> 60</span><b><span style="color: #663300;">) {</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"Congrats!! You passed in 1st division.\n"</span><b><span style="color: #663300;">);</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"Your percentage is %d.\n"</span><b><span style="color: #663300;">,</span></b>percent<b><span style="color: #663300;">);
}</span></b><span style="color: red;">
else if</span><b><span style="color: #663300;">(</span></b>percent<b><span style="color: #663300;"> >=</span></b><span style="color: #999900;"> 45</span><b><span style="color: #663300;">) {</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"You passed in 2nd division.\n"</span><b><span style="color: #663300;">);</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"Your percentage is %d.\n"</span><b><span style="color: #663300;">,</span></b>percent<b><span style="color: #663300;">);
}</span></b><span style="color: red;">
else if</span><b><span style="color: #663300;">(</span></b>percent<b><span style="color: #663300;"> >=</span></b><span style="color: #999900;"> 33</span><b><span style="color: #663300;">) {</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"You just passed in 3rd division.\n"</span><b><span style="color: #663300;">);</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"Your percentage is %d.\n"</span><b><span style="color: #663300;">,</span></b>percent<b><span style="color: #663300;">);
}</span></b><span style="color: red;">
else</span><b><span style="color: #663300;"> {</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"Sorry! you failed.\n"</span><b><span style="color: #663300;">);</span></b>
printf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"Your percentage is %d.\n"</span><b><span style="color: #663300;">,</span></b>percent<b><span style="color: #663300;">);
}</span></b>
scanf<b><span style="color: #663300;">(</span></b><span style="color: #009900;">"%s"</span><b><span style="color: #663300;">);</span></b><span style="color: red;">
return</span><span style="color: #999900;"> 1</span><b><span style="color: #663300;">;
}</span></b></code></pre>
<br />
ऊपर दिखाई गई values के अनुसार percent की value 54 आएगी.(यहाँ गौर करने लायक बात यह है कि percent कि value 54.4 नहीं आएगी क्योंकि हमने अपने program को int बनाया है. ) चूंकि percent 60 से छोटा है अतः पहली वाली condition (percent >= 60) का result false आएगा आएगा इसलिए हम अगली वाली condition (percent >= 45) check करेंगे जो कि true है अतः स्क्रीन पर print होगा<br />
<pre><code>You passed in 2nd division.
Your percentage is 54</code></pre>
<br />
अब चूंकि कोई भी condition true होने पर उसके अन्दर वाले statement ही execute होते हैं. अतः यह C/C++ program और कुछ print नहीं करेगा. यहाँ एक बात ध्यान देने लायक है कि हमने printf के अन्दर \n का उपयोग किया है परन्तु यह print नहीं हुआ, क्योंकि \n का मतलब है new line character अर्थात \n के बाद जो भी print होगा वो अगली line में print होगा. यहाँ आप \n को हटाकर run करें और output देखें. इसी तरह दिए गए example में obtained_marks की अलग अलग value रख कर C/C++ program को run करें और output को ध्यान से देखें. अगर इससे संबधित कोई सवाल आपके पास है तो नीचे comment के माध्यम से पूछ सकते हैं.<br />
<br />
अगले topic में हम if else के कुछ और अलग तरह के example देखेंगे.</div>
<!-- #hindi --><br />
<div style="text-align: center;">
<a href="http://programming-tutorial-hindi.blogspot.com/p/index.html">Index</a> <a href="http://programming-tutorial-hindi.blogspot.com/2010/10/c-hindi-tutorial-if-else-statement-ii.html">Next</a></div>
</div>
Hridayesh Guptahttp://www.blogger.com/profile/09764967341453023170noreply@blogger.com