← Back to C Course | Chapter 14: Additional Topics | Lesson 3 of 9

C में Multi-file Programming

Multi-file programming एक बड़े program को कई files में बांटता है, एक book के chapters की तरह। इन्हें अलग compile किया जाता है और अंत में साथ जोड़ा जाता है।
Syntax
c
// file.c
return_type function_name(parameter_type parameter) { /* definition */ }

// main.c
#include "file.h"
extern type variable_name;   // defined in another file

// gcc main.c file.c -o program

Code को Modularize करना

एक बढ़ते program को कई source files में बांटना हर file को एक responsibility area पर focused रखता है, पूरे codebase को navigate करना आसान बनाते हुए, isolation में test करना आसान, और एक से ज़्यादा व्यक्ति के एक साथ काम करने के लिए आसान।

उदाहरण: Modularizing Code

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
int square(int n) {
	// Send n * n back to the caller
	return n * n;
}
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d", square(5));
	// Return 0 to signal that the program finished successfully
	return 0;
}

extern Keyword

extern keyword compiler को बताता है कि एक variable एक अलग source file में defined है और इसे एक बिल्कुल नए variable की तरह treat करने की बजाय उस existing definition से link होना चाहिए, जो एक global value को कई .c files में safely share होने देता है।

उदाहरण: The extern Keyword

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Declare sharedCount and set it to 10
int sharedCount = 10;
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d", sharedCount);
	// Return 0 to signal that the program finished successfully
	return 0;
}

कई Files Compile करना

एक multi-file C program build करने का मतलब है हर relevant .c file को एक single command में compiler को pass करना, जैसे gcc main.c tools.c -o program, ताकि compiler हर file को अलग compile कर सके और linker फिर उनका object code एक executable में जोड़ सके।

उदाहरण: Compiling Multiple Files

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("Multiple .c files compiled into one program");
	// Return 0 to signal that the program finished successfully
	return 0;
}

Function Prototypes Share करना

एक function का prototype एक shared header file में रखना, फिर उस header को function define करने वाली file में और इसे call करने वाली हर file में include करना, compiler को function की actual implementation देखने की ज़रूरत के बिना हर call को उसके real signature से match verify करने देता है।

उदाहरण: Sharing Function Prototypes

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
int add(int a, int b);
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d", add(2, 3));
	// Return 0 to signal that the program finished successfully
	return 0;
}
int add(int a, int b) {
	// Send a + b back to the caller
	return a + b;
}

Multi-file Projects के Benefits

सिर्फ code organize करने से आगे, एक project को कई files में बांटना अलग contributors को merge conflicts के कहीं कम risk के साथ अलग files एक साथ edit करने देता है, और एक build system को हर बार पूरे project की बजाय सिर्फ actually बदली files recompile करने देता है।

उदाहरण: Benefits of Multi-file Projects

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
int square(int n) { return n * n; }
int cube(int n) { return n * n * n; }
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d %d", square(3), cube(3));
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक global variable share करते समय extern भूल जाना, ताकि हर file को अपनी copy मिले या linker fail हो जाए।
  2. same function को दो files में define करना, एक duplicate symbol error cause करते हुए।
  3. compile command में हर .c file list करना भूल जाना।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.