GATE CS 2024 Set 2 — Question 33
MSQ+1 / -0MediumPointers & Pointer ArithmeticC ProgrammingProgramming & Data StructuresArrays & Strings in C
Programming & Data Structures → C Programming → Arrays & Strings in C
Last updated
Question
Consider the following C function definition.Which of the following statements is/are TRUE?
int fX(char *a){
char *b = a;
while(*b)
b++;
return b - a;}
A.
The function call
fX("abcd") will always return a valueB.
Assuming a character array
c is declared as char c[] = "abcd" in main(), the function call fX(c) will always return a valueC.
The code of the function will not compile
D.
Assuming a character pointer
c is declared as char *c = "abcd" in main(), the function call fX(c) will always return a valueCorrect answer
(A) The function call fX("abcd") will always return a value; (B) Assuming a character array c is declared as char c[] = "abcd" in main(), the function call fX(c) will always return a value; (D) Assuming a character pointer c is declared as char c = "abcd" in main(), the function call fX(c) will always return a value
Solution
The function is a standard implementation of the string length function (
strlen). It operates as follows:1.It initializes a pointer to the start of the string .
2.It iterates through the memory using a
while loop until it encounters the null terminator (\0).3.It returns the difference between the final pointer position and the initial pointer position (), which corresponds to the number of characters in the string.
Evaluating the options:- Option (A):
"abcd"is a string literal. In C, string literals are stored in a read-only data segment and are automatically null-terminated. Passing this to is valid, and the function will return 4. Thus, it will always return a value. - Option (B):
char c[] = "abcd"declares a local character array and initializes it with the characters 'a', 'b', 'c', 'd', and '\0'. This is a valid null-terminated string. Passing it to will return 4. Thus, it will always return a value. - Option (C): The code is syntactically correct C. It uses valid pointer arithmetic and standard control flow. It will compile without errors.
- Option (D):
char *c = "abcd"makes point to the first character of the null-terminated string literal"abcd". Passing this pointer to will return 4. Thus, it will always return a value.
More questions on C Programming
2024 Set 2 Q13Consider the following C program. Assume parameters to a function are evaluated from right to left.…2024 Set 2 Q17Let be the adjacency matrix of a simple undirected graph . Suppose is its own inverse.…2024 Set 1 Q17Given an integer array of size , we want to check if the array is sorted (in either ascending or…2024 Set 1 Q18Consider the following C program: [code] Which one of the following statements is CORRECT?2024 Set 1 Q19Consider the following C program: [code] Assume that the input to the program from the command line…
Practice GATE CS PYQs with adaptive difficulty
Timed practice, skill tracking, and AI explanations — free to start.
Start practicing free