C Programming Language in hindi and English


PROGRAMING
IN HINDI
               
What is Programming?

Introduction to Programming 
India में हर साल लाखों students computer scientists बनते है। हालाँकि एक computer scientist सिर्फ programming ही नहीं करता है, लेकिन programming computer science का बहुत ही महत्वपूर्ण part है। इसलिए एक शुरआती programmer होने के नाते आपके लिए programming और उससे related components का concept ठीक से समझना बहुत जरुरी है। कितने ही ऐसे programmers होते है जो programming तो कर रहे होते है लेकिन उनको programming के पीछे का concept नहीं पता होता है। आइये इसके बारे में जानने का प्रयास करते है।  

आसान शब्दों में कहा जाये तो programming एक ऐसी process होती है जिसमें एक programmer CPU (Central Processing Unit) को कोई task perform करने के instructions देता है। ये task कुछ भी हो सकता है जैसे की दो numbers को add करना आदि।

Programming कुछ components से मिलकर एक समग्र रूप धारण करती है।

1. Programming Language
2. Compiler / Interpreter 
3. Programmer 
इन components के बारे में निचे detail से बताया जा रहा है। 


Programming Language 
Humans जब भी interact करते है तो किसी language के माध्यम से करते है। एक CPU को भी आप language के माध्यम से ही instructions देते है। ऐसी language जो CPU को instructions देने के लिए programmers यूज़ करते है programming language कहलाती है।

CPU सिर्फ binary language (0 & 1) को ही समझता है। लेकिन किसी भी human के लिए binary language में programming करना असंभव है। इसलिए simple English sentences को यूज़ करते हुए programming languages develop की गयी।

ऐसा करने से programming करना बहुत अधिक आसान हो गया। अब जैसा की आप देख सकते है हर कोई basic information प्राप्त करके programming करना सिख सकता है। लेकिन पहले ऐसा संभव नहीं था।  
 What is Programming?

Types Of Programming Languages
अब तक 256 से अधिक programming languages develop की जा चुकी है। हर language को एक different purpose के लिए develop किया गया है। Purpose के according programming languages को mainly 5 categories में divide किया गया है। 

1. Machine Language / Machine Code  - ये वो code होता है जिसे CPU directly execute करता है। Machine code hexadecimal में होता है। ये hardware के बहुत ही करीब होता है। इसे low level language भी कहा जाता है।     
2. Assembly Language - Machine code को और भी अधिक readable बनाने के लिए assembly language create की गयी। इसमें labels को add किया गया है जिससे tasks perform करना और भी आसान हो जाता है। ये भी एक low level language होती है।       
3. High-Level Language - High level languages hardware से independent होती है जैसे की c, c++, java आदि। इन languages का code directly machine code में convert नहीं होता है।     
4. System Language - इस प्रकार की languages computer को manage करने के लिए यूज़ की जाती है। इन्हें low level tasks perform करने के लिए यूज़ किया जाता है। जैसे की memory management, process management आदि।      
5. Scripting Languages -  इस प्रकार की languages system और दूसरी applications के बीच में bridge की तरह काम करती है। जैसे की PHP आदि।    

Some Common Programming Languages
यँहा पर कुछ programming languages की list दी जा रही है। इनमें से कुछ के बारे में आप सुन चुके होंगे। 
1. C Language
2. C++
3. Java
4. C# 
5. PHP
6. HTML
7. JavaScript
8. Pascal
9. Visual Basic
10. FORTRAN 

What is Programming?

Compiler / Interpreter 
जैसा की मैने आपको बताया computer सिर्फ binary language समझता है और humans का binary में programming करना बहुत मुश्किल है। इसलिए एक ऐसा program develop किया गया जो programmers से simple English statements के रूप में instructions ले सके और उसे binary में convert करके CPU को pass कर दे। इस program को compiler कहते है।  

Compiler एक programmer और CPU के बीच bridge की तरह काम करता है। इसे आप translator भी कह सकते है। एक compiler बहुत से tasks perform करता है इनके बारे में निचे दिया जा रहा है। 
Compiler syntax error check करता है। यदि कोई syntax error होती है तो इसके बारे में programmer को notify किया जाता है।
Compiler source code को machine language में convert करता है।    
Compiler एक machine executable file generate करता है।  
 आइये अब interpreter के बारे में जानने का प्रयास करते है। 

कुछ languages compiler की जगह interpreter भी use करती है। Compiler और interpreter दोनों एक ही काम करते है। फर्क सिर्फ इतना होता है की compiler पुरे source code को एक साथ compile करके machine code generate करता है जिसे एक बारे में completely execute किया जा सकता है। जबकि एक interpreter एक एक instructions को machine code में convert करता है और साथ की साथ execute करता जाता है। अपनी need के according languages compiler और interpreter को यूज़ करती है।         

  
Programmer 
शुरूआती दिनों में लोग interest की वजह से programming करते थे लेकिन अब इसने एक job का रूप धारण कर लिया है। Programmer वह person होता है जिसे की किसी programming language का ज्ञान होता है। एक programmer अपने ज्ञान को यूज़ करते हुए programming language की मदद से real world problems को solve करता है।  


C in Hindi

Introduction to C language 
C एक बहुत ही popular programming language है। इसे Middle level language माना जाता है। आज भी C की popularity में कोई कमी नहीं आई है। किसी भी दूसरी language को सिखने से पहले C को सीखना बहुत जरुरी है। क्योंकि C के बाद जितनी भी programming languages आई उन्होंने किसी न किसी तरह C के features को adopt किया है। आइये C के बारे में detail से जानने का प्रयास करते है।


History    
C एक structured general purpose programming language है। इसे UNIX operating system को दुबारा लिखने के लिए develop किया गया था। इससे पहले UNIX operating system को B language में लिखा गया था। पूरा UNIX operating system और उस पर run होने वाले ज्यादातर programs C language में ही लिखे गए है।

B language Ken Thompson के द्वारा 1970 में Bell laboratories में लिखी गई थी। B एक type less language थी। C language को Dennis Ritchie ने 1972 में Bell laboratories में ही develop किया था। C language बहुत से data types provide करती है।

C language के syntax को बहुत सी popular languages ने follow किया है। C का improved version C# है। C के features को Java और C++ जैसी languages ने follow किया है।


C in Hindi

C in Hindi Characteristics & Features 
C एक modular language है। C में एक बड़े program को छोटे छोटे modules (functions)  के रूप में divide किया जाता है। ऐसा करने से programs को manage और debug करने में आसानी रहती है। 
C एक flexible language है। C 32 reserved keywords provide करती है। ये keywords programmer को language पर control provide करते है। इन keywords की मदद से program के structure को भी modify किया जा सकता है।  
C language Assembly language के बाद सबसे fast language मानी जाती है। इसे कई बार low level language भी कहा जाता है। C language hardware के बहुत करीब होती हैं। इसलिए ये दूसरी programming languages से fast होती है। 
C एक case sensitive language है। 
C High level और low level दोनों तरह के programs create करने में सक्षम है। 
C एक extendable language है। C programs में libraries के द्वारा आप कभी भी नए features add कर सकते है। 

Limitations

C language में run time type checking नहीं होती है। C language run time में किसी भी variable के data type को identify करने में असमर्थ होती है। उदाहरण के लिए float data type में integer value डाली जा सकती है।  
C language में object oriented concepts नहीं होते है। 
C language में constructors और destructors का concept भी नहीं होता है।        
C language में namespaces का concept नहीं होता है। इसलिए different programmers के code को एक ही project में यूज़ करना बहुत मुश्किल होता है।  
C language code की re-usability को support नहीं करती है।    
C language में exceptions को run time में handle नहीं किया जा सकता है। 

Applications

ज्यादतर operating systems C language में ही बनाए जाते है। जैसे की UNIX, Windows आदि।  
किसी भी नयी programming language को compile करने के लिए compilers की आवश्यकता होती है। C language में compilers भी create किये जाते है। उदाहरण के लिए java का compiler javac भी C language में ही बनाया गया है। 
C language में कई प्रकार के assemblers भी create किये जाते है। Assembler basic instructions को bits में convert करते है। 
C language में text editors भी create किये जा सकते है। जैसे की gedit, आदि। 
C language में network drivers भी create किये जाते है। 
C language में database systems भी लिखे जाते है। जैसे की oracle database C में ही लिखा गया है। 

A simple program in C language
#include
int main()
{
   printf("Hello Reader ");
   return 0;
}
ऊपर दिया गया program निचे दिया गया output generate करता है। 

Hello Reader


C Compiler Installation

Introduction to Dev C++ Compiler
जैसा की आपको पता है एक compiler programmer द्वारा किसी programming language में लिखे गए code को machine executable form में convert करता है। C language के लिए अलग अलग platforms के अनुसार बहुत से compilers available है।

Dev C++ एक (Integrated Development Environment) है। इसके माध्यम से c या c++ में programming करना बहुत ही आसान हो जाता है। ये IDE आपको graphical user interface provide करती है और साथ ही आपको automatic code completion, syntax highlighting और एक साथ कई files के साथ work करने की ability provide करती है। 

Best Hindi Tutorials के readers के लिए इस ही IDE को recommend किया जाता है। हालाँकि अभी भी कई university और colleges में Turbo C compiler को use किया जा रहा है लेकिन ये IDE आपको better development environment provide करता है इसलिए आपको इसे ही use करना चाहिए।

C Compiler Installation

Installing Dev C++ compiler 
इस IDE को आप Dev-C++ Download Page से download कर सकते है| इसे install करना निचे steps के द्वारा समझाया जा रहा है। 

1.  जैसे ही आप setup.exe file को download करके उस पर click करेंगे तो आपको निचे दी गयी window show होगी।


ऊपर दी गयी window में आपको अपनी preferred language select करनी है। Default language English है। यँहा पर आप OK click करेंगे।  

2. इसके बाद आपको निचे दी गयी screen show होगी। 


      
ये licence agreement की window है। यँहा पर आप I Agree button पर click करेंगे। 

3. इसके बाद आपको निचे दी गयी window show होगी।  



इस window में आपको type of installation select करना होता है। ये window आपको 3 (Full, Typical और Custom) options provide करती है। आप अपनी need के according कोई भी installation type select कर सकते है। Installation type select करने के बाद आप next पर click करेंगे।   

4. इसके बाद आपको निचे दी गयी window show होगी। 




इस window में आप वह location बताते है जँहा पर आप Dev C++ को install करना चाहते है। By default ये c drive में install होगा। Location set करने के बाद आप Install button पर click करेंगे। 

5. इसके बाद आपको निचे दी गयी window show होगी। 


      
ये installation progress window है। इस window में आपको कोई action नहीं लेना है।  

6. ऊपर दी गयी progress window के complete होते ही आपको निचे दी गयी window show होगी। 



ये completion window है यँहा पर आपको Finish button पर click करना होगा। 

7. इसके बाद आपको निचे दी गयी window show होगी। 


  
ये first time configuration window होती है। इस window में आप language और Dev c++ की theme select करते है। इसके बाद आप next button पर click करेंगे। 

8. इसके बाद आपको निचे दी गयी window show होगी। 



इस window में आपसे पूछा जा जायेगा की क्या आप Dev C++ को Header files के साथ use करना चाहते है। यँहा पर आप Yes, I want to use this feature option को select करेंगे। 

9. इसके बाद आपको निचे दी गयी window show होगी। 


   
इस window में आपसे cache create करने के लिए पूछा जाएगा। इस window में आप Yes, create the cache option select करेंगे। 

10. इसके बाद आपको निचे गयी window show होगी।  




 ये window आपको बताती है की Dev C++ को successfully configure कर लिया गया है। इस window में आप OK button पर click करेंगे। 

11. इसके बाद आपको निचे दी गयी window show होगी। 


   
ये Dev C++ की window है। अब आपके PC में Dev C++ successfully install हो चूका है। 
 C Compiler Installation

Working with Dev C++ compiler 
यँहा पर मैं आपको Dev C++ compiler में एक simple program बनाकर उसे execute करके बता रहा हूँ जिससे आप इसके basic uses को समझ पाएंगे।

सबसे पहले आप Dev C++ compiler को open करेंगे। इसके बाद आप एक नयी file create करेंगे। नयी file create करने के लिए आप Menu bar में File option पर click करेंगे और इसके बाद new menu में जाकर source file पर click करेंगे। जैसा की निचे दी गयी window में show किया गया है।




जैसे ही आप Source File option पर click करेंगे right side में editor open हो जायेगा। जैसा की निचे दी गयी image में show हो रहा है। 




यँहा पर आप अपना code लिखते है। जैसे की निचे गयी image में show हो रहा है।




Code लिखने के बाद आप अपने program को compile करते है। इसके लिए आप Execute menu में जाकर compile option पर click करते है। जैसा की निचे दी गयी image में दिखाया गया है।





Compile option पर click करते ही आपको एक window show होगी जिसमे आपको इस program को save करने के लिए कहा जाएगा। जैसे की निचे दी गयी image में show हो रहा है।




जैसे ही आप इस program को किसी name के साथ save करते है program compile होना शुरू हो जाएगा। यदि program में कोई error है तो वह editor के निचे show होगी नहीं तो program successfully compile हो जाएगा और Done का message show करेगा।

Program के compile होने के बाद आप इसे run करेंगे। Program को run करने के लिए आप Execute menu में जाकर run option पर click करेंगे। इसके बाद आपका program run हो जाएगा और आपको console window में output show होगा। जैसा की निचे दी गयी image में show हो रहा है।



C Tokens, Identifiers & Keywords

Introduction To C Tokens  
C language का syntax कई दूसरी popular languages के द्वारा follow किया गया है, इसलिए C का syntax समझना आपके लिए बहुत आवश्यक है। C program tokens से मिलकर बना होता है। जैसे की निचे दिए गए program को देखिये। ये program tokens का सही क्रम में यूज़ है और इसके सिवा कुछ भी नहीं है। यदि आप C के सभी tokens को यूज़ करना सिख ले तो आप C के expert बन जायेंगे।   

#include <stdio.h>
int main()
{
   printf("Software programming is cool");
   print("%d",5);
}


Tokens 6 types के होते है। इनकी list निचे दी जा रही है।   
Identifier  
Keyword 
Constant
Variable
String 
Operator   
यदि इस list के according आप देखें तो ऊपर दिए गए program में int, main, printf ,+ और Software programming is cool आदि सब कुछ tokens ही है। इनमें से सभी tokens के बारे में अभी ना बताकर में यँहा पर आपको सिर्फ Identifiers और keywords के बारे में बताने जा रहा हूँ। बाकि tokens के बारे में आपको आगे की tutorials में बताया जायेगा।    

