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

The Archive Base Latest Questions

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

I was trying this code. #include <stdio.h> #include <stdlib.h> #define LOWER 0 #define UPPER

  • 0

I was trying this code.

#include <stdio.h>
#include <stdlib.h>
#define LOWER 0
#define UPPER 300

int main()
{
    printf("%d %f",LOWER,UPPER);
    return 0;
}

I read some of the answers and it says that defined constants don’t have a type and are not allocated any memory.
Then why is it giving errors if i specify different type specifiers in printf()?

  • 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-13T16:19:10+00:00Added an answer on June 13, 2026 at 4:19 pm

    In your example, both are pushed into the variadic parameter list of printf as int values. This can cause problems with the format flags in the format string of printf() do not match the underlying type. Refer to this post for reasons why undefined behavior can ensue.

    As hideous as it seems, one way to know you can get what you’re looking for in this printf is to do this:

    #include <stdio.h>
    #include <stdlib.h>
    #define LOWER 0
    #define UPPER 300
    
    int main()
    {
        printf("%d %f", (int)LOWER, (float)UPPER);
        return 0;
    }
    

    In both cases, these are preprocessor macros that are substituted before compilation. Note that if UPPER cannot be promoted to a float you will receive a compiler error, which is a good thing. if it can be, it will be, and the printf() will find the bytes it needs to print what it wants. The same is true for int and LOWER respectively. The above printf will degenerate to this after preprocessing:

    printf("%d %f", (int)0, (float)300);
    

    Now imagine your macros were instead declared as such:

    #define LOWER 100.0
    #define UPPER 300.0
    

    The original printf() will present as thus after preprocessing:

    printf("%d %f", 100.0, 300.0);
    

    which may seem right, but is that 100.0 float really going to be properly excised by the %d format string handler in printf()? Make sure. Doing what we did before:

    printf("%d %f", (int)LOWER, (float)UPPER);
    

    now preprocesses to:

    printf("%d %f", (int)100.0, (float)300.0);
    

    You may get a compiler warning on the float-to-int cast, you may not. Unless you want to roll the dice that what you’re passing is sized to what is byte-size-expected by things like printf() you need to be damn-sure everything matches. This can get especially frustrating when the format specifier expects one thing, something different is passed in, and yet it appears to all work fine, but only on some platforms mysteriously:

    printf("%ld\n", UPPER);
    

    This may “work”, but if it does it is only because a long int and a int are the same size on your platform. Move this code to a platform where long int and int are different bit-widths and its UB all the way.

    Bottom line: If you’re passing preprocessor macros into variadic parameter lists for things like printf() that have size expectations for the data being pushed (as specified by the format string for example) you better make sure what you’re pushing is what is expected.

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

Sidebar

Related Questions

I was trying to execute this code through gcc compiler: #include <stdio.h> int main()
I'm trying to use SendInput() function. I wrote this code: #include <stdio.h> #include <stdlib.h>
I'm trying to compile this C++ code: #include <stdlib.h> #include <stdio.h> #include <string.h> #include
When trying to compile this code: #include <iostream> #include <vector> using namespace std; class
I'm trying include this barcode source code Stefanhafeneger - Barcode github.com to my project
I'm trying to link to an external stylesheet using this code: <?php include(/homepages/9/myusername/htdocs/Mobile_Detect.php); $detect
I am trying this code: entLoop:for(var i:*in entities) { for(var i2:*in ignoreEntities) { if(entities[i].type==ignoreEntities[i2])
I'm trying this code to access a property but it gives me an error:
I am new to Django and was trying this code in a tutorial. But
Boost Network I am trying out this code in a small console app on

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.