C की Common Mistakes
In this page:
Missing Semicolons
हर executable C statement को एक semicolon से खत्म होना चाहिए, और एक भी छोड़ना आमतौर पर actual missing semicolon की बजाय अगली line पर point करती एक compiler error produce करता है, जो शुरुआत में error की real वजह ढूंढना confusing बना सकता है।
उदाहरण: Missing Semicolons
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare x and set it to 5
int x = 5;
// Print formatted text to the screen
printf("%d", x);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Uninitialized Variables
कुछ languages के उलट, C local variables को automatically zero-initialize नहीं करता, इसलिए एक ताज़ा declared variable जो भी leftover bit pattern पहले से उस stack memory में बैठा था उसे रखता है।
Ecplicitly एक value assign करने से पहले इसे पढ़ना unpredictable, garbage results produce करता है।
उदाहरण: Uninitialized Variables
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare x and set it to 0
int x = 0;
// Print formatted text to the screen
printf("%d", x);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
scanf में Missing Ampersand (&)
scanf() को उस variable का memory address चाहिए जिसमें यह लिख रहा है, variable की value नहीं, इसलिए scanf("%d", age); (& missing) जैसी call age में store जो भी random address हो वहाँ लिखती है, अक्सर intended input पढ़ने की बजाय program crash करते हुए।
उदाहरण: Missing Ampersand (&) in scanf
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
int age;
sscanf("25", "%d", &age);
// Print formatted text to the screen
printf("%d", age);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Array Index Out of Bounds
C array access पर कोई automatic bounds checking नहीं करता, इसलिए एक array के declared size से आगे पढ़ना या लिखना कोई error नहीं उठाता — यह चुपचाप जो भी memory array के adjacent बैठी है उसे access करता है, जो unrelated variables corrupt कर सकता है या unpredictably program crash कर सकता है।
उदाहरण: Array Index Out of Bounds
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
int arr[5] = {1,2,3,4,5};
// Print formatted text to the screen
printf("%d", arr[4]);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Infinite Loop Triggers
एक loop सिर्फ तब terminate होता है जब इसकी condition false हो जाए, इसलिए loop body के अंदर loop के controlling variable को update करना भूल जाना (जैसे एक for/while loop में i++ भूल जाना) उस condition को permanently true छोड़ता है और loop हमेशा के लिए चलता है।
उदाहरण: Infinite Loop Triggers
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare i and set it to 0
int i = 0;
// Repeat while i < 5 is true
while (i < 5) {
// Print formatted text to the screen
printf("%d ", i);
i++;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
scanf("%d", &x)में&भूल जाना, जो गलत argument pass करता है और crash करता है।- एक uninitialized variable इस्तेमाल करना, जो एक garbage value रखता है।
- एक array के अंत से एक element आगे पढ़ना या लिखना।
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