Tuesday, August 14, 2018

C Questions And Answers – 2018C14

There are many commonly asked questions regarding C programming language. Below are some collected such question-answer examples. The questions are usually related with 32-bit system, Turbo C IDE in windows or GCC under Linux environment [not always].

For more such examples, click C_Q&A label.

Predict the output or error(s) for the following:

 

-------------------------------------------------------------------------------------------------

 

main()

{

char *p = “ayqm”;

printf(“%c”,++*(p++));

}

Answer:

                   b

 

-------------------------------------------------------------------------------------------------

 

main()

{

int i=5;

printf("%d",++i++);

}

 

Answer:

                   Compiler error: Lvalue required in function main

 

Explanation:

                   ++i yields an rvalue. For postfix ++ to operate an lvalue is required.

 

-------------------------------------------------------------------------------------------------

 

main()

{

char *p = “ayqm”;

char c;

c = ++*p++;

printf(“%c”,c);

}

 

Answer:

                   b

 

Explanation:

There is no difference between the expression ++*(p++) and ++*p++. Parenthesis just works as a visual clue for the reader to see which expression is first evaluated.

 

-------------------------------------------------------------------------------------------------

 

int aaa() {printf(“Hi”);}

int bbb(){printf(“hello”);}

iny ccc(){printf(“bye”);}

main()

{

int ( * ptr[3]) ();

ptr[0] = aaa;

ptr[1] = bbb;

ptr[2] =ccc;

ptr[2]();

 

}

 

Answer:

                   bye

 

Explanation:

int (* ptr[3])() says that ptr is an array of pointers to functions that takes no arguments and returns the type int. By the assignment ptr[0] = aaa; it means that the first function pointer in the array is initialized with the address of the function aaa.

 

Similarly, the other two array elements also get initialized with the addresses of the functions bbb and ccc. Since ptr[2] contains the address of the function ccc, the call to the function ptr[2]() is same as calling ccc(). So it results in printing "bye".

 

-------------------------------------------------------------------------------------------------

 

main()

{

int i=5;

printf(“%d”,i=++i ==6);

}

 

Answer:

                   1

 

Explanation:

The expression can be treated as i = (++i==6), because == is of higher precedence than = operator. In the inner expression, ++i is equal to 6 yielding true(1). Hence the result.

 

-------------------------------------------------------------------------------------------------

 

main()

{

char p[ ]="%d\n";

p[1] = 'c';

printf(p,65);

}

 

Answer:

                   A

 

Explanation:

Due to the assignment p[1] = ‘c’ the string becomes, “%c\n”. Since this string becomes the format string for printf and ASCII value of 65 is ‘A’, the same gets printed.

 

-------------------------------------------------------------------------------------------------

 

                   void ( * abc( int, void ( *def) () ) ) ();

 

Answer::

abc is a ptr to a function which takes 2 parameters .(a). an integer variable.(b). a ptrto a funtion which returns void. The return type of the function is void.

 

Explanation:

Apply the clock-wise rule to find the result.

 

-------------------------------------------------------------------------------------------------

 

main()

{

while (strcmp(“some”,”some\0”))

 

printf(“Strings are not equal\n”);

}

 

Answer:

                   No output

 

Explanation:

Ending the string constant with \0 explicitly makes no difference. So “some” and “some\0” are equivalent. So, strcmp returns 0 (false) hence breaking out of the while loop.

 

-------------------------------------------------------------------------------------------------

 

main()

{

char str1[] = {‘s’,’o’,’m’,’e’};

char str2[] = {‘s’,’o’,’m’,’e’,’\0’};

while (strcmp(str1,str2))

        printf(“Strings are not equal\n”);

}

 

Answer:

“Strings are not equal”

“Strings are not equal”

….

 

Explanation:

If a string constant is initialized explicitly with characters, ‘\0’ is not appended automatically to the string. Since str1 doesn’t have null termination, it treats whatever the values that are in the following positions as part of the string until it randomly reaches a ‘\0’. So str1 and str2 are not the same, hence the result.

 

-------------------------------------------------------------------------------------------------

 

main()

{

int i = 3;

for (;i++=0;) printf(“%d”,i);

}

 

Answer:

                   Compiler Error: Lvalue required.

 

Explanation:

As we know that increment operators return rvalues and hence it cannot appear on the left hand side of an assignment operation.

 

-------------------------------------------------------------------------------------------------

 

void main()

{

int *mptr, *cptr;

mptr = (int*)malloc(sizeof(int));

printf(“%d”,*mptr);

int *cptr = (int*)calloc(sizeof(int),1);

printf(“%d”,*cptr);

}

 

Answer:

                   garbage-value 0

 

Explanation:

The memory space allocated by malloc is uninitialized, whereas calloc returns the allocated memory space initialized to zeros.

 

-------------------------------------------------------------------------------------------------

 

void main()

{

static int i;

while(i<=10)

        (i>2)?i++:i--;

printf(“%d”, i);

}

 

Answer:

                   32767

 

Explanation:

Since i is static it is initialized to 0. Inside the while loop the conditional operator evaluates to false, executing i--. This continues till the integer value rotates to positive value (32767).

The while condition becomes false and hence, comes out of the while loop, printing the i value.

 

-------------------------------------------------------------------------------------------------

 

…till next post, bye-bye & take care.

No comments:

Post a Comment