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

Tuesday, January 26, 2010

OR operator in expression

Logical OR(||) operator expression doesn't check second operand, if the first operand is true. i.e., d=a > b||b > c. In this expression if first operand is true then no need of checking second operand. Because though second operand is true or not, the expression will be true.
consider this program:
#include< stdio.h >
main()
{
int a=1,b=2;
if(a < b++||++a < b)
printf("%d %d",a,b);
}


output:
1 3

in this program a < b++(1 < 2 post increment so b will increment after checking) is true. So the second operand ++a < b isn't checked by OR . Now a=1,b=3 Because of second operand is not checked a won't be incremented.

For loop logical Program

We already know that, for loop syntax:
for(initialization;condition;reinitialization)
{
}
see this program.

#include< stdio.h >
main()
{
int i=1;
for(i=0;i<2;)
{
i++;
printf("%d",i);
}
}


output:
0 1

Size of variable - #define

If you create variable using #define , size of that variable is 2 bytes.

#include< stdio.h >
#define a 1
main()
{
printf("%d",sizeof(a));
return 0;
}

Printf() as a argument of another printf()

#include< stdio.h >
main()
{
printf("%d",printf("selva"));
}

output:
selva 5

for explanation see last post. If you can't able to understand, comment your queries.

Printf( ) function returning value.

Printf function returns "Number Of successful characters printed by printf function which includes white space ".


For eg:
a=printf("hi");
Obviously the printf function will print "hi".It prints two characters in console. So the printf function will return value 2.

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

selva 5

In the above program "selva" is printed in the console. The printf returns 5 to a.