c programming

Basics of File Handling

File Handling

File Handling किसी भी Programming Language का एक महत्वपूर्ण Aspect है, जो Disk पर Data Manipulation और Storage की Permission देता है। C Programming में, File Handling Standard Library Function के एक Set के माध्यम से Implement की जाती है| जो File के साथ काम करने का एक Convenient Way Provide करती है।

File Handling Functions

No. Function Description
1 fopen() इस Function के द्वारा File को Open करना होता है|
2 fprintf() इस Function के द्वारा हम File में Data को Write कर सकते हैं|
3 fscanf() इसके द्वारा File से Data को Read किया जाता है|
4 fputc() इस Function का use करके File में एक Character को Write किया जाता है|
5 fgetc() यह File से एक Character को Read करता है|
6 fclose() इस Function के द्वारा File को Close किया जाता है|
7 fseek() यह दी गयी Position पर File Pointer को Set करता है|
8 fputw() यह File में Integer को Write करता है|
9 fgetw() यह File से Integer को Read करता है|
10 ftell() यह Current Position को Return करता है|
11 rewind() यह File की शुरुआत में File Pointer को Set करता है|

Opening or Creation of a New File 

C Language में File Create या Create हुए File को Open करने के लिए fopen() Function का use किया जाता है| fopen() Function, (stdio.h) Header File में Define है|

Syntax


FILE *fp;

fp = fopen(“file_name”,”mode”);

  • यहाँ fp एक FILE Pointer है, जो fopen() द्वारा Open किये जाने वाले File को Point कर रहा है|
  • FILE एक Structure है, जो File से Related Information जैसे –  Name, Size, Buffer Size, Current Position, End of File आदि Contain करता है |
  • file_name – यहाँ पर उस File का नाम आएगा जिसे हम Open करना चाहते है |

File Opening Modes

Mode Description
r यह Text File को Read Mode में Open करता है|
w यह Text File को Write Mode में Open करता है|
a यह Text File को Append Mode में Open करता है|
r+ यह Text File को Read और Write दोनों Modes में Open करता है|
w+ यह Text File को Read और Write दोनों Modes में Open करता है|
a+ यह Text File को Read और Write दोनों Modes में Open करता है|
rb यह Binary File को Read Mode में Open करता है|
wb यह Binary File को Write Mode में Open करता है|
ab यह Binary File को Append Mode में Open करता है|
rb+ यह Binary File को Read और Write दोनों Modes में Open करता है|
wb+ यह Binary File को Read और Write दोनों Modes में Open करता है|

Example 


#include <stdio.h>
int main()
{
FILE * fp;
if (fp = fopen(“hello.txt”, “r”))
{
printf(“File opened successfully in read mode”);
}
else
printf(“The file is not present! cannot create a new file using r mode”);
fclose(fp);
return 0;
}

Output


The file is not present! cannot create a new file using r mode

Closing a File

किसी भी File को Close करने के लिए fclose() Function का use किया जाता है| fclose() के द्वारा हम कसी भी File को आसानी से Close कर सकते है |

Syntax


fclose (file_pointer);

Reading from File

किसी File को Read करने के लिए सबसे पहले उस File को Read Mode “r”  में fopen Function द्वारा Open करना पड़ता है | File को Open करने के बाद File के अंदर के Data को Read करने के लिए C Language में हम कुछ Function का use करते है |

  •  fgetc(file_pointer) – यह File Pointer द्वारा Pointed File से अगला Character Return करता है। जब यह File के अंत तक Access किया जाता है, तब यह Function EOF (End of File) Return होता है।

Example


