logical and comparision operator
आज हम C programming के इस hindi tutorial को आगे पढ़ते हुए operators के बारे में और जानेंगे.
सबसे पहले नीचे Logical operators दिए जा रहे हैं.
1. ! - यह  एक uniary operator(NOT) है, जो केवल एक bool value लेता है और उसका उल्टा कर देता है, जैसे कि !true = false, !false = true 
2. || - यह एक binary operator(OR) है जो दो bool value लेता है उसमे से कोई भी true होने पर true देता है अन्यथा false देता है. जैसे कि true || false = true, false || false = false 
3.  && - यह एक binary operator(AND) है जो दो bool value लेता है उसमे से कोई भी false होने पर false देता है अन्यथा true देता है. जैसे कि true || false = false, false || false = false 
इनको इस तरह से समझ सकते हैं कि OR यानि अथवा(या), यानि कि अगर कोई भी एक सही(true) है तो result सही(true) होगा.  अगर दोनो गलत(false) हैं तभी result गलत(false) होगा.
इसी तरह AND यानि और(दोनो), यानि कि दोनो सही(true) है तभी result सही(true) होगा.  कोई भी गलत(false) हैं तो result गलत(false) हो जायेगा.


अब Comparison Operators को एक बार देखते हैं जिनका use पहले भी कर चुके हैं. उसके बार कुछ example में इनको use करेंगे.
1. == - यह  एक binary operator(equal) है जो दो int या दो bool value लेता है, अगर दोनो बराबर है तो bool value true देता है अन्यथा false देता है. जैसे कि (5 == 5) = true , (1 == 2) == false 
2. != - यह  एक binary operator (not equal) है जो दो int या दो bool value लेता है, अगर दोनो बराबर नहीं है तो bool value true देता है अन्यथा false देता है. जैसे कि (5 != 5) = false , (1 != 2) == true 
3. > - यह  एक binary operator (greater than) है जो दो int value लेता है, अगर पहला int बड़ा है तो bool value true देता है अन्यथा false देता है. जैसे कि (5 > 5) = false , (3 > 2) == true 
4. < - यह  एक binary operator (less than) है जो दो int value लेता है, अगर पहला int छोटा है तो bool value true देता है अन्यथा false देता है. जैसे कि (5 < 5) = false , (1 < 2) == true 
3. >= - यह  एक binary operator (greater than or equal) है जो दो int value लेता है, अगर पहला int बड़ा या बराबर है तो bool value true देता है अन्यथा false देता है. जैसे कि (5 > 5) = true , (3 > 2) == true 
4. <= - यह  एक binary operator (less than or equal) है जो दो int value लेता है, अगर पहला int छोटा या बराबर है तो bool value true देता है अन्यथा false देता है. जैसे कि (5 < 5) = true , (1 < 2) == true 

एक example से समझते हैं कि इनका use कैसे होता है. इस example में वर्ष(सन) input में लेंगे और यह print करेंगे कि फरवरी 28 की होगी या 29 की.(सामान्यतः जिस वर्ष में 4 का भाग चला जाये उसमे फरवरी 29 की होती है जिस वर्ष में 100 का भाग जाता है उसमे 400 का भाग भी जाना चाहिए, जैसे क़ी 1900,2100... में फरवरी 28 क़ी है पर 2000 में 29 की)
#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");
}
% यह बताता है भाग देने पर शेष कितना आया. अगर शेष 0 आया इसका मतलब भाग चला गया. इस example में पहले हमने ये check किया है क़ी वर्ष में 4 का भाग जाये(i%4 ==0 ) और(&&) 100 का भाग न(i %100 !=0) जाये(1984, 2012) या फिर 400 का भाग चला जाये. इसी को थोडा दुसरे तरीके से भी लिख सकते हैं.
#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");
}
Binary Number System
आज इस c++ programming के इस hindi tutorial को आगे बढ़ाते हुए logical operators के बारे में जानेंगे. इसके लिए निम्न बातों का ज्ञान जरुरी है.
1. Binary Number System - इसके बारे में नीचे बताया जा रहा है, उसके बाद logical operator पढेंगे.
Binary से decimal और decimal से binary में convert करना भी सीखेंगे.

किसी चीज को गिनने के लिए गिनती जरुरत होती है. इस गिनती को लिखने के लिए एक ऐसे तरीके की जरुरत होती है जिसके द्वारा कितनी भी ज्यादा चीजों को गिन सकें. इसके लिए बचपन में हमने जो गिनती सीखी है उसमे 0 से 9 तक अंक होते हैं. इन अंको का use करके कहीं तक भी गिनती लिख सकते हैं. किसी भी संख्या का अगला अंक जानने के लिए हम इकाई का अंक एक बढ़ा देते हैं जैसे कि 85 के बाद 86 आता है(क्योंकि इकाई 5 का अगला अंक 6 है). अगर इकाई का अंक सबसे बड़ा अंक हो, तो फिर उसे वापस 0 कर देते हैं और दहाई का अंक 1 बढ़ा देते हैं. जैसे कि 59 के बाद 60 आता है क्योंकि इकाई 9 है जो कि सबसे बड़ा अंक है(अंक सिर्फ 0 से 9 तक हैं) इसलिए 0 हो गया और दहाई एक बढ़ गया.
अब मान लीजिए आपके पास 0 से 7 तक कुल 8 अंक ही हैं. सिर्फ इन 8 अंको से गिनती कैसे लिखेंगे? उसी तरह जैसे कि ऊपर बताया गया है. 7 के बाद 10 आ जायेगा, क्योंकि हमारे पास 0 से 7 तक अंक ही हैं. गिनती कुछ इस तरह होगी.
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 ...
इसे लिखने में अगला अंक निकालने के लिए में वही तरीका अपनाया गया है जो ऊपर बताया गया है. अगर समझने में कोई दिक्कत है तो मेरी discussion site पर आकर मुझसे discuss कर सकते हैं.
* * * * * * * * * * * *
अगर आपसे पूछा जाये कि ऊपर कितने * हैं लिखकर बताइए तो आप लिखेंगे 12. परन्तु अगर आपकी गिनती में 0 से 7 तक अंक ही होते तो आप लिखते 14(ऊपर लिखी गिनती के अनुसार गिनिए) .
गिनती लिखने का system(Counting system) जिसमे 0 से 9 तक कुल 10 अंक होते हैं, decimal system(आधार 10,base 10) कहते हैं. ऊपर हमने 0 से 7 तक कुल 8 अंको का use करके जिस system का use करके गिनती लिखी उसे octal system(आधार 8,base 8) कहते हैं.
अब स्थानीय मान को याद करते हैं जो कि बचपन में पढ़ा था. चूँकि हमारे Counting system 12 को स्थानीय मान के रूप में इस तरह लिखेंगे.
12 = 2x100 + 1x101 = 10 + 2
Octal system(आधार 8,base 8) में वही संख्या 14 थी. चूँकि octal system में 8 अंक ही होते हैं इसलिए इसे स्थानीय मान के रूप में इस तरह लिखेंगे.
14 = 4x80 + 1x81 = 8 + 4
संख्या वही है परन्तु अलग अलग system में लिखने का तरीका अलग है. * उतने ही हैं, परन्तु decimal system में उसे 12 और octal system में उसे 14 लिखेंगे.

इसी तरह अगर आपके पास 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 लिखते.

गिनने का तरीका ꜜ ************
decimal(0-9 अंक)123456789101112
0-7 अंक 12345671011121314
0-3 अंक 123101112132021222330
अगर अगल-बगल में अलग अलग base वाली गिनती लिखें तो यह पता चलेगा कि 11(base 10) को base 8 में 13, base 4 में 23 लिखा जायेगा. आप जानते ही हैं कि सामान्यता लिखने के लिए हम base 10 वाली संख्याए use करते हैं. किसी अन्य base वाली संख्या लिखते समय उसके नीचे base भी लिख देते हैं. जैसे कि base 8 में 75 = (75)8, base 4 में 123 = (123)4
किसी भी base वाली संख्या को base 10 वाली संख्या में convert करना
इसके लिए उस संख्या को स्थानीय मान के रूप में लिखकर जोड़ दें. नीचे example दिए जा रहे हैं.
(75)8 = 5x80 + 7x81 = 5x1 + 7x8 = 61 (base 10 में)
(123)4 = 3x40 + 2x41 + 1x42 = 3x1+ 2x4 + 1x16 = 27 (base 10 में)
base 10 वाली संख्या को किसी अन्य base वाली संख्या में convert करना
जिस base में convert करना है उस संख्या से भाग दें. शेष जो बचेगा वह  इकाई का अंक हो जायेगा. भागफल को फिर base से भाग दें. नया शेष जो बचेगा वह दहाई का अंक हो जायेगा. नए भागफल को फिर से base से भाग दें. यह process तब तक करते रहे जब तक भागफल 0 न आ जाये. नीचे कुछ example दिए जा रहे हैं.
98 को base 8 में बदलने का example:
98/8 = 12(भागफल),2(शेष) ---> 2(इकाई)
12/8 = 1(भागफल), 4(शेष) ---> 4(दहाई)
1/8 = 0(भागफल), 1(शेष) --> 1 (सैकडा)
इसलिए 98 = (142)8
98 को base 4 में बदलने का example:
98/4 = 24(भागफल),2(शेष) ---> 2(इकाई)
24/4 = 6(भागफल), 0(शेष) ---> 0(दहाई)
6/4 = 1(भागफल), 2(शेष) --> 2 (सैकडा)
1/4 = 0(भागफल), 1(शेष) --> 1(हज़ार)
इसलिए 98 = (1202)4
Binary Number System: अगर हमारे पास सिर्फ 2 अंक ही होते - 0 और 1, तो हम गिनती कुछ इस तरह लिखते:
0,1,10,11,100,101,110,111,1000,1001,1010,1011,1100 ... इस system को जिसमे सिर्फ 2 अंक ही होते हैं, Binary system(द्विआधारी) कहते हैं.
98 को base 2 में बदलने का example:
98/2 = 49(भागफल),0(शेष) ---> 0(इकाई)
49/2 = 24(भागफल), 1(शेष) ---> 1(दहाई)
24/2 = 12(भागफल), 0(शेष) --> 0(सैकडा)
12/2 = 6(भागफल), 0(शेष) --> 0(हज़ार)
6/2 = 3(भागफल), 0(शेष) --> 0(दस हज़ार)
3/2 = 1(भागफल), 1(शेष) --> 1(लाख)
1/2 = 0(भागफल), 1(शेष) --> 1(दस लाख)
इसलिए 98 = (1100010)2
1100010 को base 10 में बदलने का example:
(1100010)2 = 0x20 + 1x21 + 0x22 + 0x23 + 0x24 + 1x25 + 1x26 = 0+2+0+0+0+32+64 = 98
logical operators के बारे में अगले भाग में जानेंगे.
Arithmetic Operators
आज Hindi के इस C/C++ programming language tutorial को आगे बढ़ाते हुए हम arithmetic operators के बारे में और जानेंगे.
operator यानि संक्रिया. operator एक function की तरह ही है जिसे एक चिन्ह के द्वारा लिख सकते हैं, जो एक या एक से अधिक मान लेता है और output में एक मान देता है(एक मान return करता है) इनकी जानकारी नीचे दी गयी है.
  1. +: यह दो संख्याए लेता है और उनका sum return करता है. जैसे कि 5+3 8 return करेगा, इसलिए a = 5+3 लिखने पर a की value 8 हो जाती है.
  2. -:यह दो संख्याए लेता है और उनका difference return करता है.
  3. *:यह दो संख्याए लेता है और उनका product return करता है.
  4. /:यह दो संख्याए लेता है और उनका भागफल(divisor) return करता है.
  5. %:यह दो संख्याए लेता है और उनका शेषफल(remainder) return करता है.
