AD1

Saturday, 29 April 2017

Find the output of the following code


#include <stdio.h>
int main(int argc, char** argv) {
static int i;
while(i<=10)
{
(i>2)?i++:i--;
}
printf("%d\n",i);
return (EXIT_SUCCESS);
}
view raw q134.c hosted with ❤ by GitHub
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


#include <stdio.h>
int main(int argc, char** argv) {
char *p="hello";
char c=++*p++;
printf("%c\n",c);
return (EXIT_SUCCESS);
}
view raw q125.c hosted with ❤ by GitHub
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

#include <stdio.h>
#define concat(x,y) x##y
#define string(a) #a
void main()
{
int myvar=20;
printf("%d\n",concat(my,var));
printf("%s",string(C is sea));
}
view raw q38.c hosted with ❤ by GitHub
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


#include <stdio.h>
void main()
{
char programming_languages[4][10]={"C","C++","java","python"};
char *temp=programming_languages[2];
programming_languages[2]=programming_languages[3];
programming_languages[3]=temp;
for(int i=0;i<=3;i++)
{
printf("%s\n",programming_languages[i]);
}
}
view raw q33.c hosted with ❤ by GitHub
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



void main()
{
extern int x;
x=100;
printf("value of x=%d\n",x);
}
view raw q6.c hosted with ❤ by GitHub
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


void main()
{
char message[]="hello";
for(int i=0;message[i];i++)
{
printf("%c\t%c\n",message[i],i[message]);
}
}
view raw q2.c hosted with ❤ by GitHub

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
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int data=0x11223344;
char *testptr=(char*)&data;
if(*testptr==0x11)
{
printf("big endian\n");
}
else if(*testptr==0x44)
{
printf("little endian\n");
}
}
view raw bigvslit.c hosted with ❤ by GitHub