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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:33:35+00:00 2026-05-13T18:33:35+00:00

In C, shall I prefer constants over defines? I’ve reading a lot of code

  • 0

In C, shall I prefer constants over defines? I’ve reading a lot of code lately, and all of the examples make heavy use of defines.

  • 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-13T18:33:36+00:00Added an answer on May 13, 2026 at 6:33 pm

    No, in general you should not use const-qualified objects in C to create names constants. In order to create a named constant in C you should use either macros (#define) or enums. In fact, C language has no constants, in the sense that you seem to imply. (C is significantly different from C++ in this regard)

    In C language the notions of constant and constant expression are defined very differently from C++. In C constant means a literal value, like 123. Here are some examples of constants in C

    123
    34.58
    'x'
    

    Constants in C can be used to build constant expressions. However, since const-qualified objects of any type are not a constants in C, they cannot be used in constant expressions, and, consequently, you cannot use const-qualified objects where constant expressions are required.

    For example, the following is not a constant

    const int C = 123; /* C is not a constant!!! */
    

    and since the above C is not a constant, it cannot be used to declare an array type in file scope

    typedef int TArray[C]; /* ERROR: constant expression required */
    

    It cannot be used as a case label

    switch (i) {
      case C: ; /* ERROR: constant expression required */
    }
    

    It cannot be used as bit-field width

    struct S {
      int f : C; /* ERROR: constant expression required */
    };
    

    It cannot be used as an initializer for an object with static storage duration

    static int i = C; /* ERROR: constant expression required */
    

    It cannot be used as a enum initializer

    enum {
      E = C /* ERROR: constant expression required */
    };
    

    i.e it cannot be used anywhere where a constant is required.

    This might seem counter-intuitive, but this is how C the language is defined.

    This is why you see these numerous #define-s in the code you are working with. Again, in C language const-qualified object have very limited use. They are basically completely useless as “constants”, which is why in C language you are basically forced to use #define or enums to declare true constants.

    Of course, in situations when a const-qualified object works for you, i.e. it does what you want it to do, it is indeed superior to macros in many ways, since it is scoped and typed. You should probably prefer such objects where applicable, however in general case you’ll have to take into account the above limitations.

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

Sidebar

Related Questions

I think I shall reframe my question from Where should you use BlockingQueue Implementations
I have always wondered how delegates can be useful and why shall we use
I'm using python to implement another programming language named 'foo'. All of foo's code
I'd like to make use of ProgressMonitor 's functionality but I don't want to
I wonder why software shall be deployed with its related debugging symbols. What are
Hi I shall try to explain clearly what I need to be able to
I have a canvas in Flex that shall be able only to be scrolled
Given some (English) word that we shall assume is a plural , is it
I am implementing view controllers that shall have different behaviour. They have a common
I am working on an app which shall log in to a web site

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.