#include <stdio.h>
#include <stdlib.h>
int main()
{
/* Pointer to the file */
FILE *fp1;
/* Character variable to read the content of file */
char c;

/* Opening a file in r mode*/
fp1= fopen (“C:\\myprogram.txt”, “r”);

/* Infinite loop –I have used break to come out of the loop*/
while(1)
{
c = fgetc(fp1);
if(c==EOF)
break;
else
printf(“%c”, c);
}
fclose(fp1);
return 0;
}

  • fgets() – यह File से n-1 Character Read करता है, और String को एक Buffer में Store करता है| जिसमें NULL Character ‘\0’  Character के रूप में जोड़ा जाता है।

Syntax


fgets(buffer, n, file_pointer);

  • fscanf() – इसका use Data को Analyze करने के लिए किया जाता है। यह File से Characters को Read करता है| यह scanf Function की तरह Space तथा Newline आने पर Reading Close कर देता है |

Syntax


fscanf(file_pointer, conversion_specifiers, variable_adresses);

Example 


FILE *fp
fp = fopen (“file.txt”, “r”);
fscanf (fp, “%s %s %s %d”, str1, str2, str3, &date);

Writing to a File 

File के अंदर Writing Operation Perform करने के लिए सबसे पहले उस File को Write Mode “w” में Open करना होगा| यदि File को किसी दूसरे Mode जैसे “r” Mode में Open करते है, तो File में Writing Operation Perform नहीं कर सकते|

File को Write Mode “w” में Open करने पर यदि वो File पहले से Disk में Include नहीं है, तो fopen() Function एक New File Create करके उसे Open कर देगा और यदि File पहले से Include है, तो यह File में पहले से लिखे Data को Remove देगा और नए आने वाले Data को File में Enter कर देगा |

File Open होने के बाद File में Write करने के लिए fprintf, fputc और fputs Function का use किया जाता है |

Example 


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

int main()
{
char ch;
FILE *fp;
fp = fopen(“C:\\myprogram.txt”,”w”);

if(fp == NULL)
{
printf(“Error”);
exit(1);
}

printf(“Enter any character: “);
scanf(“%c”,&ch);

/* You can also use fputc(ch, fp);*/
fprintf(fp,”%c”,ch);
fclose(fp);

return 0;
}

c programming

Declaration of Structure

What is Structure

Structure एक Composite Data Type है, जो विभिन्न प्रकार के Multiple Variable को एक Entity में Combine करता है। Structure के प्रत्येक Variable को Member या Field कहा जाता है। ये Member किसी भी Data Type के हो सकते हैं, जिनमें int, Char, Float, Array, Pointer तथा अन्य Structure भी शामिल हैं। Structure आपको Specific Requirementsके अनुरूप User-Defined Data Type बनाने की Permission देती है, जो Complex Data को Manage करने के लिए एक Clean और Organized Approach Provide करती है।

How to Create a Structure

Structure बनाने के लिए ‘struct’ Keyword का use किया जाता है।

Example


struct address
{
char name[50];
char street[100];
char city[50];
char state[20];
int pin;
};

Declaration of Structures

C Programming में किसी Structure का use करने के लिए पहले इसे Declare करना होगा। किसी Structure को Declare करने का Syntax इस प्रकार है-

Syntax


struct structure_name {
data_type member1;
data_type member2;
// add more members as needed
};

Explanation

  • struct: यह C में एक Keyword है, जो Structure Declaration के Starting Point को Indicate करता है।
  • structure_name: यह New Data Type के लिए Identifier है। इसे Naming Convention का पालन करना चाहिए| और यह एक Keyword या Existing Identifier नहीं हो सकता है।
  • {}: Open और Close होने वाले Curly Brace Structure के Member को Enclose करता हैं।
  • data_type: Structure के प्रत्येक Member के पास एक Specific Data Type होना चाहिए| जैसे कि Int, Float, Char, या कोई अन्य Structure।

How to Initialize Structure Members

Structure Members को Declaration के साथ Initialized नहीं किया जा सकता है।

Example


struct Point
{
int x = 0; // COMPILER ERROR: cannot initialize members here
int y = 0; // COMPILER ERROR: cannot initialize members here
};

