← Back to C Course | Chapter 12: Advanced Topics | Lesson 19 of 20

C की Common Mistakes

यह lesson उन slips को list करता है जो कई नए C learners करते हैं, common potholes के एक map की तरह। इन्हें पहले से जानना आपको इनसे बचने में मदद करता है।

Missing Semicolons

हर executable C statement को एक semicolon से खत्म होना चाहिए, और एक भी छोड़ना आमतौर पर actual missing semicolon की बजाय अगली line पर point करती एक compiler error produce करता है, जो शुरुआत में error की real वजह ढूंढना confusing बना सकता है।

उदाहरण: Missing Semicolons

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}

Infinite Loop Triggers

एक loop सिर्फ तब terminate होता है जब इसकी condition false हो जाए, इसलिए loop body के अंदर loop के controlling variable को update करना भूल जाना (जैसे एक for/while loop में i++ भूल जाना) उस condition को permanently true छोड़ता है और loop हमेशा के लिए चलता है।

उदाहरण: Infinite Loop Triggers

c
// 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;
}
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. scanf("%d", &x) में & भूल जाना, जो गलत argument pass करता है और crash करता है।
  2. एक uninitialized variable इस्तेमाल करना, जो एक garbage value रखता है।
  3. एक array के अंत से एक element आगे पढ़ना या लिखना।

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.