मै समझता हूँ की आपको tokens क्या है ये  concept clear हो गया होगा। अगर ऐसा है तो आइये कुछ tokens के बारे में detail से जानने का प्रयास करते है। लेकिन इससे से पहले में आपको C language के character set के बारे में बताने जा रहा हूँ।   

C Character Set
C language में आप program characters की sequence में लिखते है। जैसे की #include <stdio.h> ये statement characters से ही बना है। यँहा पर i,n,c,l और h आदि characters है। लेकिन ऐसा नहीं है की आप C program में कोई भी character यूज़ कर सकते है। इसके लिए C के द्वारा character set define किया गया है। जो characters इस set में है केवल वे ही एक C program में यूज़ किये जा सकते है।  


Alphabet
Alphabet characters में आप A से Z upper case और a से z lower case characters को यूज़ कर सकते है।         


Digits
Digits में आप 0 से लेकर 9 तक की कोई भी digits यूज़ कर सकते है। 


Special characters
Special characters में आप निचे दिए गए characters को यूज़ कर सकते है। 

! @ # $ % ^ & * ( ) _ - + = |  { } [ ] < > ", ? /


इन alphabet digits और special characters के अलावा आप कोई और character C language में यूज़ नहीं कर सकते है। इसे ही C का character set कहते है।

C Tokens, Identifiers & Keywords

Identifiers 
Identifiers वो नाम होते है जो आप variables, constants और functions आदि को देते है। इन नामों को देने के लिए भी कुछ नियम है जो आप follow करते है। यदि आप इन नियमों को follow नहीं करते है तो program में error आ जाती है। ये नियम आगे बताये जा रहे है। 
1. Identifiers में @, %, - characters allowed नहीं है। 
2. C case sensitive language है। इसलिए age और Age 2 different identifiers है। 
3. Identifiers में आप operators नहीं यूज़ कर सकते है। 
4. Identifier की शुरआत आप या तो character या underscore से कर सकते है। Digits से identifier को start नहीं किया जा सकता है। 
5. आप A से लेकर Z तक के small और capital दोनों तरह के letters या फिर underscore में से किसी के साथ भी identifiers को start कर सकते है। इसके बाद आप digits और दूसरे characters या underscore यूज़ कर सकते है। 

Example
_age              // Valid
2name           // Invalid
Emp-name     // Invalid
Emp_2_age    // Valid      


Keywords 
Keywords को predefined tokens या reserved words भी कहते है। C language 32 keywords provide करती है। हर keyword का एक खास मतलब होता है। हर keyword को एक particular task perform करने के लिए define किये गया है। इन keywords की list निचे दी जा रही है। इन keywords को यँहा पर explain नहीं किया जा रहा है। इनके बारे में आप धीरे धीरे आगे आने वाली tutorials में जानेंगे।

auto  break  case char  const continue 
default do  double else enum extern 
float for goto if int long 
register return short signed sizeof static 
struct switch typedef union unsigned void 
volatile while  
       

C Data Types 

Introduction To C Data Types 
जब भी आप कोई variable create करते है तो उससे पहले compiler को बताते है की आप किस तरह का data उस variable में store करेंगे। इससे compiler उतनी ही memory उस variable को computer की memory में से allot कर देता है। जैसे की निचे दिए गए statement को देखिये।     

int age;

ऊपर दिया गया statement compiler को बताता है की आप age variable में एक whole number store करने वाले है। किसी भी whole number को store करने के लिए जितनी memory की आवश्यकता होती है compiler उतनी इस variable को allot कर देता है। जो की 2 bytes है।  

अब आप सोच सकते है की यदि किसी programming language में data types ना हो तो  कितनी memory waste हो सकती है। जँहा पर 2 bytes की requirement हो वँहा 20 bytes आप waste कर सकते है।
    
इस प्रकार जितनी भी तरह का data आप store कर सकते है उसके लिए पहले से ही maximum memory limit define की गई है। आइये अब C के अलग अलग तरह के data types के बारे में जानने का प्रयास करते है। 
C Data Types 

Different C data types
C language 3 तरह के data types को support करती है। इन्हें primitive types भी कहते है। 

Integer
o int
o short int
o long int
o singed int 
o unsigned int
Floating point
o float
o double
Character
o char


Integer
Integer types किसी भी whole number (बिना दशमलव के) को store करने के लिए यूज़ किये जाते है। Integer types 5 प्रकार के होते है। हालांकि ये सभी whole number को store करते है। लेकिन memory size और range के base पर इन्हें categories किया गया है। 

 Data type Size (Bytes) Range
int 2 -32768 से  32767 
short int  1 -128 से 127
long int 4 -2,147,483,648 से 2,147,483,647 
signed int 2 -32768 - 32767
unsigned int 2 0 - 65535
   

Example 

long int population = 200000000;


Floating point 
Floating point data types को दशमलव संख्याओं को store करने के लिए define किया गया है। Floating point data types 2 तरह के होते है। इनको size और range के base पर categories किया गया है। Float type में आप दशमलव के बाद  7 digits तक store कर सकते है। Double type में दशमलव के बाद 17 digits तक store की जा सकती है।    

Data types Size (Bytes) Range 
Float  4 3.4E-38 से 3.4E+38 
Double 8 1.7E-308 से 1.7E+308 
    

Example 

double balance=810.12354984;


Character 
Character type को एक character store करने के लिए यूज़ किया जाता है। इनको 2 categories में divide किया गया है। 

Data type Size (Bytes) Range 
char  1 -128 से 127  
unsigned char  1 0 से 255  


Example 

char bestlanguage = "C";

C Data Types 

Void type
Void type को उन situations में यूज़ किया जाता है जब आपको value के बारे में कोई जानकारी ना हो। इसे functions के साथ ज्यादातर यूज़ किया जाता है। C में void type को आप इन situations में यूज़ कर सकते है।
1. यदि आपका function कोई value return नहीं करता है तो आप उसका return type void define करते है। उदाहरण के लिए आप इस प्रकार function define कर सकते है। void myFunction();।   
2. यदि आप function में कोई parameters नहीं ले रहे है तो आप वँहा पर void define कर सकते है। Void type से पता चलता है की इस function में कोई argument नहीं लिया जाता है। उदाहरण के लिए आप इस प्रकार void को parameter के रूप में pass कर सकते है। int myFunction(void);।   
3. यदि आप sure नहीं है की pointer variable किस type के variable को point करेगा तो आप उसका type void declare कर सकते है। इसके बाद आप void pointer से किसी भी variable को point कर सकते है। 

C variables & constants

Introduction to C variables 
यदि आप computer की memory में data को store करना चाहते है तो इसके लिए पहले आपको उस memory location के लिए नाम देना होता है। Variable memory में किसी location का नाम होता है। 



मान लीजिए ये आपका computer memory space है। आप इसमें कुछ data store करवाना चाहते है। जैसे की किसी व्यक्ति की उम्र आदि। Computer की memory में data store करवाने से पहले आप बताते ही की आप किस तरह का data store करेंगे। ये आप data types के द्वारा define करते है। उसी के according आपको memory में space मिलता है। यानि की यदि आपने int define किया है तो 2 bytes आपको memory में compiler allot करेगा। 

इसके साथ ही आपको उस memory location का नाम भी define करना होता है। ताकि आप जब भी चाहे उस memory location में store की गई value को इस नाम के द्वारा access कर सके। इस नाम को ही variable कहते है। Variables की values changeable होती है। आप एक value को हटाकर दूसरी value डाल सकते है। ऐसा आप manually खुद भी कर सकते है या फिर dynamically (program execution के दौरान) भी कर सकते है।            

Creating variables in C
Variables create करने के लिए सबसे पहले आप data type define करते है। इसके बाद आप एक unique name define करते है। इसका structure इस प्रकार होता है। 

dataType variableName;                   // without assigning value 
dataType variableName = value;       // with value assignment 
   
उदाहरण के लिए निचे दिए गए statement को देखिये।

int Age = 25;

इस statement के द्वारा एक integer variable create किया गया है, जिसका नाम Age है और इस variable को 25 value assign की गई है। आइये अब समझते है की compiler इस statement को किस प्रकार interpret करता है।

जब compiler सबसे पहले int को interpret करता है तो वह computer की memory में से 2 bytes की memory allot करता है। इसके बाद जब compiler Age को interpret करता है तो वह उस 2 bytes की memory को age नाम दे देता है। इसके बाद जब compiler = 25 को interpret करता है तो 25 को इस memory location store कर देता है।




अब जब भी आप इस value को access करना चाहते है Age के द्वारा इसे access कर सकते है।

C variables & constants

Scope of variables 
कोई variable program में कँहा तक काम कर सकता है। ये उसका scope होता है। Scope के according variables को 2 categories में divide किया गया है। 
Local variables 
Local variables वो variables होते है जो program के किसी छोटे block में define किये जाते है जैसे की function या control statement blocks या कोई और block ({  }) भी हो सकता है। इस तरह के variables का यूज़ सिर्फ इस block तक ही limited रहता है। जैसे की यदि आपने किसी function में कोई variable create किया है तो आप उस variable को उस function के बाहर access नहीं कर सकते है। उदाहरण के लिए निचे दिए गए program को देखिये। 

#include  

void myFunction();

