Friday, January 27, 2012

Puzzle code of library function in c language

Q1) Output of the program
#include "stdio.h"
#include "conio.h"
main()
{
      fprintf("ABC");
      getch();
}
Ans:-compile time error.
Discussion:-fprintf function takes two argument, one file pointer and one variable/constant parameter.

Q2) Output of the program
#include "stdio.h"
#include "conio.h"
main()
{
      int i;
      char c;
      for(i=1;i<=5;i++)
      {
        scanf("%c",&c);
        printf("%c",c);
        ungetc(c,stdin);              
      }                
getch();
}
Ans:-You will get a chance to enter the character only once. Whichever character you enter will be printed five times.

Q3) What is the parpuse of fflushall() , fflush().
fflushall() clears all buffers associated with open input streams. And writes all buffers associated with open output streamto their respective file.
 fflush() function slush the specific stream.

 Q4)Output of the program.
#include "stdio.h"
#include "conio.h"
#include "math.h"
main()
{
  float a=2.3456;
  printf("%f\n%f",floor(a),ceil(a));
  getch();
}
Ans:-2.0000  3.0000
Discussion:-floor functions just discard the faction part of float number.
                    Ceil function makes the float number into nearest higher integer number.
Q5)Output of the program.
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
main()
{
int *p=(int *)malloc(356*256);
if(p==NULL)
 printf("Memory not allowed");
 getch();
}
Ans:-In TC/TC++ it will fail to allocate memory because 256*256=65536.Which when passed to malloc() will become a negative number since the formal argument of malloc().an unsigned int, can accommodate numbers only upto 65536

In VC/gcc/DEV C++ it will not report any error since here an unsigned integer is 4 byte wide can accommodate a number 65536

Q6)Output of the program
#include "stdio.h"
#include "conio.h"
#include "string.h"
main()
{
 char *ptr;
 gets(ptr);
 printf("%s",ptr);
 getch();
}
Ans:-Program will not work all time.
Discussion:-Since ptr is an uninitialized it must pointing at some unknown location in memory. The string that we type in will get stored at the location to which ptr is pointing. Thereby overwriting whatever is present at the location.
Q7)Output of the program.
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
main()
{
      int *p=(int *)malloc(100);
      printf("%u",sizeof(p));
      getch();
}     
Ans:-TC/TC++ it gives 2
         VC/gcc/DEV C++ it guves 4
Discussion:-It depends upon system memory byte size.

Q8)How much memory can we allocate in a single call to malloc.
Ans:-Varies from compiler to compiler 64 KB in TC/TC++.

Q9)Output of program
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
#include "string.h"
main()
{
     char c=getchar();
     printf("%d",c);
     getch();
}     
Ans:-It will print the ascii value of input.

Q9)Output of the program.
#include "stdio.h"
#include "conio.h"
main()
{
     char a[]="abc";
     char b[]="abc";
     if(a==b)
      printf("Equal");
     else
      printf("Not equal"); 
     getch();
}
Ans:-Not equal.
Discussion:-It is not possible to check two string using == operator. So the comparison operation returns 0 in if condition.
Q10)Output of the program.
#include "stdio.h"
#include "conio.h"
#include “string.h”
main()
{
     char a[5];
     char b[5];
     gets(a);
     gets(b);
     int i=strcmp(a,b);
     printf("%d",i); 
     getch();
}     
Ans:-Depends upon input.
Discussion:-strcmp function returns the ascii value of first mismatch character of two string.

Q11)Output of the program.
#include "stdio.h"
#include "conio.h"
main()
{
    int a[]={1,2,3,5};
    int i,j,k;
    i=++a[1];
    j=a[1]++;
    k=a[i++];
    printf("%d%d%d",i,j,k);
    getch();
}
Ans:-4 3 5

Q12)Output of the program.
#include "stdio.h"
#include "conio.h"
main()
{
    int a[]={1,2,3,5};
    printf("%d %d %d",a,&a[0],&a);
    getch();
}     
Ans:-Output depends upon system current process. But three output must be same.



Q13)Output of the program.
#include "stdio.h"
#include "conio.h"
main()
{
    int a[]={1,2,3,5};
    printf("%d %d %d",sizeof(a),sizeof(&a[0]),sizeof(&a));
    getch();
}     
Ans:-Output is compiler dependent.
         TC/TC++ gives 8 2 2
          VC++/DEV C++ gives 16 4 4
Discussion:-Output depends upon compiler and system memory byte size.

Q14)Output of the program.
#include "stdio.h"
#include "conio.h"
main()
{
    extern int a;
    printf("%d",a); 
    getch();
}
extern int a=100;        
Ans:-100
Discussion:-Extern variable is permitted to declare anywhere of the program .Even another file also.

Q15)Output of the program.
#include "stdio.h"
#include "conio.h"
main()
{
    static int a[5];
    printf("%d",a[4]); 
    getch();
}
Ans:-0
Discussion:-when static variable is declared, by default it is initialize by 0.


















No comments:

Post a Comment