उपरोक्त Error का Reason Simple है, जब Datatype Declare किया जाता है, तो इसके लिए कोई Memory Allocate नहीं की जाती है। Memory तभी Allocate की जाती है, जब Variable बनाए जाते हैं। Structure के Member को Curly Braces ‘{}’ का use करके Start (Initalize) किया जा सकता है।

Example


struct Point
{
int x, y;
};

int main()
{
// A valid initialization. member x gets value 0 and y
// gets value 1. The order of declaration is followed.
struct Point p1 = {0, 1};
}

How to Access Structure Elements

Structure Members को dot (.) Operator का use करके Access किया जाता है।

Example


#include <stdio.h>

struct Point {
int x, y;
};

int main()
{
struct Point p1 = { 0, 1 };

// Accessing members of point p1
p1.x = 20;
printf(“x = %d, y = %d”, p1.x, p1.y);

return 0;
}

Output


x = 20, y = 1

What is an Array of Structures

यह User-Defined Data Structure के कई Instances का एक Collection है, जहां प्रत्येक Instance विभिन्न Related Field वाले एक Record या Entity को Represent करता है। Structure Developers को Logically रूप से Related Data को एक Entity में Group में करने की Permission देता है, और Array Aspect ऐसी कई Entities को Contiguous Memory Location में Store करने में सक्षम बनाता है।

Defining an Array of Structures

C Programming में Structure के एक Array को Define करने के लिए सबसे पहले User-Defined Structure (जिसे Structure के रूप में भी जाना जाता है) बनाने की आवश्यकता है, जो Data Record को Represent करती है। किसी Structure को Define करने का Syntax इस प्रकार है-

Syntax


struct structure_name {
data_type field1;
data_type field2;
// Add more fields as needed
};

एक बार Structure Define हो जाने पर उस Structure Type की एक Array Declare कर सकते हैं-

Syntax


struct structure_name array_name[array_size];

Example


#include <stdio.h>

struct Point {
int x, y;
};

int main()
{
// Create an array of structures
struct Point arr[10];

// Access array members
arr[0].x = 10;
arr[0].y = 20;

printf(“%d %d”, arr[0].x, arr[0].y);
return 0;
}

Output


10 20

What is a Structure Pointer

Strucute Pointer एक Pointer होता है, जो Structure Variable के Memory Address को Indicate करता है। यह Structure के Efficient Handling और Manipulation को सक्षम बनाता है| Structure Pointer Developers को उनके Variable Name तक Direct Access करने के बजाय उनके Memory Location को Refer करके Structure के साथ काम करने की Permission देते हैं।

Syntax


struct MyStruct {
// Define variables here
};

struct MyStruct *ptr; // Declaration of a structure pointer

Example


#include <stdio.h>

struct Point {
int x, y;
};

int main()
{
struct Point p1 = { 1, 2 };

// p2 is a pointer to structure p1
struct Point* p2 = &p1;

// Accessing structure members using structure pointer
printf(“%d %d”, p2->x, p2->y);
return 0;
}

Output


1 2

c programming

Static and Dynamic Memory Allocation

Static Memory Allocation

Static Memory Allocation Compile-Time पर Variable के लिए Memory Reserve (Allocate) करने की Process है। C में, Static Variable को “static” Keyword का use करके Declare किया जाता है, जो Compiler को Program के पूरे Lifetime के दौरान Variable के लिए Memory Allocation करने का Instructs देता है। Static Variable के लिए Memory को एक Special Region में Allocate किया जाता है, जिसे Program की Memory के “Data Segment” के रूप में जाना जाता है।

Static Memory Allocation में जब भी Program Execute होता है| तो वह उस Size को Fix कर देता है, जो Program Use करने वाला है, और इसे आगे Change नहीं किया जा सकता है। Memory Allocation और Deallocation Compiler द्वारा Automatically रूप से किया जाता है।। जब Memory Allocation, Compiler Time (या) Run Time से पहले किया जाता है, तो इसे Static Memory Allocation कहा जाता है।

