in Programming in C edited by
2,087 views
3 votes
3 votes

​​​Consider the following $\mathrm{C}$ function definition.

int f X(char * a) {

char * b =  a;

while (*b)

b ++;

return b - a; }

Which of the following statements is/are TRUE?

  1. The function call $\text{f X("a b c d''}$) will always return a value
  2. Assuming a character array $c$ is declared as char $c[ ]=$ "abcd" in main ( ), the function call $f X$ (c) will always return a value
  3. The code of the function will not compile
  4. Assuming a character pointer $\mathrm{C}$ is declared as char ${ }^{*} \mathrm{C}=$ "abcd" in main (), the function call $\mathrm{fX}(\mathrm{c})$ will always return a value
in Programming in C edited by
by
2.1k views

1 comment

And the return value would be 1 according to pointer arithmetic
0
0

1 Answer

3 votes
3 votes

Answer is A,B,D

We are modifying the local variable inside the function.

#include<stdio.h>

int fX(char *a) {

    char *b = a;

    while (*b)

        b++;

    return b - a;

}

int main(){

    

    int result_A = fX("abcd");

    printf("Option A: Result of fX(\"abcd\"): %d\n", result_A);

    

    char c[] = "abcd";

    int result_B = fX(c);

    printf("Option B: Result of fX(c): %d\n", result_B);

    char *ptr_c = "abcd";

    int result_D = fX(ptr_c);

    printf("Option D: Result of fX(ptr_c): %d\n", result_D);

    

}

Answer:

Related questions