C के Interview Questions
In this page:
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Structure बनाम Union
एक struct अपने हर member के लिए अलग, independent memory allocate करता है, इसलिए सारे fields एक-दूसरे को affect किए बिना simultaneously पढ़े या लिखे जा सकते हैं, जबकि एक union अपने सबसे बड़े member के लिए sized memory का एक single shared block allocate करता है, मतलब किसी भी समय सिर्फ एक member valid value रखता है।
उदाहरण: Structure vs. Union
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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?
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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?
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- input के लिए
gets()इस्तेमाल करना, जो unsafe है क्योंकि इसकी कोई size limit नहीं। - strings को
strcmpकी बजाय==से compare करना। - जवाब देते समय 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 लिखने में मदद करते हैं।
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