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 7752673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:51:28+00:00 2026-06-01T11:51:28+00:00

When I use printf/printf_s with two strings, I get the same output for both

  • 0

When I use printf/printf_s with two strings, I get the same output for both %s vars.

IN_ADDR oldIP;
oldIP.S_un.S_addr = iptable[j]->ipAddress;
IN_ADDR newIP;
newIP.S_un.S_addr = adapterTbl->table[i].dwAddr;
printf_s("index %d old: %s new: %s", 
          adapterTbl->table[i].dwIndex, inet_ntoa(oldIP),
           inet_ntoa(newIP));

Output is:

index 11 old: 192.168.1.1 new: 192.168.1.1

Now, I have checked that the values of oldip and newip are different by breaking before the print statement, and I have also tried to make the following function and use it in the print statement (instead of inet_ntoa):

char *convertIP (DWORD ip)  
{  
    IN_ADDR *addr = new IN_ADDR;
    memset(addr, 0, sizeof(IN_ADDR));
    addr->S_un.S_addr = (u_long) ip;
    return inet_ntoa(*addr);
} 

The output with this was:

192.168.1.1
192.168.1.2
index 11 old: 192.168.1.1 new: 192.168.1.1

Why am I seeing this behavior, and how can I fix it?
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-06-01T11:51:29+00:00Added an answer on June 1, 2026 at 11:51 am

    inet_ntoa retuns:

    a statically allocated buffer, which subsequent calls overwrite

    (This is from the Linux manpage.)

    You can’t use it twice in a function, only the output of the second call will be visible (and you don’t know which one of the two calls is the second – order of evaluation of arguments to a function is unspecified).

    Do two separate printfs, or copy the output of inet_ntoa to local buffers and output those.

    From POSIX:

    The return value of inet_ntoa() may point to static data that may be overwritten by subsequent calls to inet_ntoa().

    So this behavior is probably not limited to Linux, and you can’t rely on it not overwriting a static buffer.

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

Sidebar

Related Questions

is there a way to get with printf colored output? #!/usr/bin/perl use warnings; use
As everybody knows, you have limited precision when you use printf to output the
I'm trying to use simpletest to compare two numeric strings, one from an array
I would like to use the syntax that printf uses, using the %d, %s
I use to run $s =~ s/[^[:print:]]//g; on Perl to get rid of non
Im trying to use printf in my assembley code in model large, and i
I have a weird issue with printing data out. I use printf to print
How does printf handle its arguments? I know that in C# I can use
If for example I should not use standard library functions like printf() , putchar()
The following two pieces of code compile, but I get a connect() failed error

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.