IndiaBIX.com
Arithmetic Aptitude Data Interpretation
Logical Reasoning Verbal Reasoning Non Verbal Reasoning
General Knowledge
Sudoku Number puzzles Missing letters puzzles Logical puzzles Playing cards puzzles Clock puzzles
C Programming C# Programming Java Programming
Networking Database Questions Computer Science Basic Electronics Digital Electronics Electronic Devices Circuit Simulation Electrical Enigneering Engineering Mechanics Technical Drawing
Placement Papers Group Disucssion HR Interview Technical Interview Body Language
Aptitude Test Verbal Ability Test Verbal Reasoning Test Logical Reasoning Test C Programming Test Java Programming Test Data Interpretation Test General Knowledge Test
Data Structures Operating Systems Networking DATABASE Database Basics SQL Server Basics SQL Server Advanced SQL Server 2008 JAVA Core Java Java Basics Advanced Java UNIX Unix File Management Unix Memory Management Unix Process Managemnt C Interview Questions The C Language Basics .NET Interview Questions .NET Framework ADO.NET ASP.NET Software Testing

C Programming - Variable Number of Arguments - Discussion

@ : Home > C Programming > Variable Number of Arguments > Find Output of Program - Discussion

Read more:

"Everything should be made as simple as possible, but not simpler."
- Albert Einstein
1. 

What will be the output of the program?

#include<stdio.h>
#include<stdarg.h>
void fun(char *msg, ...);

int main()
{
    fun("IndiaBIX", 1, 4, 7, 11, 0);
    return 0;
}
void fun(char *msg, ...)
{
    va_list ptr;
    int num;
    va_start(ptr, msg);
    num = va_arg(ptr, int);
    num = va_arg(ptr, int);
    printf("%d", num);
}

[A]. IndiaBIX 1 7 11 0[B]. 1
[C]. 4[D]. 7

Answer: Option E

Explanation:

No answer description available for this question.


Praveen said: (Mon, Nov 22, 2010 03:58:28 PM)    
 
sir i think va_start(ptr,msg)should hold 6
and num must return 6..?
give me region how it work...

Riya said: (Wed, Dec 29, 2010 12:46:28 AM)    
 
can anyone explain this please......

Onkar Tiwari said: (Sat, Jan 1, 2011 06:24:07 AM)    
 
if anyone can explain then please explain.i will grateful to you.

Virat said: (Tue, Jan 18, 2011 05:33:00 AM)    
 
plzzzzzz......... explain dis question.. if u can....

Shiv said: (Sun, Jan 23, 2011 07:52:53 AM)    
 
va_list ptr is an argument pointer, used to traverse through the variable arguments passed in the function.

va_start points 'ptr' to the first argument, in this case 'IndiaBix'

Every call to va_arg moves the ptr to next variable argument.

Hence after 2 calls to va_arg, the pointer ends up at '7' and num is '4'.

Nita said: (Tue, Feb 15, 2011 12:46:25 AM)    
 
I am aggry with shiv.

Anshu said: (Mon, Mar 7, 2011 09:34:05 AM)    
 
Yes I also thinks what shiv z saying, dats correct.

Anjali said: (Tue, Apr 5, 2011 06:44:27 AM)    
 
Sir please explane how it comes.

Saurabh said: (Sun, May 15, 2011 05:24:59 PM)    
 
I didn't get it ! Can anyone expalin in little more detail ?

Irshad said: (Thu, Jun 30, 2011 06:00:16 AM)    
 
#include<stdio.h>
#include<stdarg.h>
void irshad(char *arr, ...)
{
int num;
va_list ptr; /* create a new list ptr */
va_start(ptr,arr); /* attach ptr with arr */
printf("\n num=%d",arr); /* it will print 1st argument */
num = va_arg(ptr,int); /* fetch 2nd argument */
printf("\n num=%d",num);
num = va_arg(ptr,int); /* fetch 3rd argument */
printf("\n num=%d",num);
}
int main()
{

printf("\n variable argument:");
irshad(10,20,30,40,50,0);
return 0;
}


/* output */

variable argument:
num=10
num=20
num=30


Please let me know if any problem to understand this one.
irshadalam.mca[at]gmail.com

Pruthvi said: (Wed, Sep 14, 2011 05:36:04 PM)    
 
What does int in va_arg(ptr, int) indicates? and is there any case where we can use char over there?

Amith said: (Wed, Sep 28, 2011 08:02:47 PM)    
 
We can also use va_arg(ptr, char);

It depends on the variable arguments ur passing to the function

Ex: fun("IndiaBIX", 'I','N','D','I','A' );

Kiran said: (Fri, Nov 18, 2011 02:34:28 AM)    
 
@irshad::thanks for your explination.

Abhilash Bhaduri said: (Mon, Jan 23, 2012 07:23:42 PM)    
 
In 1 place in random test you say that va_end should be present for every va_start. But here it compiles with no error. How?

Write your comments here:
Name *:     Email:


© 2008-2011 by IndiaBIX™ Technologies. All Rights Reserved | Copyright | Terms of Use & Privacy Policy

Advertise     Contact us: info@indiabix.com     Follow us on twitter!