B.Tech Students: Apply for Live Programming Internship C, C++, Java, Python ,Web page Designing, PHP Mostly Asked C Programming Questions - Functions & Arrays | LiveCodeProgramming

Mostly Asked C Programming Questions – Functions, Arrays & Strings

Q1. What is a function in C?C में फ़ंक्शन क्या है?

A function is a block of code designed to perform a specific task. It helps in code reuse and modular programming. Functions have a name, return type, and may have parameters.
Example:

int add(int a, int b) {
  return a + b;
}

फ़ंक्शन कोड का एक ब्लॉक होता है जो कोई विशेष कार्य करता है। यह कोड पुनः उपयोग और मॉड्यूलर प्रोग्रामिंग में मदद करता है। फ़ंक्शन का नाम, रिटर्न टाइप और पैरामीटर हो सकते हैं।
उदाहरण:

int add(int a, int b) {
  return a + b;
}

Q2. Explain call by value vs call by reference.Call by value और call by reference समझाएं।

Call by value passes a copy of the argument to the function. Changes inside the function do not affect the original variable.
Call by reference passes the address of the variable, allowing the function to modify the original variable.
Example:

void func(int x) { x = 10; } // call by value
void func(int *p) { *p = 10; } // call by reference

Call by value में तर्क की एक प्रति फ़ंक्शन को भेजी जाती है। फ़ंक्शन के अंदर बदलाव मूल वेरिएबल को प्रभावित नहीं करते।
Call by reference में वेरिएबल का पता भेजा जाता है, जिससे फ़ंक्शन मूल वेरिएबल को बदल सकता है।
उदाहरण:

void func(int x) { x = 10; } // call by value
void func(int *p) { *p = 10; } // call by reference

Q3. What is recursion? Give example.रिकर्शन क्या है? उदाहरण दें।

Recursion is a process where a function calls itself to solve smaller instances of a problem. It must have a base case to stop.
Example: factorial using recursion:

int fact(int n) {
  if(n <= 1) return 1;
  else return n * fact(n-1);
}

रिकर्शन वह प्रक्रिया है जिसमें कोई फ़ंक्शन स्वयं को छोटे हिस्सों को हल करने के लिए कॉल करता है। इसमें बंद होने के लिए बेस केस होना जरूरी है।
उदाहरण: फेक्टोरियल रिकर्शन का उपयोग करते हुए:

int fact(int n) {
  if(n <= 1) return 1;
  else return n * fact(n-1);
}

Q4. What is the use of return statement?return स्टेटमेंट का उपयोग क्या है?

The return statement exits a function and optionally returns a value to the caller. It ends function execution.
Example:

int square(int x) {
  return x * x;
}

return स्टेटमेंट फ़ंक्शन को समाप्त करता है और वैकल्पिक रूप से कॉलर को मान लौटाता है। यह फ़ंक्शन के निष्पादन को समाप्त करता है।
उदाहरण:

int square(int x) {
  return x * x;
}

Q5. What is a function prototype?फ़ंक्शन प्रोटोटाइप क्या है?

A function prototype declares a function's name, return type, and parameters before its definition. It informs the compiler about the function signature.
Example:

int add(int, int);

फ़ंक्शन प्रोटोटाइप फ़ंक्शन का नाम, रिटर्न टाइप और पैरामीटर इसकी परिभाषा से पहले घोषित करता है। यह कंपाइलर को फ़ंक्शन के बारे में जानकारी देता है।
उदाहरण:

int add(int, int);

Q6. How to declare and initialize arrays in C?C में एरे कैसे डिक्लेयर और इनिशियलाइज़ करें?

Arrays are collections of elements of the same type stored contiguously.
Declaration:

int arr[5];
Initialization:
int arr[5] = {1, 2, 3, 4, 5};

एरे समान प्रकार के तत्वों का समूह होता है जो लगातार स्टोर होते हैं।
घोषणा:

int arr[5];
प्रारंभिककरण:
int arr[5] = {1, 2, 3, 4, 5};

Q7. Difference between 1D and 2D arrays?1D और 2D एरे में क्या अंतर है?

1D array is a single list of elements, e.g., int arr[5].
2D array is an array of arrays (matrix), e.g., int arr[3][4] with 3 rows and 4 columns.
Access elements using two indices for 2D arrays.

1D एरे एकल सूची होती है, जैसे int arr[5]।
2D एरे एरे का एरे होता है (मैट्रिक्स), जैसे int arr[3][4] जिसमें 3 पंक्तियाँ और 4 स्तम्भ होते हैं।
2D एरे के तत्वों तक पहुँचने के लिए दो इंडेक्स का उपयोग होता है।

Q8. How are strings stored in C?C में स्ट्रिंग्स कैसे स्टोर होती हैं?

Strings in C are stored as arrays of characters terminated by a null character '\0'.
Example:

char str[] = "Hello"; // stored as {'H','e','l','l','o','\0'}

C में स्ट्रिंग्स अक्षरों के एरे के रूप में स्टोर होती हैं, जो '\0' (null character) से समाप्त होती हैं।
उदाहरण:

char str[] = "Hello"; // {'H','e','l','l','o','\0'}

Q9. Difference between character array and string in C?C में कैरैक्टर एरे और स्ट्रिंग में क्या अंतर है?

Character array is a simple array of characters which may or may not be null-terminated.
String is a character array that ends with a null character '\0', which C functions use to identify string end.

कैरैक्टर एरे अक्षरों का साधारण एरे होता है जो null-terminated हो सकता है या नहीं।
स्ट्रिंग वह कैरैक्टर एरे है जो '\0' से समाप्त होता है, जिसका उपयोग C फ़ंक्शन स्ट्रिंग के अंत को पहचानने के लिए करते हैं।

Q10. How to copy and compare strings without using strcpy()?strcpy() के बिना स्ट्रिंग कैसे कॉपी और तुलना करें?

You can copy strings manually using a loop:

for(int i = 0; src[i] != '\0'; i++) {
  dest[i] = src[i];
}
dest[i] = '\0';

To compare, check characters one by one:
while(src[i] != '\0' && dest[i] != '\0') {
  if(src[i] != dest[i]) break;
  i++;
}

आप लूप का उपयोग करके मैन्युअली स्ट्रिंग कॉपी कर सकते हैं:

for(int i = 0; src[i] != '\0'; i++) {
  dest[i] = src[i];
}
dest[i] = '\0';

तुलना करने के लिए, अक्षरों को एक-एक करके जांचें:
while(src[i] != '\0' && dest[i] != '\0') {
  if(src[i] != dest[i]) break;
  i++;
}