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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:08:11+00:00 2026-06-13T06:08:11+00:00

My questions stem from trying to use printf to log things when trying to

  • 0

My questions stem from trying to use printf to log things when trying to build for multiple bit-depth platforms (32/64 for example).

A problem that keeps rearing its ugly head is trying to print ints on multiple architectures. On 32 bit it would be something like

printf(" my int: %d\n", myInt);

but on 64 bit, it would have to be changed to

print (" my int: %ld\n", (long)myInt);

I have two related questions:

  1. My first thought was that when you tell printf to print a variable, giving it a format, it would look at the address of that variable and grab as many bytes as it needed for that format. This seemed like a big problem at first. For example if you had a variable myChar that was a char (1 byte), but used a format specifier of %d, that would tell printf to go to the address of myChar and grab the next 4 bytes to treat it like an int. If this were the case, it seems like printf would grab garbage date from neighboring variables (because it was grabbing 4 bytes, but the real value is only 1 byte). This appears to not be the case however. By using myChar and specifying %d, printf grabs 1 byte and then pads the upper 3 bytes with 0’s. Is my understanding correct here?

  2. If the above is true, is there any real harm in always promoting variables up to their largest values to avoid the types of problems seen in the 32/64 bit case. For example if you have a short variable myShort, and an int variable, myInt, is there any downside in printing them always as:

    printf(“myShort %ld”, (long)myShort);
    printf(“myInt %ld”, (long)myInt);

Thanks for any clarification.

  • 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-13T06:08:12+00:00Added an answer on June 13, 2026 at 6:08 am

    Regarding printf: In the case you sited, “%d” must, by specification, handle the platform-defined ‘int’ data type. It doesn’t matter whether it is 32bits, 64bits, or 128bit linear AS/400 value. If you want to promote the value to a larger field type (and match that promotion with the related format string particle) you’re certainly free to do so,

    int a=0;
    printf("%ld", (long)a);
    

    is certainly defined behavior using promotion.

    I think the real crux of your question comes in cases like the following, and whether forcing promotion can “solve” any problems that arise. For example:

    char ch = 'a';
    printf("%d", ch);
    

    or what about:

    char ch = 'a';
    printf("%ld", (long)ch);
    

    or maybe this (which is the real condition you seem to be trying to avoid):

    char ch = 'a';
    printf("%ld", ch);
    

    The first of these will work, but only because the minimum size of anything stack-pushed on a va-arg list is the platform-size of an int. The compiler will auto-promote the value to an int for you. Since “%d” expects a platform int all will appear well.

    The second will work always and is fully supported. There is a clear and defined promotion from char to long. Even if long is 64bits (or larger) it will still work.

    The third is UB all the way. printf is looking for a long and will be presented with only bytes for an int. If this seems to “work” on your platform, then check your platform width for int and long. It is likely “working” only because your platform long and int are the same bit-width. It makes for fun surprises when porting the code to platforms where they are not, and since it is pushed through a va-arg, you won’t see it until real different widths come in to play.

    All of that being said, now throw an actual address to something (anything, really) such as that required by scanf and we’re looking at something entirely different.

    int val;
    sscanf("%ld",&val);
    

    This is a seg-fault waiting to happen. Just like above, you’ll never know it if your platform long and platform int are the same width. Take this code to a box where long and int are different sizes and prep yourself for a gdb-load of the ensuing core file.

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

Sidebar

Related Questions

I'm currently trying to update my in house search engine to use multiple words.
questions from a Newbie :-) I am trying to convert my MySQL query syntax
Questions (3 of them that are related) are bolded below. I'm trying to create
Other questions dealing with use of match_parent pre 2.2 have answers stating one should
Questions in the title. I've always wondered and failed to find out from the
Questions all in the title. I've been trying for the past hour to get
Questions on Rails 3.0.7, and JQuery 1.5.1 Overview I'm trying to do the following
Questions: how can I use img tag in facelet java ? I've tried using
This is a typical Multiple Choice exam, Assume a question format: <question qid='1'> <stem>What
Questions exactly like this have been asked before, i've read them all and tried

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.