Friday, 10 July 2015

C language basics 3

//Find the errors

1. #include<stdio.h>
#include<conio.h>
int main()
{
int i;
printf("enter the value of i\n");
scanf("%d",&i);
if(i=5)
printf("you entered  5\n");
else
printf("you entered something other than 5\n");
getch();
return 0;
}


output: enter the value of i
  7
you entered  5


2.//largest of three numbers
#include<stdio.h>
#include<conio.h>
int main()
{
int big, a,b,c;
printf("enter the values of a b c\n");
scanf("%d %d %d",&a, &b, &c);
big=(a>b?(a>c?a:c):(b>c?b:c));
printf("largest of three numbers is %d",big);
getch();
return 0;
}

output: enter the values of a b c
1 18 24
largest of three numbers is 24



3.// Interesting program

#include<stdio.h>
#include<conio.h>
int main()
{
int x=3;
float y=3.0;
if(x==y)
   printf("x and y are equal");
   else
      printf("x and y are not equal");
getch();
return 0;
}

output:
x and y are equal



4.//program

#include<stdio.h>
#include<conio.h>
int main()
{
int x=3, y,z;
y=x=10;
z=x<10;
printf("x=%d y=%d z=%d \n",x,y,z);
getch();
return 0;
}


output:

x=10 y=10 z=0



5.//very interesting program

#include<stdio.h>
#include<conio.h>
int main()
{
int i=4, j=-1, k=0,w,x,y,z;
w=i||j||k;
x=i&&j&&k;
y=i||j&&k;
z=i&&j||k;
printf("w=%d\n x=%d\n y=%d\n z=%d\n",w,x,y,z);
getch();
return 0;
}


output:
w=1
x=0
y=1
z=1



6.//not operator

#include<stdio.h>
#include<conio.h>
int main()
{
int i=-3, j=3;
if(!i+!j*1)
printf("expression results 1");
else
printf("expression results 0");
getch();
return 0;
}


output:
expression results 0



//Difference between && and &

7.//program on if condition

#include<stdio.h>
#include<conio.h>
int main()
{
int i=10, j=20;
if((i=5)&&(j=10))
printf("hello");
getch();
return 0;
}


output:
hello



8.//&
#include<stdio.h>
#include<conio.h>
int main()
{
int i=10, j=20;
if((i=1)&(j=1))
printf("hello");
getch();
return 0;
}

output:
hello


Explanation:

In the first program, first expression 1 be executed and then the result of this expression decides the execution of the other statement. If it is false then the AND will be false so it makes no sense to execute the other statement. The expression 2 statement is executed if and only if expression 1 returns true on execution. It is also known as the short circuit operator because it shorts the circuit (statement) on the basis of the first expression’s result. 


Now in the case of & things are different. The compiler will execute both statements and then the result will be analyzed. It’s an inefficient way of doing things because it makes no sense to execute the other statement if one is false because the result of AND is effective only for AND results evaluated to “true” and it’s possible when both statements are true.


//Difference between || and |
9. 
 #include<stdio.h>
#include<conio.h>
int main()
{
int i=10, j=20;
if((i=0)||(j=1))
printf("hello");
getch();
return 0;
}


output:
hello


10.
 #include<stdio.h>
#include<conio.h>
int main()
{
int i=10, j=20;
if((i=0)|(j=1))
printf("hello");
getch();
return 0;
}


output:
hello


Explanation:
 It’s the same as above, in the case of “||” only one statement is executed and if it returns “true” then the other statement will not be executed. But if the first is false then the other will be checked for the value “true”. The reason for this is the way the “or” operator works. The “Or” operator depends on only one true, in other words if any of the expressions are true then the result will be true.








No comments:

Post a Comment