Introduction To Programming

Why programs ? Programming is nothing but making the computers think the same way a human thinks. Programs are made for obtaining a specific solution which can even be solved by humans which when solved by us may consume time..

Sunday, April 18, 2010

Predict The Output:

int main()
{
int *p=(int *)1000;
*p=2;
printf("%d",*((int*)1000));

return 0;
}

Predict The Output:

int main()
{
char a[]="shark";
(*a)++;
printf("%s\n",a);
printf("%s",a+1);
return 0;
}

Output:
thark
hark

Predict The Output:

int main()
{
return printf("shark ");
}
int x=main();

Don't think that it will result in compilation error. It will run perfectly and prints "shark shark".

Output:
shark shark

Predict the Output

int main()
{
int a=1;
a+=a++;
printf("%d",a);
a+=++a;
printf("%d",a);
return 0;
}

Output:
3 8

Logic:
a+=a++ is equal to a=a+a++;
watch out the value of 'a' in this expression.
a=a + a++
3  1   1
a+a=2 and the post increment will increment the value of 'a' so it will result in a=3.(to understand increment operator see this post also Lparts,Pre and post,Post 18).

Now we will consider the second operation i.e., a+=++a;. This expression is equal to
a=a+ ++a;
This expression is manipulated as follows:
a = a+  ++a;
8   4     4    
Pre increment will increment the value of a first so 'a' has now 4. and a+a is 8 is assigned to a. so a=a+a=8.

What is Null?

Two type of Null , "NULL pointer","NULL character. NULL means "nothing".
NULL pointer usually assigned to pointers. This will make pointer to point nothing i.e, pointer reference to no variable.
NULL character('\0') usually added automatically at the end of each string used in c program. It defines end of string.

Predict the Output

#define tech(a,b) a##b##a
int main()
{
if(printf("%d",tech(4,0)))
{
}
return 0;
}

Output:
404

Read about Macro