ऊपर दिए गए operator arithmetic operator हैं.
int a = 5;
int b = 3;
a + b;
ऊपर दिए गए program की तीसरी line में a और b को add किया गया है जो कि 8 return करेगा. a और b की value change नहीं होंगी. return value 8 को हमने किसी variable में save नहीं किया इसलिए यह lost हो जायेगा. नीचे example में हमने उस return value को c में save किया है.
int a = 5;
int b = 3;
int c = a + b;
= भी एक operator है जो अपने right side लिखे गए expression की value को left side में लिखे गए variable में डाल देता है. जैसे कि ऊपर c = a+b लिखने पर right side में a+b 8 देगा इसलिए c की value 8 हो जायेगी.
int a = 5;
int b = 3;
a = b;
ध्यान दे कि ऊपर दिए गए 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 भी करता है.
int a = 5;
int b = 3;
int c,d;
d = (c = a % b);//% शेषफल देता है.
ऊपर दिए गए example में c = a%b करने से c की value 2 हो जायेगी और यह(c=a%b) 2 return भी करेगा इसलिए d = (c=a%b) लिखने से return value भी d में भी आ जायेगी.
ऊपर दिए गए सभी operators जिस तरह का input(int,float etc.) लेते हैं उसी तरह का output देते हैं. जैसे कि अगर 30/8 करेंगे तो 3 आएगा. 30 और 8 दोनों int हैं इसलिए result भी int आएगा, दशमलव में मान नहीं आएगा, दशमलव के बाद वाले अंक हट जायेंगे. agar 30.0/8.0 करेंगे तो 3.75 आएगा.
ऊपर दिए गे सभी operator, binary operator हैं. इसका मतलब यह हुआ कि वो 2 संख्याए लेते हैं. अब हम कुछ unary operators के बारे में जानेंगे जो सिर्फ एक ही संख्या लेते हैं.
1. x++ और ++x : ये दोनों ही variable x की value 1 बढ़ा देते हैं. x++ x की पुरानी value return करेगा और ++x x की नयी value return करेगा. अगर सिर्फ x++ या ++x लिखना हो तब दोनों एक है हैं परन्तु यदि इनका use किसी expression में करना हो तब ध्यान रखना चाहिए. नीचे example से और स्पष्ट हो जायेगा.
int a = 5;
int b = a++;
int c = ++a;
int d = (b++) + (++c);
ऊपर दिए गए 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)
x-- और --x भी ठीक इसी तरह कम करते हैं परन्तु यह x की value 1 कम करते हैं. ये दोनों operator(++ और --) सिर्फ int(पूर्णांक) के लिए हैं. float और double datatype में use करने पर error आएगी.

Read in Hindi
Read in English

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.

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.

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.

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.

Pre-processing

In a program any line starting with # is called pre-processor. In 1st step of compilation process compiler modifies them. #include is a pre-processing directive which replaces it with content file given. There are some more pre-processing directive which are described below.

#define ABC 1
#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.

#define SUM(a,b) (a+b)
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.

#ifdef xyz
...
#endif
If xyz is defined anywhere using #define then program between #ifdef and #endif will be compiled otherwise it will be removed.

Note: One pre-processor should be written in one line only and nothing else in that line.
We are seeing #include from beginning. Below we will see example of some other pre-processor.

#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;
}

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.
हमने C का basic जान लिया है, परन्तु #include <stdio.h> क्यों लिखते हैं ये नहीं जाना. आज इस यह और इससे related बाते जानेंगे.

शुरू में हमने पढ़ा था कि हम program लिखते हैं, उसके बाद computer उसे machine की भाषा में बदलता है. इस प्रक्रिया(process) को कहते हैं program को compile करना. यह compile करने कि प्रक्रिया कई चरणों(steps) में होती है. आज हम इसके पहले step को जानेंगे. इसके साथ ही हम यह भी जान लेंगे कि हर program के शुरू में #include <stdio.h> क्यों लिखते हैं.

हम जानते हैं कि 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 नहीं मिल रहा.

stdio का मतलब है Standard Input and Output. इस file(stdio.h) में वो सब function defined हैं जो input/output लेने के काम आते हैं. printf output को print करने के काम आता है जबकि scanf input लेने के काम आता है. दोनों ही function stdio.h file में defined हैं.

Pre-processing

किसी भी program में जो line # से start होती हैं उन्हें pre-processor कहते हैं. Compiler पहले step में इन्हें process करके program को modify कर देता है. जैसा कि ऊपर बताया गया है. #include एक pre-processing directive हैं जो उसके बाद लिखे गए file के content को उसकी जगह लिख देता है. कुछ और भी pre-processing directive होते हैं जिनके बारे में नीचे बताया गया है.

#define ABC 1
#define किसी भी constant को define करने के लिए उपयोग में लाया जाता है. #define ABC 1 लिखने पर जहाँ जहाँ program में ABC लिखा होगा उसकी जगह 1 compiler पहले step में 1 लिख देगा. #define को macro भी कहते हैं.

#define ADD4(a) (a+4)
यह भी एक macro है जो parameter ले सकता है. इसका use करने पर program compile करने के 1st step में जहाँ भी ADD4(x) लिखा होगा वहां (x+4) लिख जायेगा जहाँ x कुछ भी हो सकता है, variable या कुछ और. जैसे कि अगर ADD4(5) लिखा है तो उसकी जगह (5+4) हो जायेगा.
#ifdef xyz
...
#endif
अगर हमने #define का use करके xyz कहीं define किया है तो #ifdef और #endif के बीच में लिखा हुआ program compile होगा अन्यथा दोनों के बीच लिखा हुआ program हटा दिया जायेगा.

Note: एक pre-processor एक line में ही लिखा जाता है. एक line में एक से ज्यादा pre-processor या एक pre-processor एक से ज्यादा line में नहीं लिख सकते, अन्यथा program नहीं चलेगा.
#include का example हम शुरू से देखते आये हैं. नीचे अन्य pre-processor commands के example भी देखते हैं.

#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;
}

ऊपर दिए गए 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 को मिल जाती है.
अगर आपको यह लेख पसंद आया हो तो अपने दोस्तों को भी बताएं क्योंकि ये उनके लिए भी उपयोगी सिद्ध हो सकता है!! नीचे दिए गए link के माध्यम से इसे आसानी से facebook twitter और Google Buzz पर भी Share कर सकते हैं.
void pointer
अभी तक हमने जाना कि किसी भी pointer को define करते समय यह बताना पड़ता है कि वह किस तरह के variable का address store करेगा. int* p में p int का address store करेगा. void pointer वह होता है जो किसी भी तरह के variable का address store कर सकता है. इसलिए जब void pointer से value पढते हैं तो उस समय यह बताना पड़ता है कि वह value हम int की तरह पढ़ना चाहते हैं या char या float...
इसे हम एक example की help से समझते हैं.

#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;
}
ऊपर दिए गए 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 कर सकते हैं.

malloc and free
आपने pointer के बारे में पढ़ा होगा। जब कोई भी variable define करते हैं तो वह memory में चला जाता है(अर्थात उस variable के लिए memory allocate होती है) और pointer का use करके उस memory का address ले सकते हैं. memory allocate करने का दूसरा तरीका है malloc.
malloc(4) call करने पर 4 byte memory allocate हो जाएगी। और उसका address return हो जायेगा. आपको याद होगा की int 4 byte जगह लेता है, इसलिए malloc(4) call करने पर एक int के लिए memory मिल जाएगी.
malloc एक memory का address देता है उसमे किसी भी type का data रख सकते हैं चाहे int हो या float string double char array कुछ भी, इसलिए malloc void pointer (void*) return करता है. उसका use int store करने के लिए इस तरह करेंगे.
int* i = (int*) malloc(4); //malloc void* देता है. उसमे int रखने के लिए int pointer(int*)में typecast किया गया
*i = 1;
printf("%d",*i);
अलग अलग system में int अलग अलग memory भी ले सकता है, जरुरी नहीं की 4 byte ही ले. C में एक function होता है sizeof जो यह बता देता है की कोई datatype कितनी जगह लेगा.

जैसे sizeof(int) 4 देगा(अगर आपके system में int 4 byte ले रहा है).इसलिए हम int के लिए memory लेने के लिए malloc(sizeof(int )) use करते हैं.अगर 10 int store करने की जगह चाहिए तो इस तरह कर सकते हैं.
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
यहाँ पर 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 करते हैं)

