C में Multi-file Programming
In this page:
// 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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
extern Keyword
extern keyword compiler को बताता है कि एक variable एक अलग source file में defined है और इसे एक बिल्कुल नए variable की तरह treat करने की बजाय उस existing definition से link होना चाहिए, जो एक global value को कई .c files में safely share होने देता है।
उदाहरण: The extern Keyword
// 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;
}
Login to try C/C++/Java/PHP code in the editor
कई 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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- एक global variable share करते समय
externभूल जाना, ताकि हर file को अपनी copy मिले या linker fail हो जाए। - same function को दो files में define करना, एक duplicate symbol error cause करते हुए।
- compile command में हर
.cfile list करना भूल जाना।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: