Wednesday, August 15, 2018

C Questions And Answers – 2018C15

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()

{

int i=10,j=20;

j = i, j?(i,j)?i:j:j;

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

}

 

Answer:

                   10 10

 

Explanation:

The Ternary operator ( ? : ) is equivalent for if-then-else statement. So the question can be written as:

 

if(i,j)

{

if(i,j)

     j = i;

else

     j = j;

}

else

     j = j;

 

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

 

1. const char *a;

2. char* const a;

3. char const *a;

-Differentiate the above declarations.

 

Answer:

1. 'const' applies to char * rather than 'a' ( pointer to a constant char )

*a='F' : illegal

a="Hi" : legal

2. 'const' applies to 'a' rather than to the value of a (constant pointer to char )

*a='F' : legal

a="Hi" : illegal

3. Same as 1.

 

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

 

main()

{

int i=5,j=10;

i=i&=j&&10;

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

}

 

Answer:

                   1 10

 

Explanation:

The expression can be written as i=(i&=(j&&10)); The inner expression (j&&10) evaluates to 1 because j==10. i is 5. i = 5&1 is 1. Hence the result.

 

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

 

main()

{

int i=4,j=7;

j = j || i++ && printf("YOU CAN");

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

}

 

Answer:

                   4 1

 

Explanation:

The boolean expression needs to be evaluated only till the truth value of the expression is not known. j is not equal to zero itself means that the expression’s truth value is 1. Because it is followed by || and true || (anything) => true where (anything) will not be evaluated. So the remaining expression is not evaluated and so the value of i remains the same.

 

Similarly when && operator is involved in an expression, when any of the operands become false, the whole expression’s truth value becomes false and hence the remaining expression will not be evaluated.

false && (anything) => false where (anything) will not be evaluated.

 

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

 

main()

{

register int a=2;

printf("Address of a = %d",&a);

printf("Value of a = %d",a);

}

 

Answer:

                   Compier Error: '&' on register variable

 

Rule to Remember:

                   & (address of ) operator cannot be applied on register variables.

 

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

 

main()

{

float i=1.5;

switch(i)

{

case 1: printf("1");

case 2: printf("2");

default : printf("0");

}

}

 

Answer:

                   Compiler Error: switch expression not integral

 

Explanation:

                   Switch statements can be applied only to integral types.

 

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

 

main()

{

extern i;

printf("%d\n",i);

{

int i=20;

printf("%d\n",i);

}

}

 

Answer:

                   Linker Error: Unresolved external symbol i

 

Explanation:

The identifier i is available in the inner block and so using extern has no use in resolving it.

 

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

 

main()

{

int a=2,*f1,*f2;

f1=f2=&a;

*f2+=*f2+=a+=2.5;

printf("\n%d %d %d",a,*f1,*f2);

}

 

Answer:

                   16 16 16

 

Explanation:

f1 and f2 both refer to the same memory location a. So changes through f1 and f2 ultimately affects only the value of a.

 

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

 

main()

{

char *p="GOOD";

char a[ ]="GOOD";

printf("\n sizeof(p) = %d, sizeof(*p) = %d, strlen(p) = %d",

sizeof(p), sizeof(*p), strlen(p));

printf("\n sizeof(a) = %d, strlen(a) = %d", sizeof(a), strlen(a));

}

 

Answer:

 

sizeof(p) = 2, sizeof(*p) = 1, strlen(p) = 4

sizeof(a) = 5, strlen(a) = 4

 

Explanation:

sizeof(p) => sizeof(char*) => 2

sizeof(*p) => sizeof(char) => 1

Similarly,

                   sizeof(a) => size of the character array => 5

 

When sizeof operator is applied to an array it returns the sizeof the array and it is not the same as the sizeof the pointer variable. Here the sizeof(a) where a is the character array and the size of the array is 5 because the space necessary for the terminating NULL character should also be taken into account.

 

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

 

#define DIM( array, type) sizeof(array)/sizeof(type)

main()

{

int arr[10];

printf(“The dimension of the array is %d”, DIM(arr, int));

}

 

Answer:

                   10

 

Explanation:

The size of integer array of 10 elements is 10 * sizeof(int). The macro expands to sizeof(arr)/sizeof(int) => 10 * sizeof(int) /sizeof(int) => 10.

 

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

 

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

No comments:

Post a Comment