अगर ये सारे user से input में लेने हैं तो इस तरह
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 
जब आप कोई variable define करते हैं तो उसके लिए अपने आप memory बनती है. जब आप उस variable का use बंद कर देते हैं तो वह memory अपने आप free हो जाती है. पर अगर आपने malloc का use करके memory ली है तो वह कभी अपने आप free नहीं होगी. उसे free करने के लिए आप free call करना पड़ेगा. for example
int* a = (int*) malloc(10*sizeof(int));
//use memory
free(a);
अगर memory free नहीं करते तो आपका program memory(RAM) लेता ही जायेगा. अगर बहुत सारी memory ले ली तो आपका system slow या hang भी हो सकता है.जब वह program बंद होता तो उसके द्वारा ली गयी सारी memory अपने आप free हो जाती है.

Read in Hindi
Read in English

Array and pointer

Going forward with this C/C++ programming language tutorial we will understand how to access array as a pointer.
We define array as shown below.
int A[10];
A[0] = 0; A[1] = 10; A[2] = 20; ...
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.
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)
Lets see an example.
#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;
}
Run the program to see the output.

Array and pointer

आज Hindi के इस C/C++ programming language tutorial को आगे बढ़ाते हुए हम array को pointer के तरीके से जानेंगे.
हम नीचे दिए गए तरीके से array define करते हैं.
int A[10];
A[0] = 0; A[1] = 10; A[2] = 20; ...
इसमें A एक pointer ही होता है. A में array के पहली position वाले int का address store रहता है. A पहली position वाले int का address है, इसलिए A+1 दूसरी position वाले int का address हो जायेगा, A+2 तीसरी position वाले int का...
चूंकि 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)
इसका एक example देखते हैं.
#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;
}
इसको run करके output का अध्ययन करें.
Read in Hindi
Read in English

Going forward with this C/C++ programming language tutorial we will know more about pointer.
See the example given below and think for a while what it will print. you may wonder to see the actual output.

#include <stdio.h>

void add1(int i) {
  i = i + 1;
}

int main() {
  int x = 5;
  printf("before adding x = %d \n", x);
  add1(x);
  printf("after adding x = %d \n", x);

  scanf("%d", &x);
  return 1;
}

It will give the following output when run.
before adding x = 5 
after adding x = 5 
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.

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.

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.

Now see the C++ program given below which uses pointer and think what will be output.
#include <stdio.h>

void add1(int* i) {
  *i = *i + 1;
}

int main() {
  int x = 5;
  printf("before adding x = %d \n", x);
  add1(&x);
  printf("after adding x = %d \n", x);

  scanf("%d", &x);
  return 1;
}
It will give the output -
before adding x = 5 
after adding x = 6 
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.
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
Later we will know more about pointer.
आज Hindi के इस C/C++ programming language tutorial को आगे बढ़ाते हुए हम pointer के बारे में और जानेंगे.
नीचे दिए गए इस example को देखिये और सोचिये कि यह क्या print करेगा, आपको इसका actual output देखकर आश्चर्य होगा.

#include <stdio.h>

void add1(int i) {
  i = i + 1;
}

int main() {
  int x = 5;
  printf("before adding x = %d \n", x);
  add1(x);
  printf("after adding x = %d \n", x);

  scanf("%d", &x);
  return 1;
}

run करने पर इसका output यह आएगा.
before adding x = 5 
after adding x = 5 
अब समझते हैं कि यह 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 हो रहा है.

इसका कारण यह है कि जब add1 function call हुआ तो उसमे pass किये गए variable x की एक अलग copy बन गयी, add1 function ने उस copy की value change की है, इसलिए original x की value change नहीं हुई.

जब भी कोई function call होता है उसमे pass किये गए variable की copy बन जाती है और वह copy call किये गए function को दी जाती है. इसलिए call किया गया function अगर arguments की value change करता है तो original variable की value change नहीं होती, copy की value change होती है.

अब नीचे वाला C++ program देखिये जो pointer का use करके लिखा गया है और सोचिये कि इसका output क्या होगा
#include <stdio.h>

void add1(int* i) {
  *i = *i + 1;
}

int main() {
  int x = 5;
  printf("before adding x = %d \n", x);
  add1(&x);
  printf("after adding x = %d \n", x);

  scanf("%d", &x);
  return 1;
}
run करने पर इसका output यह आएगा.
before adding x = 5 
after adding x = 6 
अब इसे समझते हैं कि यहाँ function call करने पर x की value बढ़ क्यों गयी. यहाँ add1 function int नहीं लेता बल्कि int variable का address लेता है, और उस address पर जो भी value होती है उसे 1 बढ़ा देता है.
main में जब add1 function को call किया है तो उसमे x का address pass किया है. ऊपर बताये गए अनुसार x के address की एक copy बनेगी जो add1 function को दी जायेगी. अगर add1 function address को change करता तो x की value change नहीं होती पर add1 function उस address में store variable की value change कर रहा है. चाहे वह original address हो या address की copy, address तो उसी variable का ही रहेगा, इसलिए अगर हम उस address पर store variable को change करते हैं original variable भी change हो जायेगा.

अभी pointer के बारे में और जानना भी बाकी है. इसलिए आगे के लेखों का wait करें.

Read in Hindi
Read in English

Going forward with this C/C++ programming language tutorial today we will know more about pointer.
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 C/C++ struct.
#include <stdio.h>

