C Programming Reference
 
C Programming Reference >> C  Programming Reference Question Bank >>

19/07/07 - 364 Views - No Ratings Yet

Level - Advance
      

Question -

Determine  the output of the following program.
#include<stdarg.h>  
#include<stdio.h>

int ripple ( int , ...);    

int main()  
{    
  int num;    
  num = ripple ( 3, 5,7);    
  printf( " %d" , num);  
  return 0; 
}    

int ripple (int n, ...)  
{    
  int i , j;    
  int k;      
  va_list p;      
  k= 0;    
  j = 1;    
  va_start( p , n);           
  for (; j<n;  ++j)     
   {      
     i =  va_arg( p , int);      
     for (; i;    i &=i-1  )        
     ++k;    
   }    
return k;  
}  
Answer -
5
Explanation -

This is a specially difficult C program which based on some of the most rarely used topic variable argument functions. These functions are very powerfull and basic example of them is scanf and printf. Also you need to include header <stdarg.h> for variable argument functions.

Here ripple is our variable argument. va_list type variable 'p' is a special type of variable capable of storing a variable of any type.

va_start puts the first variable n in the va_list variable 'p' ie. int value 3.Now when va_arg (p,int) is called it puts value of next variable in 'p' and returns its value 5 in i.Next i just puts the value 5 into k in a complicated manner and this value is returned. So the ouput obtained is 5.

This question may be hard to grasp and read again if needed, also try an experiment with code for better understanding.

Reader Comments -

Author Comments
Add Comments 


Name :    
Reply :   


Rating :

Code :
Code

 

 
© 2006 cencyclopedia.com