Shyam IT Guru: C Programming  

A complete Site for Information Technology

मैं आपको इस ब्लॉग में आने के लिए dhanyabad कहता हूं। जय हिंद, वंदे मातरम् ❤❤

C Programming लेबलों वाले संदेश दिखाए जा रहे हैं. सभी संदेश दिखाएं
C Programming लेबलों वाले संदेश दिखाए जा रहे हैं. सभी संदेश दिखाएं

Chapter-27 C preprocessor directives in hindi C programming

सितंबर 11, 2019 0
Chapter-27 C preprocessor directives in hindi C programming
  • Introduction to C preprocessor directives in Hindi 
  • Different types of C preprocessor directives in Hindi 

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 यूज़ किये जाते है।

#include 



ये C programming में commonly यूज़ किये जाने वाला preprocessor directive है। ये directive किसी header file के content को जिस file में इसे include किया जाता है उस में copy कर देता है। 
/* For built in header files */
#include <file.h>   
/* For user defined header files */
#include “file.h”     

#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 हो जायेगा।

#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 
Previous: C header files

C programming में कुल 27 chapters हैं जिन्हें आपने अभी तक पढ़ लिया है। इसके बाद आप HTML पढ़ सकते हैं।
Next: Learn HTML

Chapter-26 Header files in C programming in hindi

सितंबर 11, 2019 0
Chapter-26 Header files in C programming in hindi
  • Introduction to C header files in Hindi 
  • Types of header files in Hindi 
  • Creating & Using header files in C program in Hindi

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 हो जाता है।


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 होता है।  

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 में यूज़ कर सकते है। 

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 ” ”  के अंदर लिखते है।


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 in mySourceFile.c */
#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;
}
Enter first number :
5
Enter second number :
3
Sum is : 8
Multiplication is : 15 
Previous: C error handling
Next: C preprocessor directives

Chapter-25 Error handling in hindi C programming

सितंबर 11, 2019 0
Chapter-25 Error handling in hindi C programming
  • Introduction to C error handling in Hindi
  • perror() function for C error handling in Hindi
  • strerror() function for C error handling in Hindi 

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 <error.h> header file में एक particular error को represent करता है। सभी errors इसी header file में define की गयी है।
इस variable को आपको अपने program में यूज़ करने के लिए इस तरह define करना चाहिए।
extern int errno;
इस variable को initially zero के साथ define करना और भी अच्छा माना जाता है। क्योंकि zero का मतलब होता है की कोई error नहीं है। साथ ही आपको <errno.h> 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 करता है।  


#include <stdio.h>
#include <errno.h>
extern int errno;
int main()
{
    FILE *fp;
    fp = fopen(“test.txt”,”r”);
    /* Checking for error, whether file is loaded or not */
    if(fp == NULL)
    { 
         /* Printing error number & description */
         fprintf(stderr,”Error Number :  %dn”,errno);
         perror(“Error Description : “);
    }
   else
    {
        fclose(fp);
    }
}    
Error Number : 2
Error Description : No such file or directory 

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 कर सकते है। 


#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
extern int errno;
int main()
{
    FILE *fp;
    fp=fopen(“test.txt”,”r”);
    /* Checking for error */
    if(fp == NULL)
    {
        fprintf(stderr,”Error Number : %d”, errno);
        perror(“Error Description “);
        fprintf(stderr,”Exiting due to error….”);

        /* Exiting program due to error */
        exit(-1);
    }
    else
    {
        fclose(fp);
    }
}
Error Number : 2
Error Description : No such file or directory
Exiting due to error….. 
Previous: C file handling
Next: C header files

Chapter-24 C file haning in hindi C programming

सितंबर 11, 2019 0
Chapter-24 C file haning in hindi C programming
  • Introduction to C file handling in Hindi 
  • Opening & closing a file using C language in Hindi
  • Reading & writing from a file using C language in Hindi

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 कर सकते है और नयी files create कर सकते है। 
  2. किसी भी program के output को file में store कर सकते है। 
  3. File के द्वारा किसी भी program में input भी दे सकते है।       
  4. सभी operations perform होने के बाद files को close कर सकते है।      

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 



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 में 2 arguments pass किये जाते है।
FILE *fopen(const char * file_name, const char * mode );


एक तो hard disk पर file का address और दूसरा वह mode जिसमें आप file open करना चाहते है। ये function FILE type का एक object return करता है। इसलिए इसका return type FILE होता है।
और इसीलिए आप इस function के द्वारा return किये गए object को FILE type के दूसरे object में store करेंगे।

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 pointer variable to store file location */
    FILE *fp;
    char data[1];

    /* Opening input.txt in read mode and assigning to file pointer variable */
    fp = fopen(“input.txt”,”r”);

    /*  Reading from file using fgetc() and storing data in data array */
    data=fgetc(fp);
    printf(“%s”,data[]);

    /* Closing file stream */
    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 pointer declaration */
    FILE *fp;
    char data[100];

    /* Opening file with read permission */
    fp=fopen(“test.txt”,”r”);

    /* Reading file using fgets() and assigning to data array */ 
    fgets(data,100,fp);
    printf(“%s”,data);
    fclose(fp);

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

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 <stdio.h>
int main()
{
    FILE *fp;

    /* Opening file with read and write permissions */
    fp = fopen(“test.txt”,”w+”);
    /* Writing to file */
    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 को यूज़ करते है। 
int fclose(FILE *fp);
उपर दिए गए function में *fp वो FILE type object है जिसमे आपने fopen() function के द्वारा return किये गए object को store किया था।

Previous: C console I/o
Next:  C error handling

Chapter -23 Console I/O in hindi C programming

सितंबर 11, 2019 0
Chapter -23 Console I/O in hindi C programming
  • Introduction to C console I/O in Hindi
  • Different C console I/O functions in Hindi

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 Functions 

Console के माध्यम से input output perform करने के लिए C language आपको built in functions provide करती है। इनमें से कुछ के बारे में आप पहले भी पढ़ चुके है। निचे इनके बारे में उदाहरण सहित समझाया जा रहा है। 

scanf() 

scanf() एक input function है। ये user से keyboard के माध्यम से input लेने के लिए use किया जाता है। इसके माध्यम से आप user से सभी तरह के primitive types का data input के रूप में ले सकते है। इस function का general syntax निचे दिया जा रहा है।
scanf(“%d”,&variable-name); 
ऊपर दिए गए syntax में %d एक format specifier है जो compiler को बताता है की input के रूप में integer value ली जायेगी। हर type के लिए C language में पहले से ही एक format specifier define किया गया है। कुछ common format specifiers की list निचे दी जा रही है। 
  1. %d – जब आप किसी integer variable में value store करना चाहते है तो इस format specifier को यूज़ करते है।  
  2. %c – किसी character variable में value store करने के लिए आप %c format specifier को use करते है।  
  3. %f – Float variables में values store करने के लिए आप %f format specifier यूज़ करते है।  
  4. %s – Strings के लिए आप %s format specifier यूज़ करते है।    
  5. %u – Unsigned integers के लिए आप इस specifier को use करते है। 
  6. %ld – Long int को specify करने के लिए यह specifier use किया जाता है।  
& symbol memory के संदर्भ में यूज़ किया जाता है। इसे address of operator कहा जाता है। जब आप एक variable को initialize करते है तो उस variable के नाम से memory में space allot होता है। & के माध्यम से उस space को target किया जाता है। आइये अब scanf() function के use को एक उदाहरण के माध्यम से समझने का प्रयास करते है।
#include <stdio.h>
int main()
{
    int num;
    printf(“Enter a number:”);

    /* Taking input from user and storing in num using scanf() function */
    scanf(“%d”,&num);
    printf(“nYour entered number is: %d”,num);

    return 0;
}    
ऊपर दिया गया program निचे दिया गया output generate करता है।
Enter a number :
7
Your entered number is : 7 


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 कर सकते है। 
Variables की value को किसी निश्चित जगह पर print() करने के लिए आप format specifier भी यूज़ कर सकते है।   
इसका general syntax निचे दिया जा रहा है। 
printf(“string/variable”);
or
printf(“string format-specifier string”,variable-name)
printf() function के यूज़ को निचे उदाहरण के माध्यम से समझाया जा रहा है।
#include <stdio.h>
int main()
{
   int a=5;
   int b=4;
   int result;
 
   result = a+b;

   /* Printing all variable values using printf() function */
   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 <stdio.h>
int main()
{
   char data;
   printf(“Enter some characters. To exit enter @ symbol.”);
   while(data != ‘@’)
   {
 
       /* Taking character from user using keyboard */
       data = getchar();

/* Displaying character to screen */
       printf(“nYou 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 : @

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 : “);

   /* Reading string */
   gets(arr);
 
   printf(“nYou entered following string:”)

   /* Writing string */
   puts(arr);

   return 0;
}  
ऊपर दिया गया program निचे दिया गया output generate करता है। 
Enter a string :
Best Hindi Tutorials
You entered following string : Best Hindi Tutorials
Previous: Storage class
Next: C file handling