struct rectangle {
  int width;
  int length;
};

int main() {
  struct rectangle r1;
  r1.width = 10;
  r1.length = 15;

  struct rectangle* r2;
  r2 = &r1;
  printf("Original width=%d, length=%d\n", (*r2).width, (*r2).length);

  r1.width = 20;
  r1.length = 25;
  printf("r1 changed, width=%d, length=%d\n", (*r2).width, (*r2).length);

  (*r2).width = 5;
  (*r2).length = 10;
  printf("*r2 changed, width=%d, length=%d\n", r1.width, r1.length);
  return 0;
}

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.

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.)
Important Note about pointer
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.
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.
आज Hindi के इस C/C++ programming language tutorial को आगे बढ़ाते हुए हम pointer के बारे में और जानेंगे.
पिछली बार हमने इस C/C++ programming language tutorial में int pointer का example देखा था परन्तु pointer किसी भी datatype का हो सकता है. struct का भी pointer हो सकता है. हम struct pointer का एक example देखते हैं. अगर आपको struct का use करना नहीं आता तो पहले इसे पढ़ लें. C/C++ struct hindi में पढ़ने के लिए यहाँ click करें.
#include <stdio.h>

struct rectangle {
  int width;
  int length;
};

int main() {
  struct rectangle r1;
  r1.width = 10;
  r1.length = 15;

  struct rectangle* r2;
  r2 = &r1;
  printf("Original width=%d, length=%d\n", (*r2).width, (*r2).length);

  r1.width = 20;
  r1.length = 25;
  printf("r1 changed, width=%d, length=%d\n", (*r2).width, (*r2).length);

  (*r2).width = 5;
  (*r2).length = 10;
  printf("*r2 changed, width=%d, length=%d\n", r1.width, r1.length);
  return 0;
}

ऊपर दिए 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 मिलेगी.

ऊपर दिए गए program को ध्यान से देखिये. r2 address(pointer) है r1 का, इसलिए *r2 हमें struct rectangle देगा(pointer वाले address की value * लगाने पर आती है इसलिए *r2 और r1 एक ही हैं. इसी तरह (*r2).width और r1.width भी एक ही हैं दोनों में से किसी एक को change करने पर दूसरा भी change हो जाता है.)
Important Note about pointer
1. (*r2).width लिखने का shortcut r2->width भी होता है. program मे (*r2).width की जगह r2->width और (*r2).length कि जगह r2->length भी लिख सकते हैं. आप ऊपर दिए गए program में यह लिखकर चलाकर देखें.
2. कोई भी variable declare करने का मतलब है कि उसमे कोई value नहीं डाली(जैसे int x;) और define करने का मतलब है कि उसमे value भी डाल दी है(जैसे int x=1;) अगर आपने pointer सिर्फ declare किया है और उसमे किसी variable का address नहीं डाला तो उससे value read करने में(* का use करके जैसे *r2) program crash हो जायेगा और segmentation fault दे देगा.
आज के लिए इतना ही. अभी pointer के बारे में बहुत कुछ जानना बाकी है.


Read in Hindi
Read in English

Going forward with this C/C++ programming language tutorial today we will learn pointer which is considered most difficult in C/C++.

What is Pointer in C/C++ programming language

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.
int x = 5;
int* p;
p = &x
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.
Address→01234
Memory→ 1000011111100101001001100000101 01100101 . . . 
p = &x = 3int x
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.
#include <stdio.h>

int main() {
  int x = 5;
  int* p = &x;
  printf("x = %d\n",x);
  printf("address of x = %d\n", p);
  printf("value at location p = %d\n", *p);

  scanf("%d", &x);
  return 0;
}
In next topic we will see use of pointer.
आज Hindi के इस C/C++ programming language tutorial को आगे बढ़ाते हुए हम pointer के बारे में जानेंगे जो C/C++ में सबसे कठिन माना जाता है.

What is Pointer in C/C++ programming language

इससे पहले हम यह जान चुके हैं कोई भी 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 करते हैं.
int x = 5;
int* p;
p = &x
यहाँ पहले एक int variable x define किया है, फिर p ऐसा variable declare किया है जो किसी int का address store करता है. फिर p variable में x का address डाल दिया है.(जैसा कि हम जानते हैं कि किसी भी variable का address जानने के लिए & का use करते हैं.)
Address→01234
Memory→ 1000011111100101001001100000101 01100101 . . . 
p = &x = 3int x
अब हमारे पास एक 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 करें.
#include <stdio.h>

int main() {
  int x = 5;
  int* p = &x;
  printf("x = %d\n",x);
  printf("address of x = %d\n", p);
  printf("value at location p = %d\n", *p);

  scanf("%d", &x);
  return 0;
}
आज के लिए इतना ही. अगले topic में Hindi में C/C++ tutorial को आगे बढ़ाते हुए pointer के use देखेंगे.

