Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8373735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:45:16+00:00 2026-06-09T14:45:16+00:00

I have learnt to use library stdarg.h to have function with unknown number of

  • 0

I have learnt to use library stdarg.h to have function with unknown number of arguments. Here is a simple function how to use this :

void print(int args,...){
    va_list ap;
    va_start(ap, args);
    int i = 0;
    for(i=0; i<args; i++){
        printf("%d\n",va_arg(ap, int));
    }
    va_end(ap);
}

Base on above code, there are two main constrains that I don’t know how printf can overcome:

1) fixed number arguments : mean, in almost vardiac function, you need to include number of args. But when I write printf, I don’t have to include this number. I have thought that before printf really using arguments, it has counted number of argument before (by counting number % in the first string). But again, I think this solution is a little bit not efficient. It must go through three pharses : counting number of arguments, and put this arguments into stack, and lastly put all into the screen.

2) All arguments must have same type: As you see at the line : printf("%d\n",va_arg(ap, int));. So, every argument in the list must have the same type. And as we know, this is not the must in printf. You can print double together with integer or the string. If we treat all of this just like a string, so this line should be error because wrong syntax :

printf("%d",4); //4 cannot treat by string
printf("%d",'4'); // :)) I think this line is better

Please help me explain above two problems.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T14:45:17+00:00Added an answer on June 9, 2026 at 2:45 pm

    1) You’ve answered the question yourself – printf must parse the format string to decide how many arguments to take in the va_list.

    2) When it parses the format list it will track the types of the arguments specified in the format list and call var_arg with the appropriate type.

    Also see – the interesting stuff is in vfprintf(), here’s the linked source, though there might be a more readable (if less efficient) version.

    Edit: There’s a portable implementation of snprintf here, that looks more readable (found here Looking for C source code for snprintf() )

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have learnt from this post that always use <a> tags or <button> tags
I am trying to learn how to use the C++ Boost.Thread library. I have
I have started to learn how to use OOP and have created a user
I'm trying to learn how to use the force.layout features in D3.js and have
I am just starting to learn how to use RMI, and I have a
I'm trying to learn how to use actionscript over mxml for flexibility. I have
I'm just starting to learn AspectJ, and I have use-case for say, User login.
I have learnt Python for about a month as a one year's PHPer.And I
I just have learnt how cool RSpec and Cabybara is, and now working around
Being a Java developer in an agile development cycle, I have learnt that it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.