AD1

Saturday, 17 June 2017

Circular Bit Shift

Write two functions RL(data,n) which performs circular left shift on variable data by n bits , RR(data,n) which performs circular right shift on variable data by n bits
note: data is 1 byte variable

refer to this video to know more about circular bit shifts




Solution

Saturday, 3 June 2017

Saturday, 27 May 2017

Reverse Two Arrays Of The Same Size

write a C program to reverse two array elements of the same size 
as example if we have int a[]={1,2,3,4},int b[]={5,6,7,8}
after reversing we would have int a[]={5,6,7,8},int b[]={1,2,3,4}

Saturday, 29 April 2017

Find the output of the following code


Answer

2147483647

Explanation

i is an uninitialized static variable so its default value is zero
also i automatically promoted to unsigned int (because we did not specify if its signed or unsigned)
then 0<=10 condition true 
inside while loop 0>2 false , i-- executed 
second iteration i becomes 2147483647 (int size on my machine is 4 bytes)
2147483647 <=10 false , while loop is bypassed and 2147483647 is printed.


Complicated Declaration

what does the following declaration mean 

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

Answer & Explanation 



Find the output of the following code


Answer
i

Explanation 

char c=++*p++
this statement will be parsed as follow 
1)p++ p which is a char pointer is post incremented so p still points to 'h'
2)*p++ p is dereferenced to get value 'h'
3)++'h' is i

note:this result is highly compiler dependent , some compilers may fail to compile this code and others may get you this output

advise : never write a code which seems to be ambiguous and compiler dependent




Tuesday, 25 April 2017

Find the output of the following code

Output
20
C is sea

Explanation
in this question I introduce the concept of preprocessor operators 
concat(x,y) x##y this operator concatenate x and y 
string(a) #a #operator convert any macro constant into string
so after preprocessing this code you will have the following code

int myvar=20;
printf("%d\n",myvar);
printf("%s","C is sea");