Read in Hindi
Read in English

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.
00000000 = 0 (in decimal)
11111111 = 28 -1 = 255 (in decimal)
Hence in 8 bit or 1 byte numbers from 0 to 255 can be stored.
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.
char: See ASCII table. 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.
short int: short int variable is stored in 2 byte(16 bits). In 2 bytes any number from 0 to 65535(216-1) can be stored. Since short int can take negative value too hence it can take values from-32768(-215) to 32767(215-1).
unsigned short int: This also takes 2 bytes but does not take negative values hence any number from 0 to 65535 can be stored
int: It takes 4 bytes and can take negative value hence any number from -231 to 231-1 can be stored in this variable.
unsigned int: This also takes 4 byte but only positive values hence any number from 0 to 232-1 can be stored.
float: format to store this type of number is complex because it can take fraction values too. I takes 4 byte.
double: It takes 8 byte and can take fraction values.
There are some other datatypes also which are not needed at this point of time.

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.
Now you are ready to know about pointer. In next topic we will learn about pointer.
आज 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 करके पढते हैं.
00000000 = 0 (in decimal)
11111111 = 28 -1 = 255 (in decimal)
इसलिए इस 8 bit या 1 byte में 0 से 255 तक की कोई भी संख्या store हो सकती है.
इसी तरह अगर हमें और ज्यादा बड़ी संख्या store करना हो तो हम 2 byte को एक साथ लेकर उसमे store करेंगे. 2 byte में 00000000 00000000 से 11111111 11111111 यानी 0 से 65535 तक कि संख्या store हो सकती है. अब हम देखते हैं कि विभिन्न datatype किस तरह से store किये जाते हैं.
char: ASCII table को देखिये इसमें हर एक अक्षर(character) के सामने एक Decimal संख्या लिखी हैं. जब हम char variable में कोई अक्षर store करते हैं तो computer उस अक्षर के सामने वाली संख्या को store कर देता है. जैसे कि अगर हम 'a' store करते हैं तो उसके सामने लिखी संख्या 97(01100001) store हो जायेगी. उस table के अनुसार किसी भी अक्षर को store करने के लिए 255 से बड़ी संख्या की जरूरत नहीं है इसलिए char को store करने के लिए 1 byte memory कि जरूरत होती है.
short int: short int variable 2 byte(16 bits) में store होता है. 2 byte में 0 से 65535(216-1) तक कि संख्या store कर सकते हैं. चूंकि short int negative value भी ले सकता है इसलिए यह -32768(-215) से 32767(215-1) तक के मान ले सकता है.
unsigned short int: यह भी 2 byte कि जगह लेता है परन्तु negative value नहीं ले सकता इसलिए 0 से 65535 तक के मान ले सकता है.
int: यह 4 byte लेता है और negative value भी ले सकता है इसलिए -231 से 231-1 तक की value ले सकता है.
unsigned int: यह भी 4 byte लेता है परन्तु केवल positive value इसलिए 0 से 232-1 तक की value ले सकता है.
float: इसके store करने का format complex है क्योंकि यह दशमलव value भी ले सकता है. यह 4 byte लेता है.
double: यह 8 byte लेता है और दशमलव value भी ले सकता है.
इसी  तरह कुछ और भी datatype होते हैं जिनके बारे में जानना अभी आवश्यक नहीं है.

पिछली बार हमने देखा था कि 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 हुआ.
अब आप pointer के बारे में जानने के लिए ready है. अगली बार हम pointer के बारे में पढेंगे.

Read in Hindi
Read in English

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.

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.
110100011 0 . . .

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.
1101000110011101001010011111100110000011 00100110 . . .

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.

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.
अब तक हमने C/C++ programming के बारे में बहुत कुछ जान लिया है और इस hindi tutorial के द्वारा आप बड़े बड़े program लिखने के लिए भी Ready हैं. अब हम pointer को सरल तरीके से सीखेंगे जिसे C/C++ में सबसे कठिन माना जाता हैं. इसे सरल तरीके से पेश करने कि कोशिश रहेगी, फिर भी समझने में दिक्कत हो तो नीचे comment करें.

Pointer समझने से पहले Memory के बारे में जानना जरूरी है इसलिए आज हम Memory Structure को समझेंगे. आप में से बहुत से लोगो को पता होगा कि computer या कोई भी electronic device जैसे calculator, micro processor, mobile phone सभी सिर्फ 0 और 1 की भाषा समझते हैं, आज हम इसे detail में समझते हैं. किसी भी device की Memory को हम एक बहुत बड़ी array की तरह देख सकते हैं. जैसा कि नीचे चित्र में दिखाया गया है.
110100011 0 . . .

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 को नीचे दिखाए गए चित्र जैसा पढता है.

1101000110011101001010011111100110000011 00100110 . . .

अगर 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 नहीं होता.

आज के लिए इतना ही. अगली बार हम ये देखेंगे कि इस Memory में इतने variable store कैसे होते हैं उसके बाद pointer सीखेंगे. अगला लेख पढ़ने से पहले Binary to Decimal Conversion सीख लें और दो Binary number को जोड़ना भी सीख लें.

Read in Hindi
Read in English
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.

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.
#include <stdio.h>

int main() {
  int length1 = 12;
  int width1 = 8;

  int length2 = 20;
  int width2 = 11;

  printf("Rectangle1: %d %d\n", length1, width1);
  printf("Rectangle2: %d %d\n", length2, width2);

  scanf("%d", &length1);
}
It is easy to understand program given above. Now we will write same thing with the use of struct.
#include <stdio.h>

