AD1

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");


Find the output of the following code


Output
lvalue required as increment operand

Explanation
after reading the code for the first time you may get tricked and say the output will be

c++
python
java 
but this not the output , because you will get compiler error in line 6 when you try to modify the base address of an array which is already a constant pointer

Find the output of the following code



Output 
linker error (undefined symbol x)

Explanation 
extern keyword tells the compiler that the value of the variable x is not in this scope so the compiler does not know variable x memory location so he will not assign x memory location , otherwise during linking time if x is defined in another scope or file , its value will be printed

Find the output of the following code



Output

h h 
e e
l l
l l
o o

Explanation
message[i]=i[message]
because compilers convert message[i] into *(message+i)
where message is the array base address ,also i[message] is converted into
 *(i+message)
so the two expressions are the same

Tuesday 4 April 2017

Big endian VS little endian

Write a C program to check if your processor is big endian or little endian 

solution