Key Features

  • Memory Allocation और Deallocation Compiler द्वारा किया जाता है।
  • यह Static Memory Allocation के लिए Stack Data Structures का use करता है।
  • Variables Permanently रूप से Allocated किए जाते हैं।
  • Execution, Dynamic Memory Allocation से Fast होता है।
  • Memory Runtime से पहले Allocate की जाती है।

Example


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

int main()
{
int size;
printf(“Enter limit of the text: \n”);
scanf(“%d”, &size);
char str[size];
printf(“Enter some text: \n”);
scanf(” “);
gets(str);
printf(“Inputted text is: %s\n”, str);
return 0;
}

Output


Enter limit of the text:

Enter some text:

Inputted text is:

Advantages of Static Memory Allocation

  • Faster Access: Static Memory Allocation का  Access Time Fast होता है, क्योंकि Memory Compile-Time के दौरान Reserved होती है, जोकि Runtime Overhead से Prevent करती है।
  • Persistent Values: Variable Function Call में अपने Value बनाए रखते हैं, जिससे यह Multiple Call में State Maitain रखने के लिए use हो जाता है।

Disadvantages of Static Memory Allocation

  • Limited Flexibility: Static Variable के लिए Memory का Size Compile-Time पर Fix किया जाता है| और Runtime के दौरान इसे Change नहीं जा सकता है, यदि कुशलतापूर्वक उपयोग नहीं किया जाता है, तो संभावित रूप से Memory Wastage हो सकती है।
  • Not Thread-Safe: Shared Memory Access के कारण Static Variable, Multi-Threaded Environment में Issues Generate कर सकते हैं।

Dynamic Memory Allocation

Dynamic Memory Allocation, Program को Runtime के दौरान Memory Allocate करने की Permission देता है| जो Specialy तब use होता है, जब Memory Requirement, Unknown होती हैं| C Programming, Dynamic Memory Management के लिए Malloc, Calloc, Realloc और (stdlib.h) Library से Built-in Function Provide करता है।

Dynamic Memory Allocation में Size का Initialization और Allocation, Programmer द्वारा किया जाता है। यह Pointers के साथ Manage और Serve किया जाता है, जो उस Area में नए Allocated Memory Space को Point करता है| जिसे Heap कहते हैं। Heap Memory, Unorganized होती है, और इसे एक Resource के रूप में माना जाता है| जब Memory Allocation, Run Time या Execution Time के दौरान किया जाता है, तो इसे Dynamic Memory Allocation के रूप में जाना जाता है।

Key Features

  • Requirement के अनुसार Memory Size को फिर से Allocate/Deallocate भी कर सकते हैं।
  • Dynamic Allocation, Run Time पर किया जाता है।
  • इसमें Memory का Wastage कम होता है|

Function used for Dynamic Memory Allocation

Stdlib.h Header File में कुछ Function उपलब्ध हैं, जिनका उपयोग Memory को Dynamic रूप से Allocate करने के लिए किया जाता है| जोकि निम्नलिखित है –

  • Malloc() Function
  • Calloc() Function
  • Realloc() Function

Malloc() Function

malloc () Function का use Program Execution के दौरान Dynamically Memory Allocate करने के लिए किया जाता है | malloc () Function की मदद से Allocate की गई Dynamically Memory में By Default Garbage Value Stored होती है| जब malloc() Function Required Memory Allocate नहीं कर पता है, तब यह NULL Pointer Return करता है |

Syntax


ptr=(cast-type*)malloc(byte-size)

Example


