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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:42:09+00:00 2026-06-12T01:42:09+00:00

My question is about the behavior after a printf with a missing argument: printf(%s

  • 0

My question is about the behavior after a printf with a missing argument:

printf("%s blah blah %d", int); // integer was given as argument (and not int written)

I already know that if there are insufficient arguments for the format, the behavior is
undefined.

The question is whether it is undefined for the printf outcome or for the whole program?

  • A crash can happen when %s tried to read from unauthorized memory address. (it happened to me)
  • Can a crash happen [long] after the printf complete? (printed some garbage string and an integer)

Edit:

To clarify I’m not asking about compilation errors or warnings, nor can a program crash when executing this line. The question is can this line make the program crash randomly after this line already executed.

  • 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-12T01:42:11+00:00Added an answer on June 12, 2026 at 1:42 am

    Basically for the whole program. Printf starts taking arguments off the stack and, in this case, takes an int worth too much. That’s usually something like the return address. So when the printf returns, it returns to whatever random number happens to be next on the stack. The usual result — if you’re lucky — is a segmentation fault.

    Because it pushes arguments onto the stack, it pops them off, so it tries to get the int first.

    If you’re not lucky, it finds an addressable chunk of code. That leads to your second case, where the address becomes the address of that hash of random characters. Now it’s going to try to print a string until it find a random NUL character.

    Update

    As Joachim points out, the specifics of this are determined by the calling convention, so let’s make an explicit example. When the printf function is to be called, either the return address is pushed first or its pushed last. We assume it’s pushed first (more common on the usual architecture), so this call is going to need PUSH return-address, PUSH address of the format string, PUSH an int value — let’s say 42. That gives us this stack:

    RTN ADDR
    ADDR OF STRING
    42
    

    and leaves the stack pointer SP pointing to the next location on the stack.

    Now the printf starts to interpret the string. It looks for the address of the int parameter, and figures out it’s SP-1. So the address of the string parameter must be SP-2 … but that’s the address of the format string, since there is no string parameter. Then when it looks for the address of the format string, it wants to find SP-3, but that’s the return address, an address of executable code. That should, on most any machine, cause a segmentation fault.

    If you go through the other options for calling convention, you’ll find every one of them looks at some wrong thing, because no matter what, printf thinks it needs to refer to three things off the stack, not the two it has.

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

Sidebar

Related Questions

After asking a question about sending 304 Not Modified for images stored in the
I have a question about the behavior of a browser( especially Firefox ) tackling
I got a question about the behavior of lookup fields when importing data. I
I've seen some question here about almost the same thing, but, after following their
Disclaimer When I wrote this question I was wrong about the behavior of the
I have a question about pthread_kill() behavior. Here's a small code I'm trying out:
I have a question about the default behavior of JavaScript's parseFloat function in different
I have a question about arithmetic behaviour in R. Regard the following piece of
Mainly this is a question about how to get a similar behaviour as java
Question about subclassing in matlab, under the new class system. I've got class A

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.