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

C के Interview Questions

Interview questions C jobs के लिए apply करते समय पूछे जाने वाले classic questions हैं। इन्हें practice करना एक बड़े game से पहले rehearse करने जैसा है।

Local बनाम Global Variables

एक local variable एक function के अंदर declared होता है और सिर्फ उस function के execution की duration के लिए exist करता है, function के return होते ही गायब हो जाता है, जबकि एक global variable किसी भी function के बाहर declared होता है और program की पूरी lifetime के लिए persist करता है, हर उस function को visible जो इसे shadow नहीं करता।

उदाहरण: Local vs. Global Variables

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Declare globalVar and set it to 100
int globalVar = 100;
void show() {
	// Declare localVar and set it to 5
	int localVar = 5;
	// Print formatted text to the screen
	printf("%d %d", localVar, globalVar);
}
// Program execution starts in main()
int main() {
	show();
	// Return 0 to signal that the program finished successfully
	return 0;
}

Value बनाम Reference Parameters

Value से Pass करना एक variable का actual data function के parameter में copy करता है, इसलिए function के अंदर किए changes कभी caller के original variable को affect नहीं करते, जबकि reference से pass करना (C में एक pointer से achieved) variable का address pass करता है, function को उस pointer के जरिए सीधे original data modify करने देते हुए।

उदाहरण: Value vs. Reference Parameters

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
void byValue(int x) { x = 100; }
void byRef(int *x) { *x = 100; }
// Program execution starts in main()
int main() {
	// Declare a and set it to 5
	int a = 5;
	byValue(a);
	byRef(&a);
	// Print formatted text to the screen
	printf("%d", a);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Structure बनाम Union

एक struct अपने हर member के लिए अलग, independent memory allocate करता है, इसलिए सारे fields एक-दूसरे को affect किए बिना simultaneously पढ़े या लिखे जा सकते हैं, जबकि एक union अपने सबसे बड़े member के लिए sized memory का एक single shared block allocate करता है, मतलब किसी भी समय सिर्फ एक member valid value रखता है।

उदाहरण: Structure vs. Union

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define a structure type named S
struct S { int a; int b; };
union U { int a; int b; };
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%zu %zu", sizeof(struct S), sizeof(union U));
	// Return 0 to signal that the program finished successfully
	return 0;
}

gets() Dangerous क्यों है?

gets() एक buffer में input की एक line पढ़ता है बिना यह limit करने के किसी भी तरीके के कि यह कितने characters लिखता है, इसलिए buffer से लंबा कोई भी input चुपचाप adjacent memory overwrite करता है — एक classic buffer overflow vulnerability।

इसे बिल्कुल इसी कारण C standard से formally हटाया गया था; fgets() safe replacement है।

उदाहरण: Why is gets() Dangerous?

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	char buffer[10];
	// Print formatted text to the screen
	printf("Use fgets() instead of gets()");
	// Return 0 to signal that the program finished successfully
	return 0;
}

Null Pointer क्या है?

एक null pointer explicitly value NULL पर set किया गया एक pointer है यह indicate करने के लिए कि यह currently किसी valid object की ओर point नहीं करता, जो 'अभी कोई target नहीं' represent करने का conventional तरीका है और code को dereference करने से पहले if (ptr != NULL) check करने देता है एक uninitialized या already-freed pointer पर crash होने से बचने के लिए।

उदाहरण: What is a Null Pointer?

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int *ptr = NULL;
	// Print formatted text to the screen
	printf("%d", ptr == NULL);
	// 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. input के लिए gets() इस्तेमाल करना, जो unsafe है क्योंकि इसकी कोई size limit नहीं।
  2. strings को strcmp की बजाय == से compare करना।
  3. जवाब देते समय call by value और call by reference मिला देना।
चैप्टर सारांश
  • typedef, type casting, bit fields, variable length arrays, और command line arguments जैसे features C programs जो कर सकते हैं उसे extend करते हैं।
  • Function pointers और callbacks आपको behavior pass around करने देते हैं, और standard headers strings, math, time, और characters के लिए ready-made functions देते हैं।
  • errno और assert के साथ error handling, debugging, code style, और common mistakes के साथ, आपको reliable code लिखने में मदद करते हैं।

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.