#include<stdio.h>
#include<stdlib.h>
int main()
{
int n, i, *ptr;
printf(“Enter number of elements: “);
scanf(“%d”,&n);

//memory allocated using malloc
ptr=(int*)malloc(n*sizeof(int));
if(ptr==NULL)
{
printf(“Sorry! unable to allocate memory”);
exit(0);
}
else
{
printf(“Memory successfully allocated using malloc.\n”);
printf(“Enter elements of array: “);
for(i=0;i<n;++i)
{
scanf(“%d”,ptr+i);
}

}
free(ptr);

return 0;
}

Output


Enter number of elements: 5
Memory successfully allocated using malloc
Enter elements of array:
10
20
30
40
50

Calloc() Function

calloc() Function के द्वारा Memory के Multiple Blocks, Dynamically Allocate कर सकते है | इसका use Array और Structure जैसे Complex Data Structures को Memory Allocate करने के लिए किया जाता है | यह malloc() Function की ही तरह Dynamically Memory Allocate करता है, मगर calloc() का use Memory Space की Multiple Block Allocate करने के लिए किया जाता है, और malloc() का use Memory Space का Signal Block Allocate करने के लिए किया जाता है |

malloc() function के द्वारा Created Dynamically Memory में By Default Garbage Value है, जबकि calloc() के द्वारा Created Dynamically Memory में By Default शून्य Initialize होता है | यदि calloc() Function की मदद से Dynamic Memory Allocate करते समय Memory में पर्याप्त Space न हो तो यह Null Return करता है |

Syntax


ptr = (cast-type*)calloc(n, element-size);

Example


#include<stdio.h>
#include<stdlib.h>
int main()
{
int n, i, *ptr;
printf(“Enter number of elements: “);
scanf(“%d”,&n);

//memory allocated using calloc
ptr=(int*)calloc(n,sizeof(int));
if(ptr==NULL)
{
printf(“Sorry! unable to allocate memory”);
exit(0);
}
else
{
printf(“Memory successfully allocated using calloc.\n”);
}
free(ptr);
return 0;
}

Output


Enter number of elements: 5
Memory successfully allocated using calloc

realloc() function

malloc() or calloc() के द्वारा Created Dynamically Memory के Size को बदलने के लिए realloc() का use किया जाता है | realloc() Function के द्वारा आसानी से Dynamically Memory के size में बदलाव कर सकते है |

Syntax


ptr=realloc(ptr, new-size);

Example


#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,n,m,*ptr;

printf(“Enter Number of Elements : “);
scanf(“%d”,&n);

ptr=(int *)malloc(n*sizeof(int));

for(i=0;i<n;i++)
{
printf(“Enter Element : “);
scanf(“%d”,ptr+i);
}

printf(“Enter Number of Elements : “);
scanf(“%d”,&m);

ptr=(int *)realloc(ptr,m*sizeof(int));

for(i=n;i<m;i++)
{
printf(“Enter Element : “);
scanf(“%d”,ptr+i);
}

printf(“All Elements : “);
for(i=0;i<m;i++)
{
printf(“%d “,*(ptr+i));
}

free(ptr);

return 0;
}

Output


Enter Number of Elements : 3
Enter Element : 12
Enter Element : 34
Enter Element : 54
Enter Number of Elements : 5
Enter Element : 76
Enter Element : 22
All Elements : 12 34 54 76 22

Advantages of Dynamic Memory Allocation

  • Flexible Memory Usage: Runtime के दौरान आवश्यकतानुसार Memory Allocate की जाती है, जिससे Efficient रूप Usage करने की Permission मिलती है, और Wastages Reduce होता है।
  • Scalability: Dynamic Memory Allocation Linked Lists, Trees और Dynamic Arrays जैसी Data Structures के Build करने की सुविधा Provide करता है।
  • Better Thread Safety: चूंकि प्रत्येक Thread की अपनी Dynamically रूप से Allocated Memory हो सकती है, यह Multi-Threaded Environment में Data Corruption की संभावना को कम कर देता है।

