C के Inline Functions
In this page:
static inline return_type function_name(parameter_type parameter) {
return expression;
}
Inline Function क्या है?
एक function को inline mark करना compiler से एक request है कि हर call site पर एक normal function call generate करने की बजाय सीधे function का actual code substitute कर दे, जो simple getters या one-line calculations जैसे छोटे, frequently-called functions के लिए call overhead खत्म कर सकता है।
उदाहरण: What is an Inline Function?
// Include standard input/output (printf, scanf)
#include <stdio.h>
inline 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
inline Keyword
एक inline function declare करने के लिए, function definition में return type से पहले inline keyword लिखें; इसे static के साथ pair करना (static inline की तरह) एक header में छोटे inline helpers define करने का conventional तरीका है बिना उस header के कई files में include होने पर multiple-definition linker errors trigger किए।
उदाहरण: The inline Keyword
// Include standard input/output (printf, scanf)
#include <stdio.h>
static inline int cube(int n) {
// Send n * n * n back to the caller
return n * n * n;
}
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("%d", cube(3));
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Inline Functions बनाम Macros
एक preprocessor #define macro के उलट, जो types की किसी भी awareness के बिना pure blind text substitution करता है, एक inline function एक असली function है जिसे compiler normally type-check करता है, इसके लिए scoping rules follow करता है, और अभी भी एक debugger से step through किया जा सकता है — genuine safety के लिए एक macro की raw flexibility का थोड़ा हिस्सा trade करते हुए।
उदाहरण: Inline Functions vs. Macros
// Include standard input/output (printf, scanf)
#include <stdio.h>
static inline 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(1 + 2));
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Inline Functions कहाँ Define करें
क्योंकि compiler को हर उस point पर function की पूरी body देखनी होती है जहाँ यह inline हो सकता है, कई .c files में इस्तेमाल के लिए intended inline functions आमतौर पर एक अलग declaration-only header plus एक .c implementation file में बांटने की बजाय सीधे एक shared header के अंदर defined होते हैं।
उदाहरण: Where to Define Inline Functions
// Include standard input/output (printf, scanf)
#include <stdio.h>
static inline int add(int a, int b) {
// Send a + b back to the caller
return a + 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;
}
Login to try C/C++/Java/PHP code in the editor
Compiler Optimizations
inline keyword हमेशा compiler के लिए सिर्फ एक hint है, एक guaranteed instruction नहीं — compiler ऐसे function को inline करने के लिए free है जो inline mark नहीं है अगर इसे beneficial लगे, और equally किसी function पर inline hint ignore करने के लिए free है जिसे यह safely inline करने के लिए बहुत बड़ा या complex (जैसे एक loop या recursion वाला) समझे।
उदाहरण: Compiler Optimizations
// Include standard input/output (printf, scanf)
#include <stdio.h>
inline 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(4));
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- यह मान लेना कि
inlineinlining force करता है, जब यह compiler के लिए सिर्फ एक hint है। - एक inline function को सिर्फ एक
.cfile में define करना और इसे किसी दूसरी से call करना, जो link होने में fail हो सकता है। - type safety चाहिए होने पर इसकी बजाय एक macro इस्तेमाल करना।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: