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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:14:18+00:00 2026-05-31T05:14:18+00:00

Consider this code; #define A 5 #define B 3 int difference = A –

  • 0

Consider this code;

#define A 5
#define B 3

int difference = A - B;

does value of “difference” is hardcoded as “2” in compile time, or does it get calculated on runtime?

  • 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-31T05:14:19+00:00Added an answer on May 31, 2026 at 5:14 am

    The A and B macros are a bit of a distraction. This:

    #define A 5
    #define B 3
    
    int difference = A - B;
    

    is exactly equivalent to this:

    int difference = 5 - 3;
    

    so let’s discuss the latter.

    5 - 3 is a constant expression, which is an expression that “can be evaluated during translation rather than runtime, and accordingly may be used in any place that a constant may be”. It’s also an *integer constant expression”. For example, a case label must be an integer constant expression, so you could write either this:

    switch (foo) {
        case 2: /* this is a constant */
        ...
    }
    

    or this:

    switch (foo) {
        case 5 - 3: /* this is a constant expression */
        ...
    }
    

    But note that the definition says that it can be evaluated during translation, not that it must be. There are some contexts that require constant expressions, and in those contexts the expression must be evaluated at compile time.

    But assuming that difference is declared inside some function, the initializer is not one of those contexts.

    Any compiler worth what you pay for it (even if it’s free) will reduce 5 - 3 to 2 at compile time, and generate code that stores the value 2 in difference. But it’s not required to do so. The C standard specifies the behavior of programs; it doesn’t specify how that behavior must be implemented. But it’s safe to assume that whatever compiler you’re using will replace 5 - 3 by 2.

    Even if you write:

    int difference = 2;
    

    a compiler could legally generate code that loads the value 5 into a register, subtracts 3 from it, and stores the contents of the register into difference. That would be a silly thing to do, but the language standard doesn’t exclude it.

    As long as the final result is that difference has the value 2, the language standard doesn’t care how it’s done.

    On the other hand, if you write:

    switch (foo) {
        case 5 - 3: /* ... */
        case 2:     /* ... */
    }
    

    then the compiler must compute the result so it can diagnose the error (you can’t have two case labels with the same value.

    Finally, if you define difference at file scope (outside any function), then the initial value does have to be constant. But the real distinction in that case is not whether 5 - 3 will be evaluated at compile time, it’s whether you’re allowed to use a non-constant expression.

    Reference: The latest draft of the 2011 C standard is N1570 (large PDF); constant expressions are discussed in section 6.6.

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

Sidebar

Related Questions

Consider this piece of code: struct Trade { float Price; char* time; int shares;
Consider this code: #include <stdio.h> #define N 5 void printMatrix(int (*matrix)[N],int n) { int
Consider this code (Java, specifically): public int doSomething() { doA(); try { doB(); }
Consider this code: int main() { int e; prn(e); return 0; } void prn(double
Consider this code: void res(int a,int n) { printf(%d %d, ,a,n); } void main(void)
Consider this code: int x = 17; int y = 013; System.out.println(x+y = +
Consider this trivial function: public static bool IsPositive(IComparable<int> value) { return value.CompareTo(0) > 0;
Consider this (horrible, terrible, no good, very bad) code structure: #define foo(x) // commented
Consider this code: #include <memory> #include <iostream> class A { public: A(int data) :
Consider this code... using System.Threading; //... Timer someWork = new Timer( delegate(object state) {

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.