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

  • SEARCH
  • Home
  • 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 821851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:42:34+00:00 2026-05-15T02:42:34+00:00

I am experimenting with variable argument lists and seeing some strange results… The piece

  • 0

I am experimenting with variable argument lists and seeing some strange results…

The piece of code I am testing is:

#include <stdio.h>
#include <stdarg.h>

void foo(int param1, int param2, ...)
{
    int param3 = 0;

    va_list ap;
    va_start(ap, param2);
    param3 = va_arg(ap, int);
    va_end(ap);

    printf("param3: %d\n", param3);
}


int main(void)
{
  foo(1,1);
  foo(1,1,42);

}

And the output for that snippet is:

param3: -1073748472
param3: 42

For the second call: ‘foo(1,1,42)’, everything seems to work as expected.

For the first call: ‘foo(1,1)’, the result look like an uninitialised int, although I do set it to 0 when I first initialise it at the beginning of the function.

I would like to be able to rely on the fact that the resultant variable should have the value 0 if the argument is not called. I would have thought va_arg() would be sensible enough to deal with that but it doesn’t seem to be the case.

Any suggestions to deal with that?

Many thanks.

  • 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-05-15T02:42:35+00:00Added an answer on May 15, 2026 at 2:42 am

    The problem is that you’re reading the value no matter whether it’s passed or not. Thus, you’re getting whatever junk happens to be on the stack at the offset where the argument was supposed to live.

    When using varargs, you have to make sure that the initial arguments you pass make it clear what other arguments are present. printf() does this by counting % signs (although the programmer sometimes gets this wrong, and hilarity ensues)

    The better answer is “don’t use varargs; find some type-safe way of doing what you need.”

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

Sidebar

Related Questions

I have been experimenting with woopra.com A web analytics tool. Which requires a piece
I was experimenting with variable constructor arguments for case classes in Scala, but am
I am trying to write a program to check that some C source code
I'm doing some experimenting with this malicious JavaScript line: var undefined = true; Every
I've been doing some experimenting with php and html, and I'd like to know
I'm experimenting with some multithreading constructions, but somehow it seems that multithreading is not
I was experiencing some weird behaviour in some of my javascript code, but only
Experimenting with new features of T-SQL, I've run into a puzzle. Here is some
I am, for some time, experimenting with LLVM , simply because. It does, however,
While experimenting with this question on collections in Spring.NET , I discovered that Spring

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.