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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:35:19+00:00 2026-05-16T03:35:19+00:00

It is always better for contants such as PI to #define them or declare

  • 0

It is always better for contants such as PI to #define them or declare them const so the compiler can optimize and it becomes less error prone. I was wondering however, how are literal numbers in statements treated? Ex:

float x;
const int y = 60;
x = y / 3.0f;

In this example how would 3.0f be treated? Would it inherit the optimizations of a constant?

  • 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-16T03:35:19+00:00Added an answer on May 16, 2026 at 3:35 am

    What optimizations will take place depends on the compiler. In your case, both C and C++ compilers will normally have enough information to optimize your source code into identical machine code. In other words, it doesn’t really depend much on what is literal and what is constant in this code.

    Having said that, the meaning of the terms literal and constant are significantly different in C and C++ (and you tagged your question C and C++ at the same time).

    • In C 60 and 3.0f are constants, but y is not a constant. You can call y a const-qualified variable if you wish, but it is not a constant in C terminology, in a sense that a single y is not a constant expression in C.

    As for literals, in C language the term literal only applies to string literals (and also compound literals in C99), i.e. there are no literals in your code at all.

    • In C++,60 and 3.0.f are literals, which form constant expressions (integral and floating -point respectively). y is also a constant of int type, in a sense that a single y is a constant expression in C++.

    The situation when you might notice the difference has nothing to do with optimizations, but rather with the way the languages are defined. For example, using the above y in a file-scope array type declaration is legal in C++, but not in C

     typedef int int_array[y]; /* OK in C++, ERROR in C */
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Generally, I had thought it was always better to store images in the filesystem
Just wondering what the recommended practice is for importing namespaces. Are you always better
I've always wondered is there a better way that I should be writing some
How to make this algorithm better and working, where I am always ending up
It is always better to learn from other developers experiences. I would like to
It seems that daemon threads are always better - because they will be stopped
I know generally it is always better performance to build mysql queries to name
What should be used and when? Or is it always better to use UTF-8?
I know it is always better to use check sum algorithms that people already
There are always some static pages such as About(about.html), Contacts(contacts.html) and other. In some

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.