Disadvantages of Dynamic Memory Allocation

  • Slower Access: Dynamic Memory Allocation में Runtime Overhead शामिल होता है, जो इसे Static Memory Allocation की तुलना में Slow बनाता है।
  • Potential Memory Leaks: यदि Free Space का use करके Memory को ठीक से Allocate नहीं किया जाता है, तो इससे Memory Leaks हो सकती है| और Program समय के साथ अत्यधिक Memory Consume कर सकता है।
c programming

Introduction to Pointers

What is Pointer

Pointers, C Programming Language में एक Fundamental Concept है, जो Efficient Memory Management और Manipulation के लिए एक Powerful Tool Provide करता है। C में Efficient और Optimize Code लिखने के लिए Pointer को समझना महत्वपूर्ण है।

Pointer एक Variable होता है, जो दूसरे Variable के Memory Address को Store करता है। Pointers, Actual Value रखने के बजाय उस Memory Location को “Point” करता है, जहां Value Stored है। Pointer, Memory Direct Access करने में सक्षम हैं, जिससे Efficient Memory Use हो सकता है| और यह Linked List और Dynamic Memory Allocation जैसी Complex Data Structure को भी Support करता है।

Declaring Pointers

C में एक Pointer Declare करने के लिए Variable Name से पहले Asterisk (*) Symbol का use करते हैं।

Syntax: 


data_type *pointer_name;

यहां, data_type उस Data के Type को Refer करता है, जिसे Pointer Point करेगा, और (pointer_name) Pointer Variable का नाम है।

Example


int *ptr;

Assigning Pointers

किसी Specific Memory Location पर Pointer Point बनाने के लिए, हम Existing Variable के साथ-साथ Operator (&) के Address का use करते हैं।

Syntax:


pointer_name = &variable_name;

Example


int num = 42;
int *ptr = &num;

Example Program


#include<stdio.h>
int main(){
int number=50;
int *p;
p=&number; 
printf(“Address of p variable is %x \n”,p);
printf(“Value of p variable is %d \n”,*p);   
return 0;
}

Output


Address of number variable is fff4
Address of p variable is fff4
Value of p variable is 50

NULL Pointers

यदि आपके पास Assign करने के लिए Exact Address नहीं है, तो Pointer Variable को NULL Value Assign करना हमेशा एक अच्छा Practice होता है। यह परिवर्तनशील Declaration के समय किया जाता है। एक Pointer जिसे NULL Assign किया गया है, उसे NULL Pointer कहा जाता है।

Example


#include <stdio.h>
int main(){
int *ptr = NULL;
printf
(“The value of ptr is : %x\n”, ptr);
return 0;
}

Output


The value of ptr is 0

Advantage of Pointer

Dynamic Memory Allocation

Pointer के सबसे Important Advantage में से एक Dynamic Memory Allocation को Facilitate बनाने की उनकी Ability है। C में, Variable के लिए Memory को Runtime के दौरान malloc(), calloc(), या realloc() जैसे Function का use करके Dynamically Allocate किया जा सकता है। यह Feature, Compile Time पर उनके Size को Predefine करने की आवश्यकता के बिना Linked Lists, Tree और Dynamic Array जैसे Data Structure के Creation को Enable बनाती है।

Efficient Memory Management

Pointer, Developers को Memory को Efficiently Manage करने की Permission देते हैं। Pointers आवश्यकतानुसार Memory को Dynamically रूप से Allocate और Deallocate कर सकते हैं| Memory Wastages को Reduce कर सकते हैं, और System Resource का बेहतर use कर सकते हैं। इसके अतिरिक्त, Pointer Variable के बीच Data की Copy बनाने के Overhead के बिना Efficient Data Manipulation को सक्षम करते हैं।

c programming

Introduction of Strings

Introduction of String

C Programming में String, Character का एक Sequence है| जो Null Character ‘\0’ के साथ End होता है। String को Character की एक Array के रूप में Define किया गया है। एक Character Array और एक String के बीच का अंतर यह है, कि String को एक Unique Character ‘\0’ के साथ End किया जाता है।