int main()
{
     int num=6;
     myFunction();
     printf("Num in main() : %d", num); 

     return 0;

void myFunction()
{
         int num= 5; 
         printf("Num in myFunction : %d ",num);
}
  
ऊपर दिया गया program निचे दिया गया output generate करता है।

Num in myFuntion() : 5
Num in main() : 6 


Global variables
Global variables वो variables होते है जिनका scope पुरे program में होता है। इन variables को आप पुरे program में कंही भी access कर सकते है। इसका उदाहरण निचे दिया जा रहा है।    

#include
int num=5; 

void myFunction();

int main()
{
    myFunction();
    printf("Num in main() : %d",num); 
    return 0;
}

void myFunction()
{
    printf("Num in myFunction : %d ",num); 
}


ऊपर दिया गया program निचे दिया गया output generate करता है।


Num in myFunction() : 5
Num in main() : 5 

C variables & constants

Constants in C
Constants वो variables होते है जिनकी value change नहीं होती है। जब भी आप कोई constant declare करते है तो program के execution के दौरान उसकी value fixed रहती है। यदि इसकी value change करने की कोशिश की जाती है तो program में error आ जाती है। इन्हें literals भी कहा जाता है।   

Constants को आप 2 तरह से declare कर सकते है। 
Using #define 
Using Const keyword 

Using #define 
#define एक pre-processor है इसे यूज़ करके आप constant declare कर सकते है। इसका उदाहरण निचे दिया जा रहा है। 

# include <stdio.h>

#define result 10

int main()
{
    int a=5, b=6;
    
    result = a + b; /* WRONG, (ERROR) Value of constant result variable can not be changed. */ 

    printf("%d", result);
    
    return 0;
}
   
ऊपर दिया गया program निचे दिया गया output generate करता है।

error : lvalue required as left operand of assignment 
           result = a + b;
                     ^

Using const keyword
Const keyword के द्वारा भी आप constants declare कर सकते है। इसका उदाहरण निचे दिया जा रहा है।

#include <stdio.h>
int main()
{

const int a=5;

const int b=6;

int c;

c = a+b;
printf("Result is : %d",a);
return 0;

}
ऊपर दिया गया program निचे दिया गया output generate करता है। 


First C program 

C program development life cycle  
C में कोई भी program create करने के 4 steps हो सकते है। ये steps एक particular order में होते है और इनका अपना कुछ महत्व होता है। आइये पहला C program create करने से पहले इस process को समझने का प्रयास करते है।


1. सबसे पहले आप एक program को लिखते है। इसे program development life cycle का editing part भी कहते है। ये program human readable format में होता है। 
2. इसके बाद आप program को compile करते हैं। ये development life cycle का second step होता है। इस part में सभी bugs को remove करके program को binary format में convert किया जाता है ताकि computer इसे process कर सके। 
3. इसके बाद linking process आती है। इस process में program को necessary libraries के साथ link किया जाता है। जैसे की आपको पता है की C का basic program भी बिना library को include किये नहीं execute हो सकता है। Libraries C program को execute होने के लिए environment provide करती है।
4. इसके बाद executable file produce कर दी जाती है। जिसे आप जितनी बार चाहे execute कर सकते है। Editing process का output .c source file होती है। Compiling process का input source file होती है और output .obj files होती है। Linking process का input .obj file होती है और output .exe file होती है। 


Structure of A C program 




Your first C program 
#include
int main() 
{
    printf("Hello Readers!");
   
    return 0;
}

ऊपर दिया गया program निचे दिया output generate करता है।

Hello Readers! 
सबसे पहली line में header file को program में include किया गया है। ये एक standard input/output header file होती है जो program में input और output को handle करती है। इन्हें pre processor directives भी कहते है।

इसके बाद main() method को start किया गया है। Main method से ही program का execution start होता है। इसी method में सभी instructions लिखे जाते है। Main method का start और end curly brackets के द्वारा show किया जाता है। इन curly brackets के भीतर के सभी instructions execute किये जाते है।

Main function को int type के साथ define किया गया है। ये एक standard है। Int type के बारे में आप आगे की tutorials में जानेंगें। Main function को एक integer value return करनी होती है। यदि आप program में main() function से कोई value return नहीं करते है तो program के आखिर में return 0 statement define करते है।

First C program 

Commenting 
Comments आपके program में वो text होता है जिसे compiler ignore कर देता है। ये text बाकी statements की तरह execute नहीं होता है। Comments program में किसी statement को या फिर program को define करने के लिए यूज़ किये जाते है। 

C language में commenting की general form नीचे दी जा रही है। 

/* your comment text here */ 


आइये अब comments के यूज़ को एक उदाहरण से समझने का प्रयास करते है। 

/* This is a c program which shows Hello World message on execution */ 
#include <stdio.h>
int main() /* Main function starts from here*/
{
   printf("Hello World!"); /* This statement will print Hello World message */
  
   return 0;



जैसा की आप ऊपर दिए गए program में देख सकते है, comments के माध्यम से program और दूसरे statements के बारे में explanation दी गयी है। ये program निचे दिया गया output generate करता है।



C Flow Chart

Introduction to Flow Chart 
Flow chart किसी process का graphical representation होता है। C language के सन्दर्भ में यह program के execution flow का graphical presentation होता है। 

प्रायः जब भी आप कोई program लिखते है तो बिना किसी flow chart के लिखते है। किसी दूसरे व्यक्ति द्वारा इसे समझना असंभव होता है। Flow chart के माध्यम से आप अपने program को graphically represent करते है। ऐसा करने से कोई भी आपके program के logic को आपके बिना भी समझ सकता है।        

जब आप किसी company के लिए programmer के रूप में काम करते है तो coding से पहले flow chart बनाना अनिवार्य होता है। ऐसा इसलिए किया जाता है क्योंकि companies में अक्सर programmers बदलते रहते है। आपके द्वारा बनाये गए किसी project को कोई दूसरा व्यक्ति आसानी से समझ सके और उस पर काम कर सके इसलिए आपको flow charts create करना अनिवार्य होता है।    

Flow chart create करने का एक महत्वपूर्ण कारण ये है की इससे आपको program की गहरी समझ प्राप्त होती है। आप coding से पहले ही उसमे आने वाली errors और result को देख पाते है। यदि flow chart completly prepare कर लिया गया है तो coding सिर्फ एक simple process रह जाती है जिसमे programmer को flow chart के अनुसार code लिखना होता है।

Flow chart की सबसे बड़ी विशेषता ये होती है की flow chart किसी विशेष programming language के लिए नहीं होता है। Flow chart को देखकर आप किसी भी programming language में process को implement कर सकते है।    

बड़ी companies project develop करने से पहले flow chart पर बहुत सा समय लगाती है। Flow chart आपके program का blue print होता है। ये मकान बनाने से पहले उसका नक्शा बनाने जैसा होता है। जितना समय coding में लगता है उससे कई अधिक समय flow chart में लगाया जाता है। Algorithms भी flow chart के अनुसार ही बनायीं जाती है।

हर अच्छे programmer को इसे सीखना चाहिए क्योंकि यह एक बहुत ही powerful tool है। आज कल बहुत से modern text editors जैसे की Microsoft word आदि flow chart create करने के लिए built in options provide करते है। आप इन्हें यूज़ करके भी flow charts create


C Flow Chart

Symbols of Flow Chart 
किसी program को graphically समझाने के लिए कुछ symbols का प्रयोग किया जाता है। ये symbols आपको पहले से provide किये गए है। इन्हें flow chart symbols कहा जाता है। हर symbol का एक मतलब होता है जिससे flow chart देखने वाला आसानी से उसे समझ सकता है। निचे आपको table के माध्यम से flow chart symbols के बारे में बताया जा रहा है।

Name Symbol  Explanation 
Begin/End  


  ये oval symbol होता है। इसे terminal symbol भी कहा जाता है। इसे flow chart को start और end करने के लिए यूज़ किया जाता है। किसी भी processing से पहले program का start और end point define करना अत्यंत आवश्यक है।    
Processing  
  जब भी आप कोई execution करते है या फिर कोई ऐसा statement जिसमे processing हो रही है उसे processing symbol से दर्शाया जाता है।  
Input/Output  
  Program के input और output को  दर्शाने के लिए आप parallelogram का इस्तेमाल करते है।  
Decision  
  Program के decision making statements को दर्शाने के लिए diamond symbol यूज़ किया जाता है।  
Arrow (Flow)  
  Program का flow दिखाने के लिए arrow symbol यूज़ किया जाता है। इसे हर symbol के बाद flow दिखाने के लिए यूज़ किया जाता है।  



C Flow Chart

Example of Flow Chart
मान लीजिये आप एक addition का program बनाना चाहते है जो user से 2 numbers read करता है और उन्हें add करके result display करता है।इसके लिए आप flow chart इस प्रकार बनाएंगे। 


ऊपर दिए गए flow chart में सबसे पहले oval symbol से flow chart को start किया गया है। इसके बाद rectangle symbol के द्वारा 3 variables (num1, num2 और Result) declare किये गए है। इसके बाद parallelogram द्वारा num1 और num2 को read करने की process को दर्शाया गया है। इसके बाद वापस rectangle symbol के द्वारा दोनों numbers के addition और Result variable को assign करने की process को दर्शाया गया है। इसके बाद parallelogram द्वारा result को display करने की process को दर्शाया गया है। सबसे आखिर में terminal symbol द्वारा flowchart को stop किया गया है। इस flow chart को देखकर कोई भी व्यक्ति आसानी से addition के program को किसी भी language में बना सकता है।

आइये flow chart का एक और उदाहरण देखते है। मान लीजिये आप एक program बनाना चाहते है जो दो integer numbers को read करता है और जो उनमें से greater हो उसे display करवाता है। इसके लिए flow chart इस प्रकार create किया जायेगा।




ऊपर दिए flow chart में सब कुछ पहले की तरह ही है बस diamond symbol के द्वारा condition के according दोनों numbers में से जो number greater है उसे display किया जा रहा है।            



C operators 

Introduction to C operators 
Variables के अंदर values store करवाकर आप उन variables पर कई प्रकार के operations perform कर सकते है। उदहारण के लिए दो integer variables के अंदर value store करवाकर आप addition का operation perform कर सकते है और उन दोनों variables की values के sum को print करवा सकते है। इसी प्रकार आप और भी अलग अलग operations variables के साथ perform कर सकते है।

Variables के साथ operations perform करने के लिए आपको अलग अलग operators यूज़ करने पड़ते है। इस chapter में आपको ऐसे ही operators के बारे में बताया जा रहा है।

Operations में operators के साथ जो variables यूज़ होते है वो operand कहलाते है। उदाहरण के लिए नीचे दिए गए statement को देखिये।

c = a + b;

उपर दिए गए statement में a और b को operator (+) के साथ यूज़ किया गया है इसलिए ये दोनों variables operands कहलायेंगे।


Operators 2 प्रकार के होते है।

Unary -  इस प्रकार के operators सिर्फ एक ही operand के साथ यूज़ किये जाते है।  
Binary - इस प्रकार के operators के साथ 2 operands यूज़ किये जाते है।  

आइये अब C language में यूज़ होने वाले अलग अलग operators के बारे में जानने का प्रयास करते है।


C operators 

Arithmetic operators 
Arithmetic operators mathematical operations perform करने के लिए यूज़ किये जाते है। जैसे की addition, subtraction, division और multiplication आदि। Arithmetic operators 5 प्रकार के होते है। ये basic mathematical operators होते है। 

 Operators Description 
+ (Addition) ये operator दो variables की values को add करता है।  
- (Subtraction) ये operator एक variable की value में से दूसरे variable की value subtract करता है। 
* (Multiplication) ये operator 2 variables की values को multiply करता है।  
/ (Division) ये operator एक variable की value से दूसरे variable की value को divide करता है। 
% (Modulus) ये operator division के बाद शेष बची हुई value को प्राप्त करने के लिए यूज़ किया जाता है। 



Relational operators 
Relational operators दो variables की values को compare करने के लिए यूज़ किये जाते है। जैसे की आप इन operators का यूज़ करके पता लगा सकते है की क्या किन्ही दो variables की values बराबर है और यदि बराबर नहीं है तो कौनसे variable की value बड़ी है और कौनसे variable की value छोटी है। 

इस तरह के operators को conditional statements (if,if-else,switch,for,while आदि) के साथ यूज़ किया जाता है। ये operators condition check करने के लिए यूज़ किये जाते है। Condition true होने पर value true हो जाती है और condition false होने पर value false हो जाती है।

C language में यूज़ होने वाले सभी relational operators के बारे में नीचे दिया जा रहा है।  

Operators  Descriptions 
== (Equal To) ये operator check करता है की क्या दोनों variables की values equal है।  
!= (Not Equal To) ये operator check करता है की क्या दोनों variables की values non equal है।  
<  (Lesser Than) ये operator check करता है की क्या left operand की value right operand से छोटी है।  
> (Greater Than) ये operator check करता है की क्या left operand की value right operand से बड़ी है।   
<=  (Lesser than equal) ये operator check करता है की क्या left operand की value right operand से छोटी या उसके समान है।  
>=  (Greater than equal) ये operator check करता है की क्या left operand की value right operand से बड़ी या उसके समान है।  
 C operators 

Assignment operators
Assignment operators variables की values को एक दूसरे को assign करने के लिए यूज़ किये जाते है। C language में यूज़ होने वाले विभिन्न assignment operators के बारे में नीचे दिया जा रहा है।

Operators Description 
ये operator right side के operand की value को left side के operand को assign करता है।   
+= ये operator left side के operand में right side के operand की value को add करके result left side वाले operand को assign करता है। इसे आप इस प्रकार भी लिख सकते है। a = a+b;   
-= ये operator left side के operand की value में से right side के operand की value को subtract करके result left side के variable में store करवाता है। इसे आप इस प्रकार भी लिख सकते है। a=a-b;   
*= ये operator left side के operand की value को right side के operand की value से multiply करके result को left side के operand में store करता है।   
/= ये operator left operand की value को right operand की value से divide करके result को left side के operand में store करता है।   
%= ये operator left side के operand की value को right side के operand की value से divide करके शेष बचे हुए result को left side के operand में store करता है।   

Increment/Decrements operators 
किसी भी variable की value को एक number से instantly increase या decrease करने के लिए आप increment/ decrements operators यूज़ कर सकते है। इनके बारे में नीचे दिया जा रहा है। 
   
 Operators Description  
 ++ (increment) ये एक unary operator होता है। ये operand की value को एक number से increase करता है। जब यह operand के पहले लगता है तो value पहले increment होती है और बाद में यूज़ की जाती है। जब ये operator operand के बाद लगता है तो operand की value यूज़ होने के बाद increment होती है।  
 --(decrements) ये भी एक unary operator होता है। ये operand की value को एक number से decrease करता है। जब इसे operand से पहले यूज़ किया जाता है तो value यूज़ होने से पहले decrease होती है। जब इसे variable के बाद यूज़ किया जाता है तो value पहले यूज़ होती है और बाद में decrease होती है।  



Conditional (?:) operator
Conditional operator को ternary operator भी कहा जाता है। ये if-else statement की short form होती है। इसका general structure इस प्रकार होता है।

contion ? stmnt1 : stmnt 2;
   
यदि condition true हो तो statement one return होगा नही तो statement 2 return होगा। इसका उदाहरण निचे दिया जा रहा है।

5>3 ? true : false;

Special C operators
Operators  Description 
sizeof (var_name ) ये operator memory में variables की size return करता है।   
& ये operator variables का memory address return करता है।   
* ये operator variable का pointer return करता है।   


 C Decision Making 

Introduction to C decision making
आप अपने program में कौनसे statements को execute करना चाहते है और कौनसे statements को skip करना चाहते है ये आप खुद decide कर सकते है। इसे decision making कहते है। ज्यादातर decision making किसी condition के base पर की जाती है।

एक particular condition आने पर आप मनचाहे statements को execute कर सकते है। इसके लिए आप कुछ built in statements को यूज़ करते है। क्योंकि ये statements conditions के साथ काम करते है इसलिए इन्हें conditional statement भी कहा जाता है। और क्योंकि ये statement program में execution को control करते है इसलिए इन्हें control statements भी कहा जाता है।

इन statements के बारे में जानने से पहले आइये decision making को एक उदाहरण से समझने का प्रयास करते है।

मान लीजिये आप किन्हीं दो students में से जिसकी age अधिक हो उसका नाम print करना चाहते है। आप ऐसा किस प्रकार कर सकते है?  नीचे दिए हुए program को देखिये।

#include <stdio.h>
#include <conio.h>
void main()
{
    raviAge = 40;
    ankitAge = 32;





उपर दिए गए उदाहरण में 2 students की age 2 variables में store की गयी है। लेकिन आपको सिर्फ उसी student की age print करनी है जिसकी age सबसे ज्यादा हो। इस situation में आपको decision लेने की आवश्यकता है। ऐसा आप किसी भी decision making statement (If, If-else, Switch) के द्वारा कर सकते है।

आइये इन statements के बारे में detail से जानने का प्रयास करते है।


If statement 
If statement का general syntax निचे दिया जा रहा है। 

if(condition)
{
    // statements 
}
            
If statement curly braces {} के द्वारा एक block define करता है। जब condition true होती है तो इस block में दिए गए statement execute होते है। यदि condition false हो तो इस पुरे block को compiler skip कर देता है। यदि उपर दिए गए example में if statement का यूज़ किया जाये तो program को इस प्रकार लिखा जा सकता है।

#include <stdio.h>
int main()
{
   int raviAge = 40;
   int ankitAge = 32;
  if(raviAge > ankitAge)
  { 
     printf("Ravi is elder.");
  }

  return 0; 
}

उपर दिए गए उदाहरण में conditional operator का यूज़ करते हुए एक condition लगायी गयी है। यदि Ravi की age Ankit से अधिक है तो if statement में दिया गया printf() statement execute होगा है। लेकिन यदि ऐसा नहीं है तो ये statement execute नहीं होगा। ये program निचे दिया गया output generate करेगा।

Ravi is elder. 



C Decision Making 

If-Else statement
If else statement को if statement का ही part माना जाता है। लेकिन इसमें else block और add किया जाता है। Else block में दिए गए statement तब execute होते है जब if की condition false हो जाती है। If-else statement का general syntax नीचे दिया जा रहा है।

if(condition)
{
    //statements
}
else
{
     //statements
}


जैसा की आपको पता है if की condition true होने पर if block में दिए गए statements execute होते है। लेकिन आप ये भी decide कर सकते है की यदि condition false हो तो क्या किया जाना चाहिए। इसके लिए आप else block यूज़ करते है। ये block हमेशा if block के बाद में आता है। इस block में वो statements लिखे जाते है जो की condition false होने पर execute होंगे। यदि ऊपर दिए गए उदाहरण में if else statement का प्रयोग किया जाये तो आप उसे इस प्रकार लिख सकते है।


#include
void main()
{
    int raviAge = 20;
    int ankitAge = 32;
   if(raviAge > ankitAge)
   {
        printf("Ravi is elder");
   }
   else
   {
         printf("Ankit is elder");
   }
}


ऊपर दिया गया program निचे दिया गया output generate करता है।


Ankit is elder. 




else if 
यदि आप if और else के बीच में एक और condition लगाना चाहते है तो ऐसा आप else if block define करके कर सकते है।

if(raviAge > ankitAge)
{
    printf("Ravi is elder");
}
else if(ankitAge > raviAge)
{
    printf("Ankit is elder");
}
else
{
    printf("Both are equal");
}



C Decision Making 

Switch statement 
Switch statement भी if statement की तरह ही होता है लेकिन इसमें condition check करने की बजाय case check किया जाता है। एक particular case आने पर आप जो statements execute करना चाहते है उन्हें आप case के अंदर लिखते है।

Case एक integer variable से match किया जाता है। जो case integer variable से match हो जाता है वही case execute हो जाता है

Switch statement का general syntax नीचे दिया जा रहा है।

int caseNumber = n;
switch(caseNumber)
{
case 1:
             //statements to be executed
             break;
case 2: 
             //statements to be executed 
             break;
default:
              //statements to be executed 
              break;
}
       
जब आप caseNumber variable को किसी integer value के साथ set करते है और इसे switch statement में pass करते है तो ये number जिस caseNumber से match करेगा वही case execute होगा।

जैसे की यदि आपने case number में 2 pass किया है तो second number का case execute होगा और break से पहले के सभी statements execute हो जायेंगे।

यदि कोई case match नहीं करता है तो default case execute होता है। यदि हर case के बाद break statement ना यूज़ किया जाये तो सभी case execute हो जाएंगे। Case को आप alphabets से भी define कर सकते है।

आइये switch statement को एक उदाहरण के माध्यम से समझने का प्रयास करते है।


#include <stdio.h>

void main()
{
int caseNumber;

printf("Enter a number ");
scanf("%d",&caseNumber); 

switch(caseNumber)
{
     case 1: 
                  printf(" First Case executed....");
                  break;
     case 2:
                  printf(" Second Case executed....");
                  break;
     default:
                  printf(" None of the case matched. You can only enter 1 or 2");
                  break;
}
}


उपर दिए गए उदाहरण में user से एक number enter करवाया जा रहा है और उस number के base पर matching case execute करवाया जा रहा है। ये program निचे दिया गया output generate करता है।


Enter a number : 
2
Second case executed.... 



C Looping

Introduction to C language looping 
यदि मै आपसे कहुँ की आपको C language in Hindi 10 बार print करवाना है तो ऐसा आप किस तरह करेंगे? सामान्यतया: ऐसा करने के लिए आप 10 printf() statements लिखेंगे। ये एक बेकार approach है। ऐसा करने से programmer का time और computer की memory waste होती है।

C language में आपको loops provide किये गए है। Loops की मदद से आप एक ही statement को बार बार execute करवा सकते है। हर तरह का loop एक block provide करता है जिसमे वो statements लिखे जाते है जिन्हें आप एक से ज्यादा बार execute करवाना चाहते है।

Loop 3 चीज़ों से मिलकर बना होता है।

Initial variable - ये वो variable होता है जँहा से आप loop को start करते है। ये एक integer variable होता है। इस variable को तब तक increment किया जाता है जब तक की दी गयी condition false ना हो जाये। इस variable को loop की condition में include किया जाता है।        
Condition - ये वो condition होती है जो loop को control करती है। जब तक ये condition true रहती है loop execute होता रहता है। जैसे ही ये condition false होती है loop terminate हो जाता है।    
Increment - आप कितने number से या कैसे initial variable/condition को increment करना चाहते है ये increment part में define किया जाता है।   


C language आपको 3 तरह के loops प्रोवाइड करती है। 
While loop
Do-while loop
For loop  

आइये अब C language में use होने वाले इन loops के बारे में detail से जानने का प्रयास करते है।



C Looping

C while loop
While एक simple loop होता है ये जब तक condition true रहती है तब तक execute होता है। Condition के false होने पर ये loop terminate हो जाता है। While loop का general syntax नीचे दिया जा रहा है। 
  
initial variable declaration;

while(condition)
{
            //statements 
           //increment


Condition यदि पहली बार में ही false हो तो compiler loop में enter ही नहीं होता है। Loop को totally skip कर दिया जाता है। आइये while लूप को एक उदाहरण से समझने का प्रयास करते है।

#include <stdio.h>
void main()
{
   int num=1;
   while(num<=10)
   {
         printf("%d ",num);
         num++;
   }

}

ऊपर दिए गए उदाहरण में पहले num variable को 1 value के साथ set किया गया है। Loop के अंदर condition दी गयी है की जब तक num 10 से काम या 10 के बराबर ना हो loop execute होता रहे। Loop के अंदर हर iteration में num variable की value print की जा रही है। इसके बाद num variable को increment किया जा रहा है। ये program निचे दिया गया output generate करता है।

1  2  3  4  5  6  7  8  9  10 


यदि initial variable को increment ना किया जाये तो condition कभी false नहीं होगी। ऐसे में loop infinite time तक चलता जायेगा।



C Looping 

C Do-while loop
Do while loop भी while loop की तरह ही होता है। लेकिन इस loop में पहले condition check होने के बजाय पहले statements execute होते है उसके बाद में condition check होती है। लेकिन ऐसा सिर्फ first time होता है। Do while loop का general syntax नीचे दिया जा रहा है।


do
{
    //statements;
   //increment;
}while(condition);
  
जैसे ही compiler do section में आता है तो do block के सभी statements execute कर दिए जाते है और initial variable को increment कर दिया जाता है। इसके बाद compiler while condition को check करता है। यदि condition true होती है तो do block को वापस execute किया जाता है। लेकिन यदि condition false हो तो loop terminate करके compiler आगे बढ़ जाता है। 

While की condition चाहे true हो या false do block के statements एक बार जरूर execute होते है। आइये do while loop को एक उदाहरण से समझने का प्रयास करते है। 

#include <stdio.h>
void main()
{
     int num=1;
     
     do 
     {
            printf("%d ",num);
            num++;
     }while(num>2);
    printf("Num is not greater than 2 that is why loop terminated!!");
}
  
उपर दिए गए उदाहरण में हालाँकि condition false है लेकिन फिर भी do block में दिया गया statement एक बार जरूर execute होगा। ये program निचे दिया गया output generate करता है।

1
Num is not greater than 2 that is why loop terminated!! 



C Looping 

C For loop 
C loops में for loop सबसे ज्यादा use किया जाता है। ये loop बहुत ही easy होता है और एक single statement में define हो जाता है। इस loop का general structure नीचे दिया जा रहा है। 

for(initialvariable;condition;increment)
{
       //statements
}
   
For loop के बारे में खास बात ये है की आप एक statement में तीनों elements define किये जाते है। इस loop को नीचे उदाहरण द्वारा समझाया गया है। 

#include <stdio.h>

void main()
{
    int num;
        for(num=1;num<=10;num++)
        {
              printf("%d",num);
        }
}

ऊपर दिया गया program निचे दिया गया output generate करता है।

1  2  3  4  5  6  7  8  9  



C Arrays

Introduction to C arrays 
मान लीजिये आप एक ऐसा प्रोग्राम बना रहे है जो employees का नाम computer में store करता है। अब मान लीजिये आपकी company में 200 employees है। आप इन 200 employees के नाम किस तरह से store करेंगे। यदि आप सोच रहे है की आप 200 variables create करेंगे तो ये एक बहुत ही complex approach होगी। 

इसमें आपको program बनाने में बहुत समय लग जायेगा और program भी हद से ज्यादा बड़ा हो जायेगा। ये आपके time और computer memory space दोनों का wastage है। और साथ ही आप इतने सारे variables के नाम सोच भी नहीं सकते है और यदि सोच भी लेंगे तो program में यूज़ करने के लिए उन्हें याद तो definitely नहीं रख सकते है।  

मेरे पास आपके लिए इससे भी better approach है और उस approach को C language में array कहते है। Array similar type की values का collection होता है। Similar type से यँहा मेरा मतलब similar data type से है जसे की int, float , Char आदि।  

C आपको arrays के द्वारा ऐसी facility provide करती है की आप सिर्फ एक variable create करे और उस variable में 200 employees के नाम (या जो भी information आप store करना चाहते है) store कर ले। आप सोच रहे होंगे की एक variable में इतने सारे नाम कैसे store करेंगे। इसके बारे में मुझे आपको बताने में बहुत ख़ुशी होगी। लेकिन उससे पहले आइये देखते है की array को create कैसे करते है।  

Creating c arrays 
Array एक structured data type होता है। जैसा की आपने C Structures की tutorial में पढ़ा होगा की एक structure में आप कई variables create कर सकते है। यँहा भी situation कुछ कुछ वैसी ही है। जब भी आप एक array create करते है तो आप array का नाम और आप उसमे कितनी values store करने वाले है ये define करते है। 

जैसे की मान लीजिये आप 5 numbers store करना चाहते है तो उसके लिए आप array create कर सकते है। C में arrays create करने का general structure नीचे दिया गया है। 

data_type array-name[size];
    
size से आप define करते है की आप कितनी values store करना चाहते है। इसका उदाहरण नीचे दिया जा रहा है। 

int num[5];

उपर दिए गए उदाहरण में array का नाम num है और उसमे आप कोई भी 5 integer values store कर सकते है।



C Arrays

Creating c arrays 
Array एक structured data type होता है। जैसा की आपने C Structures की tutorial में पढ़ा होगा की एक structure में आप कई variables create कर सकते है। यँहा भी situation कुछ कुछ वैसी ही है। जब भी आप एक array create करते है तो आप array का नाम और आप उसमे कितनी values store करने वाले है ये define करते है। 

जैसे की मान लीजिये आप 5 numbers store करना चाहते है तो उसके लिए आप array create कर सकते है। C में arrays create करने का general structure नीचे दिया गया है। 

data_type array-name[size];
    
size से आप define करते है की आप कितनी values store करना चाहते है। इसका उदाहरण नीचे दिया जा रहा है। 

int num[5];

उपर दिए गए उदाहरण में array का नाम num है और उसमे आप कोई भी 5 integer values store कर सकते है।

Initializing C arrays 
यँहा तक मैने आपको बताया है की array कैसे create करते है। अब आपके सबसे महत्वपूर्ण question पर आते है की आप इस array में 5 values कैसे store करेंगे। तो में आपको बता दूँ की जब भी आप array create करते है तो जितनी उसकी size होती है उतनी ही locations उस array को memory में allocate हो जाती है। और वो locations उतने ही index numbers के नाम से allocate होती है। 

Index numbers हर location का एक unique नाम होता है। जैसे की यँहा ऊपर दिए उदाहरण में 5 index numbers होंगे। आपको एक बात हमेशा याद रखनी चाहिए की array की index हमेशा zero से शुरू होती है।

num[0]
num[1]
num[2]
num[3]
num[4]
          
Array के नाम और index number से आप create की गयी हर location में value store करवा सकते है और बाद में उससे value access भी कर सकते है।  

(Note : Arrays की index हमेशा zero से शुरू होती है। )

जैसे की ऊपर create किये गए array में आप इस प्रकार value insert करवा सकते है। 

num[0] = 50;
num[1] = 100;
num[2] = 150;
num[3] = 200;
num[4] = 250;

आप चाहे तो हर location को अलग से value assign करने की बजाय एक साथ भी सभी values को assign कर सकते है। ऐसा आप इस प्रकार कर सकते है। 

int num[5] = {50,100,150,200,250};

और यदि आप values यूज़र से run time में store करवाना चाहते है तो ऐसा आप इस प्रकार कर सकते है। 

for(int i=0; i<=5; i++)
{
    scanf("%d",&num[i]);
}



C Arrays

Accessing array elements
अब तक मैने आपको arrays create करना और उनमे values store करना बताया है। इस section में में आपको उन values को access करना बताऊंगा। Array elements को access करके आप उन पर वो सभी operations perform कर सकते है जो आप normal variables के साथ perform कर सकते है। उदाहरण के लिए आप 2 array elements को add करवा सकते है। 

जँहा भी array element को access करना चाहते है उस जगह आप array का नाम और index number लिखते है। उदाहरण के लिए निचे लिखे गए statement को देखिये। 

num[2] = num[0] + num [1];
      
ऊपर दिए गए statement में शुरू की 2 values को add करके 3 location पर store करवाया गया है। 

यदि आप किसी एक value को print करवाना चाहते है तो ऐसा आप इस प्रकार कर सकते है।

printf("%d",num[3]);
  
ऊपर दिया गया statement 4th location की value print करेगा जो की यँहा दिए गए example में 200 है।

यदि आप पुरे array को एक साथ print करना चाहते है तो ऐसा आप loop की मदद से कर सकते है। ये ध्यान रखे की loop उतना ही चले जितनी की values आपके array में है। इसका उदाहरण नीचे दिया जा रहा है। 

for(int i=0; i<=5;i++)
{
    printf("%d",num[i]);
}           


Example
#include
#include
int main()
{
     int arr[5];
     int i;
    
     printf("Please enter 5 array elements : ");
     for(i=0;i<=4;i++)
     {
         scanf("%d",arr[i]);
     }
     printf("Array is:");
     for(i=0;i<=4;i++)
     {
          printf("%d ",arr[i]);
     }
     return 0; 


ऊपर दिया गया program निचे दिया गया output generate करता है।

Please enter 5 array elements : 5 4 3 2 1
Array is : 5 4 3 2 1 



C Arrays

Two Dimensional Arrays
एक normal array में data list की form में store किया जाता है जिसमें एक के बाद दूसरा element होता है। यदि आप data tabular form में store करना चाहते है तो इसके लिए आप two dimensional array create कर सकते है। उदाहरण के लिए आप 4 employees की id और उनका phone number store करना चाहते है। इसके लिए table इस प्रकार बनेगी। 

101 7599393
102 9384940
103 9458940
104 9129399
   
इस प्रकार की table आप two dimensional array के द्वारा memory में create कर सकते है।


Creating Two Dimensional Array
C language में two dimensional array create करने का general syntax निचे दिया जा रहा है। 

data-type array_name[row][column];


Two dimensional array को row और column के संदर्भ में define किया जाता है। सबसे पहले आप array का type define करते है। इसके बाद आप array का एक unique नाम define करते है। इसके बाद आप जितनी row create करना चाहते है उतनी संख्या angular bracket में define करेंगे। इसके बाद आप जितने columns create करना चाहते है उतनी संख्या दूसरे angular bracket में define करेंगे।

उदाहरण के लिए ऊपर दी गयी table आप two dimensional array के माध्यम से memory में इस प्रकार create करेंगे।

int myArray[4][2];

ऊपर दिया गया statement memory में एक table (two dimensional array) create करेगा जिसकी 4 rows और 2 columns होंगे।  



C Arrays

Initializing Two Dimensional Array
Two dimensional array को आप किसी variable की तरह initialize कर सकते है। जब आप इस प्रकार initialize करते है तो हर position के लिए separately value assign करते है। उदाहरण के लिए आप पहली row के पहले column में value store करना चाहते है तो ऐसा आप इस प्रकार करेंगे।
    
myArray[1][1]=101;

इसी प्रकार second column में phone number आप इस प्रकार store कर सकते है।

myArray[1][2]=7599393;

यदि आप user से इस array में value input करवाना चाहते है तो इसके लिए आप 2 loops use करेंगे। पहला loop rows को iterate करेगा और इसे row की संख्या तक ही चलाया जायेगा। दूसरा loop columns को iterate करेगा और इसे भी columns की संख्या तक ही चलाया जायेगा। इसका उदाहरण निचे दिया जा रहा है।

for(int i=0;i<4;i++)
{
    for(int j=o;j<2;j++)
    {
          scanf("%d",myArray[i][j]);
    }
}

Accessing Two Dimensional Array
Two dimensional array में से यदि आप किसी single element को access करना चाहते है तो ऐसा आप उसकी position index को access करके कर सकते है। उदाहरण के लिए यदि आप 2nd row के 1st column की value print करना चाहते है तो इसके लिए आप इस प्रकार statement लिखेंगे। 

printf("Element at 2nd row 1st column is :%d",myArray[2][1]);

यदि आप array के सभी elements को एक बार में print करना चाहते है तो इसके लिए आप 2 loops use करते है। इसका उदाहरण निचे दिया जा रहा है।

for(int i=0;i<4;i++)
{
    for(int j=o;j<2;j++)
    {
          printf("%d",myArray[i][j]);
    }
}

Example 
#include <stdio.h>
int main()
{
     int myArray[4][2];
     int i,j;

     printf("Please enter array elements : ")
   for(i=0;i<4;i++)
  {
      for(j=0;j<2;j++)
      {
           scanf("%d",&myArray[i][j]);
      }
  }

  printf("Arrays is : ")
for(i=0;i<4;i++)
{
    for(j=0;j<2;j++)
    {
          printf("%d",myArray[i][j]);
    }
    printf(" ");
}

      


ऊपर दिया program निचे दिया गया output generate करता है। 

Please enter array elements : 1 2 3 4 5 6 7 8
Arrays is : 
1  2
3  4
5  6
7  8




C Strings

Introduction to C strings
C language में strings create करने के लिए कोई data type available नहीं है। C language में string store करने के लिए आप char array create करते है। इसलिए C में character array को string भी कहते है। C language में strings null character () से terminate होती है।   

C में आप strings 2 तरह से declare और initialize कर सकते है। पहले तरीके में आप एक finite number का array create करते है और उसकी हर index में string का एक एक character store करवाते है। इस array की last index में null character () store करवाया जाता है। ऐसा करना necessary होता है। इसका उदाहरण नीचे दिया जा रहा है। 

char myArray[4] = {B,H,T,};

दूसरे तरीके में आप एक undefined char array create करते है और उसे एक string assign कर देते है। Array automatically उतनी ही size का हो जाता है जितनी की string की size है। इस तरह के initialization में null character () automatically add हो जाता है। ये तरीका C में string create करने का बहुत ही आसान तरीका है इसलिए इसे अधिक यूज़ किया जाता है। इसका उदाहरण नीचे दिया जा रहा है। 

char myArray[] = "BHT";


जब आप इस तरीके से string declare करते है तो उसे कभी change नहीं कर सकते है। C language में strings को किस प्रकार use करते है इसका complete उदाहरण नीचे दिया जा रहा है।

#include <stdio.h>
int main()
{

   char myArrayF[4] = {'B','H','T',''};
   char myArrayS[] = "BHT";

   printf("First Way : %s ",myArrayF);

   printf("Second Way : %s", myArrayS);

   return 0;

}  

ऊपर दिया गया program निचे दिया गया output generate करता है।

First Way : BHT
Second Way : BHT 



C Strings

gets() and puts() functions
यदि आप string user से run time पर read करना चाहते है तो इसके लिए आप gets() function यूज़ कर सकते है। इस function को इसी purpose के लिए define किया गया है। इस function में आप उस char array का नाम pass करते है जिसमे आप string store करवाना चाहते है। इस function का general structure नीचे दिया जा रहा है।


gets(char_Array_Name);

जो भी string आप pass करते है वह दिए गए char array में store हो जाती है। अब यदि आप इस array से उस string को print करवाएंगे तो आपको loop का इस्तेमाल करना पड़ेगा। लेकिन आपको ऐसा करने की जरुरत नहीं है। C language आपको किसी char array को एक complete string के रूप में print करने के लिए puts() function provide करवाती है। इस function में वो char array pass किया जाता है जिसे आप complete string के रूप में print करवाना चाहते है। इसका general structure नीचे दिया जा रहा है। 

puts(char_Array_Name);
      
इन दोनों functions का यूज़ complete उदाहरण के साथ नीचे समझाया जा रहा है।

#include <stdio.h>
#include <conio.h>
int main()
{
    char answer[50];
   
    printf("What is BHT?");
    gets(answer);
    
    printf(" Answer is : ");
    puts(answer);

    return 0;

}

ऊपर दिया गया program निचे दिया गया output generate करता है।

What is BHT?
Best Hindi Tutorials                               (input)
Answer is : Best Hindi Tutorials 



C Strings

Other string functions
Strings के साथ operations perform करने के लिए C language आपको और भी functions provide करती है। इन सभी functions के बारे में नीचे दिया जा रहा है। 

strlen()
ये function string की length calculate करने के लिए यूज़ किया जाता है। इस function में आप उस char array का नाम pass करते है जिसमे string को store किया गया है। इसका syntax नीचे दिया जा रहा है। 

strlen(char_Array_Name); //will return the length of string
     
strcpy()
ये function एक array से string को दूसरे array में copy करने के लिए यूज़ किया जाता है। इस function का पहला argument वह char array होता है जिसमे आप string को copy करना चाहते है और दूसरा argument वह char array होता है जिससे आप string को copy करना चाहते है। इस function का syntax नीचे दिया जा रहा है।


strcpy(destinationCharArray, sourceCharArray);


strcat()
ये function दो strings को combine (concatenate) करता है। इस function में दो arguments पास किये जाते है। ये दोनों argument ही character array होते है। जब ये function call होता है तो दोनों strings combine हो जाती है और result दूसरे किसी char array में store किया जाता है।


strcat(charArray1,charArray2);  //combine both strings and store result in first one.



C Functions

Introduction to C functions 
कई बार आपके program में कुछ ऐसे statements हो सकते है जिन्हें आपको बार बार execute करवाने की आवश्यकता होती है। ऐसी situation में इन statements को बार बार लिखना आपके लिए time consuming भी हो सकता है और साथ ही program को lengthy और unreadable भी बनाता है। 

इस situation से बचने के लिए C आपको एक mechanism provide करती है जिसे function कहते है। जिन भी statements को आप program में कई जगह बार बार execute करवाना चाहते है उन्हें एक block में लिखते है और इस block को एक unique नाम देते है। इसके बाद program में जँहा भी इन statements को आप दुबारा execute करवाना चाहते है तो नाम के द्वारा उन statements को call कर लेते है। 

C में 2 तरह के functions होते है। इनके बारे में आप आगे जानेंगे।   

Function का नाम और parameters (इनके बारे में आप आगे पढ़ेंगे) जब आप लिखते है तो उसे function declaration कहा जाता है। और जब आप function में execute होने वाले सभी statements लिखते है तो उसे function definition कहा जाता है। और जब आप पुरे program में कँही भी function को यूज़ करते है तो उसे function call कहा जाता है।  



C Functions

Predefined & user defined functions 
जैसा की मैने आपको पहले बताया C में 2 तरह के functions पाए जाते है। 
Predefined functions 
User defined functions 
आइये इनके बारे में detail से जानने का प्रयास करते है। 

Predefined functions 
Predefined functions वो functions होते है जो C library में पहले से define किये गए है। इन function को पहले से ही declare और define किया गया होता है। बस आपको इन्हें use करने के लिए header files को include करना होता है। उदाहरण के लिए यदि आप अपने program में scanf और printf functions use करना चाहते है तो इसके लिए आप <stdio> header file को अपने program में include करते है। ये दोनों ही predefined functions होते है। Predefined functions के कुछ उदाहरण नीचे दिए जा रहे है। 

scanf 
printf
strcpy
vod *malloc()
int to lower() 


User defined functions 
User defined functions वो function होते है जो programmer खुद create करता है। Programmer अपनी need के according कितने भी functions create कर सकता है। ये functions कैसे create किये जाते है और कैसे use किये जाते है इसके बारे में आगे बताया जायेगा। 

Advantages of functions 

1. Functions create करने से programmer का time और computer की memory बचती है। 
2. एक ही code को आसानी से बार बार use करने से re-usability बढ़ती है।
3. Program functions में divide हो जाता है जिससे उसे आसानी से manage और debug किया जा सकता है। 
4. Program की readability बढ़ती है। 



C Functions

Creating & using functions in C 
C में functions create करना और उन्हें use करना बहुत ही आसान है। इस process के 3 major steps होते है।

Function declaration 
Function definition 
Function call 
इनमें से हर step को आगे detail से explain किया गया है। आइये इनके बारे में जानने का प्रयास करते है।


Function declaration 
इस part में आप function का नाम, उसका return type और parameters define करते है। Function declaration का basic structure नीचे दिया गया है। 


return_type functionName (parameters_list);


return_type  -  आपका function execution complete होने पर किस प्रकार की value return करेगा ये आप return type के द्वारा define करते है। उदाहरण के लिए यदि आप एक addition का program बना रहे है जो 2 whole numbers को add करता है तो आपका return type int होगा। 
functionName  -  ये आपके function का नाम होता है। ये पुरे program में unique होना चाहिए। जब आप function को call करते है तो इस नाम को ही लिखते है। 
Parameters-list  -    ये उन variables की list होती है जो आप function को call करते समय pass करेंगे।  उदाहरण के लिए आप addition का function बना रहे है तो parameters में आप 2 numbers या 2 variables user से pass करवा सकते है। और फिर function के अंदर उन variables को add करके आप result show कर सकते है। इसका उदाहरण आगे दिया जायेगा।         
        
Function declaration का उदाहरण नीचे दिया जा रहा है। एक बात आपको हमेशा याद रखनी चाहिए की function declaration statement को आप semicolon से terminate करते है। लेकिन function definition के साथ ऐसा नहीं होता है। मान लीजिये की आप addition का function बनाने जा रहे है तो उसे आप इस प्रकार declare कर सकते है।

int add(int a, int b);  
   

Function definition 
इस part को function body भी कहा जाता है। इसमें आप वो statements लिखते है जिन्हें आप execute करवाना चाहते है। Function definition का basic structure नीचे दिया जा रहा है।

return_type functionName (Parameters_list)
{
      statement 1;
      statement 2;
       .
       .
      statement n; 
  
}
   

Function definition में return_type, functionName और parameters list उसी प्रकार होते है जैसे की function declaration में होते है। इनके बाद में curly braces के block में वो statements दिए जाते है जो आप execute करवाना चाहते है। उदाहरण के लिए यदि आप addition का function बना रहे है तो उसकी definition आप इस प्रकार लिख सकते है।

int add(int a, int b)
{
    int c;
    c = a+b;
   
    return c;
    
}


C Functions

Function call
Program में जँहा भी आप function को use करवाना चाहते है तो उसे call करते है। Function call का basic structure इस प्रकार होता है। 

functionName(arguments_list); 

Parameters/argument_list  -  Arguments वो values होती है जो आप function को call करते समय pass करते है। ये values function definition के parameters को assign होती है। इसके बाद इन values पर processing होती है और result return किया जाता है। उदाहरण के लिए यदि आप addition के function को call करते समय 2 values पास करेंगे जैसे की 2 और 7 । ये values parameter variables a और b को assign हो जाएँगी और function के अंदर इन variables पर ही processing होती है। ऐसे functions जिनमें parameters define किये गए है और यदि आप function call करते समय arguments pass नहीं करते है तो program में error आती है।  
  
उदाहरण के लिए यदि आपने addition का function create किया है तो आप उसे इस प्रकार call कर सकते है।

add(2,7); 


Example 
#include<stdio.h>
int add(int a, int b); /* function declaration */
int main()
{
   int result;
    printf("This is  addition program using function! ");
    result = add(2,7);  /* function call */
    printf("Result is : %d", result);
 }
int add(int a, int b)  /* function definition */
{
     int c;
     c = a + b;
    
     return c;
}       
ऊपर दिया गया program निचे दिया गया output generate करता है। 

This is addition program using function!
Result is : 9 


 C Structures 

Introduction to C structures 
आपने data types के बारे में तो पढ़ा ही होगा। Predefined data types (int, char, float आदि) की तरह C user defined data types (structure, unions आदि) भी allow करती है। ऐसे ही एक user defined data type को structure कहा जाता है। 

Structure में आप दूसरे predefined data types को store कर सकते है और एक record तैयार कर सकते है। जैसे की आप किसी व्यक्ति के बारे में उसका नाम,पता और उम्र store करवाना चाहते है तो उस व्यक्ति के नाम से एक structure create कर सकते है और उसमे ये तीन variables create कर सकते है। ऐसा करने से सारी information एक ही नाम के द्वारा access की जा सकती है।  

Structure किसी array की तरह ही होता है। इन में difference इतना होता है की array में आप एक ही तरह के data type को store कर सकते है लेकिन structure में आप different data types को store कर सकते है।

जब एक बार आप कोई structure create करते है तो ये एक data type बन जाता है। अब आप इस data type के कितने भी variables create कर सकते है। और आप इस data type का array भी create कर सकते है। लेकिन जब आप इस तरह के variable की value initialize करंगे तो आपको उस structure में define किये गए सभी variables की value initialize करनी होगी।

Structure को main method से पहले ही आप define कर सकते है।  



C Structures 

Defining a structure 
Structure के साथ काम करना बहुत ही आसान होता है। जैसा की मैने आपको पहले बताया ये किसी array की तरह ही होता है। आइये अब देखते है की structure को कैसे define किया जाता है और कैसे use किया जाता है। 

Structure को define करने के लिए struct keyword use किया जाता है। इस keyword के बाद structure का unique नाम दिया जाता है। इसके बाद curly braces में variables create किये जाते है। और ending curly bracket के बाद semicolon लगाया जाता है। Structure create करने का basic syntax नीचे दिया जा रहा है।  

struct struct_Name
{
    //Statement 1;
};
      
मान लीजिये आप किसी tShirt का record store करने के लिए एक structure बना रहे है तो उसे इस प्रकार define कर सकते है।  

struct tShirt
{
    int price;    
};

यँहा पर tShirt नाम से एक structure create किया गया है। इस structure में price नाम से एक variable create किया गया है। आप एक से ज्यादा variables भी create कर सकते है। आप इन variables को structure के अंदर initialize नहीं कर सकते है। क्योंकि पहले struct (tShirt) type का variable create किया जायेगा फिर उस variable के माध्यम से हर record के लिए अलग से इन variables को initialize किया जाता है। इन variables को structure members कहा जाता है। जैसा की मैने आपको पहले बताया था उसी प्रकार ending curly braces के बाद semicolon लगाया गया है।


C Structures 

Creating structure variables 
Structure variables आप 2 तरह से create कर सकते है। 

With structure definition 
Without structure definition 

With structure definition 
जब आप structure definition के साथ ही उस type के variables create करते है तो ending semicolon से पहले आप variables को comma से separate करके लिख देते है। जैसे की नीचे दिए गए उदाहरण में किया गया है। 

struct tShirt
{
   int price;
}t1,t2;

   



Without structure definition 
जब आप structure definition के बिना variables create करते है तो struct keyword use करते है। Struct keyword के बाद structure का नाम लिखा जाता है। और इसके बाद comma से separate करके जितने चाहो उतने variables लिख सकते है। इसका उदाहरण नीचे दिया जा रहा है। 

struct tShirt t1, t2, t3;


C Structures 

Accessing structure members 
Structure members को आप 2 वजह से access करते है। या तो आप members को values assign करवाने के लिए या फिर उनकी values को output के रूप में print करवाने के लिए आप structure members को access करते है। जब भी आप किसी भी structure member को access करते है तो ऐसा आप (.) dot operator द्वारा करते है। 

मान लीजिये आप tShirt structure के variables को values assign करवाना चाहते है तो आप ये इस प्रकार कर सकते है। 

t1.price=1000;  
    
यदि आप tShirt structure के variables को output के रूप में print करवाना चाहते है तो ऐसा आप इस प्रकार कर सकते है। 

printf("%d",t1.price);



Example 
#include <stdio.h>
struct tShirt
{
    int price;
};
int main()
{
      struct tShirt t1;
      t1.price=1000;
      printf("Price of tShirt is : %d",t1.price);
      return 0;


ऊपर दिया गया program निचे दिया गया output generate करता है। 

Price of tShirt is : 1000 



C Unions 

Introduction to C unions 
C आपको एक विशेष data type provide करती है। इस data type को union कहते है। Unions के द्वारा आप एक ही memory location में कई प्रकार के data type store कर सकते है। आप unions के अंदर अलग अलग data type के कई variables create कर सकते है। लेकिन एक समय में एक ही variable में value store की जा सकती है। Unions के द्वारा आप एक ही memory location को कई तरह से यूज़ कर सकते है।

Unions c language में structures की तरह ही होते है। बस इनमें difference ये होता है की structure का हर member एक separate memory location occupy करता है और उन सब की sizes अलग अलग होती है। लेकिन union के सभी members एक ही memory location को यूज़ करते है और उसकी size सबसे बड़ी size वाले member जितनी होती है।


Defining a union 
Unions define करने के लिए आप union keyword यूज़ करते है। ये structure को define करने जैसा ही होता है। Unions का basic syntax इस प्रकार होता है। 

union union_name
{
    data_type var1;
    data_type var2;
    ..
    ..
    data_type varn;
}u1.u2,....un;
   

सबसे पहले आप union keyword और unique union name define करते है। इसके बाद आप curly brackets  में variables define करते है। इसके बाद ending curly brackets के बाद comma लगाकर union variables create करते है। इसका उदाहरण नीचे दिया जा रहा है। 

union input 
{
     int a;
     float b;
}in;

अब in variable जो की input type का है एक integer value store कर सकता है या फिर एक float value store कर सकता है। आप इसके दोनों variables में एक साथ value store नहीं कर सकते है। 



C Unions 

Accessing union member variables 
Union members भी आप उस तरह ही access करते है जैसे structure members access किये जाते है। सबसे पहले आप union का नाम लिखते है। उसके बाद dot (.) operator लगाकर member का नाम लिखा जाता है। उदाहरण के लिए आप union members को value इस प्रकार assign कर सकते है।

in.a = 10;

यदि आप किसी union member को print करना चाहते है तो ऐसा आप इस प्रकार कर सकते है।

printf("%d",in.a);



A complete example
यदि आप union के सभी members को एक साथ value assign करके एक साथ print करवाने के कोशिश करेंगे तो output invalid show होगा। जब आप एक के बाद किसी दूसरे variable को value assign करते है तो memory में value उसी की रहेगी। आसान शब्दो में कहे तो जो member सबसे last में initialize किया जायेगा उसी की value memory में store होगी। इसका उदाहरण नीचे दिया जा रहा है।          

#include <stdio.h>
union input
{
    int a;
   float b;
}in;
int main()
{
    
    in.a = 15;
    printf("Value of a is : %d ",in.a);
    in.b = 20;
    printf("Value of b is : %f",in.b);

    return 0;
}


उपर दिए गए program में पहले a की value set की गयी है और उसे print किया गया है। इसके बाद b variable की value set की गयी है और उसे print किया गया है। जैसा की मैने पहले आपको बताया था की जिस variable की value सबसे last में set की जाएगी उसी की value union variable में रहेगी। इसलिए हर बार पहले value set की गयी है और दूसरे member की value set करने से पहले पुराने variable की value print की गयी है। ये program निचे दिया गया output generate करता है।

Value of a is :  15
Value of b is : 20.00000



C Recursion

Introduction to C Recursion 
C language में recursion एक ऐसी process होती है जिसमें एक function खुद को ही call करता है। ऐसा किसी problem को small parts में divide करके solve करने के लिए किया जाता है। ऐसे functions जो स्वयं को call करते है उन्हें recursive functions कहा जाता है। 

मान लीजिये आप किसी problem को solve करने के लिए कोई function create कर रहे है और इसका जो initial result है वो end result नहीं है। End result प्राप्त करने के लिए आपको इस process को वापस repeat करने की आवश्यकता है। ऐसी situation में आप same process execute करने के लिए iteration करने के बजाय इसी function को शुरूआती result के साथ call करते है। जब तक end result नहीं प्राप्त हो जाता यह process चलती रहती है। End result को condition द्वारा determine किया जाता है। End result प्राप्त होने पर इस process को terminate कर दिया जाता है। यदि इस function को terminate नहीं किया जाता है तो ये infinite time तक चलता जायेगा। 


Advantages of Recursion 
1. जब आप recursion के द्वारा किसी problem को solve करते है तो आपको बार बार function को call करने की जरुरत नहीं होती है। आप सिर्फ एक बार function को call करते है और end result नहीं आने तक वह स्वयं ही call होता रहता है। 
2. Iteration के द्वारा problems को solve करना बहुत complex रहता है लेकिन recursion में same problem बहुत ही आसानी से solve हो जाती है।



C Recursion

Structure of Recursive Function
एक recursive function का general structure निचे दिया जा रहा है।

return type myFunction(Parameters-list)
{
     statement 1;
     statement 2;
     ...
     statement N;

     myFunction(Arguments-list);  //Calling it self , Recursive process.

     ...

     other statements;




Disadvantages of Recursion 
हालाँकि recursion के द्वारा problem कम code में आसानी से solve की जा सकती है लेकिन recursive process की कुछ disadvantages भी होती है। इनके बारे में निचे दिया जा रहा है।   
1. Slower - Recursive programs साधारण programs से slow होते है। Function को बार बार call होने के लिए और end result show करने में recursive programs को normal programs से अधिक समय लगता है। 
2. Requires extra memory - साधारण programs की तुलना में recursive programs को execute होने के लिए extra memory की आवश्यकता होती है।     


Types of Recursion
C language में recursion 2 type का होता है। दोनों types के बारे में निचे detail से बताया जा रहा है। 

Direct Recursion
Direct recursion वह recursion होता है जिसमें एक function स्वयं को call करता है। इस तरह के recursion का general structure निचे दिया जा रहा है।

return_type MyFunction(parameter-list)
{
      ....statements;
      MyFunction(argument-list);
      .....other statements;

}




Indirect Recursion
Indirect recursion वह recursion होता है जिसमें एक function किसी दूसरे ऐसे function को call करता है जो वापस उसे ही call करता है। इस तरह के recursion का general structure निचे दिया जा रहा है।

First Function
return_type FunctionOne(parameter-list)
{
     ....statements;
     FunctionTwo(argument-list);
     ....other statements;
}

Second Function
return-type FunctionTwo(parameter-list)
{
       .....statements;
       FunctionOne(argument-list);
       .....other statements;
}



C Recursion

Example of Recursion (Factorial)
Recursion को example के द्वारा समझाने के लिए यँहा पर factorial का उदाहरण दिया जा रहा है। एक factorial number 1 से लेकर जिस भी number का आप factorial पाना चाहते है उस number तक के सभी numbers का  product (multiplication) होता है।

उदाहरण के लिए आप 5 का factorial calculate करना चाहते है। इसके लिए आपको 1 से लेकर 5 तक के सभी numbers को multiply करना होगा। सभी numbers को multiply करने के बाद जो result प्राप्त होगा वही 5 का factorial होगा।

1*2*3*4*5 = 120

C program के द्वारा किसी number का factorial calculate करने के दो तरीके हो सकते है।

1. Using iteration - इस तरीके में आप 1 से लेकर दिए गए number तक loop चला सकते है और सभी numbers को multiply करके result print कर सकते है।   
2. Using recursion - इस तरीके में आप एक function create कर सकते है जो दिए गए number को एक number कम से multiply करता है और स्वयं को उसी number के साथ call करता है।    
इन दोनों ही तरीकों को निचे उदाहरण के माध्यम से समझाया जा रहा है। 

Calculating factorial of a number using iteration
#include <stdio.h>

int main()
{
    int num,i;
    int result=1;
  
    printf("Enter number to find out its factorial.....");
    scanf("%d",&num);

    for(i=1; i<=num; i++)
     result = result*i;

     printf(" Factorial of %d is %d",num,result);

     return 0;
}
ऊपर दिया गया program निचे दिया गया output generate करता है।


Enter a number to find out its factorial..
5
120



C Recursion

Calculating factorial of a number using recursion
#include <stdio.h>

int MyFactFunction(int n);
int num;

int main()
{
    printf("Enter a number to find its factorial...");
    scanf("%d",&num);

    printf("Factorial of %d is : %d",num,MyFactFunction(num));
    return 0;
}

int MyFactFunction(num)
{
    if(num >= 1)
        return num*MyFactFunction(num-1);
    else
        return 1;
}


ऊपर दिए गए उदाहरण में MyFactFunction एक recursive function है। ये दिए गए number से एक number कम के साथ वापस स्वयं को call करता है और original value से (जिसके साथ function को call किया गया था) multiply करता है। ऐसा ये तब तक करता है जब तक original value 1 से अधिक होती है। जैसे ही यह value 1 हो जाती है, यह function terminate हो जाता है। ये program निचे  दिया गया output generate करता है।


Enter a number to find its factorial....
6
Factorial is : 720 



C Pointers 

Introduction to C pointers 
Pointers वो variables होते है जो दूसरे variables के address को store करते है। जैसा की आपको पता है हर variable का memory में एक unique address होता है। ये address hexadecimal form में होता है। इसलिए इस address को आप किसी normal variable में store नहीं कर सकते है। किसी भी variable के address को store करवाने के लिए आप pointer variable create करते है। आइये इसे एक उदाहरण से समझने का प्रयास करते है।

मान लीजिये आपने एक variable create किया है। इस variable का नाम Age है। और आपने इसमें 55 value assign करवायी है। इस variable की memory में location (address) 21F है। अब यदि आप चाहे तो इस address को एक pointer variable में store करवा सकते है। Pointers को यूज़ करने की कुछ advantages निचे दी जा रही है। 


Advantages of using pointers 
1. Pointers की मदद से आप dynamically memory allocate कर सकते है। 
2. Pointers की मदद से आप data structures (linked-list, stack, queue) create कर सकते है।
3. Pointers use करने से program का execution time कम हो जाता है। 
4. Pointers की मदद से आप function से एक से अधिक values return कर सकते है। 
5. Pointers की मदद से argument passing के दौरान आप variable की copy के बजाय original variable पर काम कर सकते है। 
6. Pointers के द्वारा large data को search और sort करना बहुत आसान होता है। 

यदि pointers को properly use ना किया जाये तो इसके कुछ disadvantages होते है। इनके बारे में निचे दिया जा रहा है। 

Disadvantages of using pointers

1. कई बार pointers की वजह से program में ऐसी error आ जाती है जिसे diagnose करना बहुत difficult होता है। 
2. Pointers की वजह से memory में leaks create हो जाते है। 
3. यदि run time के दौरान pointers को hold करने के लिए extra memory ना हो तो program crash हो जाता है। 
4. Pointers की मदद से restricted memory को access किया जा सकता है। 



C Pointers 

Initializing pointers 
C में pointers को initialize करने का syntax निचे दिया जा रहा है। 

pntr_variable = &variable_name;
  
C में pointers initialize करने के लिए सबसे पहले आप pointer variable को लिखते है। (Initialization part में pointer variable को बिना asterisk operator (*) के लिखा जाता है।) इसके बाद assignment operator (=) लिखा जाता है। इसके बाद आप ampersand operator (&) (इसे address-of operator भी कहा जाता है।) define करते है। इस operator के बाद आप बिना कोई space दिए variable का नाम लिखते है। आइये इसे एक उदाहरण से समझने का प्रयास करते है। 

int age = 55;               //Declaring a variable 
int *age-pntr;             //Declaring a pointer
age-pntr = &age;       //Initializing a pointer 



Operators used with pointers 
1. & (Address-of ) operator - ये operator variable के address को point करता है। इससे से आप किसी भी variable का address प्राप्त कर सकते है। 
2. * (Value-at) operator - ये operator सिर्फ pointer variables के साथ काम करता है। ये operator किसी particular address पर store की गयी value को represent करता है। 

Example 
#include<stdio.h>

int main()
{
    int age = 55;
    int *pntr;
    pntr = &age;

    printf("Address of age variable is : %d ",pntr);

    printf("Value of age variable is : %d",*pntr);

}
     
इस उदाहरण में pointers के द्वारा एक variable का address और उसकी value print करवाई गयी है। ये pointers की working का एक बहुत ही simple उदाहरण है। ये program निचे दिया गया output generate करता है।

Address of age variables is : -1074204644;
Value of age variables is : 55 



C Type Casting

Introduction to C Type Casting 
C language में type casting एक ऐसी process होती है जिसमें एक data type के variable का दूसरे data type में conversion होता है। कई बार ये variables automatically convert हो जाते है और कई बार आप इन्हें manually भी convert करते है।

कई बार बहुत से students type casting या type conversion के बीच confuse हो जाते है। इसलिए मैं आपको बताना चाहूंगा की C language में एक data type जब दूसरे data type में automatically बदलता है तो उसे type conversion कहा जाता है। लेकिन जब आप explicitly या manually type cast operator का use करते हुए किसी data type के variable को दूसरे data type में बदलते है तो उसे type casting कहा जाता है।

C language में casting/conversion की process 2 प्रकार की होती है। इनके बारे में निचे दिया जा रहा है।


From higher type to lower type 
इस तरह की casting में एक ऐसे data type का variable जो अधिक size की value store करता है उसको ऐसे data type में convert किया जाता है जो कम size की value store करता है। इस तरह के conversion में data का loss होता है। 

int result = 5/2;  /* Higher type to lower type  */

उदाहरण के लिए यदि आप 5 को 2 से divide करेंगे तो 2.5 result प्राप्त होगा। यदि इस result को आप किसी integer variable में store करने का प्रयास करेंगे तो सिर्फ 2 ही result के रूप में store होगा। यँहा पर .5 का loss हुआ है। ये एक higher type (float) से lower type (int) में conversion है।    

From lower type to higher type 
इस तरह की casting में कम size की value store करने वाले data type के variable को अधिक size की value store करने वाले data type में convert किया जाता है।


long result = 2+2;  /* Lower to higher type */

उदाहरण के लिए यदि आप किसी integer result को long type के variable में store करने का प्रयास करते है तो ऐसी casting lower type से higher type casting होती है।



C Type Casting

Type Conversion (Implicit/Automatic)

जैसा की आपको पता है type conversion automatically compiler द्वारा perform किया जाता है। इसमें आपको कुछ भी करने की आवश्यकता नहीं होती है और ना ही इसमें किसी तरह के operator की आवश्यकता होती है। ये compatible type के बीच में ही perform होता है। Compatible types ऐसे data types होते है जिनमें type conversion possible है। इसे निचे उदाहरण के माध्यम से समझाया जा रहा है।

#include <stdio.h>
#include <conio.h>

int main()
{
   int num = 5;
   float fnum;

   fnum = num; /* Type Conversion */

   printf("Number is :%f",fnum);

   return 0;
}

ऊपर दिया गया program निचे दिया गया output generate करता है।

Number is : 5.000000 



C Type Casting

Type Casting (Explicit/Manually)
Type casting आप खुद perform करते है। इसके लिए आप type cast operator यूज़ करते है। C language में () को type cast operator कहा जाता है। इस operator को आप किसी expression से पहले use करते है। इन brackets में आप उस data type का नाम लिखते है जिसमें आप expression के result को convert करना चाहते है। इसको निचे उदाहरण के माध्यम से समझाया जा रहा है।

#include

int main()
{
    int num = 5;
    char cnum;

    cnum = (char) num;  /* Type Casting */

    printf("Number is : %d",(int)cnum); /* Type Casting Again */

    return 0; 
}

ऊपर दिया गया program निचे दिया गया output generate करता है।

Number is : 5 



C Type Casting

Strings Type Casting Functions 
C language string के लिए अलग से data type नहीं provide करती है। C में string के लिए आप character array define करते है। इसलिए casting operator द्वारा string को cast करना संभव नहीं है। Strings के साथ casting operations perform करने के लिए C language आपको 5 built in functions provide करती है। इन functions को use करने के लिए आपको program में <stdlib.h> header file include करनी होती है। इन functions को use करके आप strings के साथ भी casting perform कर सकते है।   
  
atof() 
ये function string data type को float data type में convert करने के लिए use किया जाता है। ये function एक float value return करता है इसमें आप argument के रूप में एक string pass करते है। इसका general syntax निचे दिया जा रहा है। 

double atof(const char* string);



atoi()
ये function string data type को integer data type में convert करने के लिए use किया जाता है। इसका general syntax निचे दिया जा रहा है।    

int atoi(const char* string);



atol() 
ये function string data type को long data type में convert करने के लिए use किया जाता है। इसका general syntax निचे दिया जा रहा है।


long int atol(const char* string);


itoa()
ये function integer data type को string data type में convert करने के लिए use किया जाता है। इसका general syntax निचे दिया जा रहा है। इस function में argument के रूप में आप एक integer value, एक character array और  number system base pass करते है।  

char* itoa(int value, cha* string, int base);



ltoa()
ये function long data type को string data type में convert करने के लिए use किया जाता है। इसका general syntax निचे दिया जा रहा है।

char* ltoa(long value, char* string, int base);




C Storage Classes 

Introduction to C storage classes 
एक storage class variables और functions का scope और lifetime define करती है। Basically ये एक keyword होता है जो की variable या function के declaration से पहले यूज़ किया जाता है। ये keyword उस variable या function के काम करने के तरीके को बदल देता है। 


C language आपको 4 storage classes provide करती है। इनकी list निचे दी जा रही है।

1. auto storage class 
2. register storage class 
3. static storage class 
4. extern storage class 



C Storage Classes 

C auto storage class 
C की auto storage class सभी local variables के लिए default class मानी जाती है। जब भी आप कोई local variable create करते है और उसके साथ कोई दूसरी storage class define नहीं करते है तो वह by default (automatically) auto storage class को belong करता है।   

इस तरह के variables को automatic variables भी कहा जाता है। इन्हें दर्शाने के लिए आप चाहे तो auto keyword भी यूज़ कर सकते है। आप auto keyword को सिर्फ किसी functions के अंदर ही यूज़ कर सकते है। जब भी function call होता है तो ये variables create होते है और function के exit होने के साथ ही ये destroy हो जाते है।  Auto storage class का उदाहरण निचे दिया जा रहा है। 

int myFunction()
{
     int a; 
     auto int a;
}
  
ऊपर define किये गए दोनों variables ही auto storage class को belong करते है।


C register storage class 
Register storage class ऐसे variables declare करने के लिए यूज़ की जाती है जिन्हें आप RAM की जगह register में store करवाना चाहते है। अकसर ऐसा fast data access के लिए किया जाता है।

एक register computer processor का part होता है जो की small data को hold करता है। लेकिन ये बहुत छोटी value को ही hold कर सकता है। ये RAM से कई गुना तेज होता है।

इसलिए अपने program में आपको जँहा भी तेज data access की requirement हो तो आप इस storage class को यूज़ कर सकते है। एक बात आपको ये भी ध्यान रखनी चाहिए की जरुरी नहीं की variable को register define करने से ही वो register में save हो जायेगा। ये आपके hardware configuration पर depend करता है।

Register storage class का उदाहरण निचे दिया जा रहा है।

register int age;



C static storage class
Static storage class compiler को बताती है की variable program के end तक consistent रहेगा। यँहा पर consistent से मेरा मतलब की variable बार बार create और destroy नहीं होगा। 

मान लीजिये आपने function के अंदर एक variable create किया है। जब भी आप इस function को call करते है तो ये variable create होता है और function के end होने के साथ ही ये destroy हो जाता है। 

यानि यदि आप function को call करके उस variable में कोई changes करते है तो function exit होने के बाद वो changes भी नहीं रहेंगे। जब आप दुबारा function को call करेंगे तो variable उसकी initial value के साथ create होगा।     

लेकिन यदि आप static keyword को यूज़ करते है तो variable पुरे program में सिर्फ एक ही बार create होगा और program के end में destroy होगा। और जितनी भी बार आप variable की value change करेंगे वो changes भी पुरे program के दौरान रहेंगे।   

Static storage class का उदाहरण निचे दिया जा रहा है। 

#include <stdio.h>
void numFunction();

int main()
{
    numFunction();
    numFunction(); 
    numFunction();
    numFunction();
}

void numFunction
{
    static int num=0;
    num=num+2;
    printf("%d ",num);
}
   
ऊपर दिए गए program में इस बात से कोई फर्क नहीं पड़ता की आप numFunction() को कितनी बार call कर रहे है। जब भी आप इसे call करेंगे ये function पुराने function call के द्वारा change की गयी num variable की value को ही यूज़ करेगा। ये program निचे दिया गया output generate करेगा।

2   4   6   8



C Storage Classes 

C extern storage class 
कई बार ऐसा होता है की आप किसी बड़े project पर काम कर रहे हो तो आपको एक से अधिक program files को handle करना पड़ता है। ऐसी situation में extern storage class बहुत ही helpful होती है।   

मान लीजिये आपने एक program file में एक variable declare किया है। ये variable एक global variable है। अब आप इस variable को इसी project की किसी दूसरी program file में यूज़ करना चाहते है तो आप extern keyword का यूज़ करते है। 

Basically extern keyword compiler को बताता है की ये variable पहले ही किसी class में create किया जा चूका है और आप इसे इस program file में यूज़ करने वाले है। ये keyword पहले से create किये गए किसी variable या function का reference देता है। 

आइये इसे एक उदाहरण से समझने का प्रयास करते है। 

         
File 1
#include <stdio.h>
int num = 5;

int main()
{
    printf("%d",num);

    return 0;
} File 2
#include <stdio.h>
extern int num;
int main()
{
   printf("%d",num);

   return 0;



ऊपर दिए गए उदाहरण में file 2 में compiler को extern keyword द्वारा ये बताया जा रहा है की इस file num variable को यूज़ किया गया है जो की पहले ही दूसरी file में declare हो चूका है।  



C Console I/O

Introduction to C Console I/O 
C language में input और output को 2 तरीके से process किया जा सकता है। एक तो आप console के माध्यम से input दे सकते है और output देख सकते है। C language में input और output के लिए ये standard और default तरीका होता है। Console एक black window होती है जो input के समय compiler के द्वारा show की जाती है। Program के execution के बाद उसका result भी इसी window में दिखाया जाता है।   

दूसरे तरीके में आप disk files को यूज़ करते हुए भी input दे सकते है और उनमें output भी store कर सकते है। ये totally application की need पर depend करता है की आप कौनसा तरीका यूज़ करते है। 

Console के माध्यम से आप इस तरह का data input कर सकते है।

1. Strings 
2. Numbers
3. Characters
4. All primitive types of data (float,  double, long etc)

Console I/O के लिए 2 standard device होते है।

1. Keyboard - Keyboard एक standard input device है। By default एक C program keyboard के माध्यम से ही input लेता है। 
2. Monitor - Monitor (screen, LCD) एक standard output device होता है। यदि आप कोई दूसरा तरीका यूज़ नहीं करते है तो by default एक C program monitor पर ही output दिखाता है। लेकिन आप चाहे तो किसी external device पर भी output show कर सकते है। 




C Console I/O

printf()
printf() एक output function है। ये user को monitor के माध्यम से output show करने के लिए यूज़ किया जाता है। इस function के द्वारा आप सभी तरह के primitive types का data यूज़र को show कर सकते है। इस function में आप print करने के directly string भी pass कर सकते है या फिर कोई variable भी pass कर सकते है जिसकी value आप print करना चाहते है। String को double quotes में लिखा जाता है। आप चाहे तो string और variable दोनों एक साथ भी pass कर सकते है। 

Variable की value को किसी निश्चित जगह पर print() करने के लिए आप format specifier भी यूज़ कर सकते है।   


इसका general syntax निचे दिया जा रहा है। 

printf("string/variable");
or


printf("string format-specifier string",variable-name)


printf() function के यूज़ को निचे उदाहरण के माध्यम से समझाया जा रहा है।


#include

int main()
{
   int a=5;
   int b=4;
   int result;
   
   result = a+b;

   printf("Sum of %d and %d is : %d",a,b,result);

  return 0;
}

ऊपर दिया गया program निचे दिया गया output generate करता है।

Sum of 5 and 4 is :  9



getchar() & putchar()
getchar() function console के माध्यम से single character read करता है। इस character को ये integer के रूप में return करता है। यानी इसके result को आप किसी integer variable में store करते है। C में characters integer type के होते है। C में int और char को आसानी से assignment के द्वारा ही cast किया जा सकता है।चाहे आप कितने भी characters enter करें लेकिन ये एक बार में एक ही character read करता है। यदि आप एक से अधिक characters read करना चाहते है तो इस function को loop में use कर सकते है। 

putchar() function को इसमें pass किये गए character को screen पर display करने के लिए use किया जाता है। ये function भी एक बार में एक character ही screen पर display करता है। यदि आप एक से अधिक characters screen पर display करना चाहते है तो इस function को loop में use कर सकते है।

इन दोनों functions को निचे उदाहरण के माध्यम से समझाया गया है।

#include

int main()
{
   char data;

   printf("Enter some characters. To exit enter @ symbol.");
   while(data != '@')
   {
       data = getchar();
       printf(" You entered this character :",putchar(data));   
   }  
   return 0;
}

ऊपर दिया गया program निचे दिया गया output generate करता है। 

Enter some characters. To exit enter @ symbol.
a
You entered this character : a
b
You entered this character : b
@
You entered this character : @




C Console I/O

gets() and puts()
gets() function keyboard से string read करने के लिए use किया जाता है। Read की गयी string को एक character array में store किया जाता है। ये function argument के रूप में character array लेता है।  

puts() function के माध्यम से एक string को screen पर display किया जाता है। इस function में भी argument के रूप में character array pass किया जाता है। 

इन दोनों functions को निचे उदाहरण के माध्यम से समझाया गया है।  

#include <stdio.h>

int main()
{
   char arr[60];

   printf("Enter a string : ");
   gets(arr);
   
   printf(" You entered following string:")
   puts(arr);
  
   return 0;
}  
ऊपर दिया गया program निचे दिया गया output generate करता है। 

Enter a string :  
Best Hindi Tutorials
You entered following string : Best Hindi Tutorials



C File Handling

Introduction to C file handling 
एक file bytes की sequence होती है। File किसी भी data को store करने के लिए यूज़ की जाती है। Files को permanent storage के लिए यूज़ किया जाता है। जब आप किसी program को run करवाते है और उसका output देखने के बाद उसे बंद कर देते है तो उसका output वहीँ remove हो जाता है। यदि आप इस output को permanently store करना चाहते है तो C language के file handling feature को यूज़ कर सकते है।

C file handling का यूज़ यँही ख़त्म नहीं होता है। यदि program में दिए जाने वाला input भी बहुत अधिक है तो किसी human द्वारा इसे enter कराने के बजाय आप file का यूज़ कर सकते है। इससे program automatically file से ही input ले लेगा।

C language के द्वारा आप files से related ये काम कर सकते है।

1. Existing files को open कर सकते है or नयी files create कर सकते है। 
2. किसी भी program के output को file में store कर सकते है। 
3. File के द्वारा किसी भी program में input भी दे सकते है।       
4. सभी operations perform होने के बाद files को close कर सकते है।      



C File Handling

Opening a file 
किसी भी file में output store करने या उसमें से input लेने से पहले उसे open किया जाता है। File open करने के लिए आप fopen() function को use करते है। 

जब आप किसी ऐसी file को open करने के कोशिश करते है जो exist ही नहीं करती तो वो file automatically create हो जाती है। यदि file पहले से exist करती है तो वह open हो जाती है। आप किस mode में file को open करते है ये उस पर depend करता है।  

C language के द्वारा आप files को अलग अलग modes में open कर सकते है। ये mode आप function को call करते समय define करते है। उदाहरण के लिए यदि आप किसी file से सिर्फ data read करने वाले है तो आप उसको read mode में open कर सकते है। और यदि आप file में output store करना चाहते है तो उसे write mode में open कर सकते है। और आप चाहे तो किसी file को दोनों modes में भी open कर सकते है। 

File opening modes
File open करते समय यूज़ किये जाने वाले सभी modes के बारे में नीचे दिया जा रहा है। 

Mode Description 
r इस mode से आप एक existing file को open कर सकते है। इस mode में आप files को सिर्फ read कर सकते है।    
w इस mode से आप सिर्फ files में write कर सकते है। यदि file पहले से exist नहीं करती है तो वह create हो जाती है।   
a इस mode से आप output write करते है। इस mode में open की गयी file यदि exist करती है तो वह open हो जाती है और उसमें जो data होता है उसी के बाद से नया data add हो जाता है। यदि file exist नहीं करती है तो नयी file create हो जाती है।   
r+ इस mode में आप एक file को reading और writing दोनों purposes के लिए open कर सकते है।  
w+ इस mode में file reading और writing दोनों purposes के लिए open की जाती है। Write करने के लिए इस mode में पहले file zero length तक truncate की जाती है।   
a+ इस mode में file reading और writing दोनों modes में open होती है। यदि file exist नहीं करती तो create हो जाती है। और writing हमेशा existing data के बाद शुरू होती है।    


fopen() function का general structure निचे दिया जा रहा है।

FILE *fopen(const char * file_name, const char * mode );
        
इस function में 2 arguments pass किये जाते है। एक तो hard disk पर file का address और दूसरा वह mode जिसमें आप file open करना चाहते है। ये function FILE type का एक object return करता है। इसलिए इसका return type FILE होता है। और इसी लिए आप इस function के द्वारा return किये गए object को FILE type के दूसरे object में store करेंगे। इसका उदाहरण आप आगे देखेंगे।



C File Handling

Reading from a file 
C language के द्वारा किसी file को read करने के लिए आप 2 functions यूज़ कर सकते है।

1. fgetc()
2. fgets()
fgetc() function एक बार में एक ही character read करता है लेकिन इसे आप loop में यूज़ करके पूरी file को read कर सकते है। इस function में open की गयी file के pointer के रूप में एक ही argument pass किया जाता है। इसका उदाहरण नीचे दिया जा रहा है।

#include<stdio.h>

int main()
{
    FILE *fp;
    char data[1];
    fp = fopen("input.txt","r");
    data=fgetc(fp);
    printf("%s",data[]);
    fclose(fp);     

    return 0;
}
    
उपर दिए गए उदाहरण में fgetc() function का यूज़ करते हुए file में से एक character read किया गया है। ये program input.txt file से एक character read करता है। यदि file exist नहीं करती है तो run time error generate होती है।  

fgets function से आप define किये गए characters तक file को read कर सकते है। इस function में 3 arguments pass किये जाते है। पहला argument एक character array होता है। ये वो array होता है जिसमे read किया गया data store किया जाता है। दूसरा argument जितने characters आप read करना चाहते है उनकी size होती है। तीसरा argument file pointer होता है। इसका उदाहरण नीचे दिया जा रहा है।

#include<stdio.h>

int main()
{
    FILE *fp;
    char data[100];
    fp=fopen("test.txt","r");
    fgets(data,100,fp);
    printf("%s",data);
    fclose(fp);

    return 0;
}    

ऊपर दिया गया program test.txt file से 100 characters read करता है। यदि file पहले से exist नहीं करती है तो run time error generate होती है। 



C File Handling

Writing to a file
C language के द्वारा किसी file में data write करने के लिए आप 2 functions यूज़ कर सकते है। 
1. fputc()
2. fputs()
यँहा पर fputs() function का example नीचे दिया जा रहा है। इस function में 2 arguments pass किये जाते है। पहला argument वो string होती है जिसे आप file में store करना चाहते है। और दूसरा argument file pointer होता है।     
  
#include 

int main()
{
    FILE *fp;
    fp = fopen("test.txt","w+");
    fputs("This is text string",fp);
    fclose(fp); 

    return 0;
}
ऊपर दिया गया program test.txt file में This is a string write करता है। 


Closing a file
File को close करने के लिए आप fclose() function को यूज़ करते है। इसका general structure नीचे दिया जा  रहा है। 

int fclose(FILE *fp);

उपर दिए गए function में *fp वो FILE type object है जिसमे आपने fopen() function के द्वारा return किये गए object को store किया था। 



C Error Handling 

Introduction to C error handling
Errors किसी भी program में आ सकती है। इन्हें exceptions भी कहा जाता है। ये वो errors होती है जो program के run होते हुए समय आती है। इनके आने से program का execution रुक जाता है। C programming error handling को support नहीं करती है।

लेकिन c program में errors को आप return values से पहचान सकते है। जब भी C program में कोई exception आती है तो functions -1 या NULL value return करते है। इन values को पहचान कर आप पता लगा सकते है की error आयी है या नहीं।


errno variable 
C programming आपको errno नाम का एक global variable provide करती है। जब भी program में कोई exception आती है तो इस variable को error number के साथ set कर दिया जाता है। एक error number  header file में एक particular error को represent करता है। सभी errors इसी header file में define की गयी है।

इस variable को आपको अपने program में यूज़ करने के लिए इस तरह define करना चाहिए।

extern int errno;

इस variable को initially zero के साथ define करना और भी अच्छा माना जाता है। क्योंकि zero का मतलब होता है की कोई error नहीं है। साथ ही आपको header file को भी अपने program में include करना पड़ेगा।


Functions for error handling 
C language आपको errors को represent करने के लिए 2 functions provide करती है। 
perror() - ये function आपके द्वारा pass की गयी string को print करता है और साथ ही जो error generate हुई है उसका description print किया जाता है। इस function से ये पता चल जाता है की कौनसी error आयी है।   
strerror() - ये function generate की गयी error के textual representation का pointer return करता है। ये function standard errors को point करता है।  

निचे perror() function का example दिया जा रहा है। 

#include  
#include
extern int errno;
int main()
{

    FILE *fp;
    fp = fopen("test.txt","r"); 
    if(fp == NULL)
    {
         fprintf(stderr,"Error Number :  %d ",errno);
         perror("Error Description : ");
    }
   else
    {
        fclose(fp);
    }
}    
  
ऊपर दिया गया program निचे दिया गया output generate करता है।

Error Number : 2
Error Description : No such file or directory 



C Error Handling 

exit() function 
जब भी आपका program execute होता है तो आप exit status return कर सकते है। ये status बताता है की आपका program successfully execute हुआ है या error आने से terminate हुआ है। Exit function के और भी यूज़ है लेकिन यँहा पर errors के संदर्भ में आप इसे 2 तरह से यूज़ कर सकते है।

यदि आपका program किसी error की वजह से terminate हो रहा है तो आप exit function को -1 या EXIT_FAILURE string के साथ call कर सकते है। यदि आपका program बिना किसी error के successfully terminate हो रहा है तो आप exit function को 0 या EXIT_SUCCESS string के साथ call कर सकते है।   

उपर दिए गए उदाहरण को exit function के साथ निचे दिया जा रहा है। 

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>

extern int errno;

int main()
{
    FILE *fp;
    fp=fopen("test.txt","r"); 

    if(fp == NULL)
    {
        fprintf(stderr,"Error Number : %d", errno);
        perror("Error Description ");
        fprintf(stderr,"Exiting due to error....");
        exit(-1);
    }
    else
    {
        fclose(fp);
    }
}
ऊपर दिया गया program निचे दिया गया output generate करता है। 

Error Number : 2
Error Description : No such file or directory
Exiting due to error..... 



C Header Files

Introduction to C header files
अभी तक आपने हर C program में header file का प्रयोग किया है। कई बार कुछ ऐसे functions या macros होते है जिनकी जरुरत बार बार अलग अलग projects में पड़ती है। इन functions आदि को एक particular extension (.h) वाली file में store कर लिया जाता है। बाद में जब भी आपको इन functions आदि की जिस भी program में जरुरत हो वँहा इस file को include कर लेते है।

Header files वो files होती है जिनमें function का declaration आदि किसी दूसरी file में यूज़ करने के लिए declare किया जाता है। Header files में macro को भी define किया जाता है।

Header files को program में यूज़ करने से आप एक ही code को बार बार लिखने से बच जाते है। इससे समय की बचत होती है। साथ ही program भी short और readable हो जाता है।



C Header Files

Types of header files 
C language में header files 2 प्रकार की होती है। 

Built in Header files 
User defined header files 

Built in header files 
Built in header files वो header files होती है जो compiler आपको provide करता है। इन header files में आप कोई changes नहीं कर सकते है। इस तरह की header files basic programming functionality provide करती है। जैसे की conio.h header file एक built in header file है और ये आपको basic console input/output functionality provide करती है।


एक ऐसी ही common built in header file stdio.h होती है। ये header file program में input/output को support करती है। इस header file में scanf(), printf() आदि functions defined होते है। इस header file को अपने program में include करके आप इन functions को यूज़ कर सकते है। 

User defined header files 
User defined header files programmers के द्वारा create की जाती है। ये header files भी built in header files की तरह ही प्रयोग की जाती है। लेकिन इनको use करने का तरीका different होता है।  



C Header Files

Working with header files 
Header files के साथ काम करना बहुत ही आसान है। आइये सबसे पहले header files को create करना देखते है। 

Creating header files 
Header files को आप आसानी से create कर सकते है। इसके लिए आपको नीचे दी गयी 2 बातें हमेशा याद रखनी चाहिए। 

किसी भी header file में main() function नहीं होता है। 
Header files को हमेशा .h extension से save करना चाहिए। 

जब भी आप कोई header file create करते है तो उसमे normal C program की तरह functions आदि define करते है। ये functions बिना किसी main() function के define किये जाते है। क्योंकि main() function उस file में होता है जो इस header file को include करेगी।


Functions आदि define करने के बाद आप उस file को .h extension के साथ save कर देते है। अब इस file को आप किसी भी C program में यूज़ कर सकते है। 



C Header Files

Using header files 
Header files को अपने program में add करने के लिए आप #include directive यूज़ करते है। ये एक pre-processor होता है, इनके बारे में आप दूसरी tutorial में जानेंगे। जब आप किसी built in header file को include करते है तो उसे angle brackets < > में लिखते है। और जब आप किसी user defined header file को यूज़ करते है तो उसे quotation mark " "  के अंदर लिखते है।

C header files के साथ कैसे काम करते है इसका complete example नीचे दिया जा रहा है।

myHeaderfile.h mySourcefile.c
int add(int a, int b);
int multiply(int a, int b);
add(int a, int b)
{
    return a+b;


multiply(int a, int b)
{
   return a*b;
}   
 
#include <stdio.h>
#include <conio.h>
#include "myHeaderFile.h"
int main()
{
     int a,b;
     printf("Enter first number : ");
     scanf("%d", &a); 
     printf("Enter second number : ");
     scanf("%d",&b);
     printf("Sum is : %d ",add(a,b));
     printf("Multiplication is : %d",multiply(a,b));

     return 0;
}

ऊपर दिया गया program निचे दिया गया output generate करता है। 

Enter first number : 
5
Enter second number :
3
Sum is : 8
Multiplication is : 15 



C Preprocessors Directives 

Introduction to C preprocessor directives  
Preprocessor C language का एक बहुत ही powerful tool है। कोई भी C program compiler द्वारा compile किये जाने से पहले preprocessor के द्वारा process किया जाता है। Preprocessor का काम preprocessor directives को process करना होता है। आपके program की कोई भी वो line जो # से शुरू होती है preprocessor directive कहलाती है।

उदाहरण के लिए यदि आपने program में #include directive यूज़ किया है, तो preprocessor के द्वारा process किये जाने के बाद ये line header file के actual content से replace कर दी जाती है। इस process को pre-processing कहा जाता है।

C programs में अलग अलग तरह के preprocessor directives यूज़ किये जाते है। इनके बारे में detail से नीचे दिया जा रहा है।


#include 
ये C programming में commonly यूज़ किये जाने वाला preprocessor directive है। ये directive किसी header file के content को जिस file में इसे include किया जाता है उस में copy कर देता है। इस directive का general syntax नीचे दिया जा रहा है। 

#include <file.h>      //for built-in header files
#include "file.h"       // for user defined header files
       


#define
ये directive C program में constants और macros define करने के लिए यूज़ किया जाता है। इसलिए आप इसे 2 प्रकार से यूज़ कर सकते है। 

#define को आप कोई constant define करने के लिए यूज़ कर सकते है। इसका general syntax इस प्रकार होता है। 

#define PI 3.14;
    
इस statement को program में define करने के बाद जँहा भी आप program में PI को यूज़ करेंगे तो PI 3.14 से replace कर दिया जायेगा।

#define directive को macros define करने के लिए भी यूज़ किया जाता है। एक macro code की एक line होती है जिसे program में macro name से replace कर दिया जाता है। ये एक function की तरह भी हो सकता है। इसका उदाहारण नीचे दिया जा रहा है।

#define square(n) (n*n);

जँहा भी आप program में square को यूज़ करेंगे और उसमे कोई number pass करेंगे तो वह उस number के square से replace हो जायेगा।



C Preprocessors Directives 

#undef
ये directive #define directive द्वारा define किये गए constant या macro को undefine करने के लिए यूज़ किया जाता है। इसके आगे आप constant या macro का सिर्फ नाम लिखते है। इसका उदाहरण नीचे दिया जा रहा है। 

#define PI 3.14;    //defining constant
..................   //other statements 
#undefine PI;   // undefine constant 




#ifdef, #else, #endif 
ये directive ये check करने के लिए यूज़ किया जाता है की क्या दिए गए नाम से कोई macro define किया गया है। यदि define किया गया है तो #ifdef के बाद का code execute हो जाता है नहीं तो #else के बाद का का code execute होता है। यदि आप #else नहीं यूज़ कर रहे है तो #ifdef को आप #endif से terminate करते है। इसका उदाहरण नीचे दिया जा रहा है। 

#ifdef Square
#undef Square 

#else

#define Square(n) (n*n);

#endif


ऊपर दिया गया code check करता है की क्या कोई Square नाम से macro define किया गया है। यदि macro पहले से defined है तो उसे undefine किया जायेगा। और यदि macro पहले से define नहीं है तो उसे define किया जायेगा।

इसी प्रकार #ifndef directive भी होता है जो code को तब execute करता है जब दिए गए नाम वाला macro define न किया गया हो।


#error 
मान लीजिये की आपके program में किसी macro की आवश्यकता है और यदि वो macro define नहीं किया गया है तो आप इस situation से related एक error show कर सकते है। इसके लिए आप #error directive यूज़ करते है। इसका उदाहरण नीचे दिया जा रहा है।   

#ifndef Square

#error Please define the marco first

#endif
  

#pragma 
ये directive compiler को additional information देने के लिए यूज़ किया जाता है। मान लीजिये की आपने एक function create किया है अब आप इस function के बारे में compiler को additional information दे सकते है। इसका उदाहरण नीचे दिया जा रहा है। 

#pragma token 




Comments