struct Rectangle {
  int length;
  int width;
};
int main() {
  struct Rectangle rect1;
  struct Rectangle rect2;
  rect1.length = 12;
  rect1.width = 8;  
  rect2.length = 20;
  rect2.width = 11;

  printf("Rectangle1: %d %d\n", rect1.length, rect1.width);
  printf("Rectangle2: %d %d\n", rect2.length, rect2.width);

  scanf("%d", &(rect1.length));
}

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 { ... };
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.

Using struct we can make any kind of object which can store several variables.
If you liked this blog, tell your friends also because it can useful to them also!!
आज 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 ही बनाना पड़ेगा

दो आयत की length और width store करने का program struct का use किये बिना नीचे दिया जा रहा है, उसके बाद हम उसका better version struct का use करके भी देखेंगे.
#include <stdio.h>

int main() {
  int length1 = 12;
  int width1 = 8;

  int length2 = 20;
  int width2 = 11;

  printf("Rectangle1: %d %d\n", length1, width1);
  printf("Rectangle2: %d %d\n", length2, width2);

  scanf("%d", &length1);
}
ऊपर दिए गए program को समझाने कि कोई जरूरत नहीं है. अब हम इसी को struct का use करके कैसे लिखेंगे वो देखते हैं.
#include <stdio.h>

struct Rectangle {
  int length;
  int width;
};
int main() {
  struct Rectangle rect1;
  struct Rectangle rect2;
  rect1.length = 12;
  rect1.width = 8;  
  rect2.length = 20;
  rect2.width = 11;

  printf("Rectangle1: %d %d\n", rect1.length, rect1.width);
  printf("Rectangle2: %d %d\n", rect2.length, rect2.width);

  scanf("%d", &(rect1.length));
}

इसे ध्यान से समझने कि जरूरत है. सबसे पहले हमने एक 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 आएगा.
अब 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 होती है.

यहाँ rect1 एक Rectangle datatype बन गया है यहाँ यह datatype दो value length और width store कर सकता है. इस तरह से किसी भी object को इस तरह के struct बनाकर store कर सकते हैं.
इसे अपने दोस्तों को बताना न भूले. नीचे दिए गए link की सहायता से इसे facebook, twitter, Buzz पर share अपने दोस्तों से करें.

Read in Hindi
Read in English

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).

use of break in c/c++

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.
Lets see the example below in which we want to search a number divisible by 21 between 100 and 200.
#include <stdio.h>

int main() { 
  int i;
  for(i=100; i<=200; i++) { 
    if(i%21 == 0) {
      printf("1st such number is %d\n", i);
      break;
    }
  }
  scanf("%d", &i);
  return 0;
}

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.
Similarly in c/c++ to break while loop break is used. We have seen use of break to break switch case statement.

use of continue in c/c++

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.
#include <stdio.h>
int main() {
  int arr[] = {1,4,7,2,0,-5,8,17,5,-10};
  int length = 10;
  int i;
  for(i=0; i<10; i++) {
    if(arr[i]%2 == 0) {
      continue;
    }
    printf("Odd number is %d\n", arr[i]);
  }
  scanf("%d", &i);
  return 0;
}

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.
Similarly continue can be used in while loop.
If you like this tutorial, share it with your friends on facebook, orkut, twitter etc.
आज Hindi के इस C/C++ programming language tutorial को आगे बढ़ाते हुए हम दो important keywords break और continue का use करना सीखेंगे, जो loops (for loop, while loop, switch case) में कभी कभी काम आ जाते हैं.

use of break in c/c++


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 करते हैं.
इसे नीचे दिए गए example से समझते हैं. इसमें हम 100 से 200 के बीच पहली संख्या search करेंगे जो 21 से विभाजित हो जाये.
#include <stdio.h>

int main() { 
  int i;
  for(i=100; i<=200; i++) { 
    if(i%21 == 0) {
      printf("1st such number is %d\n", i);
      break;
    }
  }
  scanf("%d", &i);
  return 0;
}

यहाँ 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 खत्म हो जायेगा.
इसी तरह c/c++ में while loop को भी बीच में खत्म करने के लिए break का use करते हैं. switch case statement में हम break का use देख ही चुके हैं.

use of continue in c/c++

जैसा कि हम जानते हैं कि 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 करेंगे.
#include <stdio.h>
int main() {
  int arr[] = {1,4,7,2,0,-5,8,17,5,-10};
  int length = 10;
  int i;
  for(i=0; i<10; i++) {
    if(arr[i]%2 == 0) {
      continue;
    }
    printf("Odd number is %d\n", arr[i]);
  }
  scanf("%d", &i);
  return 0;
}

ऊपर दिए गए example में for loop का use करके शुरू से array के एक एक number को देखते हैं. अगर number even है तो हम continue का use करके skip कर देते हैं और अगली बार for loop के अन्दर आते हैं.
इसी तरह while loop में भी continue का use कर सकते हैं.
इस blog को बेहतर बनाने के लिए आपके सुझावों का सदैव स्वागत रहेगा. सुझाव देने के लिए अपना Reply यहाँ पर दें. अगले लेख में हम function के बारे में जानेंगे जो कि बहुत important है.