Declaration of String

एक String को Declare करना उतना ही Simple है, जितना कि One-Dimensional Array Declare करना। String को Declare करने के लिए निम्नलिखित Basic Syntax का प्रयोग किया जाता है।

Syntax


char str_name[size];

Syntax में (str_name) Variable का नाम है, तथा String की Length को define करने के लिए size का use किया जाता है|

Functions of String

String Function Description
strcat एक String से दूसरे String को जोड़ा जाता है|
strchr String Array के पहले Character Occurrence के Pointer को Return करता है|
strcmp दो String को Compare करने के लिए प्रयोग किया जाता है| ये Case-Sensetive होते है|
strcmpi दो String को Compare करने के लिए प्रयोग किया जाता है| ये Case-Sensetive नहीं होते है|
strcpy एक String को दूसरे String में Copy करता है |
strdup String का Duplicate Create करता है |
strlen String की Length निकालने के लिए प्रयोग किया जाता है|
strlwr Uppercase के Characters को Lowercase में Convert करता है |
strncat एक String से दूसरे String में Character को Combine करने के लिए Use किया जाता है।
strncpy Character को एक String से दूसरे String में Copy करता है |
strnset किसी String के पहले (n) Characters को किसी दिए गए Characters पर Set करने के लिए Use किया जाता है।
strrchr  किसी String में किसी Characters की Last Occurance का पता लगाता है।
strrev String को Reverse Order में Print करता है |
strrstr इसका उपयोग String के Sub-String की First Occurance को Find करने के लिए किया जाता है।
strset दिए हुए Character से पूरे String को Replace करता है |
strstr इसका उपयोग String के Sub-String की First Occurance को Find करने के लिए किया जाता है।
strupr Lowercase के Characters को Uppercase में Convert करने के लिए Use किया जाता है।

Example


#include<stdio.h>
#include<string.h>
int main(){
   char str1[12]="Hello";
   char str2[12]="World";
   char str3[12];
   int len;

   /* copy str1 into str3 */
   strcpy(str3, str1);
   printf("strcpy(str3, str1) :  %s\n", str3 );

   /* concatenates str1 and str2 */
   strcat( str1, str2);
   printf("strcat(str1, str2):   %s\n", str1 );

   /* total lenghth of str1 after concatenation */
   len = strlen(str1);
   printf("strlen(str1):  %d\n", len );
   return 0;
}

Output


strcpy(str3, str1) :  Hello
strcat(str1, str2) :   HelloWorld
strlen(str1) :  10
c programming

Introduction to Arrays

Introduction of Arrays

C Programming में Arrays, Element का एक Collection होता है| जिसमे Same Data Type के Element एक Variable Name में Grouped होते है। इन Element को Contiguous Memory Location में Store किया जाता है, जिससे इन्हें Access और Manipulate करना Efficient हो जाता है।

Array एक ही Type के Data के बड़े Set, जैसे – Integers, Floating-Point Number या Character के साथ काम करने का एक Convenient Way Provide करते हैं। Array का Index Number Zero से शुरू होता है| यदि Array का Size (n) है, तो आखिरी Index Number (n-1) होगा|

Declaration of Array 


data_type array_Name[arraySize] ;

Example


int num[10];
int num[10];

इसमें int एक Data Type है| जो दर्शाता है, कि इस Array में केवल Interger Type के Data को Store कर सकते है| और num उस Array का नाम है, जिसके द्वारा उस Array को Access करेंगे| तथा Bracket में लिखा 10 Array के Length को दर्शाता है, ये बताता है कि num Array में (10) variable है |

Length of Array

Method 1 – Using sizeof Function : Array का Length Find करने के लिए sizeof() Function का use किया जा सकता है।

Example


#include <iostream>
using namespace std;
int main() {
   int arr[5] = {4, 1, 8, 2, 9};
   int len = sizeof(arr)/sizeof(arr[0]);
   cout << "The length of the array is: " << len;
   return 0;
}

