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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:53:07+00:00 2026-05-25T09:53:07+00:00

Considering the symbolic constants using macros,enumeration constants and const objects. In macros scope is

  • 0

Considering the symbolic constants using macros,enumeration constants and const objects.
In macros scope is global and cannot be limited to local scope which is a major disadvantage.
Enumeration constants cannot be used in situations other than the integers,Enumeration constants cannot be represented in float or long.
Const objects can have local scope,can be represented in different datatypes.
But in c declaring a “int const a” or “const int a” makes the value constant and int buffer[a] is not permitted in c.. But in c++ int buffer[a] is permitted as it takes the “const a” as a compiler constant only.

Despite the disadvantages mentioned most of them generally prefer defining symbolic constants as enumeration constants rather than as const objects.

I could not understand the below statement telling that const objects cause performance penalty.How does it cause.Please help me understand..

The problem with const objects is that they may incur a performance
penalty, which enumeration constants avoid.

  • 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-25T09:53:07+00:00Added an answer on May 25, 2026 at 9:53 am

    An object declared with const is not a constant (more precisely, its name is not a constant expression). The const keyword doesn’t mean “constant”, it means “read-only”. So given:

    const int answer = 42;
    printf("The answer is %d\n", answer);
    

    in principal the evaluation of the printf call needs to fetch the value of answer from storage before passing it to the printf function.

    But in fact, any compiler worth what you pay for it (even if it’s free) will optimize the reference to answer, so that the printf call results in the same machine code as

    printf("The answer is %d\n", 42);
    

    (gcc does this with -O1 or better. If you don’t specify -O..., then the code actually fetches the value of the object – but then if you don’t ask for optimization, you’re telling the compiler you don’t care much about performance.)

    (A really clever compiler could generate code equivalent to

    puts("The answer is 42");
    

    .)

    The real difference is that the name answer cannot be used in contexts that require a constant expression. For example, case answer: ... would be legal in C++, but is illegal in C.

    Note that int arr[answer]; is actually legal, at least in C99, which allows variable-length arrays. It would be equally legal if you had written

    const int answer = rand() % 100 + 1;
    

    But VLAs can only have automatic storage duration, so they cannot be declared at file scope or with the static keyword.

    As for the enum trick:

    enum { answer = 42; }
    

    that does make answer a constant expression, but it’s restricted to values of type int (C enumeration constants are always of type int). Some might argue that this is an abuse of the enum feature. It is, but I don’t let that bother me.

    So there might be a performance penalty for const int answer = 42; as opposed to #define answer 42, but in practice it’s just going to restrict the contexts in which you can use it.

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

Sidebar

Related Questions

Considering the criteria listed below, which of Python, Groovy or Ruby would you use?
Considering the code below: How is the str1 object created without using new String()
Considering that I use code like (with or without using (considering that using(??) may
Considering the fact that wait() can only be called in a synchronized context which
Considering you're a startup with no funds for own server farm. Which existing solution
I'm looking into speeding up my python code, which is all matrix math, using
Considering such code: class ToBeTested { public: void doForEach() { for (vector<Contained>::iterator it =
Considering private is the default access modifier for class Members, why is the keyword
Considering this code, can I be absolutely sure that the finally block always executes,
Considering the following architecture: a base object 'Entity' a derived object 'Entry:Base' and 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.