C की Code Style और Best Practices
In this page:
Clean Indentation
Consistent indentation का इस पर zero असर होता है कि compiler आपका code कैसे interpret करता है, लेकिन इसका एक बड़ा असर होता है कि कोई human — आपका future self समेत — एक नज़र में nested blocks, matching braces, और control flow कितनी जल्दी visually parse कर सकता है।
उदाहरण: Clean Indentation
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Check whether 1
if (1) {
// Print formatted text to the screen
printf("Properly indented block");
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Meaningful Variable Names
x या ts की बजाय totalScore जैसे descriptive variable names चुनना type करने में लगभग कुछ खर्च नहीं करता लेकिन बाद में real time बचाता है, क्योंकि नाम खुद बिना किसी comment की ज़रूरत के document करता है कि variable क्या रखता है।
Loop counters (i, j, k) जैसे tight, conventional contexts के लिए short single-letter names अभी भी ठीक हैं।
उदाहरण: Meaningful Variable Names
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare totalScore and set it to 95
int totalScore = 95;
// Print formatted text to the screen
printf("%d", totalScore);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Constants को Centralize करना
86400 या 3.14159 जैसे unexplained numeric literals सीधे अपनी logic में बिखेरना code को समझना और safely बदलना मुश्किल बनाता है; हर एक को #define या एक const variable से एक नाम देना एक mystery number को self-documenting code में बदल देता है और मतलब है आपको इसे सिर्फ एक जगह update करना है।
उदाहरण: Centralizing Constants
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define the macro SECONDS_PER_DAY (replaced by the preprocessor)
#define SECONDS_PER_DAY 86400
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("%d", SECONDS_PER_DAY);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Function Modularity
हर function को एक single, well-defined task पर focused रखना इसे test करना आसान, कहीं और reuse करना आसान, और isolation में इसके बारे में reason करना आसान बनाता है, जबकि पांच unrelated काम करने वाला एक विशाल function बढ़ने के साथ safely modify करना progressively मुश्किल हो जाता है।
उदाहरण: Function Modularity
// 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(4));
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Magic Numbers से बचना
एक magic number कोई भी unexplained literal value है जो सीधे आपके code में embedded है जिसका मतलब context से obvious नहीं है, और इसे एक named constant से replace करना (जैसे bare 3 की बजाय MAX_ATTEMPTS) बाद में code पढ़ने वाले किसी को भी उस value की intent clear कर देता है।
उदाहरण: Avoiding Magic Numbers
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define the macro MAX_ATTEMPTS (replaced by the preprocessor)
#define MAX_ATTEMPTS 3
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("%d", MAX_ATTEMPTS);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- inconsistent indentation इस्तेमाल करना ताकि block structure देखना मुश्किल हो।
- important values के लिए
xऔरtmpजैसे vague names इस्तेमाल करना। constया#defineसे नाम देने की बजाय कई जगहों पर magic numbers दोहराना।
Chapter Quiz — Complete all 20 topics to unlock
0/20 topics done
Complete these topics first:
- C typedef
- C Type Casting
- C Bit Fields
- C Variable Length Arrays
- C Command Line Arguments
- C Function Pointers
- C Callback Functions
- C Multidimensional Pointer
- C string.h Functions
- C stdlib.h Functions
- C math.h Functions
- C time.h Functions
- C ctype.h Functions
- C errno.h
- C assert.h
- C Error Handling
- C Debugging Techniques
- C Code Style & Best Practices
- C Common Mistakes
- C Interview Questions