Output


The length of the array is: 5

Method 2 – Using Pointers : Pointer Arithmetic का use किसी Array का Length Find करने के लिए किया जा सकता है|

Example


#include <iostream>
using namespace std;
int main() {
   int arr[5] = {5, 8, 1, 3, 6};
   int len = *(&arr + 1) - arr;
   cout << "The length of the array is: " << len;
   return 0;
}

Output


The length of the array is: 5

Manipulating Array Elements

Example


#include <stdio.h>
int main(){
   int n[10]; /* n is an array of 10 integers */
   int i,j;
   /* initialize elements of array n to 0 */         
   for(i=0; i<10; i++){
      n[i] = i+100; /* set element at location i to i + 100 */
   }
   /* output each array element's value */
   for(j=0; j<10; j++){
      printf("Element[%d] = %d\n", j, n[j] );
   }
return 0;
}

Output


Element[0] = 100
Element[1] = 101
Element[2] = 102
Element[3] = 103
Element[4] = 104
Element[5] = 105
Element[6] = 106
Element[7] = 107
Element[8] = 108
Element[9] = 109

Type of Array

Single Dimensional Array

C Programming Language में Single Dimensional Array का use Same Data Type के Values की List को Store करने के लिए किया जाता है। अर्थात, Values की एक Row को Store करने के लिए Single Dimensional Array का use किया जाता है। Single Dimensional Array में Data Linear Form में Store होता है। Single Dimensional Array को One-Dimensional Arrays, Linear Array या केवल 1-D Array भी कहा जाता है।

Syntax –


datatype arrayName [size] ;

Example –


#include <stdio.h>

int main() {
int arr[5] = {7, 8};

for (int i = 0; i < 2; i++) {
printf(“%d “, arr[i]);
}
return 0;
}

Output


7 8

Two-Dimensional Array

Two-Dimensional Array एक Multidimensional Array का एक विशेष रूप है, जिसमें Two Dimension होते हैं। Data Structure में Matrix का Representation करने के लिए C Programming में सभी प्रकार की Arrays में इसका व्यापक रूप से use किया जाता है।

Syntax –


data_type array_name[sizeof_1st_dimension (row)][sizeof_2nd_dimension(column)];

Example –


#include <stdio.h>
void main()
{
int score[3][2]= {10,20,30,40,50,60};
int i,j;
for(i=0;i<3;i++)
{
printf(“\n”);
for(j=0;j<2;j++)
printf(“%d\t”,score[i][j]);
}
}

Output


10      20
30      40
50      60

Multi-Dimensional Array

Multidimensional Array, Single-Dimensional Array का एक Advanced Version है, जिसे Multilevel तक Nested किया जा सकता है।

Syntax –


data_type array_name[sizeof_1st_dimension][sizeof_2nd_dimension]…..[sizeof_nth_dimension];

Example


#include <stdio.h>
void main()
{
int arr[3][3][3],i,j,k;
printf(“\n Enter the elements for the array:”);
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
for(k=0;k<2;k++)
{
printf(“\n array [%d][%d][%d] = “,i,j,k);
scanf(“%d”,&arr[i][j][k]);
}
}

}
printf(“\n The matrix is:”);
for(i=0;i<2;i++)
{
printf(“\n\n”);
for(j=0;j<2;j++)
{
printf(“\n”);
for(k=0;k<2;k++)
printf(“\t array[%d][%d][%d]=%d”,i,j,k, arr[i][j][k]);
}
}
}

Output


Enter the elements for the array: 10,20,30,40,50,60,70,80
array [0][0][0] = 10
array [0][0][1] = 20
array [0][1][0] = 30
array [0][1][1] = 40
array [1][0][0] = 50
array [1][0][1] = 60
array [1][1][0] = 70
array [1][1][1] = 80