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..

Saturday, February 13, 2010

17

#include< stdio.h >
main()
{
int i=1;
if(++i < 1 && ++i >1)
{
printf("False");
}
printf("%d",i);
}

Output


2


How?


&&   Logical AND operator.
As far as AND operator concerned if both expression true ,it'll return true otherwise false.if first expression is false ,no need of checking the next expression.
In this program ++i < 1 is false (2 < 1, so false). so no need of checking the next expression.++i > 1 will not work. so 'i' will incremented for 1 time.
so i=2.

16

#include< stdio.h >
main()
{
int s=1,r=2;
{
int s;
s=r+4;
}
printf("%d",s);
}


output


1


How?


Though i declare 's' variable for 2 times,there's no error. How?
Block scope( '{ }' ):
if you declare a variable a block ,it's valid for that particular block only.
so
{
int s; //valid for this block
s=r+4;
}
when it come outside of the block, above declared 's' only valid. so s=6 is inside the block and s=1 is outside of the block.

To understand clearly consider this program.,
#include < stdio.h >
main()
{
int a=2;
{
int a=3;
printf("%d",a);
}
printf("%d",a);
}

OUTPUT


3   2

15

#include< stdio.h >
#define a 4
main()
{
a++;
printf("%d",a);
}

output


Compilation Error:
Lvalue Required.


How?


#define defines a constant not a variable and also 'a' is replaced with '4' while expanding code.
a++; will be 4++;. we can't increment constant value(4=4+1).
NOTE:constant won't changes its value.
variable can change its value.

14

#include< stdio.h >
#define a (5+2)
main()
{
int c;
c=2*a;
printf("%d",c);
}


Output


14


how?


as said in last post the variable defined using macro is replaced with that constant.
Here c=2*a; will become as c=2*(5+2);
'()' has higher precedence than the '*' so, 5+2 will be added and multiplied to 2.i.e
b=2*7=14;