Thread: Questions regarding programs

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    8

    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?

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    for q.no.2
    the int is getting promoted to a float.
    for q.no.3
    for loop works as
    1.initialize
    2.test
    3.execute body of the loop
    4.go to incrementation part
    for q.no.4
    u've not put a semicolon after the for loop that's why getch() is acting as the body of the loop and every time enter is to be hit.to rectify it simply put a semicolon after the for loop
    for q.no.6
    it is an indefinite for loop if u define variable 'i'.otherwise it will throw an error.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. Some C test questions: challenge
    By Mister C in forum C Programming
    Replies: 47
    Last Post: 09-10-2002, 06:47 AM
  3. C++ test questions
    By Mister C in forum C++ Programming
    Replies: 9
    Last Post: 09-08-2002, 12:05 PM
  4. questions about new and delete
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2001, 01:48 PM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM