Questions regarding programs
Question 1:
-----------
Code:
#include<stdio.h>
#include<conio.h>
int main(void)
{
int a,b;
clrscr();
printf("Enter values for a and b\n");
scanf("%d%d",&a,&b);
printf("a=%d,b=%d",a,b);
getch();
return 0;
}
Here the question is, if i write scanf() function as scanf("%d%d ",&a,&b); or
scanf("%d %d ",&a,&b); or scanf(" %d%d ",&a,&b);, it is taking 3 inputs, rather than 2 inputs. Why like this?
Question 2:
-----------
Code:
#include<stdio.h>
#include<conio.h>
int main(void)
{
int a=3;
float b=3.0
clrscr();
if(a==b)
printf("a and b are equal");
else
printf("a and b are not equal");
getch();
return 0;
}
Here at execution, it is giving output as a and b are equal. Why it is like this?
Question 3:
-----------
Code:
#include<stdio.h>
#include<conio.h>
int main(void)
{
int i;
clrscr();
for(i=0;i<=5;printf("%d\n",i))
i++;
getch();
return 0;
}
For the above program the output is
1
2
3
4
5
6
Why?
Question 4:
-----------
Code:
#include<stdio.h>
#include<conio.h>
int main(void)
{
int i;
clrscr();
for(i=1;i<=5;printf("%d\n",i++))
getch();
return 0;
}
For the above program the output is
1
2
3
4
5
But if we press every time "Enter" button then only it is printing output from 1 to 5. Why it is happening like this?
Question 5:
-----------
Code:
#include<stdio.h>
#include<conio.h>
int main(void)
{
char another;
int i;
clrscr();
do
{
printf("Enter a number\n");
scanf("%d",&i);
printf("The number is %d\n",i);
printf("Continue(y\n)\n");
scanf("%c",&another);
}while(another=='y');
getch();
return 0;
}
Code:
#include<stdio.h>
#include<conio.h>
int main(void)
{
char another='y';
int i;
clrscr();
for(;another=='y';)
{
printf("Enter a number\n");
scanf("%d",&i);
printf("The number is %d\n",i);
printf("Continue(y\n)\n");
scanf("%c",&another);
}
getch();
return 0;
}
Code:
#include<stdio.h>
#include<conio.h>
int main(void)
{
char another='y';
int i;
clrscr();
while(another=='y')
{
printf("Enter a number\n");
scanf("%d",&i);
printf("The number is %d\n",i);
printf("Continue(y\n)\n");
scanf("%c",&another);
}
getch();
return 0;
}
For the above 3 programs the syntax is correct, but if we execute the each and every program, the loop is repeating only 2 times. After 2 times the program is terminating. Why and what is the corrcet programs for the above 3 progarms?
Question 6:
-----------
Code:
#include<stdio.h>
#include<conio.h>
int main(void)
{
clrscr();
for(;;);
printf("%d\n",i);
getch();
return 0;
}
What this program mean?
The statement "for loop works faster than a while loop" is correct?